Skip to content

Instantly share code, notes, and snippets.

View cohenItay's full-sized avatar

Itay cohen cohenItay

  • Israel
View GitHub Profile
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
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()
data class DataWithState(
val isLoading: Boolean = false
val myError: MyErrorModel? = null
val myConcreteData: MyConcerteDataModel = null
)
class MyViewModel(...) {
...
val myData: StateFlow<DataWithState> = MutableStateFlow()
}
val isLoading: LiveData<Boolean> = MutableLiveData(false)
val myError: LiveData<MyErrorModel> = MutableLiveData(null)
val myConcreteData: LiveData<MyConcerteDataModel> = MutableLiveData(null)
@cohenItay
cohenItay / TypicalNeeds.csv
Last active July 18, 2022 04:52
Table for Medium article Loading state
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
fun shareAppLogs() {
val logsDirectory = logsFileProvider.provideLogsDirectory(appContext)
val innerFiles = logsDirectory.listFiles()
if (!logsDirectory.exists() || innerFiles.isNullOrEmpty()) {
Toast.makeText(appContext, "There are no logs to share", Toast.LENGTH_SHORT).show()
return
}
val urisToShare = innerFiles.map { file: File? ->
FileProvider.getUriForFile(
appContext,
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)
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.
*/
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>()