android-livedata

NetworkBoundResource helper class without Room

北慕城南 提交于 2019-12-04 02:14:49
When I tried to implements the NetworkBoundResource and Resource helper class for the Room Db and Retrofit, it works perfect. However, I need to implement the Search Result from RESTful using Retrofit only without Room . The Resources class is good and I dont need to change it. What I want to do is try to remove db source inside this class. public abstract class NetworkBoundResource<ResultType, RequestType> { private final AppExecutors appExecutors; private final MediatorLiveData<Resource<ResultType>> result = new MediatorLiveData<>(); @MainThread public NetworkBoundResource(AppExecutors

Android Architecture Components: How is LiveData in the repository observed by a ViewModel

ぃ、小莉子 提交于 2019-12-03 23:11:48
问题 i'm studying the Android Architecture Components and i'm a little bit confused. In the sample they use a repository and state that changes within the datasource of the repository are observed by the ViewModels. I don't understand how the changes within the datasource are pushed to the ViewModels, as i cannot see any code within the ViewModels that subscribes them to the repository. Analogously, the fragments observe the ViewModel's LiveData, but they actually subscribe to the LiveData: //

Refreshing MutableLiveData of list of items

怎甘沉沦 提交于 2019-12-03 16:34:38
问题 I'm using LiveData and ViewModel from the architecture components in my app. I have a list of items that is paginated, I load more as the user scrolls down. The result of the query is stored in a MutableLiveData<List<SearchResult>> When I do the initial load and set the variable to a new list, it triggers a callback on the binding adapter that loads the data into the recyclerview. However, when I load the 2nd page and I add the additional items to the list, the callback is not triggered.

Best practice: Runtime filters with Room and LiveData

时间秒杀一切 提交于 2019-12-03 16:21:55
问题 I am working on a screen that shows the contents of a Room wrapped DB using a recycler. The adapter gets the LiveData from a ViewModel that hides the query call on the Room DAO object. So, the LiveData object is actually a ComputableLiveData object that is aware of changes to the Room DB. Now I want to add filter options to the screen. Where / how would I implement this in this Room-LiveData-ViewModel setup? Should the adapter or ViewModel "postfilter" the results in the LiveData? Should I

How to combine two live data one after the other?

馋奶兔 提交于 2019-12-03 12:46:50
I have next use case: User comes to registration form, enters name, email and password and clicks on register button. After that system needs to check if email is taken or not and based on that show error message or create new user... I am trying to do that using Room, ViewModel and LiveData. This is some project that on which I try to learn these components and I do not have remote api, I will store everything in local database So I have these classes: RegisterActivity RegisterViewModel User UsersDAO UsersRepository UsersRegistrationService So the idea that I have is that there will be

Android Room : LiveData callback of update insert?

霸气de小男生 提交于 2019-12-03 11:14:52
问题 I have a Simple DAO including CRUD function FeedEntryDAO.java @Dao public interface FeedEntryDAO { @Query("SELECT * FROM feedEntrys") LiveData<List<FeedEntry>> getAll(); @Query("SELECT * FROM feedEntrys WHERE uid = :uid LIMIT 1") LiveData<FeedEntry> findByUid(int uid); @Insert void insertAll(FeedEntry... feedEntries); @Delete void delete(FeedEntry feedEntry); @Update int update(FeedEntry feedEntry); } For the select , it is okay to return the LiveData type. Inside the Activity the code is

Android ViewModel inside Service (Alternative)

若如初见. 提交于 2019-12-03 07:08:44
I have a service which provides UI that is visible to user most of the time. I was experimenting with new Application Architecture when I came with a problem. MyModelviewModel viewModel = ViewModelProviders.of(this).get(MyModelviewModel.class); But as you know this can be only AppCompat or Fragment Is there some alternative? or can I put observer directly on my LiveData like Im puting on ViewModel viewModel.getList().observe(Playground.this, new Observer<List<TestEntity>>() { @Override public void onChanged(@Nullable List<TestEntity> items) { recyclerViewAdapter.addItems(items); } }); LiveData

Live Data and 2-Way Data Binding: Custom setter not being called

佐手、 提交于 2019-12-03 06:49:45
I am using 2-way data binding to update a LiveData String object from my ViewModel with a string set in the EditText: <android.support.design.widget.TextInputEditText android:id="@+id/writeReviewTitle" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@={viewModel.liveReviewTitle}" /> So, from my understanding, the ViewModel would have its liveReviewTitle attribute updated every time the text changed in the EditText. I assume this is happening through the usage of a TextWatcher or some sort of listening mechanism that is being taken care of for me by the

Convert RxJava Observables To Live Data With Kotlin Extension Functions

五迷三道 提交于 2019-12-03 05:51:17
问题 I've been using alot of RxJava Observables converted to LiveData in my code using LiveDataReactiveStreams.fromPublisher() library. So I though of adding an extension function to the RxJava Observable to easily convert them to LiveData . These are my extension functions: fun <T> Flowable<T>.toLiveData() : LiveData<T> { return LiveDataReactiveStreams.fromPublisher(this) } fun <T> Observable<T>.toLiveData(backPressureStrategy: BackpressureStrategy) : LiveData<T> { return LiveDataReactiveStreams

Best practice: Runtime filters with Room and LiveData

佐手、 提交于 2019-12-03 05:42:31
I am working on a screen that shows the contents of a Room wrapped DB using a recycler. The adapter gets the LiveData from a ViewModel that hides the query call on the Room DAO object. So, the LiveData object is actually a ComputableLiveData object that is aware of changes to the Room DB. Now I want to add filter options to the screen. Where / how would I implement this in this Room-LiveData-ViewModel setup? Should the adapter or ViewModel "postfilter" the results in the LiveData? Should I requery the data from room for every filter change? Can I reuse the underlying (Computable)LiveData for