android-viewmodel

MediatorLiveData or switchMap transformation with multiple parameters

爱⌒轻易说出口 提交于 2019-12-03 04:18:45
问题 I am using Transformations.switchMap in my ViewModel so my LiveData collection, observed in my fragment, reacts on changes of code parameter. This works perfectly : public class MyViewModel extends AndroidViewModel { private final LiveData<DayPrices> dayPrices; private final MutableLiveData<String> code = new MutableLiveData<>(); // private final MutableLiveData<Integer> nbDays = new MutableLiveData<>(); private final DBManager dbManager; public MyViewModel(Application application) { super

Observing LiveData from ViewModel

僤鯓⒐⒋嵵緔 提交于 2019-12-02 21:01:28
I have a separate class in which I handle data fetching (specifically Firebase) and I usually return LiveData objects from it and update them asynchronously. Now I want to have the returned data stored in a ViewModel, but the problem is that in order to get said value, I need to observe the LiveData object returned from my data fetching class. The observe method required a LifecycleOwner object as the first parameter, but I obviously don't have that inside of my ViewModel and I know I am not supposed to keep a reference to the Activity/Fragment inside of the ViewModel. What should I do? In

LiveData not Observing after first Call

心已入冬 提交于 2019-12-02 10:14:40
I implemented LiveData & ViewModel to mimic AsyncTaskLoader. I load file names from the camera directory in DCIM, and then i attach a fileObserver to Observe when a File (picture) is deleted, and then a callback tells the LiveData to re-fetch the fileNames when delete event occurs The Problem: The code below should fetch the file Names from DCIM/Pictures asynchronously with the help of LiveData and then a FileObserver is attached to the directory (DCIM/Pictures), to monitor when a file is deleted and a callback is implemented with the LiveData sub-class to reload the files, as demonstrated in

ViewPager with viewmodel and live data , all 6 tabs data is replaced by last tab data

醉酒当歌 提交于 2019-12-01 03:38:22
问题 I am working on a ViewPager with 6 tabs where it has only one fragment TimesListFragment Depending on the arguments passed to TimesListFragment it calls api eg; science , technology, travel etc I have followed Google's GithubBrowserSample for my app I have TimesListFragment -> TimesViewModel -> TimesRepository There are 6 tabs , when I hit the api all the tabs show the same result which if of the last argument StoriesPagerAdapter.kt class StoriesPagerAdapter(fragmentManager: FragmentManager?)

How does Android Paging Library know to load more data?

て烟熏妆下的殇ゞ 提交于 2019-11-30 17:52:55
Iʼm fairly new to developing Android apps and Iʼm trying to do everything “the right way.” So right now, Iʼm implementing the new Android Paging Library into my project, where I need to load a list of articles from a network server. I have an ArticlesRepository class that returns an ArticleList class containing instances of ArticleListItem that I would like to display in a RecyclerView. The list of articles is paginated already on the server, so the repository sends a request for the first page and returns an ArticleList with the page property set to 1 and the articles property containing a

LiveData Transformations.map() with multiple arguments

邮差的信 提交于 2019-11-30 15:32:35
问题 I have a value in the UI that it's value depends on two LiveData objects. Imagine a shop where you need a subtotal = sum of all items price and a total = subtotal + shipment price . Using Transformations we can do the following for the subtotal LiveData object (as it only depends on itemsLiveData ): val itemsLiveData: LiveData<List<Items>> = ... val subtotalLiveData = Transformations.map(itemsLiveData) { items -> getSubtotalPrice(items) } In the case of the total it would be great to be able

Android Arch Components ViewModel and LiveData trigger after screen rotation

早过忘川 提交于 2019-11-30 04:05:12
问题 I have a problem when using ViewModel and LiveData I am new using ViewModel and LiveData arch components and have the problem when using fragments and rotate the screen the observer get triggered... I tried to move the viewModel = ViewModelProviders.of(this).get(MainViewModel::class.java) in all the fragment lifecycle methods but with no success. My scenario is kind of really easy one: Login screen with email and password User clicks on the "login" button the viewmodel calls the login(email,

ViewModelProviders with Dagger 2, not able to grasp the concept

拥有回忆 提交于 2019-11-29 11:07:47
I have a Retrofit Service like this public interface BrandsService { @GET("listBrand") Call<List<Brand>> getBrands(); } Then I have a Repository to get data from an api like this public class BrandsRepository { public static final String TAG = "BrandsRepository"; MutableLiveData<List<Brand>> mutableLiveData; Retrofit retrofit; @Inject public BrandsRepository(Retrofit retrofit) { this.retrofit = retrofit; } public LiveData<List<Brand>> getListOfBrands() { // Retrofit retrofit = ApiManager.getAdapter(); final BrandsService brandsService = retrofit.create(BrandsService.class); Log.d(TAG,

Android Architecture Components ViewModel - communication with Service/IntentService

落花浮王杯 提交于 2019-11-28 19:57:53
I'm exploring Google's Android Architecture Components . In my project I'm relying on Services and IntentServices . What is the correct way to communicate with app's ViewModel from an IntentService or Service? Is it achievable using LiveData? TL;DR It's achievable - use an observer relationship. Your IntentService and likely location service should not be aware of your ViewModel. Consider using a Repository. LiveData can be used (see postValue ). It's good for updating the UI (ViewModel to Activity communication) because it's lifecycle-aware. When you're not updating the UI, you could consider

ViewModelProviders with Dagger 2, not able to grasp the concept

江枫思渺然 提交于 2019-11-28 04:46:16
问题 I have a Retrofit Service like this public interface BrandsService { @GET("listBrand") Call<List<Brand>> getBrands(); } Then I have a Repository to get data from an api like this public class BrandsRepository { public static final String TAG = "BrandsRepository"; MutableLiveData<List<Brand>> mutableLiveData; Retrofit retrofit; @Inject public BrandsRepository(Retrofit retrofit) { this.retrofit = retrofit; } public LiveData<List<Brand>> getListOfBrands() { // Retrofit retrofit = ApiManager