Skip to content

Commit

Permalink
Merge pull request #434 from willowtreeapps/chore/407-prepopulate-dat…
Browse files Browse the repository at this point in the history
…abase-work-7

Chore/407 prepopulate database work 7
  • Loading branch information
PaulKlauser authored Oct 9, 2023
2 parents 127e5ea + 1517a1d commit 7ee17a8
Show file tree
Hide file tree
Showing 31 changed files with 520 additions and 196 deletions.
146 changes: 146 additions & 0 deletions app/schemas/com.willowtree.vocable.room.VocableDatabase/7.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
{
"formatVersion": 1,
"database": {
"version": 7,
"identityHash": "8470b9d2d5d0ffc958410af9b6617d9c",
"entities": [
{
"tableName": "Category",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`category_id` TEXT NOT NULL, `creation_date` INTEGER NOT NULL, `resource_id` INTEGER, `localized_name` TEXT, `hidden` INTEGER NOT NULL, `sort_order` INTEGER NOT NULL, PRIMARY KEY(`category_id`))",
"fields": [
{
"fieldPath": "categoryId",
"columnName": "category_id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "creationDate",
"columnName": "creation_date",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "resourceId",
"columnName": "resource_id",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "localizedName",
"columnName": "localized_name",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "hidden",
"columnName": "hidden",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "sortOrder",
"columnName": "sort_order",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"category_id"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "Phrase",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`phrase_id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, `parent_category_id` TEXT, `creation_date` INTEGER NOT NULL, `last_spoken_date` INTEGER, `localized_utterance` TEXT, `sort_order` INTEGER NOT NULL)",
"fields": [
{
"fieldPath": "phraseId",
"columnName": "phrase_id",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "parentCategoryId",
"columnName": "parent_category_id",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "creationDate",
"columnName": "creation_date",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "lastSpokenDate",
"columnName": "last_spoken_date",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "localizedUtterance",
"columnName": "localized_utterance",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "sortOrder",
"columnName": "sort_order",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": true,
"columnNames": [
"phrase_id"
]
},
"indices": [],
"foreignKeys": []
},
{
"tableName": "PresetCategory",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`category_id` TEXT NOT NULL, `hidden` INTEGER NOT NULL, `sort_order` INTEGER NOT NULL, PRIMARY KEY(`category_id`))",
"fields": [
{
"fieldPath": "categoryId",
"columnName": "category_id",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "hidden",
"columnName": "hidden",
"affinity": "INTEGER",
"notNull": true
},
{
"fieldPath": "sortOrder",
"columnName": "sort_order",
"affinity": "INTEGER",
"notNull": true
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"category_id"
]
},
"indices": [],
"foreignKeys": []
}
],
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '8470b9d2d5d0ffc958410af9b6617d9c')"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.willowtree.vocable.presets

import androidx.test.core.app.ApplicationProvider
import androidx.test.ext.junit.runners.AndroidJUnit4
import kotlinx.coroutines.test.runTest
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class RoomPresetCategoriesRepositoryTest {
private fun createRepository(): RoomPresetCategoriesRepository {
return RoomPresetCategoriesRepository(ApplicationProvider.getApplicationContext())
}

@Test
fun preset_categories_returned() = runTest {
val repository = createRepository()

assertEquals(
PresetCategories.values().filter { it != PresetCategories.MY_SAYINGS }.map {
Category.PresetCategory(
it.id,
it.initialSortOrder,
false,
it.getNameId()
)
},
repository.getPresetCategories()
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import org.junit.Test
import org.junit.runner.RunWith
import java.io.IOException
import java.util.*
import kotlin.collections.HashMap

@RunWith(AndroidJUnit4::class)
class MigrationTest {
Expand Down Expand Up @@ -338,8 +337,8 @@ class MigrationTest {
), categories
)

val customPhrases = db.categoryDao().getCategoryWithPhrases("custom")?.phrases
val recentPhrases = db.categoryDao().getCategoryWithPhrases("recents")?.phrases
val customPhrases = db.phraseDao().getPhrasesForCategory("custom")
val recentPhrases = db.phraseDao().getPhrasesForCategory("recents")
assertEquals(
listOf(
PhraseDto(
Expand Down
8 changes: 7 additions & 1 deletion app/src/main/java/com/willowtree/vocable/AppKoinModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@ package com.willowtree.vocable
import com.squareup.moshi.Moshi
import com.squareup.moshi.kotlin.reflect.KotlinJsonAdapterFactory
import com.willowtree.vocable.presets.IPresetsRepository
import com.willowtree.vocable.presets.PresetCategoriesRepository
import com.willowtree.vocable.presets.PresetsRepository
import com.willowtree.vocable.presets.PresetsViewModel
import com.willowtree.vocable.presets.RoomPresetCategoriesRepository
import com.willowtree.vocable.room.RoomStoredCategoriesRepository
import com.willowtree.vocable.room.StoredCategoriesRepository
import com.willowtree.vocable.settings.AddUpdateCategoryViewModel
import com.willowtree.vocable.settings.EditCategoriesViewModel
import com.willowtree.vocable.settings.EditCategoryMenuViewModel
Expand All @@ -28,11 +32,13 @@ object AppKoinModule {
single { PresetsRepository(get()) } bind IPresetsRepository::class
single { Moshi.Builder().add(KotlinJsonAdapterFactory()).build() }
single { LocalizedResourceUtility() } bind ILocalizedResourceUtility::class
single { CategoriesUseCase(get(), get(), get(), get()) }
single { CategoriesUseCase(get(), get(), get(), get(), get(), get()) } bind ICategoriesUseCase::class
single { PhrasesUseCase(get(), get()) }
single { RandomUUIDProvider() } bind UUIDProvider::class
single { JavaDateProvider() } bind DateProvider::class
single { JavaLocaleProvider() } bind LocaleProvider::class
single { RoomStoredCategoriesRepository(get()) } bind StoredCategoriesRepository::class
single { RoomPresetCategoriesRepository(get()) } bind PresetCategoriesRepository::class
viewModel { PresetsViewModel(get(), get()) }
viewModel { EditCategoriesViewModel(get(), get(), get()) }
viewModel { AddUpdateCategoryViewModel(get(), get(), get()) }
Expand Down
25 changes: 17 additions & 8 deletions app/src/main/java/com/willowtree/vocable/CategoriesUseCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package com.willowtree.vocable

import com.willowtree.vocable.presets.Category
import com.willowtree.vocable.presets.IPresetsRepository
import com.willowtree.vocable.presets.PresetCategoriesRepository
import com.willowtree.vocable.presets.asCategory
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
Expand All @@ -15,30 +17,37 @@ class CategoriesUseCase(
private val presetsRepository: IPresetsRepository,
private val uuidProvider: UUIDProvider,
private val dateProvider: DateProvider,
private val localeProvider: LocaleProvider
) {
private val localeProvider: LocaleProvider,
private val storedCategoriesRepository: StoredCategoriesRepository,
private val presetCategoriesRepository: PresetCategoriesRepository
) : ICategoriesUseCase {

fun categories(): Flow<List<Category>> {
return presetsRepository.getAllCategoriesFlow()
.map { categoryDtoList -> categoryDtoList.map { it.asCategory() } }
override fun categories(): Flow<List<Category>> {
return storedCategoriesRepository.getAllCategories()
.map { categoryDtoList ->
categoryDtoList.map { it.asCategory() } + presetCategoriesRepository.getPresetCategories()
}
}

suspend fun getCategoryById(categoryId: String): Category =
presetsRepository.getCategoryById(categoryId).asCategory()

suspend fun updateCategoryName(categoryId: String, localizedName: Map<String, String>) {
override suspend fun updateCategoryName(
categoryId: String,
localizedName: Map<String, String>
) {
presetsRepository.updateCategoryName(categoryId, localizedName)
}

suspend fun updateCategoryHidden(categoryId: String, hidden: Boolean) {
presetsRepository.updateCategoryHidden(categoryId, hidden)
}

suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>) {
override suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>) {
presetsRepository.updateCategorySortOrders(categorySortOrders)
}

suspend fun addCategory(categoryName: String, sortOrder: Int) {
override suspend fun addCategory(categoryName: String, sortOrder: Int) {
presetsRepository.addCategory(
CategoryDto(
uuidProvider.randomUUIDString(),
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/java/com/willowtree/vocable/ICategoriesUseCase.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.willowtree.vocable

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

interface ICategoriesUseCase {
fun categories(): Flow<List<Category>>
suspend fun updateCategoryName(categoryId: String, localizedName: Map<String, String>)
suspend fun addCategory(categoryName: String, sortOrder: Int)
suspend fun updateCategorySortOrders(categorySortOrders: List<CategorySortOrder>)
}
31 changes: 17 additions & 14 deletions app/src/main/java/com/willowtree/vocable/presets/Category.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,56 @@ sealed class Category : Parcelable {
abstract val resourceId: Int?

abstract fun withSortOrder(sortOrder: Int): Category
abstract fun withLocalizedName(localizedName: Map<String, String>?): Category
abstract fun withHidden(hidden: Boolean): Category

@Parcelize
data class StoredCategory(
override val categoryId: String,
val creationDate: Long,
override val resourceId: Int?,
override val localizedName: Map<String, String>?,
override var hidden: Boolean,
override var sortOrder: Int
) : Category() {
override fun withSortOrder(sortOrder: Int): Category = copy(sortOrder = sortOrder)
override fun withLocalizedName(localizedName: Map<String, String>?): Category =
copy(localizedName = localizedName)

override fun withHidden(hidden: Boolean): Category = copy(hidden = hidden)
}

@Parcelize
data class Recents(
override val hidden: Boolean,
override val sortOrder: Int,
override val resourceId: Int?,
override val localizedName: Map<String, String>?,
override val resourceId: Int?
override val hidden: Boolean,
override val sortOrder: Int
) : Category() {
override val categoryId: String = PresetCategories.RECENTS.id
override fun withSortOrder(sortOrder: Int): Category = copy(sortOrder = sortOrder)
override fun withLocalizedName(localizedName: Map<String, String>?): Category =
copy(localizedName = localizedName)
override fun withHidden(hidden: Boolean): Category = copy(hidden = hidden)
}

@Parcelize
data class PresetCategory(
override val categoryId: String,
override val sortOrder: Int,
override val hidden: Boolean,
override val resourceId: Int
) : Category() {
override val localizedName: Map<String, String>? = null
override fun withSortOrder(sortOrder: Int): Category = copy(sortOrder = sortOrder)
override fun withHidden(hidden: Boolean): Category = copy(hidden = hidden)
}
}

fun CategoryDto.asCategory(): Category {
return if (categoryId == PresetCategories.RECENTS.id) {
Category.Recents(
hidden = hidden,
sortOrder = sortOrder,
resourceId = resourceId,
localizedName = localizedName,
resourceId = resourceId
hidden = hidden,
sortOrder = sortOrder
)
} else {
Category.StoredCategory(
categoryId,
creationDate,
resourceId,
localizedName,
hidden,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.willowtree.vocable.presets

interface PresetCategoriesRepository {
suspend fun getPresetCategories(): List<Category.PresetCategory>
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,6 @@ class PresetsFragment : BaseFragment<FragmentPresetsBinding>() {
binding.phrasesForwardButton.isVisible = categoriesExist
binding.categoryBackButton.isVisible = categoriesExist
binding.categoryForwardButton.isVisible = categoriesExist
binding.emptyAddPhraseButton.isVisible = categoriesExist
binding.emptyPhrasesText.isVisible = categoriesExist

binding.emptyCategoriesText.isVisible = !categoriesExist

Expand Down
Loading

0 comments on commit 7ee17a8

Please sign in to comment.