Unable to update mutableList object model values Kotlin - android

all wanted to update the values in the mutable list, but loop jumping out here is my code snippet.
for ((i, dataObject) in data.withIndex()) {
channelsObject.getChannel(
dataObject.remoteChannelId,
ChatCallbackListener<Channel>() {
Log.e("last friend", it.friendlyName)
it!!.messages?.getLastMessages(1, ChatCallbackListener<List<Message>>() {
if (it.size > 0)
if (it.get(0).hasMedia()) {
dataObject.isAttachement = true
dataObject.lastMessage = "attachment"
} else {
dataObject.lastMessage = it.get(0).messageBody
dataObject.lastMessageDataAndTime = it.get(0).dateCreated
})
})
setThreadAdapter(data)
}
I am unable to update two values of the data class, looks like the loop jumping out before getting messages, any help highly appreciated.
Thanks

Looks like your adapter is getting called before the data retrieved by the callbacks.
there are multiple approached to do that.
But one is below:
fun jhaman() {
val data = mutableListOf<Any>()
// to set the adapter with empty message so you can update
setThreadAdapter(data)
val remoteChannelId = 0// todo change this
data.forEachIndexed { index, any ->
channelsObject.getChannel(
any.remoteChannelId,
ChatCallbackListener<Channel>() { channel ->
Log.e("last friend", any.friendlyName)
channel.messages?.getLastMessages(1, ChatCallbackListener<List<Message>>() {
//update your data what you wana to do in the data object
any.apply {
// update the data
}
adapter.updateThreadAdapter(index, any)
})
})
}
}
// in adapter
fun updateThreadAdapter(index: Int, any: Any) {
list.get(index) = any
notifyItemChanged(list)
}
Change any to your object.
Disclaimer: I did not run this code because I don't have that chat api in my project. thanks

Related

How to use RecyclerView or other ui elements that depend on multiple livedata and may change on interaction android kotlin

I have a rather difficult task for me here. I have a RecyclerView that depends on 3-4 livedata objects. For this I use MediatorLiveData:
class TripleMediatorLiveData<F, S, T>(
firstLiveData: LiveData<F>,
secondLiveData: LiveData<S>,
thirdLiveData: LiveData<T>
) : MediatorLiveData<Triple<F?, S?, T?>>() {
init {
addSource(firstLiveData) {
firstLiveDataValue: F -> value =
Triple(firstLiveDataValue, secondLiveData.value, thirdLiveData.value)
}
addSource(secondLiveData) {
secondLiveDataValue: S -> value =
Triple(firstLiveData.value, secondLiveDataValue, thirdLiveData.value)
}
addSource(thirdLiveData) {
thirdLiveDataValue: T -> value =
Triple(firstLiveData.value, secondLiveData.value, thirdLiveDataValue)
}
}
}
I get data from ROOM Database. As I said earlier, I have a RecyclerView, the logic of which looks like this:
viewModel.tripleMediatorLiveData.observe(viewLifecycleOwner) { triple ->
val settings = triple.first
val availability = triple.second
val chapters = triple.third
when(availability) {
0 -> {
createCourseNotAvailableItems()
binding.btnBegin.setOnClickListener {
viewModel.setTimersForChapters(chapters!!)
viewModel.updateCourseAvailability(1)
val courseEndTime = TimeManager().currentDatePlusMinutes(55)
viewModel.setCourseEndTime(courseEndTime)
}
}
1 -> {
createCourseAvailableItems(chapters!!)
binding.btnBegin.setOnClickListener {
viewModel.resetTimersForChapters(chapters)
viewModel.updateCourseAvailability(0)
viewModel.setCourseEndTime()
}
viewModel.courseCompleted.observe(viewLifecycleOwner) { endDate ->
val currentDate = TimeManager().getCurrentTimeInSeconds()
endDate?.let {
if (currentDate > it && it != 0L) {
CourseCompletedDialog(
onClick = {
viewModel.resetTimersForChapters(chapters)
viewModel.updateCourseAvailability(0)
viewModel.setCourseEndTime()
}
).show(childFragmentManager, CourseCompletedDialog.TAG)
}
}
}
}
}
}
As you can see, the RecyclerView handles a large number of LiveData objects ​​and is displayed differently depending on the state. The problem arises in the appearance of the dialog.
When OnClick triggered, the data is updated and the Observer is triggered, which is why the Observer does not work quite predictably, because of which the dialog may appear several times in a row, BUT IT SHOULD NOT APPEAR AT ALL AFTER THE ONCLICK TRIGGERED.
How can I improve the code so that the dialog does not appear multiple times in a row?

How to create a list from scanned BLE results

as invers to the question asked here How to convert Flow<List<Object>> to Flow<Object> I want to convert my Flow<Object> to Flow<List<Object>>.
At least I think I want that, so I try to explain what I want to achieve and give some background. I am working on an Android application that uses bluetooth to scan and connect to BLE devices. I'm fairly new to the Android platform and kotlin so I haven't quite grasped all the details despite all the many things I've already learnt.
My repository has a method which returns a Flow of ScanResults from the bluetooth adapter:
fun bluetoothScan(): Flow<ScanResult> {
return bluetoothStack.bluetoothScan()
}
My ViewModel consumes that function, maps the data to my BleScanResult and returns it as LiveData.
val scanResults: LiveData<BleScanResult> =
scanEnabled.flatMapLatest { doScan ->
if (doScan) {
repository.bluetoothScan().map { BleScanResult(it.device.name, it.device.address) }
} else {
emptyFlow()
}
}.asLiveData()
In my activity I want to observer on that data and display it in a RecyclerView:
val adapter = ScanResultListAdapter()
binding.rcBleScanResults.adapter = adapter
viewModel.scanResults.observe(this) { result ->
//result.let { adapter.submitList(it) }
}
The problem is that scanResults is from type Flow<BleScanResult> and not Flow<List<BleScanResult>>, so the call to adapter.submitList(it) throws an error as it is expected to be a list.
So, how do I convert Flow to Flow<List> (with additional filtering of duplicates)? Or is there something I miss about the conception of Flow/LiveData?
You can try to use a MutableList and fill it with the data you get form a Flow, something like the following:
val results: MutableList<BleScanResult> = mutableListOf()
val scanResults: LiveData<List<BleScanResult>> =
scanEnabled.flatMapLatest { doScan ->
if (doScan) {
repository.bluetoothScan().map {
results.apply {
add(BleScanResult(it.device.name, it.device.address))
}
}
} else {
emptyFlow()
}
}.asLiveData()
You can also use a MutableSet instead of MutableList if you want to have a unique list of items (assuming BleScanResult is a data class).
You could use the liveData builder to collect the Flow's values into a MutableList.
Here I copy the MutableList using toList() before emitting it since RecyclerView Adapters don't play well with mutable data sources.
val scanResults: LiveData<List<BleScanResult>> = liveData {
val cumulativeResults = mutableListOf<BleScanResult>()
scanEnabled.flatMapLatest { doScan ->
if (doScan) {
repository.bluetoothScan().map { BleScanResult(it.device.name, it.device.address) }
} else {
emptyFlow()
}
}.collect {
cumulativeResults += it
emit(cumulativeResults.toList())
}
}
If you want to avoid duplicate entries and reordering of entries, you can use a set like this:
val scanResults: LiveData<List<BleScanResult>> = liveData {
val cumulativeResults = mutableSetOf<BleScanResult>()
scanEnabled.flatMapLatest { doScan ->
if (doScan) {
repository.bluetoothScan().map { BleScanResult(it.device.name, it.device.address) }
} else {
emptyFlow()
}
}.collect {
if (it !in cumulativeResults) {
cumulativeResults += it
emit(cumulativeResults.toList())
}
}
}

How to collect data inside a loop using flow and coroutines

I'm new to using flow and coroutines and I would like you to help me with the following problem:
I have the useCases.getScheduleList() and useCases.getScheduleDetails() methods that return a Flow. And I need to call useCases.getScheduleList() to get the schedule list and then call useCases.getScheduleDetails() for each item in the schedule list. Following is my attempt:
viewModelScope.launch {
useCases.getScheduleList().collect {
val scheduleList = it
val schedulesWithDetails = arrayListOf<ScheduleWithDetails>()
for (schedule in scheduleList) {
launch {
useCases.getScheduleDetails(schedule.code)
.collect { detail ->
schedulesWithDetails.add(
newScheduleWithDetail(
schedule,
detail.body
)
)
}
}
}
// updateUI is called before collect add items to schedulesWithDetails
updateUI(schedulesWithDetails)
}
}
}
In the code above I can collect the listing and also collect the detail of each item in the listing and add the results to my schedulesWithDetails but I cannot use schedulesWithDetails with the data already added because the updateUI() method does not wait for the collect process to finish in then be executed.
Can anyone help me with ideas/suggestions for this problem?
Try this out:
viewModelScope.launch {
useCases.getScheduleList().map { scheduleList ->
scheduleList.map { async { useCases.getScheduleDetails(it).first() } }.awaitAll()
}.collect { schedulesWithDetails ->
updateUi(schedulesWithDetails)
}
}

FireStore not return all results

I want to get data from firestore, pretend that I have 10 documents in a collection. after that I must get data inside every document, so I save data in ArrayList. But FireBase never return all documents in a collection. Sometimes it returns only 5 ,6 docs in collection that has 10 docs.
my fireBaseUtil :
fun getDocumentByQueryAList( idQuery: List<String>, callBack: (ArrayList<DocumentSnapshot>) -> Unit) {
val listDocumentSnapshot = ArrayList<DocumentSnapshot>()
collRef = fireStore.collection("myCollection")
val size = idQuery.size
for (i in 0 until size) {
val query = collRef.whereEqualTo("fieldQuery", idQuery[i])
query.get().addOnSuccessListener { documents ->
for (document in documents) {
listDocumentSnapshot.add(document)
if (i == size - 1) {
callBack.invoke(listDocumentSnapshot)
}
}
}
}
}
I log out when size = 10 , but i = 8 it called invoke....
UserRepository:
FireBaseUtil.getDocumentByQueryAList{
// myList.addAll(listGettedByCallback)
}
->> when I want to have data in my list I call FireBaseUtil.getDocumentByQueryAList. I know firebase return value async but I dont know how to get all my doc then receiver.invoke("callbackValue").
Please tell me is there any solution. Thank in advance.
The problem you are experiencing is that you are expecting the queries to be run in order like so:
get idQuery[0], then add to list, then
get idQuery[1], then add to list, then
get idQuery[2], then add to list, then
...
get idQuery[8], then add to list, then
get idQuery[9], then add to list, then
invoke callback
But in reality, all of the following things happen in parallel.
get idQuery[0] (add to list when finished)
get idQuery[1] (add to list when finished)
get idQuery[2] (add to list when finished)
...
get idQuery[8] (add to list when finished)
get idQuery[9] (add to list and invoke callback when finished)
If the get idQuery[9] finishes before some of the others, you will be invoking your callback before the list is completely filled.
A primitive way to fix this would be to count the number of finished get queries, and when all of them finish, then invoke the callback.
fun getDocumentByQueryAList( idQuery: List<String>, callBack: (ArrayList<DocumentSnapshot>) -> Unit) {
val listDocumentSnapshot = ArrayList<DocumentSnapshot>()
collRef = fireStore.collection("myCollection")
val size = idQuery.size
val finishedCount = 0
for (i in 0 until size) {
val query = collRef.whereEqualTo("fieldQuery", idQuery[i])
query.get().addOnSuccessListener { documents ->
for (document in documents) {
listDocumentSnapshot.add(document)
}
if (++finishedCount == size) { // ++finishedCount will add 1 to finishedCount, and return the new value
// all tasks done
callBack.invoke(listDocumentSnapshot)
}
}
}
}
However, this will run into issues where the callback is never invoked if any of the queries fail. You could use a addOnFailureListener or addOnCompleteListener to handle these failed tasks.
The more correct and proper way to do what you are expecting is to make use of Tasks.whenAll, which is used in a similar fashion to how you see JavaScript answers using Promise.all. I'm still new to Kotlin syntax myself, so expect the following block to potentially throw errors.
fun getDocumentByQueryAList( idQueryList: List<String>, callBack: (ArrayList<DocumentSnapshot>) -> Unit) {
val listDocumentSnapshot = ArrayList<DocumentSnapshot>()
collRef = fireStore.collection("myCollection")
val getTasks = new ArrayList<Task<Void>>();
for (idQuery in idQueryList) {
val query = collRef.whereEqualTo("fieldQuery", idQuery)
getTasks.add(
query.get()
.onSuccessTask { documents ->
// if query succeeds, add matching documents to list
for (document in documents) {
listDocumentSnapshot.add(document)
}
}
)
}
Tasks.whenAll(getTasks)
.addOnSuccessListener { results ->
callback.invoke(listDocumentSnapshot)
}
.addOnFailureListener { errors ->
// one or more get queries failed
// do something
}
}
Instead of using the callback, you could return a Task instead, where the last bit would be:
return Tasks.whenAll(getTasks)
.onSuccessTask { results ->
return listDocumentSnapshot
}
This would allow you to use the following along with other Task and Tasks methods.
getDocumentByQueryAList(idQueryList)
.addOnSuccessListener { /* ... */ }
.addOnFailureListener { /* ... */ }
Use smth like this using RxJava:
override fun getAllDocs(): Single<List<MyClass>> {
return Single.create { emitter ->
db.collection("myCollection").get()
.addOnSuccessListener { documents ->
val list = mutableListOf<MyClass>()
documents.forEach { document ->
list.add(mapDocumentToMyClass(document))}
emitter.onSuccess(list)
}
.addOnFailureListener { exception ->
emitter.onError(exception)
}
}
}
private fun mapDocumentToMyClass(documentSnapshot: QueryDocumentSnapshot) =
MyClass(
documentSnapshot.get(ID).toString(),
documentSnapshot.get(SMTH).toString(),
// some extra fields
null
)
Or smth like this using coroutine:
override suspend fun getAllDocs(): List<MyClass> {
return try {
val snapshot =
db.collection("myCollection")
.get()
.await()
snapshot.map {
mapDocumentToMyClass(it)
}
} catch (e: Exception) {
throw e
}
}
This functions helps you to get all data from one doc

Kotlin - How to properly map result using Either?

I'm trying to read a list of objects from the database and mapping it to another type of list.
// Returns either a Failure or the expected result
suspend fun getCountries(): Either<Failure, List<CountryItem>> {
// Get the result from the database
val result = countryLocalDataSource.getCountries()
// Left means Failure
if (result.isLeft) {
// Retrieve the error from the database
lateinit var error: Failure
result.either({
error = it
}, {})
// Return the result
return Either.Left(error)
}
// The database returns a List of Country objects, we need to map it to another object (CountryItem)
val countryItems: MutableList<CountryItem> = mutableListOf()
// Iterate the Country List and construct a new List of CountryItems
result.map { countries -> {
countries.forEach {
// Assign some values from an Enum (localized string resources)
val countryEnumValue = Countries.fromId(it.id)
countryEnumValue?.let { countryIt ->
val countryStringNameRes = countryIt.nameStringRes;
// Create the new CountryItem object (#StringRes value: Int, isSelected: Bool)
countryItems.add(CountryItem(countryStringNameRes, false))
}
}
} }
// Because this is a success, return as Right with the newly created List of CountryItems
return Either.Right(countryItems)
}
For the sake of readability I didn't included the whole Repository or the DAO classes and I have left comments in the code snippet above.
In a nutshell: I'm using Kotlin's Coroutines for accessing the database in a separated thread and I'm handling the response on the UI Thread. Using the Either class in order to return two different results (failure or success).
The above code works, however It's too ugly. Is this the right approach to deliver the result?
What I'm trying to do is to refactor the code above.
The whole problem is caused by the two different object types. The Database Data Source API is returning an Either<Failure, List<Country>>, meanwhile the function is expected to return an Either<Failure, List<CountryItem>>.
I can't deliver a List<CountryItem> directly from the Database Data Source API, because Android Studio doesn't let me compile the project (entities implementing interfaces, compile error, etc.). What I'm trying to achieve is to map the Either result in a nicer way.
Try using Kotlin's Result
So in your case you can write something like:
return result.mapCatching { list: List<Country> -> /*return here List<CountryItem>>*/ }
And for checking result call:
result.fold(
onSuccess = {...},
onFailure = {...}
)
In order to invoke a constructor you should call Result.success(T) or Result.failure(Throwable)
Unfortunately, you'll also need to suppress use-as-return-type warnings How to
You can simplify by checking the type of Either and accessing the value directly. In your case:
access Left via result.a -> Failure
access Right via result.b -> List<Country>
ex:
when (result) {
is Either.Left -> {
val failure: Failure = result.b
...
}
is Either.Right -> {
val countries: List<Country> = result.b
...
}
}
An alternative is to use the either() function (normally this is called fold()):
result.either(
{ /** called when Left; it == Failure */ },
{ /** called when Right; it == List<Country> */ }
)
Assume your Country class is defined as follow:
data class Country(val name: String) {}
and your CountryItem class is defined as follow:
data class CountryItem(private val name: String, private val population: Int) {}
and your CountryLocalDataSource class with a method getCountries() like this:
class DataSource {
suspend fun getCountries(): Either<Exception, List<Country>> {
return Either.Right(listOf(Country("USA"), Country("France")))
//return Either.Left(Exception("Error!!!"))
}
}
then the answer to your question would be:
suspend fun getCountryItems(): Either<Exception, List<CountryItem>> {
when (val countriesOrFail = DataSource().getCountries()) {
is Either.Left -> {
return Either.Left(countriesOrFail.a)
}
is Either.Right -> {
val countryItems = countriesOrFail.b.map {
CountryItem(it.name, 1000)
}
return Either.Right(countryItems)
}
}
}
To call your getCountryItems(), here is an example:
suspend fun main() {
when (val countriesOrFail = getCountryItems()) {
is Either.Left -> {
println(countriesOrFail.a.message)
}
is Either.Right -> {
println(countriesOrFail.b)
}
}
}
Here's the sample code in the playground: https://pl.kotl.in/iiSrkv3QJ
A note about your map function:
I'm guessing you don't actually need the result to be a MutableList<CountryItem> but you had to define so because you want to add an element as you iterate through the input list List<Country>.
Perhaps the following is the case: If you have a List<Country> with 2 elements like in the example, and you want to map so that the result becomes a List<CountryItem> with also 2 corresponding elements, then you don't need to call forEach inside a fun that gets passed to the higher-order function map. But this may be an entirely new question.

Categories

Resources