Skip to content

Commit

Permalink
Merge pull request #440 from willowtreeapps/VOC-406/make-localized-ty…
Browse files Browse the repository at this point in the history
…pes-more-clear

VOC-406 Introduces LocalesWithText as a way to abstract away from Map<String,String>
  • Loading branch information
IanCrossCD authored Nov 21, 2023
2 parents 9b3702e + d629b37 commit fa87dec
Show file tree
Hide file tree
Showing 42 changed files with 295 additions and 190 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.platform.app.InstrumentationRegistry
import com.willowtree.vocable.presets.PresetCategories
import com.willowtree.vocable.utils.VocableSharedPreferences
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.coroutines.test.runTest
import org.junit.Assert
import org.junit.Assert.assertEquals
Expand Down Expand Up @@ -322,15 +323,15 @@ class MigrationTest {
"custom",
0L,
null,
mapOf("english" to "custom"),
LocalesWithText( mapOf("english" to "custom")),
false,
7
),
CategoryDto(
"recents",
0L,
null,
mapOf("english" to "recents"),
LocalesWithText( mapOf("english" to "recents")),
false,
8
)
Expand All @@ -346,7 +347,7 @@ class MigrationTest {
"custom",
0L,
0L,
mapOf("english" to "hi"),
LocalesWithText( mapOf("english" to "hi")),
0
)
), customPhrases
Expand All @@ -358,7 +359,7 @@ class MigrationTest {
"recents",
0L,
0L,
mapOf("english" to "hi"),
LocalesWithText( mapOf("english" to "hi")),
0
)
), recentPhrases
Expand Down
6 changes: 3 additions & 3 deletions app/src/main/java/com/willowtree/vocable/AppKoinModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import com.willowtree.vocable.settings.EditCategoryMenuViewModel
import com.willowtree.vocable.utils.DateProvider
import com.willowtree.vocable.utils.ILocalizedResourceUtility
import com.willowtree.vocable.utils.JavaDateProvider
import com.willowtree.vocable.utils.JavaLocaleProvider
import com.willowtree.vocable.utils.LocaleProvider
import com.willowtree.vocable.utils.LocalizedResourceUtility
import com.willowtree.vocable.utils.locale.JavaLocaleProvider
import com.willowtree.vocable.utils.locale.LocaleProvider
import com.willowtree.vocable.utils.locale.LocalizedResourceUtility
import com.willowtree.vocable.utils.RandomUUIDProvider
import com.willowtree.vocable.utils.UUIDProvider
import com.willowtree.vocable.utils.VocableSharedPreferences
Expand Down
7 changes: 4 additions & 3 deletions app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import com.willowtree.vocable.room.CategoryDto
import com.willowtree.vocable.room.CategorySortOrder
import com.willowtree.vocable.room.StoredCategoriesRepository
import com.willowtree.vocable.utils.DateProvider
import com.willowtree.vocable.utils.LocaleProvider
import com.willowtree.vocable.utils.UUIDProvider
import com.willowtree.vocable.utils.locale.LocalesWithText
import com.willowtree.vocable.utils.locale.LocaleProvider
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map

Expand All @@ -34,7 +35,7 @@ class CategoriesUseCase(

override suspend fun updateCategoryName(
categoryId: String,
localizedName: Map<String, String>
localizedName: LocalesWithText
) {
presetsRepository.updateCategoryName(categoryId, localizedName)
}
Expand All @@ -53,7 +54,7 @@ class CategoriesUseCase(
uuidProvider.randomUUIDString(),
dateProvider.currentTimeMillis(),
null,
mapOf(Pair(localeProvider.getDefaultLocaleString(), categoryName)),
LocalesWithText(mapOf(Pair(localeProvider.getDefaultLocaleString(), categoryName))),
false,
sortOrder
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package com.willowtree.vocable

import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.room.CategorySortOrder
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.coroutines.flow.Flow

interface ICategoriesUseCase {
fun categories(): Flow<List<Category>>
suspend fun updateCategoryName(categoryId: String, localizedName: Map<String, String>)
suspend fun updateCategoryName(categoryId: String, localizedName: LocalesWithText)
suspend fun addCategory(categoryName: String, sortOrder: Int)
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
}
5 changes: 3 additions & 2 deletions app/src/main/java/com/willowtree/vocable/PhrasesUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.willowtree.vocable.presets.PresetCategories
import com.willowtree.vocable.presets.asPhrase
import com.willowtree.vocable.room.PhraseDto
import com.willowtree.vocable.utils.DateProvider
import com.willowtree.vocable.utils.locale.LocalesWithText

class PhrasesUseCase(
private val presetsRepository: IPresetsRepository,
Expand All @@ -26,11 +27,11 @@ class PhrasesUseCase(
presetsRepository.deletePhrase(phraseId)
}

suspend fun updatePhrase(phraseId: Long, localizedUtterance: Map<String, String>) {
suspend fun updatePhrase(phraseId: Long, localizedUtterance: LocalesWithText) {
presetsRepository.updatePhrase(phraseId, localizedUtterance)
}

suspend fun addPhrase(localizedUtterance: Map<String, String>, parentCategoryId: String) {
suspend fun addPhrase(localizedUtterance: LocalesWithText, parentCategoryId: String) {
presetsRepository.addPhrase(PhraseDto(
0L,
parentCategoryId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import com.willowtree.vocable.customviews.CategoryButton
import com.willowtree.vocable.customviews.PointerListener
import com.willowtree.vocable.databinding.CategoriesFragmentBinding
import com.willowtree.vocable.databinding.CategoryButtonBinding
import com.willowtree.vocable.utils.LocalizedResourceUtility
import com.willowtree.vocable.utils.locale.LocalizedResourceUtility
import org.koin.android.ext.android.inject
import org.koin.androidx.viewmodel.ViewModelOwner
import org.koin.androidx.viewmodel.ext.android.viewModel
Expand Down
9 changes: 5 additions & 4 deletions app/src/main/java/com/willowtree/vocable/presets/Category.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package com.willowtree.vocable.presets

import android.os.Parcelable
import com.willowtree.vocable.room.CategoryDto
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.parcelize.Parcelize

sealed class Category : Parcelable {
abstract val categoryId: String
abstract val sortOrder: Int
abstract val hidden: Boolean
abstract val localizedName: Map<String, String>?
abstract val localizedName: LocalesWithText?
abstract val resourceId: Int?

abstract fun withSortOrder(sortOrder: Int): Category
Expand All @@ -18,7 +19,7 @@ sealed class Category : Parcelable {
data class StoredCategory(
override val categoryId: String,
override val resourceId: Int?,
override val localizedName: Map<String, String>?,
override val localizedName: LocalesWithText?,
override var hidden: Boolean,
override var sortOrder: Int
) : Category() {
Expand All @@ -29,7 +30,7 @@ sealed class Category : Parcelable {
@Parcelize
data class Recents(
override val resourceId: Int?,
override val localizedName: Map<String, String>?,
override val localizedName: LocalesWithText?,
override val hidden: Boolean,
override val sortOrder: Int
) : Category() {
Expand All @@ -45,7 +46,7 @@ sealed class Category : Parcelable {
override val hidden: Boolean,
override val resourceId: Int
) : Category() {
override val localizedName: Map<String, String>? = null
override val localizedName: LocalesWithText? = null
override fun withSortOrder(sortOrder: Int): Category = copy(sortOrder = sortOrder)
override fun withHidden(hidden: Boolean): Category = copy(hidden = hidden)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.willowtree.vocable.presets
import com.willowtree.vocable.room.CategoryDto
import com.willowtree.vocable.room.CategorySortOrder
import com.willowtree.vocable.room.PhraseDto
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.coroutines.flow.Flow

//TODO: PK - Rename this once we make the jump to rename [PresetsRepository] -> "RoomPresetsRepository"
Expand All @@ -20,13 +21,13 @@ interface IPresetsRepository {
suspend fun getAllCategories(): List<CategoryDto>
suspend fun deletePhrase(phraseId: Long)
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
suspend fun updateCategoryName(categoryId: String, localizedName: Map<String, String>)
suspend fun updateCategoryName(categoryId: String, localizedName: LocalesWithText)
suspend fun updateCategoryHidden(categoryId: String, hidden: Boolean)
suspend fun addCategory(category: CategoryDto)
suspend fun getCategoryById(categoryId: String): CategoryDto
suspend fun deleteCategory(categoryId: String)
suspend fun getRecentPhrases(): List<PhraseDto>
suspend fun updatePhraseLastSpoken(phraseId: Long, lastSpokenDate: Long)
suspend fun updatePhrase(phraseId: Long, localizedUtterance: Map<String, String>)
suspend fun updatePhrase(phraseId: Long, localizedUtterance: LocalesWithText)
suspend fun addPhrase(phrase: PhraseDto)
}
5 changes: 3 additions & 2 deletions app/src/main/java/com/willowtree/vocable/presets/Phrase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package com.willowtree.vocable.presets

import android.os.Parcelable
import com.willowtree.vocable.room.PhraseDto
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.parcelize.Parcelize

sealed class Phrase : Parcelable {
abstract val phraseId: Long
abstract val localizedUtterance: Map<String, String>?
abstract val localizedUtterance: LocalesWithText?
abstract val sortOrder: Int
}

@Parcelize
data class CustomPhrase(
override val phraseId: Long,
override val localizedUtterance: Map<String, String>?,
override val localizedUtterance: LocalesWithText?,
override val sortOrder: Int
) : Phrase(), Parcelable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.willowtree.vocable.room.PhraseDto
import com.willowtree.vocable.room.PhraseLocalizedUtterance
import com.willowtree.vocable.room.PhraseSpokenDate
import com.willowtree.vocable.room.VocableDatabase
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.coroutines.flow.Flow
import org.koin.core.component.KoinComponent
import org.koin.core.component.get
Expand Down Expand Up @@ -59,15 +60,15 @@ class PresetsRepository(val context: Context) : KoinComponent, IPresetsRepositor
database.categoryDao().deleteCategory(categoryId)
}

override suspend fun updatePhrase(phraseId: Long, localizedUtterance: Map<String, String>) {
override suspend fun updatePhrase(phraseId: Long, localizedUtterance: LocalesWithText) {
database.phraseDao().updatePhraseLocalizedUtterance(PhraseLocalizedUtterance(phraseId, localizedUtterance))
}

override suspend fun updatePhraseLastSpoken(phraseId: Long, lastSpokenDate: Long) {
database.phraseDao().updatePhraseSpokenDate(PhraseSpokenDate(phraseId, lastSpokenDate))
}

override suspend fun updateCategoryName(categoryId: String, localizedName: Map<String, String>) {
override suspend fun updateCategoryName(categoryId: String, localizedName: LocalesWithText) {
database.categoryDao().updateCategory(CategoryLocalizedName(categoryId, localizedName))
}

Expand All @@ -93,7 +94,7 @@ class PresetsRepository(val context: Context) : KoinComponent, IPresetsRepositor
presetCategory.id,
System.currentTimeMillis(),
null,
mapOf(Pair(Locale.getDefault().toString(), context.getString(phrasesIds.getResourceId(index, -1)))),
LocalesWithText(mapOf(Pair(Locale.getDefault().toString(), context.getString(phrasesIds.getResourceId(index, -1))))),
phraseObjects.size
)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import com.willowtree.vocable.R
import com.willowtree.vocable.databinding.PhraseButtonAddBinding
import com.willowtree.vocable.databinding.PhraseButtonBinding
import com.willowtree.vocable.presets.Phrase
import com.willowtree.vocable.utils.LocalizedResourceUtility
import com.willowtree.vocable.utils.locale.LocalizedResourceUtility
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
import java.util.Locale
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/java/com/willowtree/vocable/room/CategoryDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.parcelize.Parcelize

@Entity(tableName = "Category")
Expand All @@ -12,7 +13,7 @@ data class CategoryDto(
@PrimaryKey @ColumnInfo(name = "category_id") val categoryId: String,
@ColumnInfo(name = "creation_date") val creationDate: Long,
@ColumnInfo(name = "resource_id") val resourceId: Int?,
@ColumnInfo(name = "localized_name") var localizedName: Map<String, String>?,
@ColumnInfo(name = "localized_name") var localizedName: LocalesWithText?,
var hidden: Boolean,
@ColumnInfo(name = "sort_order") var sortOrder: Int
) : Parcelable
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.willowtree.vocable.room

import androidx.room.ColumnInfo
import com.willowtree.vocable.utils.locale.LocalesWithText

data class CategoryLocalizedName(
@ColumnInfo(name = "category_id") val categoryId: String,
@ColumnInfo(name = "localized_name") var localizedName: Map<String, String>
@ColumnInfo(name = "localized_name") var localizedName: LocalesWithText
)
29 changes: 25 additions & 4 deletions app/src/main/java/com/willowtree/vocable/room/Converters.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import androidx.room.TypeConverter
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Moshi
import com.squareup.moshi.Types
import com.willowtree.vocable.utils.locale.LocaleString
import com.willowtree.vocable.utils.locale.LocalesWithText
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject

Expand All @@ -14,8 +16,7 @@ object Converters : KoinComponent {
@TypeConverter
@JvmStatic
fun stringMapToJson(stringMap: Map<String, String>?): String {
val type =
Types.newParameterizedType(Map::class.java, String::class.java, String::class.java)
val type = Types.newParameterizedType(Map::class.java, String::class.java, String::class.java)
val adapter: JsonAdapter<Map<String, String>> = moshi.adapter(type)
return adapter.toJson(stringMap)
}
Expand All @@ -24,10 +25,30 @@ object Converters : KoinComponent {
@JvmStatic
fun jsonToStringMap(json: String?): Map<String, String>? {
return json?.let {
val type =
Types.newParameterizedType(Map::class.java, String::class.java, String::class.java)
val type = Types.newParameterizedType(Map::class.java, String::class.java, String::class.java)
val adapter: JsonAdapter<Map<String, String>> = moshi.adapter(type)
adapter.fromJson(it)
}
}

@TypeConverter
@JvmStatic
fun stringMapToLanguagesWithText(localesWithText: LocalesWithText): String {
val type = Types.newParameterizedType(Map::class.java, LocaleString::class.java, String::class.java)
val adapter: JsonAdapter<Map<LocaleString, String>> = moshi.adapter(type)
return adapter.toJson(localesWithText.localesTextMap)
}

@TypeConverter
@JvmStatic
fun languagesWithTextToStringMap(json: String?): LocalesWithText? {
return json?.let {
val type = Types.newParameterizedType(Map::class.java, LocaleString::class.java, String::class.java)
val adapter: JsonAdapter<Map<LocaleString, String>> = moshi.adapter(type)
adapter.fromJson(it)?.let { stringMap ->
LocalesWithText(stringMap)
}

}
}
}
3 changes: 2 additions & 1 deletion app/src/main/java/com/willowtree/vocable/room/PhraseDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.os.Parcelable
import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.parcelize.Parcelize

@Entity(tableName = "Phrase")
Expand All @@ -13,6 +14,6 @@ data class PhraseDto(
@ColumnInfo(name = "parent_category_id") val parentCategoryId: String?,
@ColumnInfo(name = "creation_date") val creationDate: Long,
@ColumnInfo(name = "last_spoken_date") val lastSpokenDate: Long?,
@ColumnInfo(name = "localized_utterance") var localizedUtterance: Map<String, String>?,
@ColumnInfo(name = "localized_utterance") var localizedUtterance: LocalesWithText?,
@ColumnInfo(name = "sort_order") var sortOrder: Int
) : Parcelable
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package com.willowtree.vocable.room

import androidx.room.ColumnInfo
import com.willowtree.vocable.utils.locale.LocalesWithText

data class PhraseLocalizedUtterance(
@ColumnInfo(name = "phrase_id") val phraseId: Long,
@ColumnInfo(name = "localized_utterance") var localizedUtterance: Map<String, String>
@ColumnInfo(name = "localized_utterance") var localizedUtterance: LocalesWithText
)
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.willowtree.vocable.PhrasesUseCase
import com.willowtree.vocable.presets.PresetsRepository
import com.willowtree.vocable.utils.locale.LocalesWithText
import kotlinx.coroutines.launch
import org.koin.core.component.KoinComponent
import org.koin.core.component.inject
Expand All @@ -26,7 +27,7 @@ class AddPhraseViewModel : ViewModel(), KoinComponent {
it.localizedUtterance?.containsValue(phraseStr) == true
}) {
phrasesUseCase.addPhrase(
mapOf(Pair(Locale.getDefault().toString(), phraseStr)),
LocalesWithText(mapOf(Pair(Locale.getDefault().toString(), phraseStr))),
categoryId
)
liveShowPhraseAdded.postValue(true)
Expand Down
Loading

0 comments on commit fa87dec

Please sign in to comment.