Skip to content

Commit

Permalink
update unpinTool to track changes
Browse files Browse the repository at this point in the history
  • Loading branch information
frett committed Sep 19, 2023
1 parent 78e31bd commit a7d7253
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ internal interface ToolsDao {
suspend fun upsertSyncTools(tools: Collection<SyncTool>)
@Update(entity = ToolEntity::class)
suspend fun update(tool: ToolFavorite)
@Query("UPDATE tools SET isFavorite = :isFavorite WHERE code = :code")
suspend fun updateIsFavorite(code: String, isFavorite: Boolean)
@Query("UPDATE tools SET `order` = ${Int.MAX_VALUE}")
fun resetToolOrder()
@Query("UPDATE tools SET `order` = :order WHERE code = :code")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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)

Expand Down Expand Up @@ -42,7 +43,12 @@ internal abstract class ToolsRoomRepository(private val db: GodToolsRoomDatabase
tool.isFavorite = true
dao.update(tool)
}
override suspend fun unpinTool(code: String) = dao.updateIsFavorite(code, false)
@Transaction
override suspend fun unpinTool(code: String) {
val tool = dao.findToolFavorite(code) ?: return
tool.trackChanges { it.isFavorite = false }
dao.update(tool)
}

@Transaction
override suspend fun storeToolOrder(tools: List<String>) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,18 +346,45 @@ abstract class ToolsRepositoryIT {
}
// endregion pinTool()

// region unpinTool()
@Test
fun verifyUnpinTool() = testScope.runTest {
fun `unpinTool()`() = testScope.runTest {
val code = "pinTool"
repository.storeInitialResources(listOf(Tool(code) { isFavorite = true }))

repository.findToolFlow(code).test {
assertTrue(assertNotNull(awaitItem()).isFavorite)
assertNotNull(awaitItem()) {
assertTrue(it.isFavorite)
assertFalse(Tool.ATTR_IS_FAVORITE in it.changedFields)
}

repository.unpinTool(code)
assertFalse(assertNotNull(awaitItem()).isFavorite)
assertNotNull(awaitItem()) {
assertFalse(it.isFavorite)
assertTrue(Tool.ATTR_IS_FAVORITE in it.changedFields)
}
}
}

@Test
fun `unpinTool() - No Change`() = testScope.runTest {
val code = "pinTool"
repository.storeInitialResources(listOf(Tool(code) { isFavorite = false }))

repository.findToolFlow(code).test {
assertNotNull(awaitItem()) {
assertFalse(it.isFavorite)
assertFalse(Tool.ATTR_IS_FAVORITE in it.changedFields)
}

repository.unpinTool(code)
assertNotNull(awaitItem()) {
assertFalse(it.isFavorite)
assertFalse(Tool.ATTR_IS_FAVORITE in it.changedFields)
}
}
}
// endregion unpinTool()

// region storeToolOrder()
@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,12 @@ interface ChangeTrackingModel {
if (isTrackingChanges) changedFieldsStr = "$changedFieldsStr,$field"
}
}

fun <T : ChangeTrackingModel> T.trackChanges(block: (T) -> Unit) {
isTrackingChanges = true
try {
block(this)
} finally {
isTrackingChanges = false
}
}

0 comments on commit a7d7253

Please sign in to comment.