Skip to content

Commit 0795da2

Browse files
authored
Merge pull request #422 from elimu-ai/421-provide-lettersounds-by-word-id
chore: provide letter-sounds by word id
2 parents 2d8fdae + c8d9834 commit 0795da2

File tree

11 files changed

+743
-13
lines changed

11 files changed

+743
-13
lines changed

app/schemas/ai.elimu.content_provider.room.db.RoomDb/31.json

Lines changed: 621 additions & 0 deletions
Large diffs are not rendered by default.

app/src/main/java/ai/elimu/content_provider/provider/LetterSoundContentProvider.kt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,24 @@ class LetterSoundContentProvider : ContentProvider() {
5555

5656
return cursor
5757
}
58+
CODE_LETTER_SOUNDS_BY_WORD_ID -> {
59+
// Extract the Word ID from the URI
60+
val pathSegments = uri.pathSegments
61+
Log.i(this::class.simpleName, "pathSegments: ${pathSegments}")
62+
val wordIdAsString = pathSegments[2]
63+
val wordId = wordIdAsString.toLong()
64+
Log.i(this::class.simpleName, "wordId: ${wordId}")
65+
66+
// Get the Room Cursor
67+
val cursor = letterSoundDao.loadAllByWord(wordId)
68+
Log.i(this::class.simpleName, "cursor: ${cursor}")
69+
70+
cursor.setNotificationUri(context.contentResolver, uri)
71+
72+
cursor.extras = prepareBundle()
73+
74+
return cursor
75+
}
5876
CODE_LETTER_SOUND_ID -> {
5977
// Extract the LetterSound ID from the URI
6078
val pathSegments = uri.pathSegments
@@ -128,11 +146,13 @@ class LetterSoundContentProvider : ContentProvider() {
128146
private const val TABLE_LETTER_SOUNDS = "letter_sounds"
129147
private const val CODE_LETTER_SOUNDS = 1
130148
private const val CODE_LETTER_SOUND_ID = 2
149+
private const val CODE_LETTER_SOUNDS_BY_WORD_ID = 3
131150
private val MATCHER = UriMatcher(UriMatcher.NO_MATCH)
132151

133152
init {
134153
MATCHER.addURI(AUTHORITY, TABLE_LETTER_SOUNDS, CODE_LETTER_SOUNDS)
135154
MATCHER.addURI(AUTHORITY, "$TABLE_LETTER_SOUNDS/#", CODE_LETTER_SOUND_ID)
155+
MATCHER.addURI(AUTHORITY, "$TABLE_LETTER_SOUNDS/by-word-id/#", CODE_LETTER_SOUNDS_BY_WORD_ID)
136156
}
137157
}
138158
}

app/src/main/java/ai/elimu/content_provider/room/dao/LetterSoundDao.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ interface LetterSoundDao {
2020
@Query("SELECT * FROM LetterSound ORDER BY usageCount DESC")
2121
fun loadAllCursor(): Cursor
2222

23+
@Query("SELECT * FROM LetterSound ls " +
24+
"INNER JOIN Word_LetterSound wls ON ls.id = wls.letterSounds_id " +
25+
"WHERE wls.Word_id = :wordId " +
26+
"ORDER BY wls.letterSounds_ORDER")
27+
fun loadAllByWord(wordId: Long): Cursor
28+
2329
@Query("DELETE FROM LetterSound")
2430
fun deleteAll()
2531
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ai.elimu.content_provider.room.dao
2+
3+
import ai.elimu.content_provider.room.entity.Word_LetterSound
4+
import androidx.room.Dao
5+
import androidx.room.Insert
6+
import androidx.room.Query
7+
8+
@Dao
9+
interface Word_LetterSoundDao {
10+
@Insert
11+
fun insert(word_letterSound: Word_LetterSound)
12+
13+
@Query("DELETE FROM Word_LetterSound")
14+
fun deleteAll()
15+
}

app/src/main/java/ai/elimu/content_provider/room/db/RoomDb.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import ai.elimu.content_provider.room.dao.StoryBookParagraphWordDao;
2727
import ai.elimu.content_provider.room.dao.VideoDao;
2828
import ai.elimu.content_provider.room.dao.WordDao;
29+
import ai.elimu.content_provider.room.dao.Word_LetterSoundDao;
2930
import ai.elimu.content_provider.room.entity.LetterSound;
3031
import ai.elimu.content_provider.room.entity.LetterSound_Letter;
3132
import ai.elimu.content_provider.room.entity.LetterSound_Sound;
@@ -42,8 +43,9 @@
4243
import ai.elimu.content_provider.room.entity.StoryBookParagraph_Word;
4344
import ai.elimu.content_provider.room.entity.Video;
4445
import ai.elimu.content_provider.room.entity.Word;
46+
import ai.elimu.content_provider.room.entity.Word_LetterSound;
4547

46-
@Database(version = 30, entities = {Letter.class, Sound.class, LetterSound.class, LetterSound_Letter.class, LetterSound_Sound.class, Word.class, Number.class, Emoji.class, Emoji_Word.class, Image.class, Image_Word.class, StoryBook.class, StoryBookChapter.class, StoryBookParagraph.class, StoryBookParagraph_Word.class, Video.class})
48+
@Database(version = 31, entities = {Letter.class, Sound.class, LetterSound.class, LetterSound_Letter.class, LetterSound_Sound.class, Word.class, Word_LetterSound.class, Number.class, Emoji.class, Emoji_Word.class, Image.class, Image_Word.class, StoryBook.class, StoryBookChapter.class, StoryBookParagraph.class, StoryBookParagraph_Word.class, Video.class})
4749
@TypeConverters({Converters.class})
4850
public abstract class RoomDb extends RoomDatabase {
4951

@@ -52,29 +54,23 @@ public abstract class RoomDb extends RoomDatabase {
5254
public abstract SoundDao soundDao();
5355

5456
public abstract LetterSoundDao letterSoundDao();
55-
5657
public abstract LetterSoundLetterDao letterSound_LetterDao();
57-
5858
public abstract LetterSoundSoundDao letterSound_SoundDao();
5959

6060
public abstract WordDao wordDao();
61+
public abstract Word_LetterSoundDao word_letterSoundDao();
6162

6263
public abstract NumberDao numberDao();
6364

6465
public abstract EmojiDao emojiDao();
65-
6666
public abstract EmojiWordDao emojiWordDao();
6767

6868
public abstract ImageDao imageDao();
69-
7069
public abstract ImageWordDao image_WordDao();
7170

7271
public abstract StoryBookDao storyBookDao();
73-
7472
public abstract StoryBookChapterDao storyBookChapterDao();
75-
7673
public abstract StoryBookParagraphDao storyBookParagraphDao();
77-
7874
public abstract StoryBookParagraphWordDao storyBookParagraph_WordDao();
7975

8076
public abstract VideoDao videoDao();
@@ -115,7 +111,8 @@ public static RoomDb getDatabase(final Context context) {
115111
MIGRATION_26_27,
116112
MIGRATION_27_28,
117113
MIGRATION_28_29,
118-
MIGRATION_29_30
114+
MIGRATION_29_30,
115+
MIGRATION_30_31
119116
)
120117
.build();
121118
}
@@ -409,4 +406,15 @@ public void migrate(SupportSQLiteDatabase database) {
409406
database.execSQL(sql);
410407
}
411408
};
409+
410+
private static final Migration MIGRATION_30_31 = new Migration(30, 31) {
411+
@Override
412+
public void migrate(SupportSQLiteDatabase database) {
413+
Log.i(getClass().getSimpleName(), "migrate (30 --> 31)");
414+
415+
String sql = "CREATE TABLE IF NOT EXISTS `Word_LetterSound` (`Word_id` INTEGER NOT NULL, `letterSounds_id` INTEGER NOT NULL, `letterSounds_ORDER` INTEGER NOT NULL, PRIMARY KEY(`Word_id`, `letterSounds_ORDER`))";
416+
Log.i(getClass().getSimpleName(), "migrate sql: ${sql}");
417+
database.execSQL(sql);
418+
}
419+
};
412420
}

app/src/main/java/ai/elimu/content_provider/room/entity/Word.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import androidx.room.Entity
88
*/
99
@Entity
1010
class Word : Content() {
11+
@Deprecated("Replaced by LetterSounds")
1112
@JvmField
1213
var text: String = ""
1314

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package ai.elimu.content_provider.room.entity
2+
3+
import androidx.room.Entity
4+
5+
/**
6+
* For documentation, see [model](https://github.com/elimu-ai/webapp/tree/main/src/main/java/ai/elimu/entity)
7+
*/
8+
@Entity(primaryKeys = ["Word_id", "letterSounds_ORDER"])
9+
class Word_LetterSound {
10+
var Word_id: Long = 0L
11+
12+
var letterSounds_id: Long = 0L
13+
14+
var letterSounds_ORDER: Int = 0
15+
}

app/src/main/java/ai/elimu/content_provider/ui/word/WordsFragment.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import ai.elimu.content_provider.databinding.FragmentWordsBinding
66
import ai.elimu.content_provider.rest.WordsService
77
import ai.elimu.content_provider.room.GsonToRoomConverter.getWord
88
import ai.elimu.content_provider.room.db.RoomDb
9+
import ai.elimu.content_provider.room.entity.Word_LetterSound
10+
import ai.elimu.model.v2.gson.content.LetterSoundGson
911
import ai.elimu.model.v2.gson.content.WordGson
1012
import android.os.Bundle
1113
import android.util.Log
@@ -113,8 +115,10 @@ class WordsFragment : Fragment() {
113115

114116
val roomDb = RoomDb.getDatabase(context)
115117
val wordDao = roomDb.wordDao()
118+
val word_LetterSoundDao = roomDb.word_letterSoundDao()
116119

117120
// Empty the database table before downloading up-to-date content
121+
word_LetterSoundDao.deleteAll()
118122
wordDao.deleteAll()
119123

120124
for (wordGson in wordGsons) {
@@ -125,6 +129,19 @@ class WordsFragment : Fragment() {
125129
wordDao.insert(word)
126130
Log.i(javaClass.name, "Stored Word in database with ID " + word.id)
127131
}
132+
133+
// Store all the Word's letter-sounds in the database
134+
val letterSoundGsons: List<LetterSoundGson> = wordGson.letterSounds
135+
Log.i(this::class.simpleName, "letterSoundGsons.size: ${letterSoundGsons.size}")
136+
for ((index, letterSoundGson) in letterSoundGsons.withIndex()) {
137+
Log.i(this::class.simpleName, "letterSoundGson.id: ${letterSoundGson.id}")
138+
val word_LetterSound = Word_LetterSound()
139+
word_LetterSound.Word_id = wordGson.id
140+
word_LetterSound.letterSounds_id = letterSoundGson.id
141+
word_LetterSound.letterSounds_ORDER = index
142+
word_LetterSoundDao.insert(word_LetterSound)
143+
Log.i(this::class.simpleName, "Stored Word_LetterSound in database. word_LetterSound.Word_id: ${word_LetterSound.Word_id}, word_LetterSound.letterSounds_id: ${word_LetterSound.letterSounds_id}")
144+
}
128145
}
129146

130147
// Update the UI

utils/src/main/java/ai/elimu/content_provider/utils/ContentProviderUtil.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ object ContentProviderUtil {
202202
wordsCursor.moveToNext()
203203

204204
// Convert from Room to Gson
205-
val wordGson = CursorToWordGsonConverter.getWordGson(wordsCursor)
205+
val wordGson = CursorToWordGsonConverter.getWordGson(wordsCursor, context, contentProviderApplicationId)
206206

207207
wordGsons.add(wordGson)
208208

@@ -249,7 +249,7 @@ object ContentProviderUtil {
249249
wordCursor.moveToFirst()
250250

251251
// Convert from Room to Gson
252-
wordGson = CursorToWordGsonConverter.getWordGson(wordCursor)
252+
wordGson = CursorToWordGsonConverter.getWordGson(wordCursor, context, contentProviderApplicationId)
253253

254254
wordCursor.close()
255255
Log.i(

utils/src/main/java/ai/elimu/content_provider/utils/converter/CursorToStoryBookParagraphGsonConverter.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ object CursorToStoryBookParagraphGsonConverter {
5757
wordsCursor.moveToNext()
5858

5959
// Convert from Room to Gson
60-
val wordGson = CursorToWordGsonConverter.getWordGson(wordsCursor)
60+
val wordGson = CursorToWordGsonConverter.getWordGson(wordsCursor, context, contentProviderApplicationId)
6161
wordGsons.add(wordGson)
6262

6363
isLast = wordsCursor.isLast

0 commit comments

Comments
 (0)