androidx.lifecycle.MediatorLiveData Java Examples
The following examples show how to use
androidx.lifecycle.MediatorLiveData.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SitePermissionViewModel.java From FirefoxReality with Mozilla Public License 2.0 | 6 votes |
public LiveData<List<SitePermission>> getAll(@SitePermission.Category int category) { MediatorLiveData<List<SitePermission>> result = mObservableSites.get(category); if (result == null) { LiveData<List<SitePermission>> sites = mRepository.getSitePermissions(); final MediatorLiveData<List<SitePermission>> mediator = new MediatorLiveData<>(); mediator.setValue(null); mediator.addSource(sites, values -> { mediator.setValue(values.stream() .filter(sitePermission -> sitePermission.category == category) .collect(Collectors.toList())); }); mObservableSites.put(category, mediator); result = mediator; } return result; }
Example #2
Source File: MoodleViewModel.java From ETSMobile-Android2 with Apache License 2.0 | 6 votes |
/** * Returns the assignments courses * <p> * Each course contains a list of Moodle assignments that the user can view for that course * * @return {@link LiveData} instance which contains the assignments courses */ public LiveData<RemoteResource<List<MoodleAssignmentCourse>>> getAssignmentCourses() { if (filteredAssignmentCourses == null || filteredAssignmentCourses.getValue() == null || filteredAssignmentCourses.getValue().data == null) { filteredAssignmentCourses = new MediatorLiveData<>(); filteredAssignmentCourses.addSource(repository.getAssignmentCourses(), assignmentCoursesRes -> { assignmentCourses.setValue(assignmentCoursesRes); if (assignmentCoursesRes != null && assignmentCoursesRes.data != null) { setFilteredAssignmentCourses(assignmentCoursesRes); } else { filteredAssignmentCourses.setValue(assignmentCoursesRes); } }); } return filteredAssignmentCourses; }
Example #3
Source File: LiveDataUtil.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
/** * Filters output of a given live data based off a predicate. */ public static @NonNull <A> LiveData<A> filter(@NonNull LiveData<A> source, @NonNull Predicate<A> predicate) { MediatorLiveData<A> mediator = new MediatorLiveData<>(); mediator.addSource(source, newValue -> { if (predicate.test(newValue)) { mediator.setValue(newValue); } }); return mediator; }
Example #4
Source File: LiveDataUtil.java From mollyim-android with GNU General Public License v3.0 | 5 votes |
/** * Runs the {@param backgroundFunction} on the supplied {@param executor}. * <p> * Regardless of the executor supplied, the background function is run serially. * <p> * The background function may not run for all {@param source} updates. Later updates taking priority. */ public static <A, B> LiveData<B> mapAsync(@NonNull Executor executor, @NonNull LiveData<A> source, @NonNull Function<A, B> backgroundFunction) { MediatorLiveData<B> outputLiveData = new MediatorLiveData<>(); Executor liveDataExecutor = new SerialMonoLifoExecutor(executor); outputLiveData.addSource(source, currentValue -> liveDataExecutor.execute(() -> outputLiveData.postValue(backgroundFunction.apply(currentValue)))); return outputLiveData; }
Example #5
Source File: TrayViewModel.java From FirefoxReality with Mozilla Public License 2.0 | 5 votes |
public TrayViewModel(@NonNull Application application) { super(application); isMaxWindows = new MutableLiveData<>(new ObservableBoolean(true)); shouldBeVisible = new MutableLiveData<>(new ObservableBoolean(true)); isKeyboardVisible = new MutableLiveData<>(new ObservableBoolean(false)); downloadsNumber = new MutableLiveData<>(new ObservableInt(0)); isVisible = new MediatorLiveData<>(); isVisible.addSource(shouldBeVisible, mIsVisibleObserver); isVisible.addSource(isKeyboardVisible, mIsVisibleObserver); isVisible.setValue(new ObservableBoolean(false)); }
Example #6
Source File: DataRepository.java From FirefoxReality with Mozilla Public License 2.0 | 5 votes |
private DataRepository(final @NonNull AppDatabase database, final @NonNull AppExecutors executors) { mDatabase = database; mExecutors = executors; mLifeCycle = new LifecycleRegistry(this); mLifeCycle.setCurrentState(Lifecycle.State.STARTED); mObservablePopUps = new MediatorLiveData<>(); mObservablePopUps.addSource(mDatabase.sitePermissionDao().loadAll(), sites -> { if (mDatabase.getDatabaseCreated().getValue() != null) { mObservablePopUps.postValue(sites); } }); }
Example #7
Source File: DataRepository.java From mapbox-plugins-android with BSD 2-Clause "Simplified" License | 5 votes |
private DataRepository(final SearchHistoryDatabase database) { this.database = database; observableSearchHistory = new MediatorLiveData<>(); observableSearchHistory.addSource(database.searchHistoryDao().getAll(), new Observer<List<SearchHistoryEntity>>() { @Override public void onChanged(@Nullable List<SearchHistoryEntity> searchHistoryEntities) { if (database.getDatabaseCreated().getValue() != null) { observableSearchHistory.postValue(searchHistoryEntities); } } }); }
Example #8
Source File: Monarchy.java From realm-monarchy with Apache License 2.0 | 5 votes |
/** * Returns a LiveData that evaluates the new results on a background looper thread. * * The resulting list is driven by a PositionalDataSource from the Paging Library. * * The fetch executor of the provided LivePagedListBuilder will be overridden with Monarchy's own FetchExecutor that Monarchy runs its queries on. */ public <R, T extends RealmModel> LiveData<PagedList<R>> findAllPagedWithChanges(DataSource.Factory<Integer, T> dataSourceFactory, LivePagedListBuilder<Integer, R> livePagedListBuilder) { assertMainThread(); final MediatorLiveData<PagedList<R>> mediator = new MediatorLiveData<>(); if(!(dataSourceFactory instanceof RealmDataSourceFactory)) { throw new IllegalArgumentException( "The DataSource.Factory provided to this method as the first argument must be the one created by Monarchy."); } RealmDataSourceFactory<T> realmDataSourceFactory = (RealmDataSourceFactory<T>) dataSourceFactory; PagedLiveResults<T> liveResults = realmDataSourceFactory.pagedLiveResults; mediator.addSource(liveResults, new Observer<PagedList<T>>() { @Override public void onChanged(@Nullable PagedList<T> ts) { // do nothing, this is to intercept `onActive()` calls to ComputableLiveData } }); LiveData<PagedList<R>> computableLiveData = livePagedListBuilder .setFetchExecutor(new RealmQueryExecutor(this)) .build(); mediator.addSource(computableLiveData, new Observer<PagedList<R>>() { @Override public void onChanged(@Nullable PagedList<R> data) { mediator.postValue(data); } }); return mediator; }
Example #9
Source File: ObjectBoxDAO.java From Hentoid with Apache License 2.0 | 5 votes |
public LiveData<Integer> countAllBooks() { // This is not optimal because it fetches all the content and returns its size only // That's because ObjectBox v2.4.0 does not allow watching Query.count or Query.findLazy using LiveData, but only Query.find // See https://github.com/objectbox/objectbox-java/issues/776 ObjectBoxLiveData<Content> livedata = new ObjectBoxLiveData<>(db.selectVisibleContentQ()); MediatorLiveData<Integer> result = new MediatorLiveData<>(); result.addSource(livedata, v -> result.setValue(v.size())); return result; }
Example #10
Source File: ObjectBoxDAO.java From Hentoid with Apache License 2.0 | 5 votes |
public LiveData<Integer> countBooks(String query, List<Attribute> metadata, boolean favouritesOnly) { // This is not optimal because it fetches all the content and returns its size only // That's because ObjectBox v2.4.0 does not allow watching Query.count or Query.findLazy using LiveData, but only Query.find // See https://github.com/objectbox/objectbox-java/issues/776 ObjectBoxLiveData<Content> livedata = new ObjectBoxLiveData<>(db.queryContentSearchContent(query, metadata, favouritesOnly, Preferences.Constant.ORDER_FIELD_NONE, false)); MediatorLiveData<Integer> result = new MediatorLiveData<>(); result.addSource(livedata, v -> result.setValue(v.size())); return result; }
Example #11
Source File: WindowViewModel.java From FirefoxReality with Mozilla Public License 2.0 | 4 votes |
@NonNull public MediatorLiveData<ObservableBoolean> getIsTopBarVisible() { return isTopBarVisible; }
Example #12
Source File: WindowViewModel.java From FirefoxReality with Mozilla Public License 2.0 | 4 votes |
@NonNull public MediatorLiveData<ObservableBoolean> getIsTitleBarVisible() { return isTitleBarVisible; }
Example #13
Source File: WindowViewModel.java From FirefoxReality with Mozilla Public License 2.0 | 4 votes |
@NonNull public MediatorLiveData<String> getTitleBarUrl() { return titleBarUrl; }
Example #14
Source File: WindowViewModel.java From FirefoxReality with Mozilla Public License 2.0 | 4 votes |
@NonNull public MediatorLiveData<ObservableBoolean> getIsInsecureVisible() { return isInsecureVisible; }
Example #15
Source File: MoodleRepository.java From ETSMobile-Android2 with Apache License 2.0 | 4 votes |
public LiveData<RemoteResource<MoodleProfile>> getProfile() { return new NetworkBoundResource<MoodleProfile, MoodleProfile>() { @Override protected void saveCallResult(@NonNull MoodleProfile item) { moodleProfileDao.insert(item); } @Override protected boolean shouldFetch(@Nullable MoodleProfile data) { return true; } @NonNull @Override protected LiveData<MoodleProfile> loadFromDb() { return moodleProfileDao.find(); } @NonNull @Override protected LiveData<ApiResponse<MoodleProfile>> createCall() { MediatorLiveData<ApiResponse<MoodleProfile>> mediatorLiveData = new MediatorLiveData<>(); LiveData<RemoteResource<MoodleToken>> tokenLiveData = getToken(); mediatorLiveData.addSource(tokenLiveData, token -> { if (token != null && token.status == RemoteResource.SUCCESS) { mediatorLiveData.removeSource(tokenLiveData); mediatorLiveData.addSource(moodleWebService.getProfile(ApplicationManager.userCredentials.getMoodleToken()), new Observer<ApiResponse<MoodleProfile>>() { @Override public void onChanged(@Nullable ApiResponse<MoodleProfile> moodleProfileApiResponse) { mediatorLiveData.setValue(moodleProfileApiResponse); } }); } else if (token != null && token.status == RemoteResource.ERROR) { String errorMsg = token.message != null ? token.message : context.getString(R.string.moodle_error_cant_get_token); mediatorLiveData.setValue(new ApiResponse<>(new Throwable(errorMsg))); mediatorLiveData.removeSource(tokenLiveData); } }); return mediatorLiveData; } }.asLiveData(); }
Example #16
Source File: MoodleRepository.java From ETSMobile-Android2 with Apache License 2.0 | 4 votes |
public LiveData<RemoteResource<MoodleAssignmentSubmission>> getAssignmentSubmission(final int assignId) { return new NetworkBoundResource<MoodleAssignmentSubmission, MoodleAssignmentSubmission>() { @Override protected void saveCallResult(@NonNull MoodleAssignmentSubmission item) { item.setAssignId(assignId); moodleAssignmentSubmissionDao.insert(item); } @Override protected boolean shouldFetch(@Nullable MoodleAssignmentSubmission data) { return true; } @NonNull @Override protected LiveData<MoodleAssignmentSubmission> loadFromDb() { return moodleAssignmentSubmissionDao.getByAssignmentId(assignId); } @NonNull @Override protected LiveData<ApiResponse<MoodleAssignmentSubmission>> createCall() { MediatorLiveData<ApiResponse<MoodleAssignmentSubmission>> submission = new MediatorLiveData<>(); LiveData<RemoteResource<MoodleToken>> tokenLD = getToken(); submission.addSource(tokenLD, token -> { if (token != null && token.status == RemoteResource.SUCCESS) { submission.removeSource(tokenLD); String tokenStr = ApplicationManager.userCredentials.getMoodleToken(); LiveData<ApiResponse<MoodleAssignmentSubmission>> submissionApi = moodleWebService.getAssignmentSubmission(tokenStr, assignId); submission.addSource(submissionApi, submission::setValue); } else if (token != null && token.status == RemoteResource.ERROR) { submission.removeSource(tokenLD); boolean displayDefaultMsg = token.message == null || token.message.isEmpty(); String errorMsg = displayDefaultMsg ? context.getString(R.string.moodle_error_cant_get_courses) : token.message; submission.setValue(new ApiResponse<>(new Throwable(errorMsg))); } }); return submission; } }.asLiveData(); }