Skip to content

Commit

Permalink
Merge pull request #24 from Jucedik/master
Browse files Browse the repository at this point in the history
Added validation feature (#7)
  • Loading branch information
janheinrichmerker committed Mar 21, 2016
2 parents b8ec16d + 3221552 commit 55d2d08
Show file tree
Hide file tree
Showing 14 changed files with 326 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
import android.widget.EditText;
import android.widget.Toast;

public class LoginFragment extends Fragment {
import com.heinrichreimersoftware.materialintro.app.IntroActivity;
import com.heinrichreimersoftware.materialintro.app.SlideFragment;

public class LoginFragment extends SlideFragment {

private EditText fakeUsername;
private EditText fakePassword;
Expand All @@ -26,6 +29,9 @@ public void run() {
fakeLogin.setEnabled(true);
fakeLogin.setText(R.string.label_fake_login);
Toast.makeText(getContext(), R.string.label_fake_login_success, Toast.LENGTH_SHORT).show();

allowNextSlide();
//showNextSlide();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

import com.heinrichreimersoftware.materialintro.app.IntroActivity;
import com.heinrichreimersoftware.materialintro.app.NavigationInterface;
import com.heinrichreimersoftware.materialintro.app.SetNavigationStateInterface;
import com.heinrichreimersoftware.materialintro.slide.FragmentSlide;
import com.heinrichreimersoftware.materialintro.slide.SimpleSlide;
import com.heinrichreimersoftware.materialintro.slide.Slide;

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

public class MaterialIntroActivity extends IntroActivity {

Expand All @@ -16,6 +24,8 @@ public class MaterialIntroActivity extends IntroActivity {
public static final String EXTRA_SKIP_ENABLED = "com.heinrichreimersoftware.materialintro.demo.EXTRA_SKIP_ENABLED";
public static final String EXTRA_FINISH_ENABLED = "com.heinrichreimersoftware.materialintro.demo.EXTRA_FINISH_ENABLED";

private List<Slide> slides;

@Override
protected void onCreate(Bundle savedInstanceState) {
Intent intent = getIntent();
Expand All @@ -33,31 +43,36 @@ protected void onCreate(Bundle savedInstanceState) {
setSkipEnabled(skipEnabled);
setFinishEnabled(finishEnabled);

addSlide(new SimpleSlide.Builder()
slides = new ArrayList<>();

slides.add(new SimpleSlide.Builder()
.title(R.string.title_material_metaphor)
.description(R.string.description_material_metaphor)
.image(R.drawable.art_material_metaphor)
.background(R.color.color_material_metaphor)
.backgroundDark(R.color.color_dark_material_metaphor)
.scrollable(scrollable)
.build());
addSlide(new SimpleSlide.Builder()

slides.add(new SimpleSlide.Builder()
.title(R.string.title_material_bold)
.description(R.string.description_material_bold)
.image(R.drawable.art_material_bold)
.background(R.color.color_material_bold)
.backgroundDark(R.color.color_dark_material_bold)
.scrollable(scrollable)
.build());
addSlide(new SimpleSlide.Builder()

slides.add(new SimpleSlide.Builder()
.title(R.string.title_material_motion)
.description(R.string.description_material_motion)
.image(R.drawable.art_material_motion)
.background(R.color.color_material_motion)
.backgroundDark(R.color.color_dark_material_motion)
.scrollable(scrollable)
.build());
addSlide(new SimpleSlide.Builder()

slides.add(new SimpleSlide.Builder()
.title(R.string.title_material_shadow)
.description(R.string.description_material_shadow)
.image(R.drawable.art_material_shadow)
Expand All @@ -67,18 +82,39 @@ protected void onCreate(Bundle savedInstanceState) {
.build());

if(customFragments){
addSlide(new FragmentSlide.Builder()
slides.add(new FragmentSlide.Builder()
.background(R.color.color_custom_fragment_1)
.backgroundDark(R.color.color_dark_custom_fragment_1)
.fragment(LoginFragment.newInstance())
.allowNext(false)
.build());
addSlide(new FragmentSlide.Builder()

slides.add(new FragmentSlide.Builder()
.background(R.color.color_custom_fragment_2)
.backgroundDark(R.color.color_dark_custom_fragment_2)
.fragment(R.layout.fragment_custom, R.style.AppThemeDark)
.build());
}

addSlides(slides);

setNavigationInterface(new NavigationInterface() {
@Override
public boolean onNextClick(int position) {
return slides.get(position).isAllowSlideNext();
}

@Override
public boolean onPreviousClick(int position) {
return slides.get(position).isAllowSlidePrevious();
}

@Override
public void onImpossibleToNavigate(int position, int direction) {
Toast.makeText(getApplicationContext(), R.string.toast_impossible_to_navigate, Toast.LENGTH_LONG).show();
}
});

//Feel free to add and remove page change listeners to request permissions or such
/*
addOnPageChangeListener(new ViewPager.OnPageChangeListener(){
Expand All @@ -99,4 +135,5 @@ public void onPageScrollStateChanged(int state) {
});
*/
}

}
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,6 @@
<string name="label_fake_login_success">Successfully faked login.</string>

<string name="title_fragment_custom">Take off!</string>

<string name="toast_impossible_to_navigate">Impossible to navigate</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,26 @@
import com.heinrichreimersoftware.materialintro.R;
import com.heinrichreimersoftware.materialintro.slide.Slide;
import com.heinrichreimersoftware.materialintro.slide.SlideAdapter;
import com.heinrichreimersoftware.materialintro.util.AnimUtils;
import com.heinrichreimersoftware.materialintro.util.CheatSheet;
import com.heinrichreimersoftware.materialintro.view.FadeableViewPager;
import com.heinrichreimersoftware.materialintro.view.InkPageIndicator;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;

@SuppressLint("Registered")
public class IntroActivity extends AppCompatActivity {
public class IntroActivity extends AppCompatActivity implements SetNavigationStateInterface {

public static final int DIRECTION_NEXT = 1;
public static final int DIRECTION_PREVIOUS = -1;

private NavigationInterface navigationInterface;

private final ArgbEvaluator evaluator = new ArgbEvaluator();
private FrameLayout frame;
private ViewPager pager;
private FadeableViewPager pager;
private InkPageIndicator pagerIndicator;
private View buttonNext;
private View buttonSkip;
Expand All @@ -51,6 +58,8 @@ public class IntroActivity extends AppCompatActivity {
private int position = 0;
private float positionOffset = 0;

private int positionTmp = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand Down Expand Up @@ -109,7 +118,7 @@ private void setFullscreenFlags(boolean fullscreen){

private void findViews(){
frame = (FrameLayout) findViewById(R.id.mi_frame);
pager = (ViewPager) findViewById(R.id.mi_pager);
pager = (FadeableViewPager) findViewById(R.id.mi_pager);
pagerIndicator = (InkPageIndicator) findViewById(R.id.mi_pager_indicator);
buttonNext = findViewById(R.id.mi_button_next);
buttonSkip = findViewById(R.id.mi_button_skip);
Expand All @@ -123,7 +132,7 @@ private void findViews(){
buttonNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextSlide();
nextSlide(false);
}
});
buttonSkip.setOnClickListener(new View.OnClickListener() {
Expand All @@ -136,22 +145,46 @@ public void onClick(View v) {
CheatSheet.setup(buttonSkip);
}

private void nextSlide(){
public void nextSlide(boolean force){
int currentItem = pager.getCurrentItem();
pager.setCurrentItem(++currentItem, true);

if (navigationInterface == null || force) {
pager.setCurrentItem(++currentItem, true);
}
else {
if (navigationInterface.onNextClick(currentItem)) {
pager.setCurrentItem(++currentItem, true);
}
else {
AnimUtils.applyShakeAnimation(getApplicationContext(), buttonNext);
navigationInterface.onImpossibleToNavigate(currentItem, DIRECTION_NEXT);
}
}
}

private void previousSlide() {
public void previousSlide(boolean force) {
int currentItem = pager.getCurrentItem();
pager.setCurrentItem(--currentItem, true);

if (navigationInterface == null || force) {
pager.setCurrentItem(--currentItem, true);
}
else {
if (navigationInterface.onPreviousClick(currentItem)) {
pager.setCurrentItem(--currentItem, true);
}
else {
AnimUtils.applyShakeAnimation(getApplicationContext(), buttonSkip);
navigationInterface.onImpossibleToNavigate(currentItem, DIRECTION_PREVIOUS);
}
}
}

private void skipIfEnabled() {
if (skipEnabled) {
int count = getCount();
pager.setCurrentItem(count);
} else {
previousSlide();
previousSlide(false);
}
}

Expand Down Expand Up @@ -415,18 +448,30 @@ public void removeOnPageChangeListener(ViewPager.OnPageChangeListener listener)


protected void addSlide(int location, Slide object) {
object.setPosition(++positionTmp);
adapter.addSlide(location, object);
}

protected boolean addSlide(Slide object) {
object.setPosition(++positionTmp);
return adapter.addSlide(object);
}

protected boolean addSlides(int location, @NonNull Collection<? extends Slide> collection) {
Iterator<? extends Slide> iterator = collection.iterator();
while(iterator.hasNext()) {
iterator.next().setPosition(++positionTmp);
}

return adapter.addSlides(location, collection);
}

protected boolean addSlides(@NonNull Collection<? extends Slide> collection) {
Iterator<? extends Slide> iterator = collection.iterator();
while(iterator.hasNext()) {
iterator.next().setPosition(++positionTmp);
}

return adapter.addSlides(collection);
}

Expand Down Expand Up @@ -521,8 +566,53 @@ public void onPageScrolled(int position, float positionOffset, int positionOffse
@Override
public void onPageSelected(int position) {
IntroActivity.this.position = position;

updateTaskDescription();
lockOrUnlockSwipeableIfNeeded(position);
}
}

public void setNavigationInterface(NavigationInterface navigationInterface) {
this.navigationInterface = navigationInterface;
}

public void setAllowNextForSlideByPosition(int position) {
getSlide(position).setAllowSlideNext(true);
}

public void setAllowNextForSlideByFragmentTag(String tag) {
pager.setSwipeable(true);

for (int i = 0; i < this.adapter.getCount(); i++) {
if (getSlide(i).getFragment() != null && getSlide(i).getFragment().getTag().equals(tag)) {
setAllowNextForSlideByPosition(i);
break;
}
}
}

public void setAllowPreviousForSlideByPosition(int position) {
getSlide(position).setAllowSlidePrevious(true);
}

public void setAllowPreviousForSlideByFragmentTag(String tag) {
pager.setSwipeable(true);

for (int i = 0; i < this.adapter.getCount(); i++) {
if (getSlide(i).getFragment() != null && getSlide(i).getFragment().getTag().equals(tag)) {
setAllowPreviousForSlideByPosition(i);
break;
}
}
}

private void lockOrUnlockSwipeableIfNeeded(int position) {
if (position < getCount()) {
if (!getSlide(position).isAllowSlideNext() || !getSlide(position).isAllowSlidePrevious()) {
pager.setSwipeable(false);
} else {
pager.setSwipeable(true);
}
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.heinrichreimersoftware.materialintro.app;

/**
* Created by juced on 15.03.2016.
*/
public interface NavigationInterface {

/**
* Calling before click next. If return true - show next page
* @param position
* @return
*/
boolean onNextClick(int position);

/**
* Calling before click previous. If return true - show previous page
* @param position
* @return
*/
boolean onPreviousClick(int position);

/**
* Calling when impossible to navigate next or previous page
* @param position
* @param direction
*/
void onImpossibleToNavigate(int position, int direction);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.heinrichreimersoftware.materialintro.app;

/**
* Created by juced on 15.03.2016.
*/
public interface SetNavigationStateInterface {
void setAllowNextForSlideByPosition(int position);
}
Loading

0 comments on commit 55d2d08

Please sign in to comment.