Skip to content

Commit

Permalink
feat: Provide letter-sound letters to other apps
Browse files Browse the repository at this point in the history
  • Loading branch information
jo-elimu committed Dec 1, 2023
1 parent 19933f6 commit 5706e29
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,20 @@
import ai.elimu.content_provider.room.dao.LetterDao;
import ai.elimu.content_provider.room.db.RoomDb;

@Deprecated
public class LetterContentProvider extends ContentProvider {

private static final String AUTHORITY = BuildConfig.APPLICATION_ID + ".provider.letter_provider";
private static final String TABLE_LETTERS = "letters";
private static final int CODE_LETTERS = 1;
private static final int CODE_LETTER_ID = 2;

private static final int CODE_LETTERS_BY_LETTER_SOUND_ID = 3;
private static final UriMatcher MATCHER = new UriMatcher(UriMatcher.NO_MATCH);

static {
MATCHER.addURI(AUTHORITY, TABLE_LETTERS, CODE_LETTERS);
MATCHER.addURI(AUTHORITY, TABLE_LETTERS + "/#", CODE_LETTER_ID);
MATCHER.addURI(AUTHORITY, TABLE_LETTERS + "/by-letter-sound-id/#", CODE_LETTERS_BY_LETTER_SOUND_ID);
}

@Override
Expand Down Expand Up @@ -64,6 +66,23 @@ public Cursor query(Uri uri, String[] projection, String selection, String[] sel

cursor.setNotificationUri(context.getContentResolver(), uri);

return cursor;
} else if (code == CODE_LETTERS_BY_LETTER_SOUND_ID) {
// Extract the letter-sound correspondence ID from the URI
List<String> pathSegments = uri.getPathSegments();
Log.i(getClass().getName(), "pathSegments: " + pathSegments);
String letterSoundIdAsString = pathSegments.get(2);
Long letterSoundId = Long.valueOf(letterSoundIdAsString);
Log.i(getClass().getName(), "letterSoundId: " + letterSoundId);

final Cursor cursor;

// Get the Room Cursor
cursor = letterDao.loadAllByLetterSound(letterSoundId);
Log.i(getClass().getName(), "cursor: " + cursor);

cursor.setNotificationUri(context.getContentResolver(), uri);

return cursor;
} else if (code == CODE_LETTER_ID) {
// Extract the Letter ID from the URI
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public interface LetterDao {
@Query("SELECT * FROM Letter ORDER BY usageCount DESC")
Cursor loadAllOrderedByUsageCount_Cursor();

@Query("SELECT * FROM Letter l WHERE l.id IN (SELECT letters_id FROM LetterSound_Letter WHERE LetterSound_id = :letterSoundId)")
Cursor loadAllByLetterSound(Long letterSoundId);

@Update
void update(Letter letter);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public class ContentProviderUtil {
* If the student has already mastered one or more letters, those will be returned, plus one
* additional letter to be mastered next.
*/
@Deprecated
public static List<LetterGson> getAvailableLetterGsons(Context context, String contentProviderApplicationId, String analyticsApplicationId) {
Log.i(ContentProviderUtil.class.getName(), "getAvailableLetterGsons");

Expand All @@ -64,8 +65,10 @@ public static List<LetterGson> getAvailableLetterGsons(Context context, String c
}

/**
* This method is only meant to be used for testing purposes during development.
* This method is only meant to be used for testing purposes during development. For production,
* use {@link #getAvailableLetterGsons} instead.
*/
@Deprecated
public static List<LetterGson> getAllLetterGsons(Context context, String contentProviderApplicationId) {
Log.i(ContentProviderUtil.class.getName(), "getAllLetterGsons");

Expand Down Expand Up @@ -103,7 +106,8 @@ public static List<LetterGson> getAllLetterGsons(Context context, String content
}

/**
* This method is only meant to be used for testing purposes during development.
* This method is only meant to be used for testing purposes during development. For production,
* use {@link #getAvailableLetterSoundGsons} instead.
*/
public static List<LetterSoundGson> getAllLetterSoundGsons(Context context, String contentProviderApplicationId) {
Log.i(ContentProviderUtil.class.getName(), "getAllLetterSoundGsons");
Expand All @@ -127,7 +131,7 @@ public static List<LetterSoundGson> getAllLetterSoundGsons(Context context, Stri
cursor.moveToNext();

// Convert from Room to Gson
LetterSoundGson letterSoundGson = CursorToLetterSoundGsonConverter.getLetterSoundGson(cursor);
LetterSoundGson letterSoundGson = CursorToLetterSoundGsonConverter.getLetterSoundGson(cursor, context, contentProviderApplicationId);

letterSoundGsons.add(letterSoundGson);

Expand Down Expand Up @@ -172,7 +176,8 @@ public static List<WordGson> getAvailableWordGsons(Context context, String conte
}

/**
* This method is only meant to be used for testing purposes during development.
* This method is only meant to be used for testing purposes during development. For production,
* use {@link #getAvailableWordGsons} instead.
*/
public static List<WordGson> getAllWordGsons(Context context, String contentProviderApplicationId) {
Log.i(ContentProviderUtil.class.getName(), "getAllWordGsons");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
package ai.elimu.content_provider.utils.converter;

import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import ai.elimu.model.v2.gson.content.LetterGson;
import ai.elimu.model.v2.gson.content.LetterSoundGson;

public class CursorToLetterSoundGsonConverter {

public static LetterSoundGson getLetterSoundGson(Cursor cursor) {
public static LetterSoundGson getLetterSoundGson(Cursor cursor, Context context, String contentProviderApplicationId) {
Log.i(CursorToLetterSoundGsonConverter.class.getName(), "getLetterSoundGson");

Log.i(CursorToLetterSoundGsonConverter.class.getName(), "Arrays.toString(cursor.getColumnNames()): " + Arrays.toString(cursor.getColumnNames()));
Expand All @@ -26,11 +32,38 @@ public static LetterSoundGson getLetterSoundGson(Cursor cursor) {
Integer usageCount = cursor.getInt(columnIndexUsageCount);
Log.i(CursorToLetterSoundGsonConverter.class.getName(), "usageCount: " + usageCount);

List<LetterGson> letterGsons = null;
Uri lettersUri = Uri.parse("content://" + contentProviderApplicationId + ".provider.letter_provider/letters/by-letter-sound-id/" + id);
Log.i(CursorToLetterSoundGsonConverter.class.getName(), "lettersUri: " + lettersUri);
Cursor lettersCursor = context.getContentResolver().query(lettersUri, null, null, null, null);
if (lettersCursor == null) {
Log.e(CursorToLetterSoundGsonConverter.class.getName(), "lettersCursor == null");
Toast.makeText(context, "lettersCursor == null", Toast.LENGTH_LONG).show();
} else {
Log.i(CursorToLetterSoundGsonConverter.class.getName(), "lettersCursor.getCount(): " + lettersCursor.getCount());

letterGsons = new ArrayList<>();

boolean isLast = false;
while (!isLast) {
lettersCursor.moveToNext();

// Convert from Room to Gson
LetterGson letterGson = CursorToLetterGsonConverter.getLetterGson(lettersCursor);
letterGsons.add(letterGson);

isLast = lettersCursor.isLast();
}

lettersCursor.close();
Log.i(CursorToLetterSoundGsonConverter.class.getName(), "lettersCursor.isClosed(): " + lettersCursor.isClosed());
}

LetterSoundGson letterSound = new LetterSoundGson();
letterSound.setId(id);
letterSound.setRevisionNumber(revisionNumber);
letterSound.setUsageCount(usageCount);
// TODO: letterSound.setLetters(letters);
letterSound.setLetters(letterGsons);
// TODO: letterSound.setSounds(sounds);

return letterSound;
Expand Down

0 comments on commit 5706e29

Please sign in to comment.