-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Download letter-sound correspondences from REST API
- Loading branch information
Showing
10 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
145 changes: 145 additions & 0 deletions
145
app/src/main/java/ai/elimu/content_provider/ui/letter_sound/LetterSoundsFragment.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,145 @@ | ||
package ai.elimu.content_provider.ui.letter_sound; | ||
|
||
import android.os.Bundle; | ||
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.annotation.NonNull; | ||
import androidx.annotation.Nullable; | ||
import androidx.fragment.app.Fragment; | ||
import androidx.lifecycle.Observer; | ||
import androidx.lifecycle.ViewModelProvider; | ||
|
||
import com.google.android.material.snackbar.Snackbar; | ||
|
||
import java.util.List; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.concurrent.Executors; | ||
|
||
import ai.elimu.content_provider.BaseApplication; | ||
import ai.elimu.content_provider.R; | ||
import ai.elimu.content_provider.rest.LetterSoundsService; | ||
import ai.elimu.content_provider.room.GsonToRoomConverter; | ||
import ai.elimu.content_provider.room.dao.LetterSoundDao; | ||
import ai.elimu.content_provider.room.db.RoomDb; | ||
import ai.elimu.content_provider.room.entity.LetterSound; | ||
import ai.elimu.model.v2.gson.content.LetterSoundGson; | ||
import retrofit2.Call; | ||
import retrofit2.Callback; | ||
import retrofit2.Response; | ||
import retrofit2.Retrofit; | ||
|
||
public class LetterSoundsFragment extends Fragment { | ||
|
||
private LetterSoundsViewModel letterSoundsViewModel; | ||
|
||
private ProgressBar progressBar; | ||
|
||
private TextView textView; | ||
|
||
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { | ||
Log.i(getClass().getName(), "onCreateView"); | ||
|
||
letterSoundsViewModel = new ViewModelProvider(this).get(LetterSoundsViewModel.class); | ||
View 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); | ||
letterSoundsViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() { | ||
@Override | ||
public void onChanged(@Nullable String s) { | ||
Log.i(getClass().getName(), "onChanged"); | ||
textView.setText(s); | ||
} | ||
}); | ||
return root; | ||
} | ||
|
||
@Override | ||
public void onStart() { | ||
Log.i(getClass().getName(), "onStart"); | ||
super.onStart(); | ||
|
||
// Download LetterSounds from REST API, and store them in the database | ||
BaseApplication baseApplication = (BaseApplication) getActivity().getApplication(); | ||
Retrofit retrofit = baseApplication.getRetrofit(); | ||
LetterSoundsService letterSoundsService = retrofit.create(LetterSoundsService.class); | ||
Call<List<LetterSoundGson>> letterSoundGsonsCall = letterSoundsService.listLetterSounds(); | ||
Log.i(getClass().getName(), "letterSoundGsonsCall.request(): " + letterSoundGsonsCall.request()); | ||
letterSoundGsonsCall.enqueue(new Callback<List<LetterSoundGson>>() { | ||
|
||
@Override | ||
public void onResponse(Call<List<LetterSoundGson>> call, Response<List<LetterSoundGson>> response) { | ||
Log.i(getClass().getName(), "onResponse"); | ||
|
||
Log.i(getClass().getName(), "response: " + response); | ||
if (response.isSuccessful()) { | ||
List<LetterSoundGson> letterSoundGsons = response.body(); | ||
Log.i(getClass().getName(), "letterSoundGsons.size(): " + letterSoundGsons.size()); | ||
|
||
if (letterSoundGsons.size() > 0) { | ||
processResponseBody(letterSoundGsons); | ||
} | ||
} else { | ||
// Handle error | ||
Snackbar.make(textView, response.toString(), Snackbar.LENGTH_LONG) | ||
.setBackgroundTint(getResources().getColor(R.color.deep_orange_darken_4)) | ||
.show(); | ||
progressBar.setVisibility(View.GONE); | ||
} | ||
} | ||
|
||
@Override | ||
public void onFailure(Call<List<LetterSoundGson>> call, Throwable t) { | ||
Log.e(getClass().getName(), "onFailure", t); | ||
|
||
Log.e(getClass().getName(), "t.getCause():", t.getCause()); | ||
|
||
// Handle error | ||
Snackbar.make(textView, t.getCause().toString(), Snackbar.LENGTH_LONG) | ||
.setBackgroundTint(getResources().getColor(R.color.deep_orange_darken_4)) | ||
.show(); | ||
progressBar.setVisibility(View.GONE); | ||
} | ||
}); | ||
} | ||
|
||
private void processResponseBody(List<LetterSoundGson> letterSoundGsons) { | ||
Log.i(getClass().getName(), "processResponseBody"); | ||
|
||
ExecutorService executorService = Executors.newSingleThreadExecutor(); | ||
executorService.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
Log.i(getClass().getName(), "run"); | ||
|
||
RoomDb roomDb = RoomDb.getDatabase(getContext()); | ||
LetterSoundDao letterSoundDao = roomDb.letterSoundDao(); | ||
|
||
// Empty the database table before downloading up-to-date content | ||
letterSoundDao.deleteAll(); | ||
|
||
for (LetterSoundGson letterSoundGson : letterSoundGsons) { | ||
Log.i(getClass().getName(), "letterSoundGson.getId(): " + letterSoundGson.getId()); | ||
|
||
// Store the LetterSound in the database | ||
LetterSound letterSound = GsonToRoomConverter.getLetterSound(letterSoundGson); | ||
letterSoundDao.insert(letterSound); | ||
Log.i(getClass().getName(), "Stored LetterSound in database with ID " + letterSound.getId()); | ||
} | ||
|
||
// Update the UI | ||
List<LetterSound> letterSounds = letterSoundDao.loadAll(); | ||
Log.i(getClass().getName(), "letterSounds.size(): " + letterSounds.size()); | ||
getActivity().runOnUiThread(() -> { | ||
textView.setText("letterSounds.size(): " + letterSounds.size()); | ||
Snackbar.make(textView, "letterSounds.size(): " + letterSounds.size(), Snackbar.LENGTH_LONG).show(); | ||
progressBar.setVisibility(View.GONE); | ||
}); | ||
} | ||
}); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
app/src/main/java/ai/elimu/content_provider/ui/letter_sound/LetterSoundsViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package ai.elimu.content_provider.ui.letter_sound; | ||
|
||
import androidx.lifecycle.LiveData; | ||
import androidx.lifecycle.MutableLiveData; | ||
import androidx.lifecycle.ViewModel; | ||
|
||
public class LetterSoundsViewModel extends ViewModel { | ||
|
||
private MutableLiveData<String> text; | ||
|
||
public LetterSoundsViewModel() { | ||
text = new MutableLiveData<>(); | ||
text.setValue("LetterSoundsViewModel"); | ||
} | ||
|
||
public LiveData<String> getText() { | ||
return text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<vector android:height="24dp" android:tint="#000000" | ||
android:viewportHeight="24" android:viewportWidth="24" | ||
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> | ||
<path android:fillColor="@android:color/white" android:pathData="M3,2h8v2h-8z"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M6,11l2,0l0,-4l3,0l0,-2l-8,0l0,2l3,0z"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M12.4036,20.1819l7.7781,-7.7781l1.4142,1.4142l-7.7781,7.7781z"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M14.5,14.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M19.5,19.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M15.5,11c1.38,0 2.5,-1.12 2.5,-2.5V4h3V2h-4v4.51C16.58,6.19 16.07,6 15.5,6C14.12,6 13,7.12 13,8.5C13,9.88 14.12,11 15.5,11z"/> | ||
<path android:fillColor="@android:color/white" android:pathData="M9.74,15.96l-1.41,1.41l-0.71,-0.71l0.35,-0.35c0.98,-0.98 0.98,-2.56 0,-3.54c-0.49,-0.49 -1.13,-0.73 -1.77,-0.73c-0.64,0 -1.28,0.24 -1.77,0.73c-0.98,0.98 -0.98,2.56 0,3.54l0.35,0.35l-1.06,1.06c-0.98,0.98 -0.98,2.56 0,3.54C4.22,21.76 4.86,22 5.5,22s1.28,-0.24 1.77,-0.73l1.06,-1.06l1.41,1.41l1.41,-1.41l-1.41,-1.41l1.41,-1.41L9.74,15.96zM5.85,14.2c0.12,-0.12 0.26,-0.15 0.35,-0.15s0.23,0.03 0.35,0.15c0.19,0.2 0.19,0.51 0,0.71l-0.35,0.35L5.85,14.9C5.66,14.71 5.66,14.39 5.85,14.2zM5.85,19.85C5.73,19.97 5.59,20 5.5,20s-0.23,-0.03 -0.35,-0.15c-0.19,-0.19 -0.19,-0.51 0,-0.71l1.06,-1.06l0.71,0.71L5.85,19.85z"/> | ||
</vector> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
android:paddingTop="@dimen/activity_vertical_margin" | ||
android:paddingRight="@dimen/activity_horizontal_margin" | ||
android:paddingBottom="@dimen/activity_vertical_margin" | ||
android:paddingLeft="@dimen/activity_horizontal_margin"> | ||
|
||
<ProgressBar | ||
android:id="@+id/progress_bar_letter_sounds" | ||
app:layout_constraintTop_toTopOf="parent" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
android:layout_height="wrap_content" | ||
android:layout_width="wrap_content" /> | ||
|
||
<TextView | ||
android:id="@+id/text_letter_sounds" | ||
android:layout_width="match_parent" | ||
android:layout_height="wrap_content" | ||
android:textAlignment="center" | ||
android:textSize="20sp" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
</androidx.constraintlayout.widget.ConstraintLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters