android.arch.lifecycle.Transformations Java Examples

The following examples show how to use android.arch.lifecycle.Transformations. 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: GrepFilter.java    From Fairy with Apache License 2.0 6 votes vote down vote up
@MainThread
static LiveData<LogcatContent> grepData(LiveData<LogcatContent> rawData, String grep) {
    return Transformations.map(rawData, logcatData -> {
        String content = logcatData.getContent();
        if (GREP_SIGNAL.equals(content)) {
            return logcatData;
        }


        if (content != null) {
            logcatData.setContent(parseHtml2(content,grep));
        }

        return logcatData;

    });
}
 
Example #2
Source File: WalletViewModel.java    From bitshares_wallet with MIT License 6 votes vote down vote up
public WalletViewModel() {
    retryData.setValue(0);

    resultData.addSource(statusChangeLiveData, statusChange -> {
        LiveData<Resource<List<BitsharesBalanceAsset>>> balanceData = Transformations.switchMap(
                Transformations.switchMap(currencyData, input -> {
                    retryData.setValue(retryData.getValue());
                    return retryData;
                }),
                retryCount -> {
                    return new BalanceRepository().getBalances(currencyData.getValue());
                });

        resultData.addSource(balanceData, result -> resultData.setValue(result));
    });

}
 
Example #3
Source File: SoldiersViewModel.java    From settlers-remake with MIT License 6 votes vote down vote up
public SoldiersViewModel(ActionControls actionControls, DrawControls drawControls, IInGamePlayer player) {
	this.actionControls = actionControls;
	this.player = player;

	DrawEvents drawEvents = new DrawEvents(drawControls);

	strengthText = Transformations.map(drawEvents, x -> strengthText());
	promotionText = Transformations.map(drawEvents, x -> promotionText());
	swordsmenPromotionEnabled = Transformations.map(drawEvents, x -> isPromotionPossible(ESoldierType.SWORDSMAN));
	bowmenPromotionEnabled = Transformations.map(drawEvents, x -> isPromotionPossible(ESoldierType.BOWMAN));
	pikemenPromotionEnabled = Transformations.map(drawEvents, x -> isPromotionPossible(ESoldierType.PIKEMAN));
	swordsmenImageLink = Transformations.map(swordsmenPromotionEnabled,
			isPromotionPossible -> getPromotionImageLink(isPromotionPossible, swordsmenPromotionPossibleImages, swordsmenPromotionNotPossibleImages, ESoldierType.SWORDSMAN));
	bowmenImageLink = Transformations.map(bowmenPromotionEnabled,
			isPromotionPossible -> getPromotionImageLink(isPromotionPossible, bowmenPromotionPossibleImages, bowmenPromotionNotPossibleImages, ESoldierType.BOWMAN));
	pikemenImageLink = Transformations.map(pikemenPromotionEnabled,
			isPromotionPossible -> getPromotionImageLink(isPromotionPossible, pikemenPromotionPossibleImages, pikemenPromotionNotPossibleImages, ESoldierType.PIKEMAN));
}
 
Example #4
Source File: CustomResultViewModel.java    From android-persistence with Apache License 2.0 6 votes vote down vote up
private void subscribeToDbChanges() {
    LiveData<List<LoanWithUserAndBook>> loans
            = mDb.loanModel().findLoansByNameAfter("Mike", getYesterdayDate());

    // Instead of exposing the list of Loans, we can apply a transformation and expose Strings.
    mLoansResult = Transformations.map(loans,
            new Function<List<LoanWithUserAndBook>, String>() {
        @Override
        public String apply(List<LoanWithUserAndBook> loansWithUserAndBook) {
            StringBuilder sb = new StringBuilder();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm",
                    Locale.US);

            for (LoanWithUserAndBook loan : loansWithUserAndBook) {
                sb.append(String.format("%s\n  (Returned: %s)\n",
                        loan.bookTitle,
                        simpleDateFormat.format(loan.endTime)));
            }
            return sb.toString();
        }
    });
}
 
Example #5
Source File: CustomResultViewModel.java    From android-persistence with Apache License 2.0 6 votes vote down vote up
private void subscribeToDbChanges() {
    // TODO: Modify this query to show only recent loans from specific user
    LiveData<List<LoanWithUserAndBook>> loans
            = mDb.loanModel().findAllWithUserAndBook();

    // Instead of exposing the list of Loans, we can apply a transformation and expose Strings.
    mLoansResult = Transformations.map(loans,
            new Function<List<LoanWithUserAndBook>, String>() {
        @Override
        public String apply(List<LoanWithUserAndBook> loansWithUserAndBook) {
            StringBuilder sb = new StringBuilder();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm",
                    Locale.US);

            for (LoanWithUserAndBook loan : loansWithUserAndBook) {
                sb.append(String.format("%s\n  (Returned: %s)\n",
                        loan.bookTitle,
                        simpleDateFormat.format(loan.endTime)));
            }
            return sb.toString();
        }
    });
}
 
Example #6
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 #7
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 #8
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 #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: 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 #11
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 #12
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 #13
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 #14
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 #15
Source File: GameMenuViewModel.java    From settlers-remake with MIT License 5 votes vote down vote up
public GameMenuViewModel(
		@NonNull Application application,
		@NonNull GameMenu gameMenu) {
	this.application = application;
	this.gameMenu = gameMenu;

	quitTextLiveData = Transformations.map(gameMenu.getGameState(), this::mapQuitText);
	pauseTextLiveData = Transformations.map(gameMenu.isPausedState(), this::mapPausedText);
	gameSpeedTextLiveData = Transformations.map(gameMenu.getGameSpeed(), this::mapGameSpeedText);
	gameSpeedLiveData = Transformations.map(gameMenu.getGameSpeed(), this::mapGameSpeed);
}
 
Example #16
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 #17
Source File: BuildingsCategoryViewModel.java    From settlers-remake with MIT License 5 votes vote down vote up
public BuildingsCategoryViewModel(ActionControls actionControls, DrawControls drawControls, PositionControls positionControls, MenuNavigator menuNavigator, EBuildingsCategory buildingsCategory) {
	this.actionControls = actionControls;
	this.positionControls = positionControls;
	this.menuNavigator = menuNavigator;
	this.buildingsCategory = buildingsCategory;

	DrawEvents drawEvents = new DrawEvents(drawControls);
	buildingStates = Transformations.map(drawEvents, x -> buildingStates());
}
 
Example #18
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 #19
Source File: ProductionViewModel.java    From settlers-remake with MIT License 5 votes vote down vote up
public ProductionViewModel(ActionControls actionControls, PositionControls positionControls, DrawControls drawControls) {
	this.actionControls = actionControls;
	this.positionControls = positionControls;

	DrawEvents drawEvents = new DrawEvents(drawControls);
	productionStates = Transformations.map(drawEvents, x -> productionStates());
}
 
Example #20
Source File: UserRepositoryLocal.java    From NewAndroidArchitecture-Component-Github with Apache License 2.0 5 votes vote down vote up
@Override
public LiveData<User> searchUser(String userName){
    return Transformations.map(userDao.getUser(userName), new Function<UserLocal, User>() {
        @Override
        public User apply(UserLocal input) {
            if (input != null) {
                return input.toUser();
            } else {
                return null;
            }
        }
    });
}
 
Example #21
Source File: InventoryViewModel.java    From settlers-remake with MIT License 4 votes vote down vote up
public InventoryViewModel(DrawControls drawControls, PositionControls positionControls) {
	this.positionControls = positionControls;

	DrawEvents drawEvents = new DrawEvents(drawControls);
	inventoryMaterialStatesData = Transformations.map(drawEvents, x -> productionStates());
}
 
Example #22
Source File: JoinMultiPlayerPickerViewModel.java    From settlers-remake with MIT License 4 votes vote down vote up
public JoinMultiPlayerPickerViewModel(GameStarter gameStarter, ChangingList<IJoinableGame> changingJoinableGames) {
	this.gameStarter = gameStarter;
	this.changingJoinableGames = changingJoinableGames;

	showNoGamesMessage = Transformations.map(joinableGames, joinableGames -> joinableGames.length == 0);
}
 
Example #23
Source File: LoadSinglePlayerPickerViewModel.java    From settlers-remake with MIT License 4 votes vote down vote up
public LoadSinglePlayerPickerViewModel(GameStarter gameStarter, ChangingList<? extends MapLoader> changingMaps) {
	super(gameStarter, changingMaps);
	this.gameStarter = gameStarter;

	showNoMapsMessage = Transformations.map(getMaps(), maps -> maps.length == 0);
}
 
Example #24
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 #25
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 #26
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 #27
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 #28
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 #29
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 #30
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;
    });
}