diff --git a/README.md b/README.md index 10968945..1f672690 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,96 @@ This is the basic usage of a single showcase view, you should check out the samp ``` +This is the advance usage of MaterialShowCase using Adapters, this will encapsulate everything for you. + +**In your Activity** +```java + + @Override + protected void onStart() { + super.onStart(); + // With Pool Management to run 1 Sequence or 1 View at a time. + // Material Show Case Initialization + MaterialShowCaseAdapter.initialize(this, CustomAdapter.class); + // Simple Material Show Case Initialization + MaterialShowCaseAdapter.initialize(this, R.array.showcase_simple_activity_adapter_example, "SimpleActivityAdapterExample"); + } + +``` + +**If you're using custom adapter** +```java + + public class CustomAdapter extends MaterialShowCaseAdapter { + + public CustomAdapter(Activity activity) { + super( + activity, + MaterialShowCaseAdapter.SHOW_CASE_DELAY, // DELAY + "ActivityAdapterExample" // ID + ); + } + + @Override // Setup of all show cases views + public void setup() { + super.addToQueue(R.id.btn_one, "", "This is button one","GOT IT", MaterialShowCaseViewShape.CIRCLE); + super.addToQueue(R.id.btn_two, "", "This is button two","GOT IT", MaterialShowCaseViewShape.RECTANGLE); + super.addToQueue(R.id.btn_three, "", "This is button three","GOT IT", MaterialShowCaseViewShape.RECTANGLE); + } + + @Override // Behavior + public void onDismiss(MaterialShowcaseView materialShowcaseView, int i) { + super.onDismiss(materialShowcaseView, i); + switch (currentQueueItemId){ + case 1: + Toast.makeText(activity, "You GOT Button One, nice!", Toast.LENGTH_SHORT).show(); + break; + } + } + } + +``` + +**If you're using simple adapter, use a resource array**, you can use this for simple show cases. +```xml + + + + + + + + + @id/btn_one + @id/btn_two + @id/btn_three + + + This is button one + This is button two + This is button three + + + GOT IT + GOT IT + GOT IT + + + circle + rectangle + rectangle + + + @array/showcase_simple_activity_adapter_example_views + @array/showcase_simple_activity_adapter_example_titles + @array/showcase_simple_activity_adapter_example_contents + @array/showcase_simple_activity_adapter_example_buttons + @array/showcase_simple_activity_adapter_example_shapes + + + +``` + # Why Jitpack ------------ Publishing libraries to Maven is a chore that takes time and effort. Jitpack.io allows me to release without ever leaving GitHub so I can release easily and more often. diff --git a/library/build.gradle b/library/build.gradle index 75df1862..c87219f9 100644 --- a/library/build.gradle +++ b/library/build.gradle @@ -20,4 +20,5 @@ android { dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) + compile 'com.android.support:support-annotations:23.1.1' } diff --git a/library/src/main/java/uk/co/deanwild/materialshowcaseview/adapter/MaterialShowCaseAdapter.java b/library/src/main/java/uk/co/deanwild/materialshowcaseview/adapter/MaterialShowCaseAdapter.java new file mode 100644 index 00000000..1b0614ba --- /dev/null +++ b/library/src/main/java/uk/co/deanwild/materialshowcaseview/adapter/MaterialShowCaseAdapter.java @@ -0,0 +1,187 @@ +package uk.co.deanwild.materialshowcaseview.adapter; + +import android.app.Activity; +import android.os.Handler; +import android.support.annotation.ArrayRes; +import android.support.annotation.IdRes; +import android.support.annotation.StringRes; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; + +import uk.co.deanwild.materialshowcaseview.MaterialShowcaseSequence; +import uk.co.deanwild.materialshowcaseview.MaterialShowcaseView; +import uk.co.deanwild.materialshowcaseview.ShowcaseConfig; + +/** + * Created by TurboCoder (Yamil García Hernández) on 7/7/16. + */ +public abstract class MaterialShowCaseAdapter implements MaterialShowcaseSequence.OnSequenceItemDismissedListener { + + // Constants + protected String SHOW_CASE_ID; + public final static int SHOW_CASE_DELAY = 5; + + public static final ArrayList POOL = new ArrayList<>(); + + // Variables + protected Activity activity; + private MaterialShowcaseSequence sequence; + protected ShowcaseConfig config; + private ArrayList queue = new ArrayList<>(); + protected int currentQueueItemId = 0; + + public MaterialShowCaseAdapter(Activity activity, int delay, String id) { + this.activity = activity; + this.config = new ShowcaseConfig(); + this.SHOW_CASE_ID = id; + config.setDelay(SHOW_CASE_DELAY); + sequence = new MaterialShowcaseSequence(activity, id); + sequence.setConfig(config); + } + + public abstract void setup(); + + public void start() { + setup(); + MaterialShowCaseAdapter.POOL.add(sequence); + sequence.setOnItemDismissedListener(this); + if (POOL.size() <= 1) + sequence.start(); + } + + public static void initialize(Activity activity, Class clazz) { + try { + Constructor constructor = clazz.getConstructor(Activity.class); + Object obj = constructor.newInstance(new Object[]{activity}); + + if (obj instanceof MaterialShowCaseAdapter) + ((MaterialShowCaseAdapter) obj).start(); + + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + + public static void initializeWithDelay(final Activity activity, final Class clazz, long delay) { + try { + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + try { + Constructor constructor = clazz.getConstructor(Activity.class); + Object obj = constructor.newInstance(new Object[]{activity}); + + if (obj instanceof MaterialShowCaseAdapter) + ((MaterialShowCaseAdapter) obj).start(); + } catch (NoSuchMethodException e) { + e.printStackTrace(); + } catch (IllegalAccessException e) { + e.printStackTrace(); + } catch (InstantiationException e) { + e.printStackTrace(); + } catch (InvocationTargetException e) { + e.printStackTrace(); + } + } + }, delay); + + } catch (Exception e) { + e.printStackTrace(); + } + } + + + public static void initialize(Activity activity, @ArrayRes int config, String id) { + try { + (new SimpleMaterialShowCaseAdapter(activity, activity.getResources().obtainTypedArray(config), id)).start(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public static void initializeWithDelay(final Activity activity, @ArrayRes final int config, final String id, long delay) { + try { + + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + (new SimpleMaterialShowCaseAdapter(activity, activity.getResources().obtainTypedArray(config), id)).start(); + } + }, delay); + } catch (Exception e) { + e.printStackTrace(); + } + } + + public void addToQueue(MaterialShowcaseView v) { + queue.add(v); + sequence.addSequenceItem(v); + } + + public void addToQueue(@IdRes int i, String title, String content, String button, MaterialShowCaseViewShape shape) { + MaterialShowcaseView.Builder msvb = new MaterialShowcaseView.Builder(activity) + .setTarget(activity.findViewById(i)) + .setTitleText(title) + .setContentText(content) + .setDismissText(button); + + switch (shape) { + case CIRCLE: + msvb.withCircleShape(); + break; + case RECTANGLE: + msvb.withRectangleShape(); + break; + } + + MaterialShowcaseView msv = msvb.build(); + + queue.add(msv); + sequence.addSequenceItem(msv); + } + + public void addToQueue(@IdRes int i, @StringRes int title, @StringRes int content, @StringRes int button, MaterialShowCaseViewShape shape) { + MaterialShowcaseView.Builder msvb = new MaterialShowcaseView.Builder(activity) + .setTarget(activity.findViewById(i)) + .setTitleText(title) + .setContentText(content) + .setDismissText(button); + + switch (shape) { + case CIRCLE: + msvb.withCircleShape(); + break; + case RECTANGLE: + msvb.withRectangleShape(); + break; + } + + MaterialShowcaseView msv = msvb.build(); + + queue.add(msv); + sequence.addSequenceItem(msv); + } + + + @Override + public void onDismiss(MaterialShowcaseView materialShowcaseView, int i) { + if (currentQueueItemId == (queue.size() - 1)) { + MaterialShowCaseAdapter.POOL.remove(0); + if (POOL.size() > 0) + MaterialShowCaseAdapter.POOL.get(0).start(); + } + currentQueueItemId++; + } + + public enum MaterialShowCaseViewShape { + CIRCLE, RECTANGLE + } +} diff --git a/library/src/main/java/uk/co/deanwild/materialshowcaseview/adapter/SimpleMaterialShowCaseAdapter.java b/library/src/main/java/uk/co/deanwild/materialshowcaseview/adapter/SimpleMaterialShowCaseAdapter.java new file mode 100644 index 00000000..5268d2c3 --- /dev/null +++ b/library/src/main/java/uk/co/deanwild/materialshowcaseview/adapter/SimpleMaterialShowCaseAdapter.java @@ -0,0 +1,53 @@ +package uk.co.deanwild.materialshowcaseview.adapter; + +import android.app.Activity; +import android.content.res.Resources; +import android.content.res.TypedArray; + +/** + * Created by TurboCoder (Yamil García Hernández) on 7/7/16. + */ +public class SimpleMaterialShowCaseAdapter extends MaterialShowCaseAdapter { + + public int[] ids; + public String[] titles; + public String[] contents; + public String[] buttons; + public String[] shapes; + + public SimpleMaterialShowCaseAdapter(Activity activity, TypedArray array, String id) { + super( + activity, + MaterialShowCaseAdapter.SHOW_CASE_DELAY, + id + ); + Resources res = activity.getResources(); + TypedArray idz = res.obtainTypedArray(array.getResourceId(0, 0)); + this.ids = new int[idz.length()]; + for (int n = 0; n < idz.length(); n++) + this.ids[n] = idz.getResourceId(n, 0); + idz.recycle(); + this.titles = res.getStringArray(array.getResourceId(1, 0)); + this.contents = res.getStringArray(array.getResourceId(2, 0)); + this.buttons = res.getStringArray(array.getResourceId(3, 0)); + this.shapes = res.getStringArray(array.getResourceId(4, 0)); + array.recycle(); + + } + + @Override + public void setup() { + + for (int n = 0; n < ids.length; n++) { + switch (shapes[n]) { + case "circle": + addToQueue(ids[n], titles[n], contents[n], buttons[n], MaterialShowCaseViewShape.CIRCLE); + break; + case "rectangle": + addToQueue(ids[n], titles[n], contents[n], buttons[n], MaterialShowCaseViewShape.RECTANGLE); + break; + } + } + + } +} \ No newline at end of file diff --git a/sample/src/main/AndroidManifest.xml b/sample/src/main/AndroidManifest.xml index 9f182fa3..2b2746ee 100644 --- a/sample/src/main/AndroidManifest.xml +++ b/sample/src/main/AndroidManifest.xml @@ -28,6 +28,10 @@ android:name=".SequenceExample" android:label="@string/title_activity_sequence_example" > + + diff --git a/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/ActivityAdapterExample.java b/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/ActivityAdapterExample.java new file mode 100644 index 00000000..7846e809 --- /dev/null +++ b/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/ActivityAdapterExample.java @@ -0,0 +1,44 @@ +package uk.co.deanwild.materialshowcaseviewsample; + +import android.os.Bundle; +import android.support.v7.app.AppCompatActivity; +import android.view.View; +import android.widget.Button; +import android.widget.Toast; + +import uk.co.deanwild.materialshowcaseview.MaterialShowcaseView; +import uk.co.deanwild.materialshowcaseview.adapter.MaterialShowCaseAdapter; + +/** + * Created by TurboCoder (Yamil García Hernández) on 11/7/16. + */ +public class ActivityAdapterExample extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_sequence_example); + findViewById(R.id.btn_reset).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + reset(); + } + }); + } + + @Override + protected void onStart() { + super.onStart(); + // With Pool Management to run 1 Sequence or 1 View at a time. + // Material Show Case Initialization + MaterialShowCaseAdapter.initialize(this, CustomAdapter.class); + // Simple Material Show Case Initialization + MaterialShowCaseAdapter.initialize(this, R.array.showcase_simple_activity_adapter_example, "SimpleActivityAdapterExample"); + } + + protected void reset() { + MaterialShowcaseView.resetSingleUse(this, "ActivityAdapterExample"); + MaterialShowcaseView.resetSingleUse(this, "SimpleActivityAdapterExample"); + Toast.makeText(this, "Showcase reset", Toast.LENGTH_SHORT).show(); + } +} diff --git a/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/CustomAdapter.java b/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/CustomAdapter.java new file mode 100644 index 00000000..981e2a66 --- /dev/null +++ b/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/CustomAdapter.java @@ -0,0 +1,38 @@ +package uk.co.deanwild.materialshowcaseviewsample; + +import android.app.Activity; +import android.widget.Toast; + +import uk.co.deanwild.materialshowcaseview.MaterialShowcaseView; +import uk.co.deanwild.materialshowcaseview.adapter.MaterialShowCaseAdapter; + +/** + * Created by TurboCoder (Yamil García Hernández) on 11/7/16. + */ +public class CustomAdapter extends MaterialShowCaseAdapter { + + public CustomAdapter(Activity activity) { + super( + activity, + MaterialShowCaseAdapter.SHOW_CASE_DELAY, // DELAY + "ActivityAdapterExample" // ID + ); + } + + @Override // Setup of all show cases views + public void setup() { + super.addToQueue(R.id.btn_one, "", "This is button one","GOT IT", MaterialShowCaseViewShape.CIRCLE); + super.addToQueue(R.id.btn_two, "", "This is button two","GOT IT", MaterialShowCaseViewShape.RECTANGLE); + super.addToQueue(R.id.btn_three, "", "This is button three","GOT IT", MaterialShowCaseViewShape.RECTANGLE); + } + + @Override // Behavior + public void onDismiss(MaterialShowcaseView materialShowcaseView, int i) { + super.onDismiss(materialShowcaseView, i); + switch (currentQueueItemId){ + case 1: + Toast.makeText(activity, "You GOT Button One, nice!", Toast.LENGTH_SHORT).show(); + break; + } + } +} diff --git a/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/MainActivity.java b/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/MainActivity.java index 8f0a3ca8..65408045 100644 --- a/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/MainActivity.java +++ b/sample/src/main/java/uk/co/deanwild/materialshowcaseviewsample/MainActivity.java @@ -42,7 +42,7 @@ public void onClick(View v) { break; case R.id.btn_sequence_example: - intent = new Intent(this, SequenceExample.class); + intent = new Intent(this, ActivityAdapterExample.class); break; case R.id.btn_reset_all: diff --git a/sample/src/main/res/values/showcase.xml b/sample/src/main/res/values/showcase.xml new file mode 100644 index 00000000..74cd0f5e --- /dev/null +++ b/sample/src/main/res/values/showcase.xml @@ -0,0 +1,37 @@ + + + + + + + + + + @id/btn_one + @id/btn_two + @id/btn_three + + + This is button one + This is button two + This is button three + + + GOT IT + GOT IT + GOT IT + + + circle + rectangle + rectangle + + + @array/showcase_simple_activity_adapter_example_views + @array/showcase_simple_activity_adapter_example_titles + @array/showcase_simple_activity_adapter_example_contents + @array/showcase_simple_activity_adapter_example_buttons + @array/showcase_simple_activity_adapter_example_shapes + + + \ No newline at end of file