Skip to content

Commit

Permalink
feat: Download letter-sound correspondences from REST API
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-elimu committed Nov 27, 2023
1 parent 9dd3540 commit 14b1fd4
Show file tree
Hide file tree
Showing 10 changed files with 248 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ protected void onCreate(Bundle savedInstanceState) {
R.id.nav_home,
R.id.nav_letters,
R.id.nav_sounds,
R.id.nav_letter_sounds,
R.id.nav_words,
R.id.nav_numbers,
R.id.nav_emojis,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ai.elimu.content_provider.room;

import ai.elimu.content_provider.room.entity.LetterSound;
import ai.elimu.content_provider.room.entity.Sound;
import ai.elimu.content_provider.room.entity.Audio;
import ai.elimu.content_provider.room.entity.Emoji;
Expand All @@ -11,6 +12,7 @@
import ai.elimu.content_provider.room.entity.StoryBookParagraph;
import ai.elimu.content_provider.room.entity.Video;
import ai.elimu.content_provider.room.entity.Word;
import ai.elimu.model.v2.gson.content.LetterSoundGson;
import ai.elimu.model.v2.gson.content.SoundGson;
import ai.elimu.model.v2.gson.content.AudioGson;
import ai.elimu.model.v2.gson.content.EmojiGson;
Expand Down Expand Up @@ -67,6 +69,27 @@ public static Sound getSound(SoundGson soundGson) {
}
}

public static LetterSound getLetterSound(LetterSoundGson letterSoundGson) {
if (letterSoundGson == null) {
return null;
} else {
LetterSound letterSound = new LetterSound();

// BaseEntity
letterSound.setId(letterSoundGson.getId());

// Content
letterSound.setRevisionNumber(letterSoundGson.getRevisionNumber());
letterSound.setUsageCount(letterSoundGson.getUsageCount());

// LetterSound
// TODO: letters
// TODO: sounds

return letterSound;
}
}

public static Word getWord(WordGson wordGson) {
if (wordGson == null) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.Query;

import java.util.List;

import ai.elimu.content_provider.room.entity.LetterSound;

Expand All @@ -10,4 +13,10 @@ public interface LetterSoundDao {

@Insert
void insert(LetterSound letterSound);

@Query("SELECT * FROM LetterSound")
List<LetterSound> loadAll();

@Query("DELETE FROM LetterSound")
void deleteAll();
}
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);
});
}
});
}
}
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;
}
}
11 changes: 11 additions & 0 deletions app/src/main/res/drawable/ic_menu_emoji_symbols.xml
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>
29 changes: 29 additions & 0 deletions app/src/main/res/layout/fragment_letter_sounds.xml
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>
4 changes: 4 additions & 0 deletions app/src/main/res/menu/activity_main_drawer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
android:id="@+id/nav_sounds"
android:icon="@drawable/ic_menu_music_note"
android:title="@string/menu_sounds" />
<item
android:id="@+id/nav_letter_sounds"
android:icon="@drawable/ic_menu_emoji_symbols"
android:title="@string/menu_letter_sounds" />
<item
android:id="@+id/nav_words"
android:icon="@drawable/ic_menu_sms"
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/res/navigation/mobile_navigation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@
android:label="@string/menu_sounds"
tools:layout="@layout/fragment_sounds" />

<fragment
android:id="@+id/nav_letter_sounds"
android:name="ai.elimu.content_provider.ui.letter_sound.LetterSoundsFragment"
android:label="@string/menu_letter_sounds"
tools:layout="@layout/fragment_letter_sounds" />

<fragment
android:id="@+id/nav_words"
android:name="ai.elimu.content_provider.ui.word.WordsFragment"
Expand Down
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 @@ -9,6 +9,7 @@

<string name="menu_home">Home</string>
<string name="menu_letters">Letters</string>
<string name="menu_letter_sounds">Letter-Sounds</string>
<string name="menu_sounds">Sounds</string>
<string name="menu_words">Words</string>
<string name="menu_numbers">Numbers</string>
Expand Down

0 comments on commit 14b1fd4

Please sign in to comment.