Java Code Examples for android.arch.lifecycle.Transformations#switchMap()

The following examples show how to use android.arch.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: ProjectsViewModel.java    From MVVM with MIT License 5 votes vote down vote up
public LiveData<Lcee<Projects>> getProjects() {
    if (null == ldProjects) {
        ldPage = new MutableLiveData<>();
        ldProjects = Transformations.switchMap(ldPage, new Function<Integer, LiveData<Lcee<Projects>>>() {
            @Override
            public LiveData<Lcee<Projects>> apply(Integer page) {
                return projectsRepository.getProjects(page);
            }
        });
    }
    return ldProjects;
}
 
Example 2
Source File: VideosViewModel.java    From leanback-showcase 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 3
Source File: UserViewModel.java    From AndroidBlueprints with Apache License 2.0 5 votes vote down vote up
@Inject
public UserViewModel(UserRepository userRepository) {
    this.repository = userRepository;
    if (user == null) {
        Timber.d("Init UserViewModel");
        userIdentifier = new MutableLiveData<>();
        user = Transformations.switchMap(userIdentifier, input -> {
            if (input == null) {
                return AbsentLiveData.create();
            }
            return repository.login(new LoginRequest(input.username, input.password));
        });
    }
}
 
Example 4
Source File: SellBuyViewModel.java    From bitshares_wallet with MIT License 5 votes vote down vote up
public LiveData<Resource<BitsharesAsset>> getAvaliableBalance() {
    LiveData<Resource<BitsharesAsset>> balanceData = Transformations.switchMap(
            Transformations.switchMap(currencyData, input -> {
                return statusChangeLiveData;
            }),
            statusChange -> {
                return new AvailableBalanceRepository().getTargetAvaliableBlance(currencyData.getValue());
            });


    return balanceData;
}
 
Example 5
Source File: UserViewModel.java    From MVVM with MIT License 5 votes vote down vote up
public LiveData<Lcee<User>> getUser() {
    if (null == ldUser) {
        ldUsername = new MutableLiveData<>();
        ldUser = Transformations.switchMap(ldUsername, new Function<String, LiveData<Lcee<User>>>() {
            @Override
            public LiveData<Lcee<User>> apply(String username) {
                return userRepository.getUser(username);
            }
        });
    }
    return ldUser;
}
 
Example 6
Source File: UserViewModel.java    From MVVM with MIT License 5 votes vote down vote up
public LiveData<Lcee<User>> getUser() {
    if (null == ldUser) {
        ldUsername = new MutableLiveData<>();
        ldUser = Transformations.switchMap(ldUsername, new Function<String, LiveData<Lcee<User>>>() {
            @Override
            public LiveData<Lcee<User>> apply(String username) {
                return userRepository.getUser(username);
            }
        });
    }
    return ldUser;
}
 
Example 7
Source File: UserViewModel.java    From MVVM with MIT License 5 votes vote down vote up
public LiveData<Lcee<User>> getUser() {
    if (null == ldUser) {
        ldUsername = new MutableLiveData<>();
        ldUser = Transformations.switchMap(ldUsername, new Function<String, LiveData<Lcee<User>>>() {
            @Override
            public LiveData<Lcee<User>> apply(String username) {
                return userRepository.getUser(username);
            }
        });
    }
    return ldUser;
}
 
Example 8
Source File: UserViewModel.java    From MVVM with MIT License 5 votes vote down vote up
public LiveData<Lcee<User>> getUser() {
    if (null == ldUser) {
        ldUsername = new MutableLiveData<>();
        ldUser = Transformations.switchMap(ldUsername, new Function<String, LiveData<Lcee<User>>>() {
            @Override
            public LiveData<Lcee<User>> apply(String username) {
                return userRepository.getUser(username);
            }
        });
    }
    return ldUser;
}
 
Example 9
Source File: UserViewModel.java    From MVVM with MIT License 5 votes vote down vote up
public LiveData<Lcee<User>> getUser() {
    if (null == ldUser) {
        ldUsername = new MutableLiveData<>();
        ldUser = Transformations.switchMap(ldUsername, new Function<String, LiveData<Lcee<User>>>() {
            @Override
            public LiveData<Lcee<User>> apply(String username) {
                return userRepository.getUser(username);
            }
        });
    }
    return ldUser;
}
 
Example 10
Source File: MovieViewModel.java    From GracefulMovies with Apache License 2.0 5 votes vote down vote up
public LiveData<DataResource<List<MovieEntity>>> getMovieList(boolean now) {
    return Transformations.switchMap(mCity, input ->
            Transformations.switchMap(getLoadLive(), aBoolean -> {
                        if (now) {
                            return MovieRepository.getInstance().getMovieNowList(input.getId());
                        } else {
                            return MovieRepository.getInstance().getMovieFutureList(input.getId());
                        }
                    }
            )
    );
}
 
Example 11
Source File: MainViewModel.java    From firestore-android-arch-components with Apache License 2.0 5 votes vote down vote up
@Inject
MainViewModel(MainRepository repository) {
    this.repository = repository;
    filters.setValue(Filters.getDefault());
    isSignedIn = new LiveData<Boolean>() {
        @Override
        protected void onActive() {
            super.onActive();
            setValue(FirebaseAuth.getInstance().getCurrentUser() != null);
        }
    };

    restaurants = Transformations.switchMap(filters, repository::restaurants);
}
 
Example 12
Source File: LogcatContentViewModel.java    From Fairy with Apache License 2.0 4 votes vote down vote up
public LogcatContentViewModel(@NonNull Application application, LogcatContentRepository repository) {
    super(application);
    this.repository = repository;
    contentLiveData = Transformations.switchMap(isStartFirstLoad, isStart -> repository.getLogcatContent());
}
 
Example 13
Source File: BoxOfficeViewModel.java    From GracefulMovies with Apache License 2.0 4 votes vote down vote up
public LiveData<DataResource<List<BoxOfficeEntity>>> getBoxOffices() {
    return Transformations.switchMap(getLoadLive(), input -> new BoxOfficeRepository().getBoxOffices());
}
 
Example 14
Source File: MovieDetailViewModel.java    From GracefulMovies with Apache License 2.0 4 votes vote down vote up
public LiveData<DataResource<MovieDetail>> getMovieDetails(String locationId, int movieId) {
    return Transformations.switchMap(getLoadLive(), input ->
            new MovieDetailRepository().getMovieDetails(locationId, movieId)
    );
}
 
Example 15
Source File: RatingViewModel.java    From firestore-android-arch-components with Apache License 2.0 4 votes vote down vote up
@Inject
RatingViewModel(RestaurantRepository repository) {
    this.repository = repository;
    restaurant = Transformations.switchMap(id, repository::restaurant);
    ratings = Transformations.switchMap(id, repository::ratings);
}
 
Example 16
Source File: QuotationViewModel.java    From bitshares_wallet with MIT License 4 votes vote down vote up
public LiveData<Resource<List<BitsharesMarketTicker>>> getMarketTicker() {
    return Transformations.switchMap(marketChangeLiveData, marketChange -> {
        return new MarketTickerRepository().queryMarketTicker();
    });
}
 
Example 17
Source File: QuotationViewModel.java    From bitshares_wallet with MIT License 4 votes vote down vote up
public LiveData<Resource<List<HistoryPrice>>> getHistoryPrice() {
    return Transformations.switchMap(mutableLiveDataSelected, currencyPair -> {
        return new HistoryPriceRepository(currencyPair).getHistoryPrice();
    });
}
 
Example 18
Source File: MainActivityViewModel.java    From AndroidNewArchitectureExample with MIT License 4 votes vote down vote up
@Inject
public MainActivityViewModel() {
    App.getComponent().inject(this);
    this.weatherInfoLiveData = Transformations.switchMap(cityNameLiveData, weatherRepository::getWeather);
}
 
Example 19
Source File: ExampleUnitTest.java    From Fairy with Apache License 2.0 4 votes vote down vote up
public LiveData<String> creat(String grep) {
    return Transformations.switchMap(transData, boo -> {
        return data;
    });
}