@@ -15,7 +15,6 @@ import android.view.View
1515import android.view.ViewGroup
1616import androidx.core.content.ContextCompat
1717import androidx.fragment.app.Fragment
18- import androidx.lifecycle.Observer
1918import androidx.lifecycle.ViewModelProvider
2019import com.google.android.material.snackbar.Snackbar
2120import retrofit2.Call
@@ -36,14 +35,12 @@ class EmojisFragment : Fragment() {
3635 ): View {
3736 Log .i(TAG , " onCreateView" )
3837
39- emojisViewModel = ViewModelProvider (this ).get( EmojisViewModel ::class .java)
38+ emojisViewModel = ViewModelProvider (this )[ EmojisViewModel ::class .java]
4039 binding = FragmentEmojisBinding .inflate(layoutInflater)
41- emojisViewModel?.getText()?.observe(viewLifecycleOwner, object : Observer <String ?> {
42- override fun onChanged (s : String? ) {
43- Log .i(TAG , " onChanged" )
44- binding.textEmojis.text = s
45- }
46- })
40+ emojisViewModel?.getText()?.observe(viewLifecycleOwner) { s ->
41+ Log .i(TAG , " onChanged" )
42+ binding.textEmojis.text = s
43+ }
4744 return binding.root
4845 }
4946
@@ -103,54 +100,56 @@ class EmojisFragment : Fragment() {
103100 Log .i(TAG , " processResponseBody" )
104101
105102 val executorService = Executors .newSingleThreadExecutor()
106- executorService.execute(object : Runnable {
107- override fun run () {
108- Log .i(TAG , " run" )
109-
110- val roomDb = RoomDb .getDatabase(context)
111- val emojiDao = roomDb.emojiDao()
112- val emojiWordDao = roomDb.emoji_WordDao()
113-
114- // Empty the database table before downloading up-to-date content
115- emojiWordDao.deleteAll()
116- emojiDao.deleteAll()
117-
118- for (emojiGson in emojiGsons) {
119- Log .i(TAG , " emojiGson.getId(): " + emojiGson.id)
120-
121- // Store the Emoji in the database
122- val emoji = getEmoji(emojiGson)
123- emoji?.let {
124- emojiDao.insert(emoji)
125- Log .i(TAG , " Stored Emoji in database with ID " + emoji.id)
126- }
103+ executorService.execute {
104+ Log .i(TAG , " run" )
127105
128- // Store all the Emoji's Word labels in the database
129- val wordGsons = emojiGson.words
130- Log .i(TAG , " wordGsons.size(): " + wordGsons.size)
131- for (wordGson in wordGsons) {
132- Log .i(TAG , " wordGson.getId(): " + wordGson.id)
133- val emojiWord = Emoji_Word ()
134- emojiWord.emoji_id = emojiGson.id
135- emojiWord.words_id = wordGson.id
136- emojiWordDao.insert(emojiWord)
137- Log .i(
138- TAG ,
139- " Stored Emoji_Word in database. Emoji_id: " + emojiWord.emoji_id + " , words_id: " + emojiWord.words_id
140- )
141- }
106+ val roomDb = RoomDb .getDatabase(context)
107+ val emojiDao = roomDb.emojiDao()
108+ val emojiWordDao = roomDb.emoji_WordDao()
109+
110+ // Empty the database table before downloading up-to-date content
111+ emojiWordDao.deleteAll()
112+ emojiDao.deleteAll()
113+
114+ for (emojiGson in emojiGsons) {
115+ Log .i(TAG , " emojiGson.getId(): " + emojiGson.id)
116+
117+ // Store the Emoji in the database
118+ val emoji = getEmoji(emojiGson)
119+ emoji?.let {
120+ emojiDao.insert(emoji)
121+ Log .i(TAG , " Stored Emoji in database with ID " + emoji.id)
142122 }
143123
144- // Update the UI
145- val emojis = emojiDao.loadAll()
146- Log .i(TAG , " emojis.size(): " + emojis.size)
147- activity?.runOnUiThread {
148- binding.textEmojis.text = getString(R .string.emojis_size, emojis.size)
149- Snackbar .make(binding.textEmojis, " emojis.size(): " + emojis.size, Snackbar .LENGTH_LONG )
150- .show()
151- binding.progressBarEmojis.visibility = View .GONE
124+ // Store all the Emoji's Word labels in the database
125+ val wordGsons = emojiGson.words
126+ Log .i(TAG , " wordGsons.size(): " + wordGsons.size)
127+ for (wordGson in wordGsons) {
128+ Log .i(TAG , " wordGson.getId(): " + wordGson.id)
129+ val emojiWord = Emoji_Word ()
130+ emojiWord.emoji_id = emojiGson.id
131+ emojiWord.words_id = wordGson.id
132+ emojiWordDao.insert(emojiWord)
133+ Log .i(
134+ TAG ,
135+ " Stored Emoji_Word in database. Emoji_id: " + emojiWord.emoji_id + " , words_id: " + emojiWord.words_id
136+ )
152137 }
153138 }
154- })
139+
140+ // Update the UI
141+ val emojis = emojiDao.loadAll()
142+ Log .i(TAG , " emojis.size(): " + emojis.size)
143+ activity?.runOnUiThread {
144+ binding.textEmojis.text = getString(R .string.emojis_size, emojis.size)
145+ Snackbar .make(
146+ binding.textEmojis,
147+ " emojis.size(): " + emojis.size,
148+ Snackbar .LENGTH_LONG
149+ )
150+ .show()
151+ binding.progressBarEmojis.visibility = View .GONE
152+ }
153+ }
155154 }
156155}
0 commit comments