Colton Idle
02/23/2023, 6:36 PMclass NetworkBackedBookRepository(
private val api: ApiService,
) : BookRepository {
override suspend fun getBook(id: String): Book? {
val result = api.getBookById(id).execute()
return result.body()?.firstOrNull()
}
}
yschimke
02/23/2023, 6:55 PMCasey Brooks
02/23/2023, 7:04 PMFlow
for any data that needs to be read/queried, and to expose suspend
methods for any changes that need to be made. Even for “one-shot” queries like an API call, I wrap the value in a loading-type monad (something like this) and have the query emit multiple states as it’s being loaded, to help structure the UI as fully reactive and drive things like progress indicators from the RepositoryColton Idle
02/23/2023, 7:06 PMoverride fun getBook(id): Book?
but yeah what both Yuri and Casey says make sense.
It just felt "wrong" but I think it only felt wrong because I know Android ViewModels are typically frowned upon to have suspending functions exposed.Colton Idle
02/23/2023, 7:06 PMFrancesc
02/23/2023, 11:40 PMAndroid ViewModels are typically frowned upon to have suspending functions exposed.that's because the idea is that the UI makes a call to the viewmodel which immediately puts a message into a queue to be processed asynchronously, the UI call is supposed to return immediately
Francesc
02/23/2023, 11:44 PMEven for “one-shot” queries like an API call, I wrap the value in a loading-type monad (something like this) and have the query emit multiple states as it’s being loadedI've seen this used, but I'm not one to use this pattern. I prefer my repositories to just return the data or an error, the viewmodel informs the UI when it's loading, rather than the repository telling the caller what steps it's going through, I find that this sort of leaks implementation details and, if you have a domain layer where you map repository models to domain models, you have quite a bit of boilerplate to handle the different possible states
zt
02/24/2023, 5:28 AMwithContext(<http://Dispatchers.IO|Dispatchers.IO>)
to keep it off the main threadColton Idle
02/24/2023, 3:26 PMyschimke
02/24/2023, 3:37 PMyschimke
02/24/2023, 3:38 PM