Java Code Examples for androidx.lifecycle.Transformations#switchMap()

The following examples show how to use androidx.lifecycle.Transformations#switchMap() . 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: UserListingViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
public UserListingViewModel(Retrofit retrofit, String query, SortType sortType) {
    userListingDataSourceFactory = new UserListingDataSourceFactory(retrofit, query, sortType);

    initialLoadingState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
            UserListingDataSource::getInitialLoadStateLiveData);
    paginationNetworkState = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
            UserListingDataSource::getPaginationNetworkStateLiveData);
    hasUserLiveData = Transformations.switchMap(userListingDataSourceFactory.getUserListingDataSourceMutableLiveData(),
            UserListingDataSource::hasUserLiveData);

    sortTypeLiveData = new MutableLiveData<>();
    sortTypeLiveData.postValue(sortType);

    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setPageSize(25)
                    .build();

    users = Transformations.switchMap(sortTypeLiveData, sort -> {
        userListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
        return (new LivePagedListBuilder(userListingDataSourceFactory, pagedListConfig)).build();
    });
}
 
Example 2
Source File: QueryRepository.java    From lttrs-android with Apache License 2.0 6 votes vote down vote up
public LiveData<PagedList<ThreadOverviewItem>> getThreadOverviewItems(final EmailQuery query) {
    return Transformations.switchMap(databaseLiveData, new Function<LttrsDatabase, LiveData<PagedList<ThreadOverviewItem>>>() {
        @Override
        public LiveData<PagedList<ThreadOverviewItem>> apply(LttrsDatabase database) {
            return new LivePagedListBuilder<>(database.queryDao().getThreadOverviewItems(query.toQueryString()), 30)
                    .setBoundaryCallback(new PagedList.BoundaryCallback<ThreadOverviewItem>() {
                        @Override
                        public void onZeroItemsLoaded() {
                            Log.d("lttrs", "onZeroItemsLoaded");
                            requestNextPage(query, null); //conceptually in terms of loading indicators this is more of a page request
                            super.onZeroItemsLoaded();
                        }

                        @Override
                        public void onItemAtEndLoaded(@NonNull ThreadOverviewItem itemAtEnd) {
                            Log.d("lttrs", "onItemAtEndLoaded(" + itemAtEnd.emailId + ")");
                            requestNextPage(query, itemAtEnd.emailId);
                            super.onItemAtEndLoaded(itemAtEnd);
                        }
                    })
                    .build();
        }
    });
}
 
Example 3
Source File: CommentViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
public CommentViewModel(Retrofit retrofit, Locale locale, String accessToken, String username, SortType sortType,
                        boolean areSavedComments) {
    commentDataSourceFactory = new CommentDataSourceFactory(retrofit, locale, accessToken, username, sortType,
            areSavedComments);

    initialLoadingState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
            CommentDataSource::getInitialLoadStateLiveData);
    paginationNetworkState = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
            CommentDataSource::getPaginationNetworkStateLiveData);
    hasCommentLiveData = Transformations.switchMap(commentDataSourceFactory.getCommentDataSourceLiveData(),
            CommentDataSource::hasPostLiveData);

    sortTypeLiveData = new MutableLiveData<>();
    sortTypeLiveData.postValue(sortType);

    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setPageSize(25)
                    .build();

    comments = Transformations.switchMap(sortTypeLiveData, sort -> {
        commentDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
        return (new LivePagedListBuilder(commentDataSourceFactory, pagedListConfig)).build();
    });
}
 
Example 4
Source File: MessageViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
public MessageViewModel(Retrofit retrofit, Locale locale, String accessToken, String where) {
    messageDataSourceFactory = new MessageDataSourceFactory(retrofit, locale, accessToken, where);

    initialLoadingState = Transformations.switchMap(messageDataSourceFactory.getMessageDataSourceLiveData(),
            MessageDataSource::getInitialLoadStateLiveData);
    paginationNetworkState = Transformations.switchMap(messageDataSourceFactory.getMessageDataSourceLiveData(),
            MessageDataSource::getPaginationNetworkStateLiveData);
    hasMessageLiveData = Transformations.switchMap(messageDataSourceFactory.getMessageDataSourceLiveData(),
            MessageDataSource::hasPostLiveData);

    whereLiveData = new MutableLiveData<>();
    whereLiveData.postValue(where);

    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setPageSize(25)
                    .build();

    messages = Transformations.switchMap(whereLiveData, newWhere -> {
        messageDataSourceFactory.changeWhere(whereLiveData.getValue());
        return (new LivePagedListBuilder(messageDataSourceFactory, pagedListConfig)).build();
    });
}
 
Example 5
Source File: SubredditListingViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 6 votes vote down vote up
public SubredditListingViewModel(Retrofit retrofit, String query, SortType sortType) {
    subredditListingDataSourceFactory = new SubredditListingDataSourceFactory(retrofit, query, sortType);

    initialLoadingState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
            SubredditListingDataSource::getInitialLoadStateLiveData);
    paginationNetworkState = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
            SubredditListingDataSource::getPaginationNetworkStateLiveData);
    hasSubredditLiveData = Transformations.switchMap(subredditListingDataSourceFactory.getSubredditListingDataSourceMutableLiveData(),
            SubredditListingDataSource::hasSubredditLiveData);

    sortTypeLiveData = new MutableLiveData<>();
    sortTypeLiveData.postValue(sortType);

    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setPageSize(25)
                    .build();

    subreddits = Transformations.switchMap(sortTypeLiveData, sort -> {
        subredditListingDataSourceFactory.changeSortType(sortTypeLiveData.getValue());
        return new LivePagedListBuilder(subredditListingDataSourceFactory, pagedListConfig).build();
    });
}
 
Example 6
Source File: FlexibleViewModel.java    From FlexibleAdapter with Apache License 2.0 6 votes vote down vote up
public FlexibleViewModel() {
    identifier = new MutableLiveData<>();
    liveItems = Transformations.switchMap(identifier, new Function<Identifier, LiveData<List<AdapterItem>>>() {
        @Override
        public LiveData<List<AdapterItem>> apply(Identifier input) {
            return Transformations.map(getSource(input), new Function<Source, List<AdapterItem>>() {
                @Override
                public List<AdapterItem> apply(Source source) {
                    if (isSourceValid(source)) {
                        return map(source);
                    } else {
                        return liveItems.getValue();
                    }
                }
            });
        }
    });
}
 
Example 7
Source File: ReactionsViewModel.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public @NonNull LiveData<List<Reaction>> getRecipients() {
  return Transformations.switchMap(filterEmoji,
                                   emoji -> Transformations.map(repository.getReactions(),
                                                                reactions -> Stream.of(reactions)
                                                                                   .filter(reaction -> emoji == null || reaction.getEmoji().equals(emoji))
                                                                                   .toList()));
}
 
Example 8
Source File: PostViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 5 votes vote down vote up
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
                     SortType sortType, String where, int filter, boolean nsfw) {
    postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
            postType, sortType, where, filter, nsfw);

    initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
            PostDataSource::getInitialLoadStateLiveData);
    paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
            PostDataSource::getPaginationNetworkStateLiveData);
    hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
            PostDataSource::hasPostLiveData);

    nsfwLiveData = new MutableLiveData<>();
    nsfwLiveData.postValue(nsfw);
    sortTypeLiveData = new MutableLiveData<>();
    sortTypeLiveData.postValue(sortType);

    nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);

    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setPageSize(25)
                    .build();

    posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> {
        postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue());
        return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
    });
}
 
Example 9
Source File: PostViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 5 votes vote down vote up
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, String subredditName, int postType,
                     SortType sortType, int filter, boolean nsfw) {
    postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, subredditName,
            postType, sortType, filter, nsfw);

    initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
            PostDataSource::getInitialLoadStateLiveData);
    paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
            PostDataSource::getPaginationNetworkStateLiveData);
    hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
            PostDataSource::hasPostLiveData);

    nsfwLiveData = new MutableLiveData<>();
    nsfwLiveData.postValue(nsfw);
    sortTypeLiveData = new MutableLiveData<>();
    sortTypeLiveData.postValue(sortType);

    nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);

    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setPageSize(25)
                    .build();

    posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> {
        postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue());
        return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
    });
}
 
Example 10
Source File: PostViewModel.java    From Infinity-For-Reddit with GNU Affero General Public License v3.0 5 votes vote down vote up
public PostViewModel(Retrofit retrofit, String accessToken, Locale locale, int postType, SortType sortType,
                     int filter, boolean nsfw) {
    postDataSourceFactory = new PostDataSourceFactory(retrofit, accessToken, locale, postType,
            sortType, filter, nsfw);

    initialLoadingState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
            PostDataSource::getInitialLoadStateLiveData);
    paginationNetworkState = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
            PostDataSource::getPaginationNetworkStateLiveData);
    hasPostLiveData = Transformations.switchMap(postDataSourceFactory.getPostDataSourceLiveData(),
            PostDataSource::hasPostLiveData);

    nsfwLiveData = new MutableLiveData<>();
    nsfwLiveData.postValue(nsfw);
    sortTypeLiveData = new MutableLiveData<>();
    sortTypeLiveData.postValue(sortType);

    nsfwAndSortTypeLiveData = new NSFWAndSortTypeLiveData(nsfwLiveData, sortTypeLiveData);

    PagedList.Config pagedListConfig =
            (new PagedList.Config.Builder())
                    .setEnablePlaceholders(false)
                    .setPageSize(25)
                    .build();

    posts = Transformations.switchMap(nsfwAndSortTypeLiveData, nsfwAndSort -> {
        postDataSourceFactory.changeNSFWAndSortType(nsfwLiveData.getValue(), sortTypeLiveData.getValue());
        return (new LivePagedListBuilder(postDataSourceFactory, pagedListConfig)).build();
    });
}
 
Example 11
Source File: ManageNotificationsViewModel.java    From zephyr with MIT License 5 votes vote down vote up
public ManageNotificationsViewModel(Application application) {
    super(application);

    mSearchQuery = new MutableLiveData<>();
    mSearchQuery.setValue(null);

    mObservableNotificationPreferences = Transformations.switchMap(mSearchQuery, input -> {
        if (StringUtils.isNullOrEmpty(input)) {
            return mDataRepository.getNotificationPreferences();
        } else {
            return mDataRepository.getNotificationPreferencesByName(input);
        }
    });
}
 
Example 12
Source File: QueryRepository.java    From lttrs-android with Apache License 2.0 5 votes vote down vote up
public LiveData<MailboxOverviewItem> getMailboxOverviewItem(final String mailboxId) {
    if (mailboxId == null) {
        return Transformations.switchMap(databaseLiveData, database -> database.mailboxDao().getMailboxOverviewItemLiveData(Role.INBOX));
    } else {
        return Transformations.switchMap(databaseLiveData, database -> database.mailboxDao().getMailboxOverviewItemLiveData(mailboxId));
    }
}
 
Example 13
Source File: VideosViewModel.java    From tv-samples with Apache License 2.0 5 votes vote down vote up
@Inject
public VideosViewModel(Application application, VideosRepository repository) {
    super(application);

    mRepository = repository;

    mAllCategories = mRepository.getAllCategories();

    mSearchResults = Transformations.switchMap(
            mQuery, new Function<String, LiveData<List<VideoEntity>>>() {
                @Override
                public LiveData<List<VideoEntity>> apply(final String queryMessage) {
                    return mRepository.getSearchResult(queryMessage);
                }
            });


    mVideoById = Transformations.switchMap(
            mVideoId, new Function<Long, LiveData<VideoEntity>>() {
                @Override
                public LiveData<VideoEntity> apply(final Long videoId) {
                    return mRepository.getVideoById(videoId);
                }
            });

    /**
     * Using switch map function to react to the change of observed variable, the benefits of
     * this mapping method is we don't have to re-create the live data every time.
     */
    mAllVideosByCategory = Transformations.switchMap(mVideoCategory, new Function<String, LiveData<List<VideoEntity>>>() {
        @Override
        public LiveData<List<VideoEntity>> apply(String category) {
            return mRepository.getVideosInSameCategoryLiveData(category);
        }
    });
}
 
Example 14
Source File: LiveGroup.java    From mollyim-android with GNU General Public License v3.0 5 votes vote down vote up
public LiveGroup(@NonNull GroupId groupId) {
  Context                        context       = ApplicationDependencies.getApplication();
  MutableLiveData<LiveRecipient> liveRecipient = new MutableLiveData<>();

  this.groupDatabase = DatabaseFactory.getGroupDatabase(context);
  this.recipient     = Transformations.switchMap(liveRecipient, LiveRecipient::getLiveData);
  this.groupRecord   = LiveDataUtil.filterNotNull(LiveDataUtil.mapAsync(recipient, groupRecipient-> groupDatabase.getGroup(groupRecipient.getId()).orNull()));

  SignalExecutors.BOUNDED.execute(() -> liveRecipient.postValue(Recipient.externalGroup(context, groupId).live()));
}
 
Example 15
Source File: ThreadRepository.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
public LiveData<List<MailboxWithRoleAndName>> getMailboxes(String threadId) {
    return Transformations.switchMap(databaseLiveData, database ->database.mailboxDao().getMailboxesForThreadLiveData(threadId));
}
 
Example 16
Source File: AbstractQueryViewModel.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
void init() {
    this.threads = Transformations.switchMap(getQuery(), queryRepository::getThreadOverviewItems);
    this.refreshing = Transformations.switchMap(getQuery(), queryRepository::isRunningQueryFor);
    this.runningPagingRequest = Transformations.switchMap(getQuery(), queryRepository::isRunningPagingRequestFor);
}
 
Example 17
Source File: ThreadRepository.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
public LiveData<PagedList<FullEmail>> getEmails(String threadId) {
    return Transformations.switchMap(databaseLiveData, database -> new LivePagedListBuilder<>(database.threadAndEmailDao().getEmails(threadId), 30).build());
}
 
Example 18
Source File: LttrsRepository.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
public LiveData<List<MailboxOverviewItem>> getMailboxes() {
    return Transformations.switchMap(this.databaseLiveData, database -> database.mailboxDao().getMailboxes());
}
 
Example 19
Source File: QueryRepository.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
public LiveData<String[]> getTrashAndJunk() {
    return Transformations.switchMap(databaseLiveData, database -> database.mailboxDao().getMailboxesLiveData(Role.TRASH, Role.JUNK));
}
 
Example 20
Source File: ComposeRepository.java    From lttrs-android with Apache License 2.0 4 votes vote down vote up
public LiveData<List<IdentityWithNameAndEmail>> getIdentities() {
    return Transformations.switchMap(this.databaseLiveData, database -> database.identityDao().getIdentitiesLiveData());
}