Slackbot
03/14/2023, 11:19 AMuli
03/14/2023, 11:43 AMviewModelScope.launch {
lateinit var newTx: ITransaction
cacheRepository.createChainTxAsFlow(SwapTransaction(match = subj))
.map {
newTx = it
repository.swap(subj)
}
.onEach { preProcessResponse(it, newTx) }
.flowOn(backgroundDispatcher)
.collect { processResponse(it) }
}
You’d usually want flow operators to be ‘pure’. I.e. have no side effects. In this case you should get rid of onEach
. For map
that means you should generally only map input data to output data and not set newTx or call repository.swap.
Additionally calling back to the repository from the view model should be reserved for view-triggered actions.uli
03/14/2023, 11:46 AMDmitry
03/14/2023, 12:02 PMDmitry
03/14/2023, 12:03 PMuli
03/14/2023, 12:03 PMuli
03/14/2023, 12:04 PMDmitry
03/14/2023, 12:05 PMDmitry
03/14/2023, 12:06 PMDmitry
03/14/2023, 12:06 PMDmitry
03/14/2023, 12:06 PMDmitry
03/14/2023, 12:07 PMDmitry
03/14/2023, 12:07 PMDmitry
03/14/2023, 12:08 PMDmitry
03/14/2023, 12:09 PMDmitry
03/14/2023, 12:10 PMuli
03/14/2023, 5:08 PMuli
03/14/2023, 9:08 PMuli
03/14/2023, 9:08 PMcreateChainTxAsFlow
as this is where the blocking stuff happensuli
03/14/2023, 9:11 PMSingle
in general maps to kotlin suspend function)uli
03/14/2023, 9:20 PMcollect { newTx ->
val sawp = repository.swap()
preprocessResponse(swap, chainTx)
processResponse(swap)
}
uli
03/14/2023, 9:31 PMuli
03/14/2023, 9:44 PMfun itemFlow(item) = flow<Pair<ITransaction, Service>> {
// TODO big part of code
}
itemFlow().flatMapConcat { item ->
handleItem(item)
}
.onStart { state.update { state -> state.copy(isLoading = true) } }
.flatMapConcat {
if (it is Response.Data) { personRepository.cacheAccount(it.data) }
flow { emit(it) }
}
.collect { it ->
Log.d(App.TAG, "[add offer] start collect data in viewmodel")
processServerResponse(it) { addOfferSpecialHandler(it) }
}
fun handleItem() {
// TODO consider to move each branch case into the separate class function
when(item.first.type) {
MintTransaction::class.java.simpleName -> {
/* token are created only for offer, demands registered just as a db record */
// val mintItem = (item.first as MintTransaction).tokenId
logger.d("[queue polling] process MintTransaction item")
item.second.chainService.tokenId = (item.first as MintTransaction).tokenId
// val chainService = item.second//ChainService(userWalletAddress = state.value.profile.userWalletAddress, tokenId = mintItem)
val newService = item.second//Service(title = proposal.get()!!, date = 0L, index = listOf(), chainService = chainService)
logger.d("[addOffer::server] ${item.first} \n and \n ${item.second}")
personRepository.addOffer(
contact = uiState.value.profile.contact,
secret = uiState.value.profile.secret,
newService = newService
)
}
else -> { throw UnsupportedOperationException("Did you forget to add support of ${item.first.type} class tx?") }
}
}
Dmitry
03/15/2023, 4:31 PM