Skip to content

Commit

Permalink
Fixed #73
Browse files Browse the repository at this point in the history
  • Loading branch information
janheinrichmerker committed Jun 7, 2016
1 parent 01eba77 commit 2364d62
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

import com.heinrichreimersoftware.materialintro.app.IntroActivity;
import com.heinrichreimersoftware.materialintro.app.OnNavigationBlockedListener;
Expand Down Expand Up @@ -65,6 +67,17 @@ protected void onCreate(Bundle savedInstanceState) {
.background(R.color.color_material_bold)
.backgroundDark(R.color.color_dark_material_bold)
.scrollable(scrollable)
.buttonCtaLabel("Hello")
.buttonCtaClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast toast = Toast.makeText(MainIntroActivity.this, R.string.toast_button_cta, Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();

nextSlide();
}
})
.build());

addSlide(new SimpleSlide.Builder()
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values-de/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<string name="description_permissions">Diese App braucht die Kamera- und Speicher-Berechtigung.\nDas ist nur, um zu zeigen, wie Berechtigungsanfragen in dieser Bibliothek funktionieren und wir werden nichts mit den Berechtigungen machen.</string>
<string name="label_grant_permissions">Bitte gewähre die Berechtigungen, um fortzufahren.</string>

<string name="label_button_cta">Klick mich!</string>
<string name="toast_button_cta">Hey, so funktionieren eigene CTA buttons in material-intro!</string>

<string name="title_fragment_login">Das ist ein eigenes Fragment.</string>
<string name="hint_fake_username">Fake Benutzername</string>
<string name="hint_fake_password">Fake Password</string>
Expand Down
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
<string name="description_permissions">This app needs the camera and storage permission.\nIt\'s only to show how permission requests work in this library and we won\'t do anything with the granted permissions.</string>
<string name="label_grant_permissions">Please grant the permissions before proceeding.</string>

<string name="label_button_cta">Click me!</string>
<string name="toast_button_cta">Hey, that\'s how custom CTA buttons work in material-intro!</string>

<string name="title_fragment_login">This is a custom fragment.</string>
<string name="hint_fake_username">Fake username</string>
<string name="hint_fake_password">Fake password</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ private void finishIfNeeded() {
}
}

private Pair<String, ? extends View.OnClickListener> getButtonCta(int position) {
private Pair<CharSequence, ? extends View.OnClickListener> getButtonCta(int position) {
if (position < getCount() && getSlide(position) instanceof ButtonCtaSlide) {
ButtonCtaSlide slide = (ButtonCtaSlide) getSlide(position);
if (slide.getButtonCtaClickListener() != null &&
Expand All @@ -332,13 +332,13 @@ private void finishIfNeeded() {
return Pair.create(slide.getButtonCtaLabel(),
slide.getButtonCtaClickListener());
} else {
return Pair.create(getString(slide.getButtonCtaLabelRes()),
return Pair.create((CharSequence) getString(slide.getButtonCtaLabelRes()),
slide.getButtonCtaClickListener());
}
}
}
if (buttonCtaVisible) {
return Pair.create(getString(R.string.mi_label_button_cta),
return Pair.create((CharSequence) getString(R.string.mi_label_button_cta),
new ButtonCtaClickListener());
}
return null;
Expand Down Expand Up @@ -579,10 +579,8 @@ private void updateButtonCta() {
//Button CTA transition
if (position + positionOffset < adapter.getCount()) {
//Before fade
Pair<String, ? extends View.OnClickListener> button = getButtonCta(position);
Log.i("button cta", "button for position " + position + ": " + (button == null ? null : button.first));
Pair<String, ? extends View.OnClickListener> buttonNext = getButtonCta(position + 1);
Log.i("button cta", "button for position " + (position + 1) + ": " + (buttonNext == null ? null : buttonNext.first));
Pair<CharSequence, ? extends View.OnClickListener> button = getButtonCta(position);
Pair<CharSequence, ? extends View.OnClickListener> buttonNext = getButtonCta(position + 1);

if (button == null) {
if (buttonNext == null) {
Expand All @@ -596,7 +594,6 @@ private void updateButtonCta() {
buttonCta.setText(buttonNext.first);
buttonCta.getChildAt(0).setOnClickListener(buttonNext.second);
buttonCta.getChildAt(1).setOnClickListener(buttonNext.second);
Log.i("button cta", "alpha1: " + (positionOffset));
buttonCta.setAlpha(positionOffset);
}
}
Expand All @@ -608,7 +605,6 @@ private void updateButtonCta() {
buttonCta.setText(button.first);
buttonCta.getChildAt(0).setOnClickListener(button.second);
buttonCta.getChildAt(1).setOnClickListener(button.second);
Log.i("button cta", "alpha2: " + (1 - positionOffset));
buttonCta.setAlpha(1 - positionOffset);
}
else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@ public interface ButtonCtaSlide extends Slide {
View.OnClickListener getButtonCtaClickListener();

/**
* Note: you must either define a {@link String} or a {@link StringRes} label
* Note: you must either define a {@link CharSequence} or a {@link StringRes} label
*/
String getButtonCtaLabel();
CharSequence getButtonCtaLabel();

/**
* Note: you must either define a {@link String} or a {@link StringRes} label
* Note: you must either define a {@link CharSequence} or a {@link StringRes} label
*/
@StringRes
int getButtonCtaLabelRes();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.heinrichreimersoftware.materialintro.slide;

import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.ColorInt;
Expand Down Expand Up @@ -38,8 +39,10 @@ public class SimpleSlide implements Slide, RestorableSlide, ButtonCtaSlide {
private final boolean canGoBackward;
private String[] permissions;
private int permissionsRequestCode;
private View.OnClickListener buttonClickListener = null;
private String buttonLabel;
private CharSequence buttonCtaLabel = null;
@StringRes
private int buttonCtaLabelRes = 0;
private View.OnClickListener buttonCtaClickListener = null;

private SimpleSlide(Builder builder) {
fragment = SimpleSlideFragment.newInstance(builder.title, builder.titleRes,
Expand All @@ -51,6 +54,9 @@ private SimpleSlide(Builder builder) {
canGoBackward = builder.canGoBackward;
permissions = builder.permissions;
permissionsRequestCode = builder.permissionsRequestCode;
buttonCtaLabel = builder.buttonCtaLabel;
buttonCtaLabelRes = builder.buttonCtaLabelRes;
buttonCtaClickListener = builder.buttonCtaClickListener;
updatePermissions();
}

Expand Down Expand Up @@ -90,17 +96,38 @@ public boolean canGoBackward() {
@Override
public View.OnClickListener getButtonCtaClickListener() {
updatePermissions();
return buttonClickListener;
if (permissions == null) {
return buttonCtaClickListener;
}
return new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fragment.getActivity() != null)
ActivityCompat.requestPermissions(fragment.getActivity(), permissions,
permissionsRequestCode);
}
};
}

@Override
public String getButtonCtaLabel() {
public CharSequence getButtonCtaLabel() {
updatePermissions();
return buttonLabel;
if (permissions == null) {
return buttonCtaLabel;
}
Context context = fragment.getContext();
if (context != null)
return context.getResources().getQuantityText(
R.plurals.mi_label_grant_permission, permissions.length);
return null;
}

@Override
public int getButtonCtaLabelRes() {
updatePermissions();
if (permissions == null) {
return buttonCtaLabelRes;
}
return 0;
}

Expand All @@ -118,35 +145,11 @@ private synchronized void updatePermissions() {
if (permissionsNotGranted.size() > 0) {
permissions = permissionsNotGranted.toArray(
new String[permissionsNotGranted.size()]);
if (fragment.getActivity() == null) {
if (permissions.length == 0) {
permissions = null;
buttonLabel = null;
buttonClickListener = null;
}
}
else {
buttonLabel = fragment.getContext().getResources().getQuantityText(
R.plurals.mi_label_grant_permission, permissionsNotGranted.size())
.toString();
buttonClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (fragment.getActivity() != null)
ActivityCompat.requestPermissions(fragment.getActivity(), permissions,
permissionsRequestCode);
}
};
}
} else {
permissions = null;
buttonLabel = null;
buttonClickListener = null;
}
} else {
permissions = null;
buttonLabel = null;
buttonClickListener = null;
}
}

Expand All @@ -155,11 +158,9 @@ public static class Builder {
private int backgroundRes = 0;
@ColorRes
private int backgroundDarkRes = 0;

private CharSequence title = null;
@StringRes
private int titleRes = 0;

private CharSequence description = null;
@StringRes
private int descriptionRes = 0;
Expand All @@ -170,6 +171,10 @@ public static class Builder {
private boolean canGoForward = true;
private boolean canGoBackward = true;
private String[] permissions = null;
private CharSequence buttonCtaLabel = null;
@StringRes
private int buttonCtaLabelRes = 0;
private View.OnClickListener buttonCtaClickListener = null;

private int permissionsRequestCode = DEFAULT_PERMISSIONS_REQUEST_CODE;

Expand Down Expand Up @@ -260,6 +265,29 @@ public Builder permissionsRequestCode(int permissionsRequestCode) {
return this;
}

public Builder buttonCtaLabel(CharSequence buttonCtaLabel) {
this.buttonCtaLabel = buttonCtaLabel;
this.buttonCtaLabelRes = 0;
return this;
}

public Builder buttonCtaLabelHtml(String buttonCtaLabelHtml) {
this.buttonCtaLabel = Html.fromHtml(buttonCtaLabelHtml);
this.buttonCtaLabelRes = 0;
return this;
}

public Builder buttonCtaLabel(@StringRes int buttonCtaLabelRes) {
this.buttonCtaLabelRes = buttonCtaLabelRes;
this.buttonCtaLabel = null;
return this;
}

public Builder buttonCtaClickListener(View.OnClickListener buttonCtaClickListener) {
this.buttonCtaClickListener = buttonCtaClickListener;
return this;
}

public SimpleSlide build() {
return new SimpleSlide(this);
}
Expand Down

0 comments on commit 2364d62

Please sign in to comment.