worker8 / TourGuide

TourGuide is an Android library that aims to provide an easy way to add pointers with animations over a desired Android View
MIT License
2.63k stars 416 forks source link

Ability to display TourGuide only once then disable it. #15

Open TommyAsAService opened 9 years ago

TommyAsAService commented 9 years ago

Tour only display when open app for the first time

defaultbr commented 9 years ago

??? i dont know if its my english that is poor or your question is a little confuse!

you want only once? add a SharedPreference

open app: check if SharedPreferences exist or its false; if(false) { show TourGuide() set the SharedPreference to TRUE(already displayed) }

that this will open only once on the first run...

if you want to open more than once(everytime), just call the .playOn like the tutorial

cleemansen commented 7 years ago

I also need that feature. But I do not like the if-else-dance suggested by @defaultbr each time using a TourGuide. So I extended this library by my own. Feel free to use this snippet if it helps.

import android.app.Activity;
import android.content.Context;
import android.view.View;

import tourguide.tourguide.TourGuide;

/**
 * Created by clemens on 22.06.17.
 */
public class FooTourGuide extends TourGuide {

    private static final String TOUR_GUIDE_PREFS = "com.example.foo.foo_tour_guide";
    private Activity mActivity;
    private String mSingleShotId;
    private boolean mForceShow;
    private boolean mPresent;

    public FooTourGuide(Activity activity) {
        super(activity);
        this.mActivity = activity;
    }

    public static FooTourGuide init(Activity activity){
        return new FooTourGuide(activity);
    }

    /**
     * Shows the TourGuide only once.
     * @param id a unique ID for this TourGuide.
     */
    public FooTourGuide singleShot(String id) {
        this.mSingleShotId = id;
        return this;
    }

    /**
     * Forces the TourGuide to show up, overrides {@link #singleShot(String)}.
     * @param force
     */
    public FooTourGuide forceShow(boolean force) {
        this.mForceShow = force;
        return this;
    }

    private boolean alreadyShowed() {
        return mActivity.getSharedPreferences(TOUR_GUIDE_PREFS, Context.MODE_PRIVATE)
                .getBoolean(mSingleShotId, false);
    }

    public FooTourGuide playOn(View view) {
        if (this.mForceShow) {
            // force showing (useful during dev)
            showTourGuide(view);
        } else if (! alreadyShowed()) {
            showTourGuide(view);
            // mark as shown
            mActivity.getSharedPreferences(TOUR_GUIDE_PREFS, Context.MODE_PRIVATE)
                    .edit()
                    .putBoolean(mSingleShotId, true)
                    .apply();
        }
        return this;
    }

    private void showTourGuide(View view) {
        super.playOn(view);
        this.mPresent = true;
    }

    public void cleanUp() {
        if (mPresent) {
            super.cleanUp();
            this.mPresent = false;
        } else {
            // nothing to do, do not call super.cleanUp()
            // avoids possible NPE at TourGuide#cleanUp().
        }
    }
}

Example usage:

FooTourGuide
                .init(getActivity())
                .singleShot("special-use-case")
                .setPointer(null)
                .setToolTip(new ToolTip()
                        .setTitle(getString(R.string.special_use_case_title))
                        .setDescription(getString(R.string.special_use_case_text)))
                .setOverlay(null)
                .playOn(target);