Skip to content

Commit

Permalink
add data cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Razeeman committed Dec 23, 2023
1 parent df3a13a commit 222e430
Show file tree
Hide file tree
Showing 23 changed files with 621 additions and 464 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import com.example.util.simpletimetracker.utils.clickOnRecyclerItem
import com.example.util.simpletimetracker.utils.clickOnViewWithText
import com.example.util.simpletimetracker.utils.longClickOnView
import com.example.util.simpletimetracker.utils.scrollRecyclerToView
import com.example.util.simpletimetracker.utils.tryAction
import com.example.util.simpletimetracker.utils.typeTextIntoView
import com.example.util.simpletimetracker.utils.withCardColor
import dagger.hilt.android.testing.HiltAndroidTest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ interface RecordTypeGoalDao {
@Query("SELECT * FROM recordTypeGoals WHERE category_id = :categoryId")
suspend fun getByCategory(categoryId: Long): List<RecordTypeGoalDBO>

@Transaction
@Query("SELECT * FROM recordTypeGoals WHERE category_id IN (:categoryIds)")
suspend fun getByCategories(categoryIds: List<Long>): List<RecordTypeGoalDBO>

@Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insert(recordTypeGoal: RecordTypeGoalDBO): Long

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.example.util.simpletimetracker.data_local.repo

import com.example.util.simpletimetracker.data_local.database.ActivityFilterDao
import com.example.util.simpletimetracker.data_local.utils.withLockedCache
import com.example.util.simpletimetracker.data_local.mapper.ActivityFilterDataLocalMapper
import com.example.util.simpletimetracker.data_local.utils.removeIf
import com.example.util.simpletimetracker.domain.model.ActivityFilter
import com.example.util.simpletimetracker.domain.repo.ActivityFilterRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
import javax.inject.Singleton

Expand All @@ -16,37 +16,43 @@ class ActivityFilterRepoImpl @Inject constructor(
private val activityFilterDataLocalMapper: ActivityFilterDataLocalMapper,
) : ActivityFilterRepo {

override suspend fun getAll(): List<ActivityFilter> = withContext(Dispatchers.IO) {
Timber.d("getAll")
activityFilterDao.getAll()
.map(activityFilterDataLocalMapper::map)
}

override suspend fun get(id: Long): ActivityFilter? = withContext(Dispatchers.IO) {
Timber.d("get")
activityFilterDao.get(id)
?.let(activityFilterDataLocalMapper::map)
}

override suspend fun add(activityFilter: ActivityFilter): Long = withContext(Dispatchers.IO) {
Timber.d("add")
return@withContext activityFilterDao.insert(
activityFilter.let(activityFilterDataLocalMapper::map)
)
}

override suspend fun changeSelected(id: Long, selected: Boolean) = withContext(Dispatchers.IO) {
Timber.d("changeSelected")
activityFilterDao.changeSelected(id, if (selected) 1 else 0)
}

override suspend fun remove(id: Long) = withContext(Dispatchers.IO) {
Timber.d("remove")
activityFilterDao.delete(id)
}

override suspend fun clear() = withContext(Dispatchers.IO) {
Timber.d("clear")
activityFilterDao.clear()
}
private var cache: List<ActivityFilter>? = null
private val mutex: Mutex = Mutex()

override suspend fun getAll(): List<ActivityFilter> = mutex.withLockedCache(
logMessage = "getAll",
accessCache = { cache },
accessSource = { activityFilterDao.getAll().map(activityFilterDataLocalMapper::map) },
afterSourceAccess = { cache = it },
)

override suspend fun get(id: Long): ActivityFilter? = mutex.withLockedCache(
logMessage = "get",
accessCache = { cache?.firstOrNull { it.id == id } },
accessSource = { activityFilterDao.get(id)?.let(activityFilterDataLocalMapper::map) },
)

override suspend fun add(activityFilter: ActivityFilter): Long = mutex.withLockedCache(
logMessage = "add",
accessSource = { activityFilterDao.insert(activityFilter.let(activityFilterDataLocalMapper::map)) },
afterSourceAccess = { cache = null },
)

override suspend fun changeSelected(id: Long, selected: Boolean) = mutex.withLockedCache(
logMessage = "changeSelected",
accessSource = { activityFilterDao.changeSelected(id, if (selected) 1 else 0) },
afterSourceAccess = { cache = cache?.map { if (it.id == id) it.copy(selected = selected) else it } },
)

override suspend fun remove(id: Long) = mutex.withLockedCache(
logMessage = "remove",
accessSource = { activityFilterDao.delete(id) },
afterSourceAccess = { cache = cache?.removeIf { it.id == id } },
)

override suspend fun clear() = mutex.withLockedCache(
logMessage = "clear",
accessSource = { activityFilterDao.clear() },
afterSourceAccess = { cache = null },
)
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,52 @@
package com.example.util.simpletimetracker.data_local.repo

import com.example.util.simpletimetracker.data_local.database.CategoryDao
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.utils.removeIf
import com.example.util.simpletimetracker.domain.model.Category
import com.example.util.simpletimetracker.domain.repo.CategoryRepo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import kotlinx.coroutines.sync.Mutex
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class CategoryRepoImpl @Inject constructor(
private val categoryDao: CategoryDao,
private val categoryDataLocalMapper: CategoryDataLocalMapper
private val categoryDataLocalMapper: CategoryDataLocalMapper,
) : CategoryRepo {

override suspend fun getAll(): List<Category> = withContext(Dispatchers.IO) {
Timber.d("getAll")
categoryDao.getAll()
.map(categoryDataLocalMapper::map)
}

override suspend fun get(id: Long): Category? = withContext(Dispatchers.IO) {
Timber.d("get id")
categoryDao.get(id)?.let(categoryDataLocalMapper::map)
}

override suspend fun add(category: Category): Long = withContext(Dispatchers.IO) {
Timber.d("add")
return@withContext categoryDao.insert(
category.let(categoryDataLocalMapper::map)
)
}

override suspend fun remove(id: Long) = withContext(Dispatchers.IO) {
Timber.d("remove")
categoryDao.delete(id)
}

override suspend fun clear() = withContext(Dispatchers.IO) {
Timber.d("clear")
categoryDao.clear()
}
private var cache: List<Category>? = null
private val mutex: Mutex = Mutex()

override suspend fun getAll(): List<Category> = mutex.withLockedCache(
logMessage = "getAll",
accessCache = { cache },
accessSource = { categoryDao.getAll().map(categoryDataLocalMapper::map) },
afterSourceAccess = { cache = it },
)

override suspend fun get(id: Long): Category? = mutex.withLockedCache(
logMessage = "get id",
accessCache = { cache?.firstOrNull { it.id == id } },
accessSource = { categoryDao.get(id)?.let(categoryDataLocalMapper::map) },
)

override suspend fun add(category: Category): Long = mutex.withLockedCache(
logMessage = "add",
accessSource = { categoryDao.insert(category.let(categoryDataLocalMapper::map)) },
afterSourceAccess = { cache = null },
)

override suspend fun remove(id: Long) = mutex.withLockedCache(
logMessage = "remove",
accessSource = { categoryDao.delete(id) },
afterSourceAccess = { cache = cache?.removeIf { it.id == id } },
)

override suspend fun clear() = mutex.withLockedCache(
logMessage = "clear",
accessSource = { categoryDao.clear() },
afterSourceAccess = { cache = null },
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package com.example.util.simpletimetracker.data_local.repo

import com.example.util.simpletimetracker.data_local.database.FavouriteCommentDao
import com.example.util.simpletimetracker.data_local.mapper.FavouriteCommentDataLocalMapper
import com.example.util.simpletimetracker.data_local.utils.logDataAccess
import com.example.util.simpletimetracker.domain.model.FavouriteComment
import com.example.util.simpletimetracker.domain.repo.FavouriteCommentRepo
import javax.inject.Inject
import javax.inject.Singleton
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import timber.log.Timber
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
class FavouriteCommentRepoImpl @Inject constructor(
Expand All @@ -17,34 +17,34 @@ class FavouriteCommentRepoImpl @Inject constructor(
) : FavouriteCommentRepo {

override suspend fun getAll(): List<FavouriteComment> = withContext(Dispatchers.IO) {
Timber.d("getAll")
logDataAccess("getAll")
favouriteCommentDao.getAll().map(FavouriteCommentDataLocalMapper::map)
}

override suspend fun get(id: Long): FavouriteComment? = withContext(Dispatchers.IO) {
Timber.d("get id")
logDataAccess("get id")
favouriteCommentDao.get(id)?.let(FavouriteCommentDataLocalMapper::map)
}

override suspend fun get(text: String): FavouriteComment? = withContext(Dispatchers.IO) {
Timber.d("get text")
logDataAccess("get text")
favouriteCommentDao.get(text)?.let(FavouriteCommentDataLocalMapper::map)
}

override suspend fun add(comment: FavouriteComment): Long = withContext(Dispatchers.IO) {
Timber.d("add")
logDataAccess("add")
return@withContext favouriteCommentDao.insert(
comment.let(FavouriteCommentDataLocalMapper::map)
)
}

override suspend fun remove(id: Long) = withContext(Dispatchers.IO) {
Timber.d("remove")
logDataAccess("remove")
favouriteCommentDao.delete(id)
}

override suspend fun clear() = withContext(Dispatchers.IO) {
Timber.d("clear")
logDataAccess("clear")
favouriteCommentDao.clear()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.example.util.simpletimetracker.data_local.repo

import android.content.SharedPreferences
import com.example.util.simpletimetracker.data_local.extension.delegate
import com.example.util.simpletimetracker.data_local.utils.delegate
import com.example.util.simpletimetracker.domain.extension.orZero
import com.example.util.simpletimetracker.domain.model.ChartFilterType
import com.example.util.simpletimetracker.domain.model.QuickSettingsWidgetType
Expand Down
Loading

0 comments on commit 222e430

Please sign in to comment.