All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.arellomobile.mvp.viewstate.ViewCommands Maven / Gradle / Ivy

The newest version!
package com.arellomobile.mvp.viewstate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.arellomobile.mvp.MoxyReflector;
import com.arellomobile.mvp.MvpView;
import com.arellomobile.mvp.viewstate.strategy.StateStrategy;

/**
 * Date: 17.12.2015
 * Time: 11:09
 *
 * @author Yuri Shmakov
 */
@SuppressWarnings({"unused", "WeakerAccess"})
public class ViewCommands {
	private List> mState = new ArrayList<>();
	private Map, StateStrategy> mStrategies = new HashMap<>();

	public void beforeApply(ViewCommand viewCommand) {
		StateStrategy stateStrategy = getStateStrategy(viewCommand);

		stateStrategy.beforeApply(mState, viewCommand);
	}

	public void afterApply(ViewCommand viewCommand) {
		StateStrategy stateStrategy = getStateStrategy(viewCommand);

		stateStrategy.afterApply(mState, viewCommand);
	}

	private StateStrategy getStateStrategy(ViewCommand viewCommand) {
		StateStrategy stateStrategy = (StateStrategy) MoxyReflector.getStrategy(viewCommand.getStrategyType());
		if (stateStrategy == null) {
			//noinspection TryWithIdenticalCatches
			try {
				stateStrategy = viewCommand.getStrategyType().newInstance();
			} catch (InstantiationException e) {
				throw new IllegalArgumentException("Unable to create state strategy: " + viewCommand.toString());
			} catch (IllegalAccessException e) {
				throw new IllegalArgumentException("Unable to create state strategy: " + viewCommand.toString());
			}

			mStrategies.put(viewCommand.getStrategyType(), stateStrategy);
		}

		return stateStrategy;
	}

	public boolean isEmpty() {
		return mState.isEmpty();
	}

	public void reapply(View view, Set> currentState) {
		final ArrayList> commands = new ArrayList<>(mState);

		for (ViewCommand command : commands) {
			if (currentState.contains(command)) {
				continue;
			}

			command.apply(view);

			afterApply(command);
		}
	}

	public List> getCurrentState() {
		return mState;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy