Skip to content

Commit 222e430

Browse files
committed
add data cache
1 parent df3a13a commit 222e430

File tree

23 files changed

+621
-464
lines changed

23 files changed

+621
-464
lines changed

app/src/androidTest/java/com/example/util/simpletimetracker/ChangeCategoryTest.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import com.example.util.simpletimetracker.utils.clickOnRecyclerItem
1717
import com.example.util.simpletimetracker.utils.clickOnViewWithText
1818
import com.example.util.simpletimetracker.utils.longClickOnView
1919
import com.example.util.simpletimetracker.utils.scrollRecyclerToView
20-
import com.example.util.simpletimetracker.utils.tryAction
2120
import com.example.util.simpletimetracker.utils.typeTextIntoView
2221
import com.example.util.simpletimetracker.utils.withCardColor
2322
import dagger.hilt.android.testing.HiltAndroidTest

data_local/src/main/java/com/example/util/simpletimetracker/data_local/database/RecordTypeGoalDao.kt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,6 @@ interface RecordTypeGoalDao {
2727
@Query("SELECT * FROM recordTypeGoals WHERE category_id = :categoryId")
2828
suspend fun getByCategory(categoryId: Long): List<RecordTypeGoalDBO>
2929

30-
@Transaction
31-
@Query("SELECT * FROM recordTypeGoals WHERE category_id IN (:categoryIds)")
32-
suspend fun getByCategories(categoryIds: List<Long>): List<RecordTypeGoalDBO>
33-
3430
@Insert(onConflict = OnConflictStrategy.REPLACE)
3531
suspend fun insert(recordTypeGoal: RecordTypeGoalDBO): Long
3632

data_local/src/main/java/com/example/util/simpletimetracker/data_local/extension/PrefsExtensions.kt

Lines changed: 0 additions & 37 deletions
This file was deleted.
Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
package com.example.util.simpletimetracker.data_local.repo
22

33
import com.example.util.simpletimetracker.data_local.database.ActivityFilterDao
4+
import com.example.util.simpletimetracker.data_local.utils.withLockedCache
45
import com.example.util.simpletimetracker.data_local.mapper.ActivityFilterDataLocalMapper
6+
import com.example.util.simpletimetracker.data_local.utils.removeIf
57
import com.example.util.simpletimetracker.domain.model.ActivityFilter
68
import com.example.util.simpletimetracker.domain.repo.ActivityFilterRepo
7-
import kotlinx.coroutines.Dispatchers
8-
import kotlinx.coroutines.withContext
9-
import timber.log.Timber
9+
import kotlinx.coroutines.sync.Mutex
1010
import javax.inject.Inject
1111
import javax.inject.Singleton
1212

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

19-
override suspend fun getAll(): List<ActivityFilter> = withContext(Dispatchers.IO) {
20-
Timber.d("getAll")
21-
activityFilterDao.getAll()
22-
.map(activityFilterDataLocalMapper::map)
23-
}
24-
25-
override suspend fun get(id: Long): ActivityFilter? = withContext(Dispatchers.IO) {
26-
Timber.d("get")
27-
activityFilterDao.get(id)
28-
?.let(activityFilterDataLocalMapper::map)
29-
}
30-
31-
override suspend fun add(activityFilter: ActivityFilter): Long = withContext(Dispatchers.IO) {
32-
Timber.d("add")
33-
return@withContext activityFilterDao.insert(
34-
activityFilter.let(activityFilterDataLocalMapper::map)
35-
)
36-
}
37-
38-
override suspend fun changeSelected(id: Long, selected: Boolean) = withContext(Dispatchers.IO) {
39-
Timber.d("changeSelected")
40-
activityFilterDao.changeSelected(id, if (selected) 1 else 0)
41-
}
42-
43-
override suspend fun remove(id: Long) = withContext(Dispatchers.IO) {
44-
Timber.d("remove")
45-
activityFilterDao.delete(id)
46-
}
47-
48-
override suspend fun clear() = withContext(Dispatchers.IO) {
49-
Timber.d("clear")
50-
activityFilterDao.clear()
51-
}
19+
private var cache: List<ActivityFilter>? = null
20+
private val mutex: Mutex = Mutex()
21+
22+
override suspend fun getAll(): List<ActivityFilter> = mutex.withLockedCache(
23+
logMessage = "getAll",
24+
accessCache = { cache },
25+
accessSource = { activityFilterDao.getAll().map(activityFilterDataLocalMapper::map) },
26+
afterSourceAccess = { cache = it },
27+
)
28+
29+
override suspend fun get(id: Long): ActivityFilter? = mutex.withLockedCache(
30+
logMessage = "get",
31+
accessCache = { cache?.firstOrNull { it.id == id } },
32+
accessSource = { activityFilterDao.get(id)?.let(activityFilterDataLocalMapper::map) },
33+
)
34+
35+
override suspend fun add(activityFilter: ActivityFilter): Long = mutex.withLockedCache(
36+
logMessage = "add",
37+
accessSource = { activityFilterDao.insert(activityFilter.let(activityFilterDataLocalMapper::map)) },
38+
afterSourceAccess = { cache = null },
39+
)
40+
41+
override suspend fun changeSelected(id: Long, selected: Boolean) = mutex.withLockedCache(
42+
logMessage = "changeSelected",
43+
accessSource = { activityFilterDao.changeSelected(id, if (selected) 1 else 0) },
44+
afterSourceAccess = { cache = cache?.map { if (it.id == id) it.copy(selected = selected) else it } },
45+
)
46+
47+
override suspend fun remove(id: Long) = mutex.withLockedCache(
48+
logMessage = "remove",
49+
accessSource = { activityFilterDao.delete(id) },
50+
afterSourceAccess = { cache = cache?.removeIf { it.id == id } },
51+
)
52+
53+
override suspend fun clear() = mutex.withLockedCache(
54+
logMessage = "clear",
55+
accessSource = { activityFilterDao.clear() },
56+
afterSourceAccess = { cache = null },
57+
)
5258
}
Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,52 @@
11
package com.example.util.simpletimetracker.data_local.repo
22

33
import com.example.util.simpletimetracker.data_local.database.CategoryDao
4+
import com.example.util.simpletimetracker.data_local.utils.withLockedCache
45
import com.example.util.simpletimetracker.data_local.mapper.CategoryDataLocalMapper
6+
import com.example.util.simpletimetracker.data_local.utils.removeIf
57
import com.example.util.simpletimetracker.domain.model.Category
68
import com.example.util.simpletimetracker.domain.repo.CategoryRepo
7-
import kotlinx.coroutines.Dispatchers
8-
import kotlinx.coroutines.withContext
9-
import timber.log.Timber
9+
import kotlinx.coroutines.sync.Mutex
1010
import javax.inject.Inject
1111
import javax.inject.Singleton
1212

1313
@Singleton
1414
class CategoryRepoImpl @Inject constructor(
1515
private val categoryDao: CategoryDao,
16-
private val categoryDataLocalMapper: CategoryDataLocalMapper
16+
private val categoryDataLocalMapper: CategoryDataLocalMapper,
1717
) : CategoryRepo {
1818

19-
override suspend fun getAll(): List<Category> = withContext(Dispatchers.IO) {
20-
Timber.d("getAll")
21-
categoryDao.getAll()
22-
.map(categoryDataLocalMapper::map)
23-
}
24-
25-
override suspend fun get(id: Long): Category? = withContext(Dispatchers.IO) {
26-
Timber.d("get id")
27-
categoryDao.get(id)?.let(categoryDataLocalMapper::map)
28-
}
29-
30-
override suspend fun add(category: Category): Long = withContext(Dispatchers.IO) {
31-
Timber.d("add")
32-
return@withContext categoryDao.insert(
33-
category.let(categoryDataLocalMapper::map)
34-
)
35-
}
36-
37-
override suspend fun remove(id: Long) = withContext(Dispatchers.IO) {
38-
Timber.d("remove")
39-
categoryDao.delete(id)
40-
}
41-
42-
override suspend fun clear() = withContext(Dispatchers.IO) {
43-
Timber.d("clear")
44-
categoryDao.clear()
45-
}
19+
private var cache: List<Category>? = null
20+
private val mutex: Mutex = Mutex()
21+
22+
override suspend fun getAll(): List<Category> = mutex.withLockedCache(
23+
logMessage = "getAll",
24+
accessCache = { cache },
25+
accessSource = { categoryDao.getAll().map(categoryDataLocalMapper::map) },
26+
afterSourceAccess = { cache = it },
27+
)
28+
29+
override suspend fun get(id: Long): Category? = mutex.withLockedCache(
30+
logMessage = "get id",
31+
accessCache = { cache?.firstOrNull { it.id == id } },
32+
accessSource = { categoryDao.get(id)?.let(categoryDataLocalMapper::map) },
33+
)
34+
35+
override suspend fun add(category: Category): Long = mutex.withLockedCache(
36+
logMessage = "add",
37+
accessSource = { categoryDao.insert(category.let(categoryDataLocalMapper::map)) },
38+
afterSourceAccess = { cache = null },
39+
)
40+
41+
override suspend fun remove(id: Long) = mutex.withLockedCache(
42+
logMessage = "remove",
43+
accessSource = { categoryDao.delete(id) },
44+
afterSourceAccess = { cache = cache?.removeIf { it.id == id } },
45+
)
46+
47+
override suspend fun clear() = mutex.withLockedCache(
48+
logMessage = "clear",
49+
accessSource = { categoryDao.clear() },
50+
afterSourceAccess = { cache = null },
51+
)
4652
}

data_local/src/main/java/com/example/util/simpletimetracker/data_local/repo/FavouriteCommentRepoImpl.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package com.example.util.simpletimetracker.data_local.repo
22

33
import com.example.util.simpletimetracker.data_local.database.FavouriteCommentDao
44
import com.example.util.simpletimetracker.data_local.mapper.FavouriteCommentDataLocalMapper
5+
import com.example.util.simpletimetracker.data_local.utils.logDataAccess
56
import com.example.util.simpletimetracker.domain.model.FavouriteComment
67
import com.example.util.simpletimetracker.domain.repo.FavouriteCommentRepo
7-
import javax.inject.Inject
8-
import javax.inject.Singleton
98
import kotlinx.coroutines.Dispatchers
109
import kotlinx.coroutines.withContext
11-
import timber.log.Timber
10+
import javax.inject.Inject
11+
import javax.inject.Singleton
1212

1313
@Singleton
1414
class FavouriteCommentRepoImpl @Inject constructor(
@@ -17,34 +17,34 @@ class FavouriteCommentRepoImpl @Inject constructor(
1717
) : FavouriteCommentRepo {
1818

1919
override suspend fun getAll(): List<FavouriteComment> = withContext(Dispatchers.IO) {
20-
Timber.d("getAll")
20+
logDataAccess("getAll")
2121
favouriteCommentDao.getAll().map(FavouriteCommentDataLocalMapper::map)
2222
}
2323

2424
override suspend fun get(id: Long): FavouriteComment? = withContext(Dispatchers.IO) {
25-
Timber.d("get id")
25+
logDataAccess("get id")
2626
favouriteCommentDao.get(id)?.let(FavouriteCommentDataLocalMapper::map)
2727
}
2828

2929
override suspend fun get(text: String): FavouriteComment? = withContext(Dispatchers.IO) {
30-
Timber.d("get text")
30+
logDataAccess("get text")
3131
favouriteCommentDao.get(text)?.let(FavouriteCommentDataLocalMapper::map)
3232
}
3333

3434
override suspend fun add(comment: FavouriteComment): Long = withContext(Dispatchers.IO) {
35-
Timber.d("add")
35+
logDataAccess("add")
3636
return@withContext favouriteCommentDao.insert(
3737
comment.let(FavouriteCommentDataLocalMapper::map)
3838
)
3939
}
4040

4141
override suspend fun remove(id: Long) = withContext(Dispatchers.IO) {
42-
Timber.d("remove")
42+
logDataAccess("remove")
4343
favouriteCommentDao.delete(id)
4444
}
4545

4646
override suspend fun clear() = withContext(Dispatchers.IO) {
47-
Timber.d("clear")
47+
logDataAccess("clear")
4848
favouriteCommentDao.clear()
4949
}
5050
}

data_local/src/main/java/com/example/util/simpletimetracker/data_local/repo/PrefsRepoImpl.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package com.example.util.simpletimetracker.data_local.repo
22

33
import android.content.SharedPreferences
4-
import com.example.util.simpletimetracker.data_local.extension.delegate
4+
import com.example.util.simpletimetracker.data_local.utils.delegate
55
import com.example.util.simpletimetracker.domain.extension.orZero
66
import com.example.util.simpletimetracker.domain.model.ChartFilterType
77
import com.example.util.simpletimetracker.domain.model.QuickSettingsWidgetType

0 commit comments

Comments
 (0)