android-viewmodel

Two-way data binding Converter

血红的双手。 提交于 2021-01-29 14:16:43
问题 I'm trying to use a converter from CharArray to String for my password two-way databinding field. var password = ObservableField<CharArray>() Following the doc : public class Converter { @InverseMethod("stringToCharArray") public static String charArrayToString( ClearableEditText view, char[] oldValue, char[] value ){ return value.toString(); } public static char [] stringToCharArray( ClearableEditText view, String oldValue, String value ){ return value.toCharArray(); } } In XML file I used

How to read DataBaseRoom and display a textView

陌路散爱 提交于 2021-01-29 06:49:21
问题 I am practicing with dataBaseRoom, for them I have created a button, which allow me to read database , i want to put the name of a user and when pressing the button (read data base) reflects me, his name, last name and age in the texview,the problem is that I do not know how to display the data in a textView. Activity where I want to carry out the actions //Entity @Entity(tableName = "user_table") data class User( @PrimaryKey(autoGenerate = true)@ColumnInfo(name = "id") val id: Int, val

Right place to offer or send Channel in MVI pattern

风流意气都作罢 提交于 2021-01-29 06:41:04
问题 I load data in recycleView in advance. In order to do that I have following code in onCreate() of Activity : override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) setupUI() setupViewModel() observeViewModel() if (savedInstanceState == null) { mainViewModel.userIntent.offer(MainIntent.FetchUser) } } As you see I offer() when savedInstanceState is null, The problem is when we have process death ( you can simply create it

Savedstateviewmodelfactory is finall, cannot inherit

别说谁变了你拦得住时间么 提交于 2021-01-29 05:13:47
问题 class SettingsViewModelFactory(application: Application, owner: SavedStateRegistryOwner) : SavedStateViewModelFactory(application, owner){ override fun <T : ViewModel?> create(modelClass: Class<T>): T { if (modelClass.isAssignableFrom(SettingsViewModel::class.java)){ return SettingsViewModel() as T } throw IllegalArgumentException("Invalid ViewModel class") } } I get a red underline below SavedStateViewModelFactory saying that the type is final and cannot be inherited from. What do I do? The

Unit Test for a ViewModel class with lazy loading

女生的网名这么多〃 提交于 2021-01-28 04:58:33
问题 I am trying to write a unit test for my viewModel class which has lazy loading. I have following ViewModel class : abstract class DetailViewModel(item: TmdbItem) : BaseViewModel() { private val _trailers = MutableLiveData<List<Video>>() val trailers: LiveData<List<Video>> get() = _trailers private val _cast: MutableLiveData<List<Cast>> by lazy { MutableLiveData<List<Cast>>().also { arrayOf(composeObservable { getTrailers(item.id) } .subscribe({ videos -> _trailers.postValue(videos) }) {

MVVM architecture in android Using Volley

我们两清 提交于 2021-01-27 19:50:18
问题 I'm studying the MVVM to see if it can help me for my upcoming projects. What I understand so far, is that I need to use a ViewModel for holding my UI datas. I also need to use a Repository class to perform all my Requests to WebServices, and I'm using the Volley Library. So here's what I did : The ViewModel public class MyViewModel extends ViewModel { private MyRepository repository; private MutableLiveData<MyPojo> pojo; public MyViewModel(MyRepository repository) { this.repository =

viewModelScope not cancelled

拜拜、爱过 提交于 2021-01-27 05:56:20
问题 After watching Sean's explanation on Android (Google I/O'19) I've tried the same: init{ viewModelScope.launch { Timber.i("coroutine awake") while (true){ delay(2_000) Timber.i("another round trip") } } } Unfortunately onCleared it's called when the activity is killed but not when it's put in background ("when we move away from the Activity...", background is "moving away" imho ^^). And I get the following output: > ---- Activity in Foreground > 12:41:10.195 TEST: coroutine awake > 12:41:12

View models for RecyclerView items

你离开我真会死。 提交于 2021-01-20 16:42:05
问题 My activity has a Google's ViewModel that fetches some model items. These items are then transformed into adapter items of a RecyclerView. There are also many types of adapter items supported by one RecyclerView. I would like to have separate view model object for each of these model objects so that I can have more complex logic encapsulated only within that "small" view model. Currently when I have some asynchronous logic (that needs to be stopped in onCleared()) that is related only to some

Properly Scoping ViewModel Across Screens

余生长醉 提交于 2021-01-05 07:20:10
问题 Let's say we have a signup form which span multiple screens such as: So I created a Scoped ViewModel spanning across these screens: The problem is that since the state is scoped across screens, going: PersonalInfo Screen -> Set Username Screen -> (pressed back) Personal Info Screen make it like this: Notice that the username state is retained (since the ViewModel is scoped across the whole flow). So when the user goes back to Set Username Screen instead of a blank state, it retains the

Properly Scoping ViewModel Across Screens

六眼飞鱼酱① 提交于 2021-01-05 07:19:24
问题 Let's say we have a signup form which span multiple screens such as: So I created a Scoped ViewModel spanning across these screens: The problem is that since the state is scoped across screens, going: PersonalInfo Screen -> Set Username Screen -> (pressed back) Personal Info Screen make it like this: Notice that the username state is retained (since the ViewModel is scoped across the whole flow). So when the user goes back to Set Username Screen instead of a blank state, it retains the