Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
* Copyright 2013 str4d
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.tech.freak.wizardpager.model;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Conditional implements ModelCallbacks {
private Object mData = null;
private List<Page> mConditionalPages = new ArrayList<Page>();

public void onPageDataChanged(Page page) {
mData = page.getData().get(Page.SIMPLE_DATA_KEY);
for (Page p : mConditionalPages)
p.isSatisfied();
}

public void onPageTreeChanged() {
}

public interface Condition {
public boolean isSatisfied();
}

public class EqualCondition<T> implements Condition {
private T mCompValue;

public EqualCondition(Page page, T compValue) {
mCompValue = compValue;
mConditionalPages.add(page);
}

public boolean isSatisfied() {
return mCompValue.equals(mData);
}
}

public class NotEqualCondition<T> implements Condition {
private T mCompValue;

public NotEqualCondition(Page page, T compValue) {
mCompValue = compValue;
mConditionalPages.add(page);
}

public boolean isSatisfied() {
return !(mCompValue.equals(mData));
}
}

public class EqualAnyCondition<T> implements Condition {
private ArrayList<T> mChoices = new ArrayList<T>();

public EqualAnyCondition(Page page, T... choices) {
mChoices.addAll(Arrays.asList(choices));
mConditionalPages.add(page);
}

public boolean isSatisfied() {
return mChoices.contains(mData);
}
}
}
80 changes: 76 additions & 4 deletions library/src/main/java/com/tech/freak/wizardpager/model/Page.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/*
* Copyright 2013 str4d
* Copyright 2012 Roman Nurik
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -20,6 +21,7 @@
import android.support.v4.app.Fragment;

import java.util.ArrayList;
import java.util.List;

/**
* Represents a single page in the wizard.
Expand All @@ -32,6 +34,24 @@ public abstract class Page implements PageTreeNode {

protected ModelCallbacks mCallbacks;

/**
* Conditionals that rely on this page.
*/
protected List<ModelCallbacks> mConditionals = new ArrayList<ModelCallbacks>();

/**
* Conditions on whether this page should be used.
*/
protected List<Conditional.Condition> mConditions = new ArrayList<Conditional.Condition>();
/**
* Should all conditions be satisfied, or any of them?
*/
protected boolean mConditionAnd = false;
/**
* The last condition status.
*/
protected boolean mSatisfied = true;

/**
* Current wizard values/selections.
*/
Expand All @@ -53,22 +73,43 @@ public String getTitle() {
return mTitle;
}

public boolean isSatisfied() {
boolean ret = true;
if (mConditions.size() > 0) {
ret = false;
for (Conditional.Condition c : mConditions) {
if (c.isSatisfied()) {
ret = true;
if (!mConditionAnd) break;
} else if (mConditionAnd) {
ret = false;
break;
}
}
}
// If the conditions have changed, update the page tree.
if (!(mSatisfied == ret)) {
mSatisfied = ret;
mCallbacks.onPageTreeChanged();
}
return mSatisfied;
}

public boolean isRequired() {
return mRequired;
return isSatisfied() && mRequired;
}

void setParentKey(String parentKey) {
mParentKey = parentKey;
}

@Override
public Page findByKey(String key) {
return getKey().equals(key) ? this : null;
}

@Override
public void flattenCurrentPageSequence(ArrayList<Page> dest) {
dest.add(this);
if (isSatisfied())
dest.add(this);
}

public abstract Fragment createFragment();
Expand All @@ -89,11 +130,42 @@ public void resetData(Bundle data) {
}

public void notifyDataChanged() {
for (ModelCallbacks c : mConditionals) {
c.onPageDataChanged(this);
}
mCallbacks.onPageDataChanged(this);
}

public Page setRequired(boolean required) {
mRequired = required;
return this;
}

public Page makeConditional(Conditional conditional) {
mConditionals.add(conditional);
return this;
}

public <T> Page setEqualCondition(Conditional conditional, T comp) {
Conditional.Condition c = conditional.new EqualCondition<T>(this, comp);
mConditions.add(c);
return this;
}

public <T> Page setNotEqualCondition(Conditional conditional, T comp) {
Conditional.Condition c = conditional.new NotEqualCondition<T>(this, comp);
mConditions.add(c);
return this;
}

public <T> Page setEqualAnyCondition(Conditional conditional, T... choices) {
Conditional.Condition c = conditional.new EqualAnyCondition<T>(this, choices);
mConditions.add(c);
return this;
}

public Page satisfyAllConditions(boolean conditionAnd) {
mConditionAnd = conditionAnd;
return this;
}
}