icerockdev / moko-widgets

Multiplatform UI DSL with screen management in common code for mobile (android & ios) Kotlin Multiplatform development
https://moko.icerock.dev
Apache License 2.0
387 stars 32 forks source link

Help add MutableLiveData from ListWidget #264

Closed Diy2210 closed 4 years ago

Diy2210 commented 4 years ago

Hi, I try add MutableLiveData use Database, but I don't understand how.

My List in database: Снимок экрана 2020-07-20 в 11 52 56

Database class: Снимок экрана 2020-07-20 в 11 54 58

My old MutableLiveData in ViewModelClass: Снимок экрана 2020-07-20 в 11 57 35

UnitItem class: Снимок экрана 2020-07-20 в 11 54 36

And use in ListWidget: Снимок экрана 2020-07-20 в 11 55 31 Снимок экрана 2020-07-20 в 11 55 41

Diy2210 commented 4 years ago

DONE! I refactor code. MutableLiveData: Снимок экрана 2020-07-20 в 15 32 53

Table Units: Снимок экрана 2020-07-20 в 15 33 19

Result: Screenshot_1595248317

But I have one more question, how implement auto add new items from ListWidget, like notifyDataSetChanged() in JAVA =)? Now to see the new entry in the list I need to restart the application.

Alex009 commented 4 years ago

@Diy2210 to notify changes you should use livedata's value field to change value. _servers.value = myNewList and all will be updated. LiveData it's reactive data holder, all widgets subscribed to livedata and update automatically when value of livedata was changed

Diy2210 commented 4 years ago

@Diy2210 to notify changes you should use livedata's value field to change value. _servers.value = myNewList and all will be updated. LiveData it's reactive data holder, all widgets subscribed to livedata and update automatically when value of livedata was changed

I do not quite understand, you can use my example plz?

My ViewModel: Снимок экрана 2020-07-21 в 16 18 48

ListWidget: Снимок экрана 2020-07-21 в 16 19 04

TableUnits: Снимок экрана 2020-07-21 в 16 19 15

Diy2210 commented 4 years ago

I try add fun reload list items, but it not works:

Снимок экрана 2020-07-22 в 14 20 02

Снимок экрана 2020-07-22 в 14 20 14

Diy2210 commented 4 years ago

I get it, gust add _servers.value = list to fun reload(). Now all be updated! Tnx!

fun reload() {
     list = serverRepository.list()
     _servers.value = list
}