Skip to content

Commit

Permalink
Merge pull request #3157 from CruGlobal/toolsRepositoryCleanup
Browse files Browse the repository at this point in the history
ToolsRepository cleanup
  • Loading branch information
frett authored Oct 13, 2023
2 parents 1f13db5 + 8a06f50 commit 2f65350
Show file tree
Hide file tree
Showing 26 changed files with 208 additions and 172 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class ToolsViewModel @Inject constructor(
.map { if (!it) BannerType.TOOL_LIST_FAVORITES else null }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), null)

val spotlightTools = toolsRepository.getToolsFlow()
val spotlightTools = toolsRepository.getNormalToolsFlow()
.map { it.filter { !it.isHidden && it.isSpotlight }.sortedWith(Tool.COMPARATOR_DEFAULT_ORDER) }
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), emptyList())

Expand All @@ -59,7 +59,7 @@ class ToolsViewModel @Inject constructor(

private val toolsForLocale = selectedLocale
.flatMapLatest {
if (it != null) toolsRepository.getToolsFlowForLanguage(it) else toolsRepository.getToolsFlow()
if (it != null) toolsRepository.getToolsFlowForLanguage(it) else toolsRepository.getNormalToolsFlow()
}
.map { it.filterNot { it.isHidden }.sortedBy { it.defaultOrder } }
.combine(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ToolDetailsViewModel @Inject constructor(
.flatMapLatest { metatool ->
when (metatool) {
null -> flowOf(emptyList())
else -> toolsRepository.getToolsFlow().map { it.filter { it.metatoolCode == metatool } }
else -> toolsRepository.getNormalToolsFlow().map { it.filter { it.metatoolCode == metatool } }
}
}
.stateIn(viewModelScope, SharingStarted.WhileSubscribed(), emptyList())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class ExternalSingletonsModule {
mockk {
every { getFavoriteToolsFlow() } returns flowOf(emptyList())
every { getLessonsFlow() } returns flowOf(emptyList())
every { getToolsFlow() } returns flowOf(emptyList())
every { getNormalToolsFlow() } returns flowOf(emptyList())
every { getMetaToolsFlow() } returns flowOf(emptyList())
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import org.cru.godtools.db.repository.ToolsRepository
import org.cru.godtools.model.Lesson
import org.cru.godtools.model.Tool
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers.contains
Expand All @@ -23,7 +22,7 @@ import org.junit.Test

@OptIn(ExperimentalCoroutinesApi::class)
class LessonsViewModelTest {
private val lessonsFlow = MutableStateFlow(emptyList<Lesson>())
private val lessonsFlow = MutableStateFlow(emptyList<Tool>())

private val toolsRepository: ToolsRepository = mockk {
every { getLessonsFlow() } returns lessonsFlow
Expand All @@ -45,15 +44,8 @@ class LessonsViewModelTest {

@Test
fun `Property lessons - Filter hidden lessons`() = testScope.runTest {
val visible = Lesson().apply {
type = Tool.Type.LESSON
code = "visible"
}
val hidden = Lesson().apply {
type = Tool.Type.LESSON
code = "hidden"
isHidden = true
}
val visible = Tool("visible", Tool.Type.LESSON)
val hidden = Tool("hidden", Tool.Type.LESSON) { isHidden = true }

viewModel.lessons.test {
lessonsFlow.value = listOf(visible, hidden)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ class ToolsViewModelTest {
}
private val testScope = TestScope()
private val toolsRepository: ToolsRepository = mockk {
every { getToolsFlow() } returns toolsFlow
every { getNormalToolsFlow() } returns toolsFlow
every { getMetaToolsFlow() } returns metaToolsFlow
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class ToolDetailsViewModelTest {

private val toolsRepository: ToolsRepository = mockk {
every { findToolFlow(any()) } returns toolFlow
every { getToolsFlow() } returns toolsFlow
every { getNormalToolsFlow() } returns toolsFlow
}
private val testScope = TestScope()

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,27 @@
package org.cru.godtools.db.repository

import androidx.annotation.WorkerThread
import java.util.Locale
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import org.cru.godtools.model.Lesson
import org.cru.godtools.model.Resource
import org.cru.godtools.model.Tool

interface ToolsRepository {
suspend fun findTool(code: String): Tool?
@WorkerThread
fun findResourceBlocking(code: String): Tool?
suspend fun getResources(): List<Resource>
@WorkerThread
fun getResourcesBlocking(): List<Resource>
suspend fun getTools(): List<Tool>

fun findToolFlow(code: String): Flow<Tool?>
fun getResourcesFlow(): Flow<List<Resource>>
fun getToolsFlow(): Flow<List<Tool>>

suspend fun getAllTools(): List<Tool>
suspend fun getToolsByType(types: Collection<Tool.Type>): List<Tool>
suspend fun getNormalTools() = getToolsByType(Tool.Type.NORMAL_TYPES)

fun getAllToolsFlow(): Flow<List<Tool>>
fun getToolsFlowByType(vararg types: Tool.Type) = getToolsFlowByType(types.toSet())
fun getToolsFlowByType(types: Collection<Tool.Type>): Flow<List<Tool>>
fun getToolsFlowForLanguage(locale: Locale): Flow<List<Tool>>
fun getMetaToolsFlow(): Flow<List<Tool>>
fun getFavoriteToolsFlow(): Flow<List<Tool>> =
getToolsFlow().map { it.filter { it.isFavorite }.sortedWith(Tool.COMPARATOR_FAVORITE_ORDER) }
fun getLessonsFlow(): Flow<List<Lesson>>
fun getMetaToolsFlow() = getToolsFlowByType(Tool.Type.META)
fun getLessonsFlow() = getToolsFlowByType(Tool.Type.LESSON)
fun getNormalToolsFlow() = getToolsFlowByType(Tool.Type.NORMAL_TYPES)
fun getFavoriteToolsFlow() = getNormalToolsFlow()
.map { it.filter { it.isFavorite }.sortedWith(Tool.COMPARATOR_FAVORITE_ORDER) }

fun toolsChangeFlow(): Flow<Any?>

Expand All @@ -35,7 +32,7 @@ interface ToolsRepository {
suspend fun updateToolViews(code: String, delta: Int)

// region Initial Content Methods
suspend fun storeInitialResources(tools: Collection<Resource>)
suspend fun storeInitialTools(tools: Collection<Tool>)
// endregion Initial Content Methods

// region Sync Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,16 @@ internal interface ToolsDao {
@Query("SELECT * FROM tools WHERE code = :code")
suspend fun findTool(code: String): ToolEntity?
@Query("SELECT * FROM tools WHERE code = :code")
fun findToolBlocking(code: String): ToolEntity?
@Query("SELECT * FROM tools WHERE code = :code")
fun findToolFlow(code: String): Flow<ToolEntity?>
suspend fun findToolFavorite(code: String): ToolFavorite?
@Query("SELECT * FROM tools WHERE id = :id")
fun findToolByIdBlocking(id: Long): ToolEntity?
@Query("SELECT * FROM tools WHERE code = :code")
fun findToolFavorite(code: String): ToolFavorite?
fun findToolFlow(code: String): Flow<ToolEntity?>

@Query("SELECT * FROM tools")
suspend fun getResources(): List<ToolEntity>
@Query("SELECT * FROM tools")
fun getResourcesBlocking(): List<ToolEntity>
suspend fun getTools(): List<ToolEntity>
@Query("SELECT * FROM tools")
fun getResourcesFlow(): Flow<List<ToolEntity>>
fun getToolsFlow(): Flow<List<ToolEntity>>
@Query("SELECT * FROM tools WHERE type in (:types)")
suspend fun getToolsByType(types: Collection<Tool.Type>): List<ToolEntity>
@Query("SELECT * FROM tools WHERE type in (:types)")
Expand All @@ -53,9 +49,9 @@ internal interface ToolsDao {
@Update(entity = ToolEntity::class)
suspend fun updateToolFavorites(tools: Collection<ToolFavorite>)
@Query("UPDATE tools SET `order` = ${Int.MAX_VALUE}")
fun resetToolOrder()
suspend fun resetToolOrder()
@Query("UPDATE tools SET `order` = :order WHERE code = :code")
fun updateToolOrder(code: String, order: Int)
suspend fun updateToolOrder(code: String, order: Int)
@Query("UPDATE tools SET pendingShares = pendingShares + :views WHERE code = :code")
suspend fun updateToolViews(code: String, views: Int)
@Delete
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,23 @@ import org.cru.godtools.db.repository.ToolsRepository
import org.cru.godtools.db.room.GodToolsRoomDatabase
import org.cru.godtools.db.room.entity.ToolEntity
import org.cru.godtools.db.room.entity.partial.SyncTool
import org.cru.godtools.model.Resource
import org.cru.godtools.model.Tool
import org.cru.godtools.model.trackChanges

private val TOOL_TYPES = setOf(Tool.Type.TRACT, Tool.Type.CYOA, Tool.Type.ARTICLE)

@Dao
internal abstract class ToolsRoomRepository(private val db: GodToolsRoomDatabase) : ToolsRepository {
private val dao get() = db.toolsDao

override suspend fun findTool(code: String) = dao.findTool(code)?.toModel()
override fun findResourceBlocking(code: String) = dao.findToolBlocking(code)?.toModel()
override fun findToolFlow(code: String) = dao.findToolFlow(code).map { it?.toModel() }

override suspend fun getResources() = dao.getResources().map { it.toModel() }
override fun getResourcesBlocking() = dao.getResourcesBlocking().map { it.toModel() }
override fun getResourcesFlow() = dao.getResourcesFlow().map { it.map { it.toModel() } }
override suspend fun getTools() = dao.getToolsByType(TOOL_TYPES).map { it.toModel() }
override fun getToolsFlow() = dao.getToolsByTypeFlow(TOOL_TYPES).map { it.map { it.toModel() } }
override suspend fun getAllTools() = dao.getTools().map { it.toModel() }
override suspend fun getToolsByType(types: Collection<Tool.Type>) = dao.getToolsByType(types).map { it.toModel() }
override fun getAllToolsFlow() = dao.getToolsFlow().map { it.map { it.toModel() } }
override fun getToolsFlowByType(types: Collection<Tool.Type>) =
dao.getToolsByTypeFlow(types).map { it.map { it.toModel() } }
override fun getToolsFlowForLanguage(locale: Locale) =
dao.getToolsFlowByTypeAndLanguage(TOOL_TYPES, locale).map { it.map { it.toModel() } }
override fun getMetaToolsFlow() = dao.getToolsByTypeFlow(setOf(Tool.Type.META)).map { it.map { it.toModel() } }
override fun getLessonsFlow() = dao.getToolsByTypeFlow(setOf(Tool.Type.LESSON)).map { it.map { it.toModel() } }
dao.getToolsFlowByTypeAndLanguage(Tool.Type.NORMAL_TYPES, locale).map { it.map { it.toModel() } }

override fun toolsChangeFlow(): Flow<Any?> = db.changeFlow("tools")

Expand Down Expand Up @@ -60,7 +54,7 @@ internal abstract class ToolsRoomRepository(private val db: GodToolsRoomDatabase

override suspend fun updateToolViews(code: String, delta: Int) = dao.updateToolViews(code, delta)

override suspend fun storeInitialResources(tools: Collection<Resource>) =
override suspend fun storeInitialTools(tools: Collection<Tool>) =
dao.insertOrIgnoreTools(tools.map { ToolEntity(it) })

// region Sync Methods
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ abstract class LanguagesRepositoryIT {
repository.getLanguagesFlowForToolCategory("cat1").test {
assertThat(awaitItem(), empty())

toolsRepository.storeInitialResources(
toolsRepository.storeInitialTools(
listOf(
Tool("tool1", category = "cat1"),
Tool("tool2", category = "cat2")
Expand All @@ -143,7 +143,7 @@ abstract class LanguagesRepositoryIT {
@Test
fun `getLanguagesFlowForToolCategory() - No Duplicate Languages`() = testScope.runTest {
val english = Language(Locale.ENGLISH)
toolsRepository.storeInitialResources(
toolsRepository.storeInitialTools(
listOf(
Tool("tool1", category = "cat1"),
Tool("tool2", category = "cat1")
Expand Down
Loading

0 comments on commit 2f65350

Please sign in to comment.