Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ai.elimu.content_provider.ui.letter_sound

import ai.elimu.content_provider.BaseApplication
import ai.elimu.content_provider.R
import ai.elimu.content_provider.databinding.FragmentLetterSoundsBinding
import ai.elimu.content_provider.rest.LetterSoundsService
import ai.elimu.content_provider.room.GsonToRoomConverter
import ai.elimu.content_provider.room.db.RoomDb
Expand All @@ -13,8 +14,6 @@ import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ProgressBar
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import com.google.android.material.snackbar.Snackbar
Expand All @@ -24,38 +23,34 @@ import retrofit2.Response
import java.util.concurrent.Executors

class LetterSoundsFragment : Fragment() {
private var letterSoundsViewModel: LetterSoundsViewModel? = null

private var progressBar: ProgressBar? = null

private var textView: TextView? = null
private var letterSoundsViewModel: LetterSoundsViewModel? = null
private lateinit var binding: FragmentLetterSoundsBinding

private val TAG = javaClass.name

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
): View {
Log.i(TAG, "onCreateView")

letterSoundsViewModel = ViewModelProvider(this)[LetterSoundsViewModel::class.java]
val root = inflater.inflate(R.layout.fragment_letter_sounds, container, false)
progressBar = root.findViewById(R.id.progress_bar_letter_sounds)
textView = root.findViewById(R.id.text_letter_sounds) as? TextView
binding = FragmentLetterSoundsBinding.inflate(layoutInflater)
letterSoundsViewModel?.text?.observe(viewLifecycleOwner) { s ->
Log.i(TAG, "onChanged")
textView?.text = s
binding.textLetterSounds.text = s
}
return root
return binding.root
Comment on lines +40 to +45
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Refine binding inflation for correct attachment and lifecycle.
Use the passed inflater and container when inflating to respect theme and layout parameters.

Apply this diff:

-        binding = FragmentLetterSoundsBinding.inflate(layoutInflater)
+        _binding = FragmentLetterSoundsBinding.inflate(inflater, container, false)
         letterSoundsViewModel?.text?.observe(viewLifecycleOwner) { s ->
             Log.i(TAG, "onChanged")
             binding.textLetterSounds.text = s
         }
-        return binding.root
+        return binding.root

Committable suggestion skipped: line range outside the PR's diff.

}

override fun onStart() {
Log.i(TAG, "onStart")
super.onStart()

// Download LetterSounds from REST API, and store them in the database
val baseApplication = activity!!.application as BaseApplication
val baseApplication = activity?.application as? BaseApplication ?: return
val retrofit = baseApplication.retrofit
val letterSoundsService = retrofit.create(
LetterSoundsService::class.java
Expand All @@ -71,18 +66,18 @@ class LetterSoundsFragment : Fragment() {

Log.i(TAG, "response: $response")
if (response.isSuccessful) {
val letterSoundGsons = response.body()!!
val letterSoundGsons = response.body() ?: return
Log.i(TAG, "letterSoundGsons.size(): " + letterSoundGsons.size)

if (letterSoundGsons.size > 0) {
if (letterSoundGsons.isNotEmpty()) {
processResponseBody(letterSoundGsons)
}
} else {
// Handle error
Snackbar.make(textView!!, response.toString(), Snackbar.LENGTH_LONG)
Snackbar.make(binding.textLetterSounds, response.toString(), Snackbar.LENGTH_LONG)
.setBackgroundTint(resources.getColor(R.color.deep_orange_darken_4))
.show()
progressBar!!.visibility = View.GONE
binding.progressBarLetterSounds.visibility = View.GONE
}
}

Expand All @@ -92,10 +87,10 @@ class LetterSoundsFragment : Fragment() {
Log.e(TAG, "t.getCause():", t.cause)

// Handle error
Snackbar.make(textView!!, t.cause.toString(), Snackbar.LENGTH_LONG)
Snackbar.make(binding.textLetterSounds, t.cause.toString(), Snackbar.LENGTH_LONG)
.setBackgroundTint(resources.getColor(R.color.deep_orange_darken_4))
.show()
progressBar!!.visibility = View.GONE
binding.progressBarLetterSounds.visibility = View.GONE
}
})
}
Expand All @@ -104,79 +99,78 @@ class LetterSoundsFragment : Fragment() {
Log.i(TAG, "processResponseBody")

val executorService = Executors.newSingleThreadExecutor()
executorService.execute(object : Runnable {
override fun run() {
Log.i(TAG, "run")

val roomDb = RoomDb.getDatabase(context)
val letterSoundDao = roomDb.letterSoundDao()
val letterSound_LetterDao = roomDb.letterSound_LetterDao()
val letterSound_SoundDao = roomDb.letterSound_SoundDao()

// Empty the database table before downloading up-to-date content
letterSound_LetterDao.deleteAll()
letterSound_SoundDao.deleteAll()
letterSoundDao.deleteAll()

for (letterSoundGson in letterSoundGsons) {
Log.i(TAG, "letterSoundGson.getId(): " + letterSoundGson.id)

// Store the LetterSound in the database
val letterSound = GsonToRoomConverter.getLetterSound(letterSoundGson)
letterSound.let {
letterSoundDao.insert(letterSound)
Log.i(
TAG,
"Stored LetterSound in database with ID " + letterSound.id
)
}

// Store all the LetterSound's letters in the database
val letterGsons = letterSoundGson.letters
Log.i(TAG, "letterGsons.size(): " + letterGsons.size)
for ((index, letterGson) in letterGsons.withIndex()) {
Log.i(TAG, "letterGson.getId(): " + letterGson.id)
val letterSound_Letter = LetterSound_Letter()
letterSound_Letter.letterSound_id = letterSoundGson.id
letterSound_Letter.letters_id = letterGson.id
letterSound_Letter.letters_ORDER = index
letterSound_LetterDao.insert(letterSound_Letter)
Log.i(
TAG,
"Stored LetterSound_Letter in database. LetterSound_id: " + letterSound_Letter.letterSound_id + ", letters_id: " + letterSound_Letter.letters_id
)
}
executorService.execute {
Log.i(TAG, "run")

val roomDb = RoomDb.getDatabase(context)
val letterSoundDao = roomDb.letterSoundDao()
val letterSoundLetterDao = roomDb.letterSound_LetterDao()
val letterSoundSoundDao = roomDb.letterSound_SoundDao()

// Empty the database table before downloading up-to-date content
letterSoundLetterDao.deleteAll()
letterSoundSoundDao.deleteAll()
letterSoundDao.deleteAll()

for (letterSoundGson in letterSoundGsons) {
Log.i(TAG, "letterSoundGson.getId(): " + letterSoundGson.id)

// Store the LetterSound in the database
val letterSound = GsonToRoomConverter.getLetterSound(letterSoundGson)
letterSound.let {
letterSoundDao.insert(letterSound)
Log.i(
TAG,
"Stored LetterSound in database with ID " + letterSound.id
)
}

// Store all the LetterSound's sounds in the database
val soundGsons = letterSoundGson.sounds
Log.i(TAG, "soundGsons.size():" + soundGsons.size)
for ((index, soundGson) in soundGsons.withIndex()) {
Log.i(TAG, "soundGson.getId(): " + soundGson.id)
val letterSound_Sound = LetterSound_Sound()
letterSound_Sound.letterSound_id = letterSoundGson.id
letterSound_Sound.sounds_id = soundGson.id
letterSound_Sound.sounds_ORDER = index
letterSound_SoundDao.insert(letterSound_Sound)
Log.i(
TAG,
"Stored LetterSound_Sound in database. LetterSound_id: " + letterSound_Sound.letterSound_id + ", sounds_id: " + letterSound_Sound.sounds_id
)
}
// Store all the LetterSound's letters in the database
val letterGsons = letterSoundGson.letters
Log.i(TAG, "letterGsons.size(): " + letterGsons.size)
for ((index, letterGson) in letterGsons.withIndex()) {
Log.i(TAG, "letterGson.getId(): " + letterGson.id)
val letterSoundLetter = LetterSound_Letter()
letterSoundLetter.letterSound_id = letterSoundGson.id
letterSoundLetter.letters_id = letterGson.id
letterSoundLetter.letters_ORDER = index
letterSoundLetterDao.insert(letterSoundLetter)
Log.i(
TAG,
"Stored LetterSound_Letter in database. LetterSound_id: " + letterSoundLetter.letterSound_id + ", letters_id: " + letterSoundLetter.letters_id
)
}

// Update the UI
val letterSounds = letterSoundDao.loadAll()
Log.i(TAG, "letterSounds.size(): " + letterSounds.size)
activity!!.runOnUiThread {
textView!!.text = "letterSounds.size(): " + letterSounds.size
Snackbar.make(
textView!!,
"letterSounds.size(): " + letterSounds.size,
Snackbar.LENGTH_LONG
).show()
progressBar!!.visibility = View.GONE
// Store all the LetterSound's sounds in the database
val soundGsons = letterSoundGson.sounds
Log.i(TAG, "soundGsons.size():" + soundGsons.size)
for ((index, soundGson) in soundGsons.withIndex()) {
Log.i(TAG, "soundGson.getId(): " + soundGson.id)
val letterSoundSound = LetterSound_Sound()
letterSoundSound.letterSound_id = letterSoundGson.id
letterSoundSound.sounds_id = soundGson.id
letterSoundSound.sounds_ORDER = index
letterSoundSoundDao.insert(letterSoundSound)
Log.i(
TAG,
"Stored LetterSound_Sound in database. LetterSound_id: " + letterSoundSound.letterSound_id + ", sounds_id: " + letterSoundSound.sounds_id
)
}
}
})

// Update the UI
val letterSounds = letterSoundDao.loadAll()
Log.i(TAG, "letterSounds.size(): " + letterSounds.size)
activity?.runOnUiThread {
binding.textLetterSounds.text =
getString(R.string.lettersounds_size, letterSounds.size)
Snackbar.make(
binding.textLetterSounds,
"letterSounds.size(): " + letterSounds.size,
Snackbar.LENGTH_LONG
).show()
binding.progressBarLetterSounds.visibility = View.GONE
}
}
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@
<string name="storybooks_size">storyBooks.size(): %d</string>
<string name="sounds_size">sounds.size(): %d</string>
<string name="numbers_size">numbers.size(): %d</string>
<string name="lettersounds_size">letterSounds.size(): %d</string>
</resources>
Loading