package world.respect.datalayer /** * Wrapper interface for the state of data being loaded. Allows a data layer flow to contain * both the data and the loading state. * * @property metaInfo metadata about the data being loaded e.g. URL, last modified info, etc. * @property localState the state of the data as loaded from the local data source, if applicable. * This is set by the Repository when there is a distinct local and network result. * @property remoteState the state of the data as loaded from the network data source, if applicable. * This is set by the Repository when there is a distinct local and network result. */ sealed interface DataLoadState { val metaInfo: DataLoadMetaInfo val localState: DataLoadState? val remoteState: DataLoadState<*>? } /** * Data loading is in progress */ data class DataLoadingState( override val metaInfo: DataLoadMetaInfo = DataLoadMetaInfo(), override val localState: DataLoadState? = null, override val remoteState: DataLoadState<*>? = null, ): DataLoadState /** * Data has not been loaded and this is not an error - e.g. when a remote datasource returns 304 * not modified, no content, or when the local datasource is empty. */ data class NoDataLoadedState( val reason: Reason, override val metaInfo: DataLoadMetaInfo = DataLoadMetaInfo(), override val localState: DataLoadState? = null, override val remoteState: DataLoadState<*>? = null, ): DataLoadState { enum class Reason { NOT_MODIFIED, NOT_FOUND } companion object { @Suppress("unused") //reserved for future use fun notModified( metaInfo: DataLoadMetaInfo = DataLoadMetaInfo(), ): NoDataLoadedState = NoDataLoadedState(Reason.NOT_MODIFIED) @Suppress("unused") //reserved for future use fun notFound( metaInfo: DataLoadMetaInfo = DataLoadMetaInfo(), ): NoDataLoadedState = NoDataLoadedState(Reason.NOT_FOUND) } } /** * Data is loaded and ready. If loaded from a repository, it may be that the local data is ready * for display and the remote data is still being loaded. */ data class DataReadyState( val data: T, override val metaInfo: DataLoadMetaInfo = DataLoadMetaInfo(), override val localState: DataLoadState? = null, override val remoteState: DataLoadState<*>? = null, ): DataLoadState /** * There has been an error loading data. */ data class DataErrorResult( val error: Throwable, override val metaInfo: DataLoadMetaInfo = DataLoadMetaInfo(), override val localState: DataLoadState? = null, override val remoteState: DataLoadState<*>? = null, ): DataLoadState