🛠️ Data layer code reformat
* Reformatted/commented part of data layer
This commit is contained in:
parent
1d713b6ab4
commit
57cbdfd057
8 changed files with 30 additions and 52 deletions
|
|
@ -1,10 +1,8 @@
|
|||
package ua.acclorite.book_story.data.local.data_store
|
||||
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
|
||||
interface DataStore {
|
||||
suspend fun <T> getData(key: Preferences.Key<T>, defaultValue: T): Flow<T>
|
||||
suspend fun <T> getNullableData(key: Preferences.Key<T>): T?
|
||||
suspend fun getAllData(): Set<Preferences.Key<*>>?
|
||||
suspend fun <T> putData(key: Preferences.Key<T>, value: T)
|
||||
|
|
|
|||
|
|
@ -6,7 +6,6 @@ import androidx.datastore.preferences.core.Preferences
|
|||
import androidx.datastore.preferences.core.edit
|
||||
import androidx.datastore.preferences.core.emptyPreferences
|
||||
import androidx.datastore.preferences.preferencesDataStore
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import kotlinx.coroutines.flow.catch
|
||||
import kotlinx.coroutines.flow.firstOrNull
|
||||
import kotlinx.coroutines.flow.map
|
||||
|
|
@ -18,38 +17,30 @@ private val Context.dataStore by preferencesDataStore("data_store")
|
|||
class DataStoreImpl @Inject constructor(context: Application) : DataStore {
|
||||
private val dataStore = context.dataStore
|
||||
|
||||
override suspend fun <T> getData(key: Preferences.Key<T>, defaultValue: T): Flow<T> =
|
||||
dataStore.data.catch { exception ->
|
||||
if (exception is IOException) {
|
||||
emit(emptyPreferences())
|
||||
} else {
|
||||
throw exception
|
||||
}
|
||||
}.map { preferences ->
|
||||
val result = preferences[key] ?: defaultValue
|
||||
result
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets data from DataStore by given [key]. If no such [key] exists, returns null.
|
||||
*/
|
||||
override suspend fun <T> getNullableData(key: Preferences.Key<T>): T? =
|
||||
dataStore.data.catch { exception ->
|
||||
if (exception is IOException) {
|
||||
emit(emptyPreferences())
|
||||
} else {
|
||||
throw exception
|
||||
}
|
||||
if (exception is IOException) emit(emptyPreferences())
|
||||
else throw exception
|
||||
}.map { preferences ->
|
||||
val result = preferences[key]
|
||||
result
|
||||
preferences[key]
|
||||
}.firstOrNull()
|
||||
|
||||
/**
|
||||
* Gets all keys from DataStore.
|
||||
*/
|
||||
override suspend fun getAllData(): Set<Preferences.Key<*>>? {
|
||||
val keys = dataStore.data
|
||||
return dataStore.data
|
||||
.map {
|
||||
it.asMap().keys
|
||||
}
|
||||
return keys.firstOrNull()
|
||||
}.firstOrNull()
|
||||
}
|
||||
|
||||
/**
|
||||
* Puts data in DataStore by given [key].
|
||||
*/
|
||||
override suspend fun <T> putData(key: Preferences.Key<T>, value: T) {
|
||||
dataStore.edit { preferences ->
|
||||
preferences[key] = value
|
||||
|
|
|
|||
|
|
@ -20,6 +20,11 @@ class UpdatesNotificationServiceImpl @Inject constructor(
|
|||
private val notificationManager = application.getSystemService(Context.NOTIFICATION_SERVICE)
|
||||
as NotificationManager
|
||||
|
||||
/**
|
||||
* Sends notification about new update.
|
||||
*
|
||||
* @param releaseInfo [LatestReleaseInfo].
|
||||
*/
|
||||
override fun postNotification(releaseInfo: LatestReleaseInfo) {
|
||||
val downloadLatestVersionIntent = Intent(
|
||||
Intent.ACTION_VIEW,
|
||||
|
|
|
|||
|
|
@ -12,9 +12,13 @@ import ua.acclorite.book_story.data.local.dto.ColorPresetEntity
|
|||
import ua.acclorite.book_story.data.local.dto.FavoriteDirectoryEntity
|
||||
import ua.acclorite.book_story.data.local.dto.HistoryEntity
|
||||
|
||||
/**
|
||||
* Class to manipulate Room database.
|
||||
*/
|
||||
@Dao
|
||||
interface BookDao {
|
||||
|
||||
/* ------ BookEntity ------------------------ */
|
||||
@Insert(onConflict = OnConflictStrategy.REPLACE)
|
||||
suspend fun insertBooks(
|
||||
books: List<BookEntity>
|
||||
|
|
@ -39,8 +43,10 @@ interface BookDao {
|
|||
|
||||
@Update
|
||||
suspend fun updateBooks(books: List<BookEntity>)
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - */
|
||||
|
||||
|
||||
/* ------ HistoryEntity --------------------- */
|
||||
@Query("SELECT * FROM historyentity")
|
||||
suspend fun getHistory(): List<HistoryEntity>
|
||||
|
||||
|
|
@ -60,8 +66,10 @@ interface BookDao {
|
|||
|
||||
@Delete
|
||||
suspend fun deleteHistory(history: List<HistoryEntity>)
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - */
|
||||
|
||||
|
||||
/* ------ ColorPresetEntity ----------------- */
|
||||
@Upsert
|
||||
suspend fun updateColorPreset(colorPreset: ColorPresetEntity)
|
||||
|
||||
|
|
@ -79,8 +87,10 @@ interface BookDao {
|
|||
|
||||
@Query("DELETE FROM colorpresetentity")
|
||||
suspend fun deleteColorPresets()
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - */
|
||||
|
||||
|
||||
/* ------ FavoriteDirectoryEntity ----------- */
|
||||
@Upsert
|
||||
suspend fun insertFavoriteDirectory(favoriteDirectoryEntity: FavoriteDirectoryEntity)
|
||||
|
||||
|
|
@ -89,4 +99,5 @@ interface BookDao {
|
|||
|
||||
@Delete
|
||||
suspend fun deleteFavoriteDirectory(favoriteDirectoryEntity: FavoriteDirectoryEntity)
|
||||
/* - - - - - - - - - - - - - - - - - - - - - - */
|
||||
}
|
||||
|
|
@ -6,7 +6,6 @@ import ua.acclorite.book_story.domain.model.ColorPreset
|
|||
import javax.inject.Inject
|
||||
|
||||
class ColorPresetMapperImpl @Inject constructor() : ColorPresetMapper {
|
||||
|
||||
override suspend fun toColorPresetEntity(
|
||||
colorPreset: ColorPreset,
|
||||
order: Int
|
||||
|
|
|
|||
|
|
@ -429,13 +429,6 @@ class BookRepositoryImpl @Inject constructor(
|
|||
return true
|
||||
}
|
||||
|
||||
override suspend fun <T> retrieveDataFromDataStore(
|
||||
key: Preferences.Key<T>,
|
||||
defaultValue: T
|
||||
): Flow<T> {
|
||||
return dataStore.getData(key, defaultValue)
|
||||
}
|
||||
|
||||
override suspend fun <T> putDataToDataStore(key: Preferences.Key<T>, value: T) {
|
||||
dataStore.putData(key, value)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -58,12 +58,6 @@ interface BookRepository {
|
|||
|
||||
suspend fun resetCoverImage(bookId: Int): Boolean
|
||||
|
||||
|
||||
suspend fun <T> retrieveDataFromDataStore(
|
||||
key: Preferences.Key<T>,
|
||||
defaultValue: T
|
||||
): Flow<T>
|
||||
|
||||
suspend fun <T> putDataToDataStore(
|
||||
key: Preferences.Key<T>,
|
||||
value: T
|
||||
|
|
|
|||
|
|
@ -1,13 +0,0 @@
|
|||
package ua.acclorite.book_story.domain.use_case
|
||||
|
||||
import androidx.datastore.preferences.core.Preferences
|
||||
import kotlinx.coroutines.flow.Flow
|
||||
import ua.acclorite.book_story.domain.repository.BookRepository
|
||||
import javax.inject.Inject
|
||||
|
||||
class GetDatastore @Inject constructor(private val repository: BookRepository) {
|
||||
|
||||
suspend fun <T> execute(key: Preferences.Key<T>, defaultValue: T): Flow<T> {
|
||||
return repository.retrieveDataFromDataStore(key, defaultValue)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue