|
1 |
| -package ai.elimu.content_provider.ui.letter_sound; |
2 |
| - |
3 |
| -import android.os.Bundle; |
4 |
| -import android.util.Log; |
5 |
| -import android.view.LayoutInflater; |
6 |
| -import android.view.View; |
7 |
| -import android.view.ViewGroup; |
8 |
| -import android.widget.ProgressBar; |
9 |
| -import android.widget.TextView; |
10 |
| - |
11 |
| -import androidx.annotation.NonNull; |
12 |
| -import androidx.annotation.Nullable; |
13 |
| -import androidx.fragment.app.Fragment; |
14 |
| -import androidx.lifecycle.Observer; |
15 |
| -import androidx.lifecycle.ViewModelProvider; |
16 |
| - |
17 |
| -import com.google.android.material.snackbar.Snackbar; |
18 |
| - |
19 |
| -import java.util.List; |
20 |
| -import java.util.concurrent.ExecutorService; |
21 |
| -import java.util.concurrent.Executors; |
22 |
| - |
23 |
| -import ai.elimu.content_provider.BaseApplication; |
24 |
| -import ai.elimu.content_provider.R; |
25 |
| -import ai.elimu.content_provider.rest.LetterSoundsService; |
26 |
| -import ai.elimu.content_provider.room.GsonToRoomConverter; |
27 |
| -import ai.elimu.content_provider.room.dao.LetterSoundDao; |
28 |
| -import ai.elimu.content_provider.room.dao.LetterSound_LetterDao; |
29 |
| -import ai.elimu.content_provider.room.dao.LetterSound_SoundDao; |
30 |
| -import ai.elimu.content_provider.room.db.RoomDb; |
31 |
| -import ai.elimu.content_provider.room.entity.LetterSound; |
32 |
| -import ai.elimu.content_provider.room.entity.LetterSound_Letter; |
33 |
| -import ai.elimu.content_provider.room.entity.LetterSound_Sound; |
34 |
| -import ai.elimu.model.v2.gson.content.LetterGson; |
35 |
| -import ai.elimu.model.v2.gson.content.LetterSoundGson; |
36 |
| -import ai.elimu.model.v2.gson.content.SoundGson; |
37 |
| -import retrofit2.Call; |
38 |
| -import retrofit2.Callback; |
39 |
| -import retrofit2.Response; |
40 |
| -import retrofit2.Retrofit; |
41 |
| - |
42 |
| -public class LetterSoundsFragment extends Fragment { |
43 |
| - |
44 |
| - private LetterSoundsViewModel letterSoundsViewModel; |
45 |
| - |
46 |
| - private ProgressBar progressBar; |
47 |
| - |
48 |
| - private TextView textView; |
49 |
| - |
50 |
| - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { |
51 |
| - Log.i(getClass().getName(), "onCreateView"); |
52 |
| - |
53 |
| - letterSoundsViewModel = new ViewModelProvider(this).get(LetterSoundsViewModel.class); |
54 |
| - View root = inflater.inflate(R.layout.fragment_letter_sounds, container, false); |
55 |
| - progressBar = root.findViewById(R.id.progress_bar_letter_sounds); |
56 |
| - textView = root.findViewById(R.id.text_letter_sounds); |
57 |
| - letterSoundsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() { |
58 |
| - @Override |
59 |
| - public void onChanged(@Nullable String s) { |
60 |
| - Log.i(getClass().getName(), "onChanged"); |
61 |
| - textView.setText(s); |
| 1 | +package ai.elimu.content_provider.ui.letter_sound |
| 2 | + |
| 3 | +import ai.elimu.content_provider.BaseApplication |
| 4 | +import ai.elimu.content_provider.R |
| 5 | +import ai.elimu.content_provider.rest.LetterSoundsService |
| 6 | +import ai.elimu.content_provider.room.GsonToRoomConverter |
| 7 | +import ai.elimu.content_provider.room.db.RoomDb |
| 8 | +import ai.elimu.content_provider.room.entity.LetterSound_Letter |
| 9 | +import ai.elimu.content_provider.room.entity.LetterSound_Sound |
| 10 | +import ai.elimu.model.v2.gson.content.LetterSoundGson |
| 11 | +import android.os.Bundle |
| 12 | +import android.util.Log |
| 13 | +import android.view.LayoutInflater |
| 14 | +import android.view.View |
| 15 | +import android.view.ViewGroup |
| 16 | +import android.widget.ProgressBar |
| 17 | +import android.widget.TextView |
| 18 | +import androidx.fragment.app.Fragment |
| 19 | +import androidx.lifecycle.Observer |
| 20 | +import androidx.lifecycle.ViewModelProvider |
| 21 | +import com.google.android.material.snackbar.Snackbar |
| 22 | +import retrofit2.Call |
| 23 | +import retrofit2.Callback |
| 24 | +import retrofit2.Response |
| 25 | +import java.util.concurrent.Executors |
| 26 | + |
| 27 | +class LetterSoundsFragment : Fragment() { |
| 28 | + private var letterSoundsViewModel: LetterSoundsViewModel? = null |
| 29 | + |
| 30 | + private var progressBar: ProgressBar? = null |
| 31 | + |
| 32 | + private var textView: TextView? = null |
| 33 | + |
| 34 | + override fun onCreateView( |
| 35 | + inflater: LayoutInflater, |
| 36 | + container: ViewGroup?, |
| 37 | + savedInstanceState: Bundle? |
| 38 | + ): View? { |
| 39 | + Log.i(javaClass.name, "onCreateView") |
| 40 | + |
| 41 | + letterSoundsViewModel = ViewModelProvider(this).get( |
| 42 | + LetterSoundsViewModel::class.java |
| 43 | + ) |
| 44 | + val root = inflater.inflate(R.layout.fragment_letter_sounds, container, false) |
| 45 | + progressBar = root.findViewById(R.id.progress_bar_letter_sounds) |
| 46 | + textView = root.findViewById(R.id.text_letter_sounds) as? TextView |
| 47 | + letterSoundsViewModel!!.text.observe(viewLifecycleOwner, object : Observer<String?> { |
| 48 | + override fun onChanged(s: String?) { |
| 49 | + Log.i(javaClass.name, "onChanged") |
| 50 | + textView?.text = s |
62 | 51 | }
|
63 |
| - }); |
64 |
| - return root; |
| 52 | + }) |
| 53 | + return root |
65 | 54 | }
|
66 | 55 |
|
67 |
| - @Override |
68 |
| - public void onStart() { |
69 |
| - Log.i(getClass().getName(), "onStart"); |
70 |
| - super.onStart(); |
| 56 | + override fun onStart() { |
| 57 | + Log.i(javaClass.name, "onStart") |
| 58 | + super.onStart() |
71 | 59 |
|
72 | 60 | // Download LetterSounds from REST API, and store them in the database
|
73 |
| - BaseApplication baseApplication = (BaseApplication) getActivity().getApplication(); |
74 |
| - Retrofit retrofit = baseApplication.getRetrofit(); |
75 |
| - LetterSoundsService letterSoundsService = retrofit.create(LetterSoundsService.class); |
76 |
| - Call<List<LetterSoundGson>> letterSoundGsonsCall = letterSoundsService.listLetterSounds(); |
77 |
| - Log.i(getClass().getName(), "letterSoundGsonsCall.request(): " + letterSoundGsonsCall.request()); |
78 |
| - letterSoundGsonsCall.enqueue(new Callback<List<LetterSoundGson>>() { |
79 |
| - |
80 |
| - @Override |
81 |
| - public void onResponse(Call<List<LetterSoundGson>> call, Response<List<LetterSoundGson>> response) { |
82 |
| - Log.i(getClass().getName(), "onResponse"); |
83 |
| - |
84 |
| - Log.i(getClass().getName(), "response: " + response); |
85 |
| - if (response.isSuccessful()) { |
86 |
| - List<LetterSoundGson> letterSoundGsons = response.body(); |
87 |
| - Log.i(getClass().getName(), "letterSoundGsons.size(): " + letterSoundGsons.size()); |
88 |
| - |
89 |
| - if (letterSoundGsons.size() > 0) { |
90 |
| - processResponseBody(letterSoundGsons); |
| 61 | + val baseApplication = activity!!.application as BaseApplication |
| 62 | + val retrofit = baseApplication.retrofit |
| 63 | + val letterSoundsService = retrofit.create( |
| 64 | + LetterSoundsService::class.java |
| 65 | + ) |
| 66 | + val letterSoundGsonsCall = letterSoundsService.listLetterSounds() |
| 67 | + Log.i(javaClass.name, "letterSoundGsonsCall.request(): " + letterSoundGsonsCall.request()) |
| 68 | + letterSoundGsonsCall.enqueue(object : Callback<List<LetterSoundGson>> { |
| 69 | + override fun onResponse( |
| 70 | + call: Call<List<LetterSoundGson>>, |
| 71 | + response: Response<List<LetterSoundGson>> |
| 72 | + ) { |
| 73 | + Log.i(javaClass.name, "onResponse") |
| 74 | + |
| 75 | + Log.i(javaClass.name, "response: $response") |
| 76 | + if (response.isSuccessful) { |
| 77 | + val letterSoundGsons = response.body()!! |
| 78 | + Log.i(javaClass.name, "letterSoundGsons.size(): " + letterSoundGsons.size) |
| 79 | + |
| 80 | + if (letterSoundGsons.size > 0) { |
| 81 | + processResponseBody(letterSoundGsons) |
91 | 82 | }
|
92 | 83 | } else {
|
93 | 84 | // Handle error
|
94 |
| - Snackbar.make(textView, response.toString(), Snackbar.LENGTH_LONG) |
95 |
| - .setBackgroundTint(getResources().getColor(R.color.deep_orange_darken_4)) |
96 |
| - .show(); |
97 |
| - progressBar.setVisibility(View.GONE); |
| 85 | + Snackbar.make(textView!!, response.toString(), Snackbar.LENGTH_LONG) |
| 86 | + .setBackgroundTint(resources.getColor(R.color.deep_orange_darken_4)) |
| 87 | + .show() |
| 88 | + progressBar!!.visibility = View.GONE |
98 | 89 | }
|
99 | 90 | }
|
100 | 91 |
|
101 |
| - @Override |
102 |
| - public void onFailure(Call<List<LetterSoundGson>> call, Throwable t) { |
103 |
| - Log.e(getClass().getName(), "onFailure", t); |
| 92 | + override fun onFailure(call: Call<List<LetterSoundGson>>, t: Throwable) { |
| 93 | + Log.e(javaClass.name, "onFailure", t) |
104 | 94 |
|
105 |
| - Log.e(getClass().getName(), "t.getCause():", t.getCause()); |
| 95 | + Log.e(javaClass.name, "t.getCause():", t.cause) |
106 | 96 |
|
107 | 97 | // Handle error
|
108 |
| - Snackbar.make(textView, t.getCause().toString(), Snackbar.LENGTH_LONG) |
109 |
| - .setBackgroundTint(getResources().getColor(R.color.deep_orange_darken_4)) |
110 |
| - .show(); |
111 |
| - progressBar.setVisibility(View.GONE); |
| 98 | + Snackbar.make(textView!!, t.cause.toString(), Snackbar.LENGTH_LONG) |
| 99 | + .setBackgroundTint(resources.getColor(R.color.deep_orange_darken_4)) |
| 100 | + .show() |
| 101 | + progressBar!!.visibility = View.GONE |
112 | 102 | }
|
113 |
| - }); |
| 103 | + }) |
114 | 104 | }
|
115 | 105 |
|
116 |
| - private void processResponseBody(List<LetterSoundGson> letterSoundGsons) { |
117 |
| - Log.i(getClass().getName(), "processResponseBody"); |
| 106 | + private fun processResponseBody(letterSoundGsons: List<LetterSoundGson>) { |
| 107 | + Log.i(javaClass.name, "processResponseBody") |
118 | 108 |
|
119 |
| - ExecutorService executorService = Executors.newSingleThreadExecutor(); |
120 |
| - executorService.execute(new Runnable() { |
121 |
| - @Override |
122 |
| - public void run() { |
123 |
| - Log.i(getClass().getName(), "run"); |
| 109 | + val executorService = Executors.newSingleThreadExecutor() |
| 110 | + executorService.execute(object : Runnable { |
| 111 | + override fun run() { |
| 112 | + Log.i(javaClass.name, "run") |
124 | 113 |
|
125 |
| - RoomDb roomDb = RoomDb.getDatabase(getContext()); |
126 |
| - LetterSoundDao letterSoundDao = roomDb.letterSoundDao(); |
127 |
| - LetterSound_LetterDao letterSound_LetterDao = roomDb.letterSound_LetterDao(); |
128 |
| - LetterSound_SoundDao letterSound_SoundDao = roomDb.letterSound_SoundDao(); |
| 114 | + val roomDb = RoomDb.getDatabase(context) |
| 115 | + val letterSoundDao = roomDb.letterSoundDao() |
| 116 | + val letterSound_LetterDao = roomDb.letterSound_LetterDao() |
| 117 | + val letterSound_SoundDao = roomDb.letterSound_SoundDao() |
129 | 118 |
|
130 | 119 | // Empty the database table before downloading up-to-date content
|
131 |
| - letterSound_LetterDao.deleteAll(); |
132 |
| - letterSound_SoundDao.deleteAll(); |
133 |
| - letterSoundDao.deleteAll(); |
| 120 | + letterSound_LetterDao.deleteAll() |
| 121 | + letterSound_SoundDao.deleteAll() |
| 122 | + letterSoundDao.deleteAll() |
134 | 123 |
|
135 |
| - for (LetterSoundGson letterSoundGson : letterSoundGsons) { |
136 |
| - Log.i(getClass().getName(), "letterSoundGson.getId(): " + letterSoundGson.getId()); |
| 124 | + for (letterSoundGson in letterSoundGsons) { |
| 125 | + Log.i(javaClass.name, "letterSoundGson.getId(): " + letterSoundGson.id) |
137 | 126 |
|
138 | 127 | // Store the LetterSound in the database
|
139 |
| - LetterSound letterSound = GsonToRoomConverter.getLetterSound(letterSoundGson); |
140 |
| - letterSoundDao.insert(letterSound); |
141 |
| - Log.i(getClass().getName(), "Stored LetterSound in database with ID " + letterSound.getId()); |
| 128 | + val letterSound = GsonToRoomConverter.getLetterSound(letterSoundGson) |
| 129 | + letterSoundDao.insert(letterSound) |
| 130 | + Log.i( |
| 131 | + javaClass.name, |
| 132 | + "Stored LetterSound in database with ID " + letterSound.id |
| 133 | + ) |
142 | 134 |
|
143 | 135 | // Store all the LetterSound's letters in the database
|
144 |
| - List<LetterGson> letterGsons = letterSoundGson.getLetters(); |
145 |
| - Log.i(getClass().getName(), "letterGsons.size(): " + letterGsons.size()); |
146 |
| - for (LetterGson letterGson : letterGsons) { |
147 |
| - Log.i(getClass().getName(), "letterGson.getId(): " + letterGson.getId()); |
148 |
| - LetterSound_Letter letterSound_Letter = new LetterSound_Letter(); |
149 |
| - letterSound_Letter.setLetterSound_id(letterSoundGson.getId()); |
150 |
| - letterSound_Letter.setLetters_id(letterGson.getId()); |
151 |
| - letterSound_LetterDao.insert(letterSound_Letter); |
152 |
| - Log.i(getClass().getName(), "Stored LetterSound_Letter in database. LetterSound_id: " + letterSound_Letter.getLetterSound_id() + ", letters_id: " + letterSound_Letter.getLetters_id()); |
| 136 | + val letterGsons = letterSoundGson.letters |
| 137 | + Log.i(javaClass.name, "letterGsons.size(): " + letterGsons.size) |
| 138 | + for (letterGson in letterGsons) { |
| 139 | + Log.i(javaClass.name, "letterGson.getId(): " + letterGson.id) |
| 140 | + val letterSound_Letter = LetterSound_Letter() |
| 141 | + letterSound_Letter.letterSound_id = letterSoundGson.id |
| 142 | + letterSound_Letter.letters_id = letterGson.id |
| 143 | + letterSound_LetterDao.insert(letterSound_Letter) |
| 144 | + Log.i( |
| 145 | + javaClass.name, |
| 146 | + "Stored LetterSound_Letter in database. LetterSound_id: " + letterSound_Letter.letterSound_id + ", letters_id: " + letterSound_Letter.letters_id |
| 147 | + ) |
153 | 148 | }
|
154 | 149 |
|
155 | 150 | // Store all the LetterSound's sounds in the database
|
156 |
| - List<SoundGson> soundGsons = letterSoundGson.getSounds(); |
157 |
| - Log.i(getClass().getName(), "soundGsons.size():" + soundGsons.size()); |
158 |
| - for (SoundGson soundGson : soundGsons) { |
159 |
| - Log.i(getClass().getName(), "soundGson.getId(): " + soundGson.getId()); |
160 |
| - LetterSound_Sound letterSound_Sound = new LetterSound_Sound(); |
161 |
| - letterSound_Sound.setLetterSound_id(letterSoundGson.getId()); |
162 |
| - letterSound_Sound.setSounds_id(soundGson.getId()); |
163 |
| - letterSound_SoundDao.insert(letterSound_Sound); |
164 |
| - Log.i(getClass().getName(), "Stored LetterSound_Sound in database. LetterSound_id: " + letterSound_Sound.getLetterSound_id() + ", sounds_id: " + letterSound_Sound.getSounds_id()); |
| 151 | + val soundGsons = letterSoundGson.sounds |
| 152 | + Log.i(javaClass.name, "soundGsons.size():" + soundGsons.size) |
| 153 | + for (soundGson in soundGsons) { |
| 154 | + Log.i(javaClass.name, "soundGson.getId(): " + soundGson.id) |
| 155 | + val letterSound_Sound = LetterSound_Sound() |
| 156 | + letterSound_Sound.letterSound_id = letterSoundGson.id |
| 157 | + letterSound_Sound.sounds_id = soundGson.id |
| 158 | + letterSound_SoundDao.insert(letterSound_Sound) |
| 159 | + Log.i( |
| 160 | + javaClass.name, |
| 161 | + "Stored LetterSound_Sound in database. LetterSound_id: " + letterSound_Sound.letterSound_id + ", sounds_id: " + letterSound_Sound.sounds_id |
| 162 | + ) |
165 | 163 | }
|
166 | 164 | }
|
167 | 165 |
|
168 | 166 | // Update the UI
|
169 |
| - List<LetterSound> letterSounds = letterSoundDao.loadAll(); |
170 |
| - Log.i(getClass().getName(), "letterSounds.size(): " + letterSounds.size()); |
171 |
| - getActivity().runOnUiThread(() -> { |
172 |
| - textView.setText("letterSounds.size(): " + letterSounds.size()); |
173 |
| - Snackbar.make(textView, "letterSounds.size(): " + letterSounds.size(), Snackbar.LENGTH_LONG).show(); |
174 |
| - progressBar.setVisibility(View.GONE); |
175 |
| - }); |
| 167 | + val letterSounds = letterSoundDao.loadAll() |
| 168 | + Log.i(javaClass.name, "letterSounds.size(): " + letterSounds.size) |
| 169 | + activity!!.runOnUiThread { |
| 170 | + textView!!.text = "letterSounds.size(): " + letterSounds.size |
| 171 | + Snackbar.make( |
| 172 | + textView!!, |
| 173 | + "letterSounds.size(): " + letterSounds.size, |
| 174 | + Snackbar.LENGTH_LONG |
| 175 | + ).show() |
| 176 | + progressBar!!.visibility = View.GONE |
| 177 | + } |
176 | 178 | }
|
177 |
| - }); |
| 179 | + }) |
178 | 180 | }
|
179 | 181 | }
|
0 commit comments