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
MyRepositoryImplementation(...) : MyRepositoryInterface { | |
... | |
private val _myData = MutableStateFlow<ApiState<MyConcreteModel>>(ApiState.Idle()) | |
override val myData: StateFlow<ApiState<MyConcreteModel>> | |
get() = _myData | |
override fun updateMyData() = someCoroutineScope.launch { | |
try { | |
_myData.value = ApiState.InWork(previousModel = null) // Instead of null you can contruct it with the previous model |
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
sealed class LoadState<T> { | |
/** | |
* There is no concrete data at all, and nothing is being loaded. | |
*/ | |
class Idle<T>: LoadState<T>() { | |
override fun equals(other: Any?): Boolean = | |
this === other || other is Idle<*> | |
override fun hashCode(): Int = javaClass.hashCode() |
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
data class DataWithState( | |
val isLoading: Boolean = false | |
val myError: MyErrorModel? = null | |
val myConcreteData: MyConcerteDataModel = null | |
) | |
class MyViewModel(...) { | |
... | |
val myData: StateFlow<DataWithState> = MutableStateFlow() | |
} |
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
val isLoading: LiveData<Boolean> = MutableLiveData(false) | |
val myError: LiveData<MyErrorModel> = MutableLiveData(null) | |
val myConcreteData: LiveData<MyConcerteDataModel> = MutableLiveData(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
Scenario | Concrete data | Loading state naming | |
---|---|---|---|
No action has been made to retrieve this data or nothing yet happend | None | Idle | |
Loading or working to retrive the data | None or previous state of your <DataModel> | InWork | |
Data loaded successfully | <DataModel> | Success | |
Problem occurred while loading | It could be an Exception or error message retrieved by the server | Failure |
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
class LogsFileProviderDaily @Inject constructor() : LogsFileProvider { | |
private val TAG = LogsFileProvider::class.simpleName!! | |
private val fileNameFormat = SimpleDateFormat("dd_MM_yyyy", Locale.getDefault()) | |
override fun provideLogsDirectory(appContext: Context) = | |
File(("${appContext.filesDir}/${appContext.getString(R.string.logsDirectoryName)}")) | |
override suspend fun provideFile(appContext: Context): File? { | |
val fileName = fileNameFormat.format(Calendar.getInstance().time) |
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
interface LogsFileProvider { | |
/** | |
* @return [File] directory in which the logs file will be saved in. | |
*/ | |
fun provideLogsDirectory(appContext: Context): File | |
/** | |
* @return [File] of type .log, which you can write logs to it. | |
*/ |
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
class DiskLoggerImpl( | |
private val appContext: Context, | |
private val logsFileProvider: LogsFileProvider | |
) : DiskLogger { | |
private val TAG = DiskLogger::class.simpleName!! | |
private val timeFormat = SimpleDateFormat("HH:mm:ss.SSS", Locale.getDefault()) | |
private var bufferWriter: BufferedWriter? = null | |
private val loggerScope = CoroutineScope(Dispatchers.Default + Job()) | |
private val channel = Channel<Operation>() |