-
Notifications
You must be signed in to change notification settings - Fork 81
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
21 changed files
with
287 additions
and
9 deletions.
There are no files selected for viewing
19 changes: 19 additions & 0 deletions
19
...com/example/util/simpletimetracker/data_local/activitySuggestion/ActivitySuggestionDBO.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 |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.example.util.simpletimetracker.data_local.activitySuggestion | ||
|
||
import androidx.room.ColumnInfo | ||
import androidx.room.Entity | ||
import androidx.room.PrimaryKey | ||
|
||
@Entity(tableName = "activitySuggestion") | ||
data class ActivitySuggestionDBO( | ||
@PrimaryKey(autoGenerate = true) | ||
@ColumnInfo(name = "id") | ||
val id: Long, | ||
|
||
@ColumnInfo(name = "forTypeId") | ||
val forTypeId: Long, | ||
|
||
// Longs stored in string comma separated | ||
@ColumnInfo(name = "suggestionIds") | ||
val suggestionIds: String, | ||
) |
32 changes: 32 additions & 0 deletions
32
...com/example/util/simpletimetracker/data_local/activitySuggestion/ActivitySuggestionDao.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 |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example.util.simpletimetracker.data_local.activitySuggestion | ||
|
||
import androidx.room.Dao | ||
import androidx.room.Insert | ||
import androidx.room.OnConflictStrategy | ||
import androidx.room.Query | ||
import androidx.room.Transaction | ||
|
||
@Dao | ||
interface ActivitySuggestionDao { | ||
|
||
@Transaction | ||
@Query("SELECT * FROM activitySuggestion") | ||
suspend fun getAll(): List<ActivitySuggestionDBO> | ||
|
||
@Transaction | ||
@Query("SELECT * FROM activitySuggestion WHERE id = :id LIMIT 1") | ||
suspend fun get(id: Long): ActivitySuggestionDBO? | ||
|
||
@Transaction | ||
@Query("SELECT * FROM activitySuggestion WHERE :typeId in (forTypeId)") | ||
suspend fun getByTypeId(typeId: Long): List<ActivitySuggestionDBO> | ||
|
||
@Insert(onConflict = OnConflictStrategy.REPLACE) | ||
suspend fun insert(activityFilters: List<ActivitySuggestionDBO>) | ||
|
||
@Query("DELETE FROM activitySuggestion WHERE id = :id") | ||
suspend fun delete(id: Long) | ||
|
||
@Query("DELETE FROM activitySuggestion") | ||
suspend fun clear() | ||
} |
23 changes: 23 additions & 0 deletions
23
...util/simpletimetracker/data_local/activitySuggestion/ActivitySuggestionDataLocalMapper.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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.example.util.simpletimetracker.data_local.activitySuggestion | ||
|
||
import com.example.util.simpletimetracker.domain.activitySuggestion.model.ActivitySuggestion | ||
import javax.inject.Inject | ||
|
||
class ActivitySuggestionDataLocalMapper @Inject constructor() { | ||
|
||
fun map(dbo: ActivitySuggestionDBO): ActivitySuggestion { | ||
return ActivitySuggestion( | ||
id = dbo.id, | ||
forTypeId = dbo.forTypeId, | ||
suggestionIds = dbo.suggestionIds.split(',').mapNotNull(String::toLongOrNull), | ||
) | ||
} | ||
|
||
fun map(domain: ActivitySuggestion): ActivitySuggestionDBO { | ||
return ActivitySuggestionDBO( | ||
id = domain.id, | ||
forTypeId = domain.forTypeId, | ||
suggestionIds = domain.suggestionIds.joinToString(separator = ","), | ||
) | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
...xample/util/simpletimetracker/data_local/activitySuggestion/ActivitySuggestionRepoImpl.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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.example.util.simpletimetracker.data_local.activitySuggestion | ||
|
||
import com.example.util.simpletimetracker.data_local.base.removeIf | ||
import com.example.util.simpletimetracker.data_local.base.withLockedCache | ||
import com.example.util.simpletimetracker.domain.activitySuggestion.model.ActivitySuggestion | ||
import com.example.util.simpletimetracker.domain.activitySuggestion.repo.ActivitySuggestionRepo | ||
import kotlinx.coroutines.sync.Mutex | ||
import javax.inject.Inject | ||
import javax.inject.Singleton | ||
|
||
@Singleton | ||
class ActivitySuggestionRepoImpl @Inject constructor( | ||
private val dao: ActivitySuggestionDao, | ||
private val mapper: ActivitySuggestionDataLocalMapper, | ||
) : ActivitySuggestionRepo { | ||
|
||
private var cache: List<ActivitySuggestion>? = null | ||
private val mutex: Mutex = Mutex() | ||
|
||
override suspend fun getAll(): List<ActivitySuggestion> = mutex.withLockedCache( | ||
logMessage = "getAll", | ||
accessCache = { cache }, | ||
accessSource = { dao.getAll().map(mapper::map) }, | ||
afterSourceAccess = { cache = it }, | ||
) | ||
|
||
override suspend fun get(id: Long): ActivitySuggestion? = mutex.withLockedCache( | ||
logMessage = "get", | ||
accessCache = { cache?.firstOrNull { it.id == id } }, | ||
accessSource = { dao.get(id)?.let(mapper::map) }, | ||
) | ||
|
||
override suspend fun getByTypeId(typeId: Long): List<ActivitySuggestion> = mutex.withLockedCache( | ||
logMessage = "getByTypeId", | ||
accessCache = { cache?.filter { it.forTypeId == typeId } }, | ||
accessSource = { dao.getByTypeId(typeId).map(mapper::map) }, | ||
) | ||
|
||
override suspend fun add(activityFilters: List<ActivitySuggestion>) = mutex.withLockedCache( | ||
logMessage = "add", | ||
accessSource = { dao.insert(activityFilters.map(mapper::map)) }, | ||
afterSourceAccess = { cache = null }, | ||
) | ||
|
||
override suspend fun remove(id: Long) = mutex.withLockedCache( | ||
logMessage = "remove", | ||
accessSource = { dao.delete(id) }, | ||
afterSourceAccess = { cache = cache?.removeIf { it.id == id } }, | ||
) | ||
|
||
override suspend fun clear() = mutex.withLockedCache( | ||
logMessage = "clear", | ||
accessSource = { dao.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
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
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
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.