Skip to content

Commit

Permalink
fix tags not changing on running record, widgets accessing cache betw…
Browse files Browse the repository at this point in the history
…een tag remove and add
  • Loading branch information
Razeeman committed Jan 20, 2024
1 parent ce7f7e1 commit 4ecec72
Show file tree
Hide file tree
Showing 14 changed files with 25 additions and 80 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.example.util.simpletimetracker.data_local.model.RecordToRecordTagDBO

@Suppress("unused")
@Dao
interface RecordToRecordTagDao {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import androidx.room.Transaction
import com.example.util.simpletimetracker.data_local.model.RecordTypeCategoryDBO
import com.example.util.simpletimetracker.data_local.model.RecordTypeWithCategoriesDBO

@Dao
interface RecordTypeCategoryDao {
Expand All @@ -21,10 +19,6 @@ interface RecordTypeCategoryDao {
@Query("SELECT record_type_id FROM recordTypeCategory WHERE category_id = :categoryId")
suspend fun getTypeIdsByCategory(categoryId: Long): List<Long>

@Transaction
@Query("SELECT * FROM recordTypes WHERE id = :typeId LIMIT 1")
suspend fun getTypeWithCategories(typeId: Long): RecordTypeWithCategoriesDBO?

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(recordTypeCategoryDBO: List<RecordTypeCategoryDBO>)

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ class RecordToRecordTagRepoImpl @Inject constructor(
dao.getAll().map(mapper::map)
}

override suspend fun getTagIdsByRecordId(recordId: Long): List<Long> =
withContext(Dispatchers.IO) {
logDataAccess("get tag ids")
dao.getTagIdsByRecordId(recordId)
}

override suspend fun getRecordIdsByTagId(tagId: Long): List<Long> =
withContext(Dispatchers.IO) {
logDataAccess("get record ids")
Expand All @@ -54,16 +48,6 @@ class RecordToRecordTagRepoImpl @Inject constructor(
}
}

override suspend fun removeRecordTags(recordId: Long, tagIds: List<Long>) =
withContext(Dispatchers.IO) {
logDataAccess("remove record tags")
tagIds.map {
mapper.map(recordId = recordId, recordTagId = it)
}.let {
dao.delete(it)
}
}

override suspend fun removeAllByTagId(tagId: Long) =
withContext(Dispatchers.IO) {
logDataAccess("remove all by tagId")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.example.util.simpletimetracker.data_local.repo

import com.example.util.simpletimetracker.data_local.database.RecordTypeCategoryDao
import com.example.util.simpletimetracker.data_local.utils.withLockedCache
import com.example.util.simpletimetracker.data_local.mapper.CategoryDataLocalMapper
import com.example.util.simpletimetracker.data_local.mapper.RecordTypeCategoryDataLocalMapper
import com.example.util.simpletimetracker.data_local.utils.removeIf
import com.example.util.simpletimetracker.domain.model.Category
import com.example.util.simpletimetracker.data_local.utils.withLockedCache
import com.example.util.simpletimetracker.domain.model.RecordTypeCategory
import com.example.util.simpletimetracker.domain.repo.RecordTypeCategoryRepo
import kotlinx.coroutines.sync.Mutex
Expand All @@ -15,7 +13,6 @@ import javax.inject.Singleton
@Singleton
class RecordTypeCategoryRepoImpl @Inject constructor(
private val recordTypeCategoryDao: RecordTypeCategoryDao,
private val categoryDataLocalMapper: CategoryDataLocalMapper,
private val recordTypeCategoryDataLocalMapper: RecordTypeCategoryDataLocalMapper,
) : RecordTypeCategoryRepo {

Expand All @@ -29,16 +26,6 @@ class RecordTypeCategoryRepoImpl @Inject constructor(
afterSourceAccess = { cache = it },
)

override suspend fun getCategoriesByType(typeId: Long): List<Category> = mutex.withLockedCache(
logMessage = "get categories",
accessSource = {
recordTypeCategoryDao.getTypeWithCategories(typeId)
?.categories
?.map(categoryDataLocalMapper::map)
.orEmpty()
},
)

override suspend fun getCategoryIdsByType(typeId: Long): Set<Long> = mutex.withLockedCache(
logMessage = "get category ids",
accessCache = { cache?.filter { it.recordTypeId == typeId }?.map { it.categoryId }?.toSet() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.example.util.simpletimetracker.data_local.database.RecordTypeGoalDao
import com.example.util.simpletimetracker.data_local.mapper.RecordTypeGoalDataLocalMapper
import com.example.util.simpletimetracker.data_local.utils.logDataAccess
import com.example.util.simpletimetracker.data_local.utils.removeIf
import com.example.util.simpletimetracker.data_local.utils.replaceWith
import com.example.util.simpletimetracker.data_local.utils.withLockedCache
import com.example.util.simpletimetracker.domain.model.RecordTypeGoal
import com.example.util.simpletimetracker.domain.model.RecordTypeGoal.IdData
Expand Down Expand Up @@ -57,7 +58,7 @@ class RecordTypeGoalRepoImpl @Inject constructor(
override suspend fun add(recordTypeGoal: RecordTypeGoal): Long = mutex.withLockedCache(
logMessage = "add",
accessSource = { dao.insert(recordTypeGoal.let(mapper::map)) },
afterSourceAccess = { cache = null },
afterSourceAccess = { cache = cache?.replaceWith(recordTypeGoal) { it.id == recordTypeGoal.id } },
)

override suspend fun remove(id: Long) = mutex.withLockedCache(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.example.util.simpletimetracker.data_local.repo
import com.example.util.simpletimetracker.data_local.database.RunningRecordDao
import com.example.util.simpletimetracker.data_local.mapper.RunningRecordDataLocalMapper
import com.example.util.simpletimetracker.data_local.utils.removeIf
import com.example.util.simpletimetracker.data_local.utils.replaceWith
import com.example.util.simpletimetracker.data_local.utils.withLockedCache
import com.example.util.simpletimetracker.domain.model.RunningRecord
import com.example.util.simpletimetracker.domain.repo.RunningRecordRepo
Expand Down Expand Up @@ -41,7 +42,7 @@ class RunningRecordRepoImpl @Inject constructor(
override suspend fun add(runningRecord: RunningRecord): Long = mutex.withLockedCache(
logMessage = "add",
accessSource = { dao.insert(runningRecord.let(mapper::map)) },
afterSourceAccess = { cache = null },
afterSourceAccess = { cache = cache?.replaceWith(runningRecord) { it.id == runningRecord.id } },
)

override suspend fun remove(id: Long) = mutex.withLockedCache(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import com.example.util.simpletimetracker.domain.model.Range
import com.example.util.simpletimetracker.domain.model.Record
import com.example.util.simpletimetracker.domain.model.RecordTag
import com.example.util.simpletimetracker.domain.model.RecordType
import com.example.util.simpletimetracker.domain.repo.CategoryRepo
import com.example.util.simpletimetracker.domain.repo.RecordRepo
import com.example.util.simpletimetracker.domain.repo.RecordTagRepo
import com.example.util.simpletimetracker.domain.repo.RecordTypeCategoryRepo
Expand All @@ -34,6 +35,7 @@ import timber.log.Timber
class CsvRepoImpl @Inject constructor(
private val contentResolver: ContentResolver,
private val recordTypeRepo: RecordTypeRepo,
private val categoryRepo: CategoryRepo,
private val recordRepo: RecordRepo,
private val recordTypeCategoryRepo: RecordTypeCategoryRepo,
private val recordTagRepo: RecordTagRepo,
Expand All @@ -56,9 +58,10 @@ class CsvRepoImpl @Inject constructor(
fileOutputStream?.write(CSV_HEADER.toByteArray())

val recordTypes = recordTypeRepo.getAll().associateBy { it.id }
val categories = categoryRepo.getAll().associateBy { it.id }
val recordTags = recordTagRepo.getAll()
val categories = recordTypes.map { (id, _) ->
id to recordTypeCategoryRepo.getCategoriesByType(id)
val typeToCategories = recordTypes.map { (id, _) ->
id to recordTypeCategoryRepo.getCategoryIdsByType(id).mapNotNull { categories[it] }
}.toMap()

// Write data
Expand All @@ -73,7 +76,7 @@ class CsvRepoImpl @Inject constructor(
toCsvString(
record = record,
recordType = recordTypes[record.typeId],
categories = categories[record.typeId].orEmpty(),
categories = typeToCategories[record.typeId].orEmpty(),
recordTags = recordTags.filter { it.id in record.tagIds }
)
?.toByteArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.example.util.simpletimetracker.domain.model.Range
import com.example.util.simpletimetracker.domain.model.Record
import com.example.util.simpletimetracker.domain.model.RecordTag
import com.example.util.simpletimetracker.domain.model.RecordType
import com.example.util.simpletimetracker.domain.repo.CategoryRepo
import com.example.util.simpletimetracker.domain.repo.RecordRepo
import com.example.util.simpletimetracker.domain.repo.RecordTagRepo
import com.example.util.simpletimetracker.domain.repo.RecordTypeCategoryRepo
Expand All @@ -30,6 +31,7 @@ import javax.inject.Inject
class IcsRepoImpl @Inject constructor(
private val contentResolver: ContentResolver,
private val recordTypeRepo: RecordTypeRepo,
private val categoryRepo: CategoryRepo,
private val recordRepo: RecordRepo,
private val recordTypeCategoryRepo: RecordTypeCategoryRepo,
private val recordTagRepo: RecordTagRepo,
Expand Down Expand Up @@ -62,9 +64,10 @@ class IcsRepoImpl @Inject constructor(
fileOutputStream?.write(ICS_HEADER.toByteArray())

val recordTypes = recordTypeRepo.getAll().associateBy { it.id }
val categories = categoryRepo.getAll().associateBy { it.id }
val recordTags = recordTagRepo.getAll()
val categories = recordTypes.map { (id, _) ->
id to recordTypeCategoryRepo.getCategoriesByType(id)
val typeToCategories = recordTypes.map { (id, _) ->
id to recordTypeCategoryRepo.getCategoryIdsByType(id).mapNotNull { categories[it] }
}.toMap()

// Write data
Expand All @@ -79,7 +82,7 @@ class IcsRepoImpl @Inject constructor(
toIcsString(
record = record,
recordType = recordTypes[record.typeId],
categories = categories[record.typeId].orEmpty(),
categories = typeToCategories[record.typeId].orEmpty(),
recordTags = recordTags.filter { it.id in record.tagIds }
)
?.toByteArray()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,11 @@ internal inline fun logPrefsDataAccess(logMessage: String) {
*/
inline fun <T> List<T>.removeIf(crossinline filter: (T) -> Boolean): List<T> {
return this.toMutableList().apply { removeAll { filter(it) } }
}

/**
* Produces a new list from original list by replacing elements satisfying filter block with new element.
*/
inline fun <T> List<T>.replaceWith(new: T, crossinline filter: (T) -> Boolean): List<T> {
return this.removeIf(filter).toMutableList().apply { add(new) }
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,4 @@ class FavouriteCommentInteractor @Inject constructor(
suspend fun remove(id: Long) {
favouriteCommentRepo.remove(id)
}

private fun sortByName(favouriteComment: List<FavouriteComment>): List<FavouriteComment> {
return favouriteComment
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,6 @@ class RecordTagInteractor @Inject constructor(
runningRecordToRecordTagRepo.removeAllByTagId(id)
}

suspend fun removeByType(typeId: Long) {
repo.removeByType(typeId)
}

// TODO remove sort and sort when needed.
private fun sort(items: List<RecordTag>): List<RecordTag> {
return items.sortedBy { it.name.lowercase(Locale.getDefault()) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@ interface RecordToRecordTagRepo {

suspend fun getAll(): List<RecordToRecordTag>

suspend fun getTagIdsByRecordId(recordId: Long): List<Long>

suspend fun getRecordIdsByTagId(tagId: Long): List<Long>

suspend fun add(recordToRecordTag: RecordToRecordTag)

suspend fun addRecordTags(recordId: Long, tagIds: List<Long>)

suspend fun removeRecordTags(recordId: Long, tagIds: List<Long>)

suspend fun removeAllByTagId(tagId: Long)

suspend fun removeAllByRecordId(recordId: Long)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.example.util.simpletimetracker.domain.repo

import com.example.util.simpletimetracker.domain.model.Category
import com.example.util.simpletimetracker.domain.model.RecordTypeCategory

interface RecordTypeCategoryRepo {

suspend fun getAll(): List<RecordTypeCategory>

suspend fun getCategoriesByType(typeId: Long): List<Category>

suspend fun getCategoryIdsByType(typeId: Long): Set<Long>

suspend fun getTypeIdsByCategory(categoryId: Long): Set<Long>
Expand Down

0 comments on commit 4ecec72

Please sign in to comment.