Callback to Observable - android

I've got this function getting documents from Cloud Firestore:
fun getBasicItems(callback: (MutableList<FireStoreBasicItem>) -> Unit) {
fireStore.collection("BasicItems")
.get()
.addOnCompleteListener { task ->
if (task.isSuccessful) {
val basicItems = mutableListOf<FireStoreBasicItem>()
for (document in task.result!!) {
val fireStoreBasicItem = document.toObject(FireStoreBasicItem::class.java)
basicItems.add(fireStoreBasicItem)
callback(basicItems)
}
}
}
}
In my ViewModel I want to transform this to an Observable an then to a ViewState:
private fun loadDataTransformer(): ObservableTransformer<ItemEvent.LoadDataEvent, ItemsViewState> {
return ObservableTransformer { event ->
event.map {
itemRepository.getBasicItems(){myBasicItemList -> Observable.just(myBasicItemList)}
}
}
I tried it also with Observable.fromCallable. What am I doing wrong?
EDIT: My Solution
private fun loadDataTransformer(): ObservableTransformer<ItemEvent.LoadDataEvent, ItemsViewState> {
return ObservableTransformer { event ->
event.flatMap {
Single.create<MutableList<FireStoreBasicItem>> {
itemRepository.getBasicItems { myBasicItemList ->
it.onSuccess(myBasicItemList)
}
}.toObservable()
.map {
ItemsViewState.ItemDataState(it)
}
}
}
}

I would like to suggest you to use Single instead of Observable if you are expecting only one list of items. Then you can use Single.create:
private fun loadDataTransformer(): Single<ItemsViewState> =
Single.create { emitter ->
itemRepository.getBasicItems() { myBasicItemList ->
val viewState = // do some transformations
emitter.onSuccess(viewState)
}
}

Related

Coroutine StateFlow.collect{} not firing

I'm seeing some odd behavior. I have a simple StateFlow<Boolean> in my ViewModel that is not being collected in the fragment. Definition:
private val _primaryButtonClicked = MutableStateFlow(false)
val primaryButtonClicked: StateFlow<Boolean> = _primaryButtonClicked
and here is where I set the value:
fun primaryButtonClick() {
_primaryButtonClicked.value = true
}
Here is where I'm collecting it.
repeatOnOwnerLifecycle {
launch(dispatchProvider.io()) {
freeSimPurchaseFragmentViewModel.primaryButtonClicked.collect {
if (it) {
autoCompletePlacesStateFlowModel.validateErrors()
formValidated = autoCompletePlacesStateFlowModel.validateAddress()
if (formValidated) {
freeSimPurchaseFragmentViewModel
.sumbitForm(autoCompletePlacesStateFlowModel.getStateFlowCopy())
}
}
}
}
}
repeatOnOwnerLifecycle:
inline fun Fragment.repeatOnOwnerLifecycle(
state: Lifecycle.State = Lifecycle.State.RESUMED,
crossinline block: suspend CoroutineScope.() -> Unit
) {
viewLifecycleOwner.lifecycleScope.launch {
repeatOnLifecycle(state) {
block()
}
}
What am I doing wrong? The collector never fires.
Does this make sense?
val primaryButtonClicked: StateFlow<Boolean> = _primaryButtonClicked.asStateFlow()
Also I couldn't understand the inline function part, because under the hood seems you wrote something like this
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.RESUMED) {
launch(dispatchProvider.io()) {
freeSimPurchaseFragmentViewModel.primaryButtonClicked.collect {
if (it) {
autoCompletePlacesStateFlowModel.validateErrors()
formValidated = autoCompletePlacesStateFlowModel.validateAddress()
if (formValidated) {
freeSimPurchaseFragmentViewModel
.sumbitForm(autoCompletePlacesStateFlowModel.getStateFlowCopy())
}
}
}
}
}
}
Why are you launching one coroutine in another and collect the flow from IO dispatcher? You need to collect the values from the main dispatcher.

How to chain flows with different return types based on emitted values and collect their results?

I have a situation where I have to execute 3 network requests one after the other collect their results (which are of different types).
Following is the relevant part of the code :
Resource.kt
sealed class Resource<T>(val data: T? = null, val message: String? = null) {
class Loading<T>(data: T? = null): Resource<T>(data)
class Success<T>(data: T?): Resource<T>(data)
class Error<T>(message: String, data: T? = null): Resource<T>(data, message)
}
Repository.kt
override fun getReportData(profileId: Int): Flow<Resource<ProfileReport>> =
flow {
emit(Resource.Loading<ProfileReport>())
var report: ProfileReport? = null
try {
// Api is available as a retrofit implementation
report = api.getReport(profileId).toProfileReport()
} catch (e: HttpException) {
emit(
Resource.Error<ProfileReport>(
message = "An unknown http exception occured"
)
)
}
if (report!= null) {
emit(Resource.Success<ProfileReport>(data = report))
}
}
Say I have 3 such flows to fetch data in my repository and they have different return types (ex: ProfileReport, ProfileInfo, ProfileStatus).
Now in my viewmodel I have a function to execute these flows and perform actions on the values emitted such as :
ViewModel.kt
fun getProfileData(profileId: Int) {
getReportData(profileId)
.onEach { result ->
when (result) {
is Resource.Loading -> {
_loading.value = true
}
is Resource.Error -> {
_loading.value = false
// UI event to display error snackbar
}
is Resource.Success -> {
_loading.value = false
if (result.data != null) {
_report.value = _report.value.copy(
// Use result here
)
}
}
}
}.launchIn(viewModelScope)
}
This works ok for one flow but how can I execute 3 flows one after the other.
That is, execute first one and if its successful, execute second one and so on, and if all of them are successful use the results.
I did it like this :
fun getProfileData(profileId: Int) {
getReportData(profileId)
.onEach { result1 ->
when (result1) {
is Resource.Loading -> {/*do stuff*/}
is Resource.Error -> {/*do stuff*/}
is Resource.Success -> {
getProfileStatus(profileId)
.onEach { result2 ->
is Resource.Loading -> {/*do stuff*/}
is Resource.Error -> {/*do stuff*/}
is Resource.Success -> {
getProfileInfo(profileId)
.onEach { result3 ->
is Resource.Loading -> {/*do stuff*/}
is Resource.Error -> {/*do stuff*/}
is Resource.Success -> {
/*
Finally update viewmodel state
using result1, result2 and result3
*/
}
}.launchIn(viewModelScope)
}
}.launchIn(viewModelScope)
}
}
}.launchIn(viewModelScope)
}
But, this feels too cumbersome and probably there is a better way to chain flows based on success condition and collect results at the end. I checked some ways that use combine() or flatMapMerge() but was unable to use them in this situation.
Is there a way to achieve this? Or is this approach itself wrong from a design perspective maybe?
I think this could be modeled much more cleanly using imperative coroutines than with flows. Since you're overriding functions, this depends on you being able to modify the supertype abstract function signatures.
This solution doesn't use Resource.Loading, so you should remove that to make smart casting easier.
suspend fun getReportData(profileId: Int): Resource<ProfileReport> =
try {
val report = api.getReport(profileId).toProfileReport()
Resource.Success<ProfileReport>(data = report)
} catch (e: HttpException) {
Resource.Error<ProfileReport>(
message = "An unknown http exception occured"
)
}
//.. similar for the other two functions that used to return flows.
fun getProfileData(profileId: Int) {
viewModelScope.launch {
// do stuff to indicate 1st loading state
when(val result = getReportData(profileId)) {
Resource.Error<ProfileReport> -> {
// do stuff for error state
return#launch
}
Resource.Success<ProfileReport> -> {
// do stuff with result
}
}
// Since we returned when there was error, we know first
// result was successful.
// do stuff to indicate 2nd loading state
when(val result = getProfileStatus(profileId)) {
Resource.Error<ProfileStatus> -> {
// do stuff for error state
return#launch
}
Resource.Success<ProfileStatus> -> {
// do stuff with result
}
}
// do stuff to indicate 3rd loading state
when(val result = getProfileInfo(profileId)) {
Resource.Error<ProfileInfo> -> {
// do stuff for error state
return#launch
}
Resource.Success<ProfileInfo> -> {
// do stuff with result
}
}
}
}
If you want to keep your current Flows, you could collect your flows this way to avoid the deep nesting. This works because your source flows are designed to be finite (they aren't repeatedly emitting new values indefinitely, but have only one final result).
fun getProfileData(profileId: Int) = viewModelScope.launch {
var shouldBreak = false
getReportData(profileId).collect { result ->
when (result) {
is Resource.Loading -> { /*do stuff*/ }
is Resource.Error -> {
/*do stuff*/
shouldBreak = true
}
is Resource.Success -> { /*do stuff*/ }
}
}
if (shouldBreak) return#launch
getProfileStatus(profileId).collect { result ->
when (result) {
is Resource.Loading -> { /*do stuff*/ }
is Resource.Error -> {
/*do stuff*/
shouldBreak = true
}
is Resource.Success -> { /*do stuff*/ }
}
}
if (shouldBreak) return#launch
getProfileInfo(profileId).collect { result ->
when (result) {
is Resource.Loading -> { /*do stuff*/ }
is Resource.Error -> { /*do stuff*/ }
is Resource.Success -> { /*do stuff*/ }
}
}
}

Coroutine scope cancel

I know that there are a lot of posts "How to cancel Coroutines Scope" but I couldn't find the answer for my case.
I have an Array of objects that I want to send each of them to Server using Coroutines.
What I need is, if one of my requests returns error, canceling others.
Here is my code:
private fun sendDataToServer(function: () -> Unit) {
LiabilitiesWizardSessionManager.getLiabilityAddedDocuments().let { documents ->
if (documents.isEmpty().not()) {
CoroutineScope(Dispatchers.IO).launch {
documents.mapIndexed { index, docDetail ->
async {
val result = uploadFiles(docDetail)
}
}.map {
var result = it.await()
}
}
} else function.invoke()
}
}
Below is my uploadFiles() function:
private suspend fun uploadFiles(docDetail: DocDetail): ArchiveFileResponse? {
LiabilitiesWizardSessionManager.mCreateLiabilityModel.let { model ->
val file = File(docDetail.fullFilePath)
val crmCode = docDetail.docTypeCode
val desc = docDetail.docTypeDesc
val id = model.commitmentMember?.id
val idType = 1
val createArchiveFileModel = CreateArchiveFileModel(108, desc, id, idType).apply {
this.isLiability = true
this.adaSystem = 3
}
val result = mRepositoryControllerKotlin.uploadFile(file, createArchiveFileModel)
return when (result) {
is ResultWrapper.Success -> {
result.value
}
is ResultWrapper.GenericError -> {
null
}
is ResultWrapper.NetworkError -> {
null
}
}
}
}
I know, I'm missing something.

Why Live data not observe some value in race condition?

I have some problem with my live data. when I want to set value in my live data in race condition some value not observe in my fragement. here is my detail code:
Here Is My View Model
class AuthNewViewModel(
private val coroutineDispatcher: CoroutineDispatcher,
private val authUseCase: AuthUseCase
): ViewModel() {
private val _sendCode = MutableLiveData<DataResult<SendCode?>>()
val sendCode: LiveData<DataResult<SendCode?>> = _sendCode
fun sendCode(req:CodeReq) {
viewModelScope.launch(coroutineDispatcher) {
_sendCode.postValue(DataResult.Loading(true))
authUseCase.sendCode(req)
.flowOn(Dispatchers.IO)
.catch { error ->
_sendCode.postValue(error.isError())
}
.onCompletion {
_sendCode.postValue(DataResult.Loading(false))
}
.collect { data ->
when (data.meta?.code ?: 0) {
in 200..299 -> {
_sendCode.postValue(DataResult.Success(data.data))
}
else -> {
_sendCode.postValue(DataResult.ErrorMeta(data.data, data.errorData))
}
}
}
}
}
}
Here is when I want to triggred my view mode
authNewVM.sendCode(
CodeReq(
phone = phoneOrEmail,
channel = channel,
type = Constants.AuthConstants.TYPE_SIGNUP
)
)
Here is when the data is observe
authNewVM.sendCode.observe(viewLifecycleOwner, Observer {
when(it){
is DataResult.Success -> {
Logs.d("result success")
}
is DataResult.Error -> {
Logs.d("result error")
}
is DataResult.Loading -> {
Logs.d("result loading ${it.isLoading}")
}
is DataResult.ErrorMeta -> {
Logs.d("result error meta")
}
}
})
And the result
result loading true
result loading false
View some time not observe state success, whereas my view model is set value on succes to..

How to make this nicer in Kotlin?

I this piece of code I would like to know instead of having convert() separate can I have anonymous in map {}
fun <A, B> LiveData<A>.map(function: (A) -> B): LiveData<B> = Transformations.map(this, function)
fun loadSettings() {
configLiveData.map { configFile ->
return#map convert(configFile)
}
}
fun convert(configFile: Response<ConfigFile>): MutableLiveData<Settings> {
val mutableData = MutableLiveData<Setting>()
when (configFile) {
is Response.Success<ConfigFile> -> {
mutableData.postValue(configFile.data.config?.settings)
}
is Response.Failure -> {
errorMessageMutableData.postValue(it.message)
}
}
return mutableData
}
final result I have
fun loadTheme(): LiveData<Response<Theme?>> {
return configLiveData.map { configFile ->
when (configFile) {
is Response.Success<ConfigFile> -> {
Response.Success(configFile.data.config?.theme)
}
is Response.Failure -> {
Response.Failure(configFile.message)
}
}
}
}
can I have anonymous in map {}
Yes, of course. You already do: { configFile -> return#map convert(configFile) } is a lambda (which could equally be written { configFile -> convert(configFile) } or { convert(it) }, or even ::convert). If you don't want to make convert a separate function, just inline it into the lambda:
configLiveData.map { configFile ->
val mutableData = MutableLiveData<Setting>()
when (configFile) {
is Response.Success<ConfigFile> -> {
mutableData.postValue(configFile.data.config?.settings)
}
is Response.Failure -> {
errorMessageMutableData.postValue(it.message)
}
}
mutableData // no need for return#map
}
But the problem is that if that's your real code, it probably doesn't actually do what you want, because
you create a LiveData<MutableLiveData<Settings>> (did you want switchMap instead of map?);
you then throw it away;
if you get a Response.Success, it's effectively ignored (because you just post data from it into an unobserved LiveData).

Categories

Resources