Vishnu Haridas
03/19/2022, 7:50 PMgetNews()
class NewsRepository( localSource, remoteSource ){
val flow: Flow< Resource<NewsItem> > = // exposes the data, updates, and statuses
// Will you return a Resource<> here...
suspend fun getNews(): Resource<NewsItem> {
.
if(success) return Resource.Success(list)
else return Resource.Error(exception)
}
// ...Or return nothing and just emit the result?
suspend fun getNews(){
if(success) _flowData.emit( Resource.Success(list) )
else _flowData.emit( Resource.Error(exception) )
}
}
There can be other functions in the repository that will update the Flow and return nothing, for example:
suspend fun removeItem( newsId ){
// Remove item from the local copy
list.remove(newsId)
// Notify listeners
_flowData.emit( Result.success( list ) )
}
So my feeling is that I should make all the CRUD functions in the repository to return nothing but just update the Flow to avoid confusion.
Comments?K Merle
03/20/2022, 5:08 AMflow
there? If you need a flow, you could use flowBuilder.Vishnu Haridas
03/20/2022, 5:13 AMflow: Flow< Resource >
and a backing field _flowData: MutableStateFlow<Resource>
just like a LiveData.K Merle
03/20/2022, 5:14 AMArun Joseph
03/20/2022, 7:08 AMcurioustechizen
03/21/2022, 9:22 AMfun performSearch(query: String): Flow<List<SearchResult>>
or fun scanDevices(): Flow<List<ScanResult>>
• If the flow is something that we want to the UI to always react to, and that depends on different user actions, then I make the repository method return nothing; and have a separate flow that the UI always observes. Example: fun addTodo()
, fun deleteTodo()
, fun editTodo()
- all of these return nothing; there's a separate val todos: Flow<List<TodoItem>>
Vishnu Haridas
03/21/2022, 9:30 AMList<TodoItem>
, do you keep another Flow for the API status, or do you bundle both into a sealed class / Resource and expose that, like val todos: Flow< Resource<List<TodoItem>> >
.
This is to ensure that the VM is always aware of the latest status - because there may be multiple VMs or VMs being created and destroyed.curioustechizen
03/21/2022, 10:14 AMVishnu Haridas
03/21/2022, 10:58 AMResouce<T>
.curioustechizen
03/21/2022, 11:20 AMEither<T, Error>
for single-shot operations or Flow<Either<T, Error>>
for flows).
There do exist micro-libraries to model LCE (Loading, Content, Error) types so you could look into those.Alex Prince
03/21/2022, 1:34 PMcurioustechizen
03/21/2022, 1:38 PMAlex Prince
03/21/2022, 1:40 PMMarek Defeciński
03/22/2022, 11:13 AM