-
Notifications
You must be signed in to change notification settings - Fork 80
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
23 changed files
with
621 additions
and
464 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 0 additions & 37 deletions
37
.../src/main/java/com/example/util/simpletimetracker/data_local/extension/PrefsExtensions.kt
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 37 additions & 31 deletions
68
...ocal/src/main/java/com/example/util/simpletimetracker/data_local/repo/CategoryRepoImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
data_local/src/main/java/com/example/util/simpletimetracker/data_local/repo/PrefsRepoImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.