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

The following examples show how to use android.arch.lifecycle.Transformations#map() . 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: 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 3
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 4
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 5
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 6
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 7
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 8
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 9
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 10
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 11
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);
}