🛠️ Data layer code reformat

* Reformatted/commented part of data layer
This commit is contained in:
Acclorite 2024-09-11 18:02:35 +03:00
parent 1d713b6ab4
commit 57cbdfd057
8 changed files with 30 additions and 52 deletions

View file

@ -1,10 +1,8 @@
package ua.acclorite.book_story.data.local.data_store package ua.acclorite.book_story.data.local.data_store
import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.Preferences
import kotlinx.coroutines.flow.Flow
interface DataStore { 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 <T> getNullableData(key: Preferences.Key<T>): T?
suspend fun getAllData(): Set<Preferences.Key<*>>? suspend fun getAllData(): Set<Preferences.Key<*>>?
suspend fun <T> putData(key: Preferences.Key<T>, value: T) suspend fun <T> putData(key: Preferences.Key<T>, value: T)

View file

@ -6,7 +6,6 @@ import androidx.datastore.preferences.core.Preferences
import androidx.datastore.preferences.core.edit import androidx.datastore.preferences.core.edit
import androidx.datastore.preferences.core.emptyPreferences import androidx.datastore.preferences.core.emptyPreferences
import androidx.datastore.preferences.preferencesDataStore import androidx.datastore.preferences.preferencesDataStore
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.firstOrNull import kotlinx.coroutines.flow.firstOrNull
import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.map
@ -18,38 +17,30 @@ private val Context.dataStore by preferencesDataStore("data_store")
class DataStoreImpl @Inject constructor(context: Application) : DataStore { class DataStoreImpl @Inject constructor(context: Application) : DataStore {
private val dataStore = context.dataStore private val dataStore = context.dataStore
override suspend fun <T> getData(key: Preferences.Key<T>, defaultValue: T): Flow<T> = /**
dataStore.data.catch { exception -> * Gets data from DataStore by given [key]. If no such [key] exists, returns null.
if (exception is IOException) { */
emit(emptyPreferences())
} else {
throw exception
}
}.map { preferences ->
val result = preferences[key] ?: defaultValue
result
}
override suspend fun <T> getNullableData(key: Preferences.Key<T>): T? = override suspend fun <T> getNullableData(key: Preferences.Key<T>): T? =
dataStore.data.catch { exception -> dataStore.data.catch { exception ->
if (exception is IOException) { if (exception is IOException) emit(emptyPreferences())
emit(emptyPreferences()) else throw exception
} else {
throw exception
}
}.map { preferences -> }.map { preferences ->
val result = preferences[key] preferences[key]
result
}.firstOrNull() }.firstOrNull()
/**
* Gets all keys from DataStore.
*/
override suspend fun getAllData(): Set<Preferences.Key<*>>? { override suspend fun getAllData(): Set<Preferences.Key<*>>? {
val keys = dataStore.data return dataStore.data
.map { .map {
it.asMap().keys it.asMap().keys
} }.firstOrNull()
return keys.firstOrNull()
} }
/**
* Puts data in DataStore by given [key].
*/
override suspend fun <T> putData(key: Preferences.Key<T>, value: T) { override suspend fun <T> putData(key: Preferences.Key<T>, value: T) {
dataStore.edit { preferences -> dataStore.edit { preferences ->
preferences[key] = value preferences[key] = value

View file

@ -20,6 +20,11 @@ class UpdatesNotificationServiceImpl @Inject constructor(
private val notificationManager = application.getSystemService(Context.NOTIFICATION_SERVICE) private val notificationManager = application.getSystemService(Context.NOTIFICATION_SERVICE)
as NotificationManager as NotificationManager
/**
* Sends notification about new update.
*
* @param releaseInfo [LatestReleaseInfo].
*/
override fun postNotification(releaseInfo: LatestReleaseInfo) { override fun postNotification(releaseInfo: LatestReleaseInfo) {
val downloadLatestVersionIntent = Intent( val downloadLatestVersionIntent = Intent(
Intent.ACTION_VIEW, Intent.ACTION_VIEW,

View file

@ -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.FavoriteDirectoryEntity
import ua.acclorite.book_story.data.local.dto.HistoryEntity import ua.acclorite.book_story.data.local.dto.HistoryEntity
/**
* Class to manipulate Room database.
*/
@Dao @Dao
interface BookDao { interface BookDao {
/* ------ BookEntity ------------------------ */
@Insert(onConflict = OnConflictStrategy.REPLACE) @Insert(onConflict = OnConflictStrategy.REPLACE)
suspend fun insertBooks( suspend fun insertBooks(
books: List<BookEntity> books: List<BookEntity>
@ -39,8 +43,10 @@ interface BookDao {
@Update @Update
suspend fun updateBooks(books: List<BookEntity>) suspend fun updateBooks(books: List<BookEntity>)
/* - - - - - - - - - - - - - - - - - - - - - - */
/* ------ HistoryEntity --------------------- */
@Query("SELECT * FROM historyentity") @Query("SELECT * FROM historyentity")
suspend fun getHistory(): List<HistoryEntity> suspend fun getHistory(): List<HistoryEntity>
@ -60,8 +66,10 @@ interface BookDao {
@Delete @Delete
suspend fun deleteHistory(history: List<HistoryEntity>) suspend fun deleteHistory(history: List<HistoryEntity>)
/* - - - - - - - - - - - - - - - - - - - - - - */
/* ------ ColorPresetEntity ----------------- */
@Upsert @Upsert
suspend fun updateColorPreset(colorPreset: ColorPresetEntity) suspend fun updateColorPreset(colorPreset: ColorPresetEntity)
@ -79,8 +87,10 @@ interface BookDao {
@Query("DELETE FROM colorpresetentity") @Query("DELETE FROM colorpresetentity")
suspend fun deleteColorPresets() suspend fun deleteColorPresets()
/* - - - - - - - - - - - - - - - - - - - - - - */
/* ------ FavoriteDirectoryEntity ----------- */
@Upsert @Upsert
suspend fun insertFavoriteDirectory(favoriteDirectoryEntity: FavoriteDirectoryEntity) suspend fun insertFavoriteDirectory(favoriteDirectoryEntity: FavoriteDirectoryEntity)
@ -89,4 +99,5 @@ interface BookDao {
@Delete @Delete
suspend fun deleteFavoriteDirectory(favoriteDirectoryEntity: FavoriteDirectoryEntity) suspend fun deleteFavoriteDirectory(favoriteDirectoryEntity: FavoriteDirectoryEntity)
/* - - - - - - - - - - - - - - - - - - - - - - */
} }

View file

@ -6,7 +6,6 @@ import ua.acclorite.book_story.domain.model.ColorPreset
import javax.inject.Inject import javax.inject.Inject
class ColorPresetMapperImpl @Inject constructor() : ColorPresetMapper { class ColorPresetMapperImpl @Inject constructor() : ColorPresetMapper {
override suspend fun toColorPresetEntity( override suspend fun toColorPresetEntity(
colorPreset: ColorPreset, colorPreset: ColorPreset,
order: Int order: Int

View file

@ -429,13 +429,6 @@ class BookRepositoryImpl @Inject constructor(
return true 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) { override suspend fun <T> putDataToDataStore(key: Preferences.Key<T>, value: T) {
dataStore.putData(key, value) dataStore.putData(key, value)
} }

View file

@ -58,12 +58,6 @@ interface BookRepository {
suspend fun resetCoverImage(bookId: Int): Boolean suspend fun resetCoverImage(bookId: Int): Boolean
suspend fun <T> retrieveDataFromDataStore(
key: Preferences.Key<T>,
defaultValue: T
): Flow<T>
suspend fun <T> putDataToDataStore( suspend fun <T> putDataToDataStore(
key: Preferences.Key<T>, key: Preferences.Key<T>,
value: T value: T

View file

@ -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)
}
}