Skip to content

Commit

Permalink
Support for predictive back gesture
Browse files Browse the repository at this point in the history
  • Loading branch information
wasky committed May 15, 2023
1 parent 2c35737 commit 28e32d7
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 19 deletions.
50 changes: 42 additions & 8 deletions library/src/main/java/com/stephentuso/welcome/WelcomeActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import android.view.View;
import android.widget.FrameLayout;

import androidx.activity.OnBackPressedCallback;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -128,9 +129,14 @@ public void onViewHidden() {
responsiveItems.setup(configuration);

viewPager.addOnPageChangeListener(responsiveItems);
viewPager.addOnPageChangeListener(onPageChangeListener);
viewPager.setCurrentItem(configuration.firstPageIndex());

responsiveItems.onPageSelected(viewPager.getCurrentItem());

WelcomeSharedPreferencesHelper.storeWelcomeCompleted(this, getKey());

getOnBackPressedDispatcher().addCallback(this, onBackPressedCallback);
}

@Override
Expand Down Expand Up @@ -206,25 +212,24 @@ protected void onButtonBarSecondPressed() {
* unless the key is changed.
*/
protected void completeWelcomeScreen() {
WelcomeSharedPreferencesHelper.storeWelcomeCompleted(this, getKey());
setWelcomeScreenResult(RESULT_OK);
//WelcomeSharedPreferencesHelper.storeWelcomeCompleted(this, getKey());
//setWelcomeScreenResult(RESULT_OK);
finish();
if (configuration.getExitAnimation() != WelcomeConfiguration.NO_ANIMATION_SET)
/*if (configuration.getExitAnimation() != WelcomeConfiguration.NO_ANIMATION_SET) {
overridePendingTransition(R.anim.wel_none, configuration.getExitAnimation());
}*/
}

/**
* Closes the activity, doesn't save as completed.
* A subsequent call to WelcomeScreenHelper.show() would show this again.
*/
protected void cancelWelcomeScreen() {
setWelcomeScreenResult(RESULT_CANCELED);
//setWelcomeScreenResult(RESULT_CANCELED);
finish();
}

@Override
public void onBackPressed() {

private void handleBackPressed() {
// Scroll to previous page and return if back button navigates
if (configuration.getBackButtonNavigatesPages() && scrollToPreviousPage()) {
return;
Expand All @@ -238,11 +243,16 @@ public void onBackPressed() {

}

private boolean isOnBackPressedCallbackShouldBeEnabled() {
if (!configuration.getBackButtonNavigatesPages()) return false;
return canScrollToPreviousPage();
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

if (configuration.getShowActionBarBackButton() && item.getItemId() == android.R.id.home) {
onBackPressed();
handleBackPressed();
return true;
}

Expand All @@ -261,6 +271,30 @@ private String getKey() {

protected abstract WelcomeConfiguration configuration();

private final OnBackPressedCallback onBackPressedCallback = new OnBackPressedCallback(false) {
@Override
public void handleOnBackPressed() {
handleBackPressed();
}
};

private final ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

@Override
public void onPageSelected(int position) {
onBackPressedCallback.setEnabled(isOnBackPressedCallbackShouldBeEnabled());
}

@Override
public void onPageScrollStateChanged(int state) {
}

};

private class WelcomeFragmentPagerAdapter extends FragmentPagerAdapter {

public WelcomeFragmentPagerAdapter(FragmentManager fm) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -422,10 +422,10 @@ public Builder swipeToDismiss(boolean swipeToDismiss) {
* @param canSkip True to allow skipping, false to disable it
* @return this Builder object to allow method calls to be chained
*/
public Builder canSkip(boolean canSkip) {
/*public Builder canSkip(boolean canSkip) {
this.canSkip = canSkip;
return this;
}
}*/

/**
* Only applies if skipping is allowed. Sets whether or not the back button can skip the welcome screen.
Expand All @@ -434,10 +434,10 @@ public Builder canSkip(boolean canSkip) {
* @param backButtonSkips True to allow the back button to skip the welcome screen, false to disable it
* @return this Builder object to allow method calls to be chained
*/
public Builder backButtonSkips(boolean backButtonSkips) {
/*public Builder backButtonSkips(boolean backButtonSkips) {
this.backButtonSkips = backButtonSkips;
return this;
}
}*/

/**
* Set whether or not pressing the back button will move to the previous page in the welcome screen.
Expand Down Expand Up @@ -573,10 +573,10 @@ public Builder defaultDescriptionTypefacePath(String typefacePath) {
* @param exitAnimationResId The resource id of the animation to use
* @return this Builder object to allow method calls to be chained
*/
public Builder exitAnimation(@AnimRes int exitAnimationResId) {
/*public Builder exitAnimation(@AnimRes int exitAnimationResId) {
this.exitAnimationResId = exitAnimationResId;
return this;
}
}*/

/**
* Set the color to be used when no background color is specified for a page
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ protected WelcomeConfiguration configuration() {
.defaultTitleTypefacePath("Roboto-Bold.ttf")
.defaultHeaderTypefacePath("Roboto-Bold.ttf")
.bottomLayout(WelcomeConfiguration.BottomLayout.BUTTON_BAR_SINGLE)
.exitAnimation(android.R.anim.fade_out)
// .exitAnimation(android.R.anim.fade_out)
.page(new TitlePage(R.drawable.ic_image_white, "Title Page"))
.page(new BasicPage(R.drawable.ic_image_white, "Basic Page", "A page with a title and description").background(R.color.purple_background))
.build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class ButtonBarWelcomeActivity extends WelcomeActivity {
@Override
protected WelcomeConfiguration configuration() {
return new WelcomeConfiguration.Builder(this)
.canSkip(false)
// .canSkip(false)
.backButtonNavigatesPages(false)
.defaultTitleTypefacePath("Roboto-Bold.ttf")
.defaultHeaderTypefacePath("Roboto-Bold.ttf")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected WelcomeConfiguration configuration() {
"All colors can be customized with styles")
.headerColorResource(this, R.color.colorAccent))
.swipeToDismiss(true)
.exitAnimation(android.R.anim.fade_out)
// .exitAnimation(android.R.anim.fade_out)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected WelcomeConfiguration configuration() {
.page(new BasicPage(R.drawable.ic_check_white,
"Completion",
"RESULT_OK will only be returned after navigating all the way to the end"))
.canSkip(false)
// .canSkip(false)
.swipeToDismiss(true)
.build();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ protected WelcomeConfiguration configuration() {
)

.swipeToDismiss(true)
.exitAnimation(android.R.anim.fade_out)
// .exitAnimation(android.R.anim.fade_out)
.build();
}

Expand Down

0 comments on commit 28e32d7

Please sign in to comment.