package org.wikipedia.onboarding;

import org.wikipedia.settings.Prefs;

public final class PrefsOnboardingStateMachine implements OnboardingStateMachine {
    private static final PrefsOnboardingStateMachine INSTANCE = new PrefsOnboardingStateMachine();

    private final boolean initialTocTutorialEnabled = Prefs.isTocTutorialEnabled();

    public static PrefsOnboardingStateMachine getInstance() {
        return INSTANCE;
    }

    @Override
    public boolean isTocTutorialEnabled() {
        return Prefs.isTocTutorialEnabled();
    }

    @Override
    public void setTocTutorial() {
        Prefs.setTocTutorialEnabled(false);
    }

    @Override
    public boolean isSelectTextTutorialEnabled() {
        return !initialTocTutorialEnabled && Prefs.isSelectTextTutorialEnabled();
    }

    @Override
    public void setSelectTextTutorial() {
        Prefs.setSelectTextTutorialEnabled(false);
    }

    @Override
    public boolean isShareTutorialEnabled() {
        return !initialTocTutorialEnabled
                && !isSelectTextTutorialEnabled()
                && Prefs.isShareTutorialEnabled();
    }

    @Override
    public void setShareTutorial() {
        Prefs.setShareTutorialEnabled(false);
    }

    private PrefsOnboardingStateMachine() { }
}
package org.wikipedia.onboarding;

import org.wikipedia.settings.Prefs;
import java.util.concurrent.TimeUnit;

public final class PrefsOnboardingStateMachine implements OnboardingStateMachine {
    private static final PrefsOnboardingStateMachine INSTANCE = new PrefsOnboardingStateMachine();
    private static final long MIN_MINUTES_PER_TUTORIAL = 1;

    private long millisSinceLastTutorial;

    public static PrefsOnboardingStateMachine getInstance() {
        return INSTANCE;
    }

    @Override
    public boolean isTocTutorialEnabled() {
        // don't care about time since last tutorial here, since the ToC tooltip
        // is always the first one shown.
        return Prefs.isTocTutorialEnabled();
    }

    @Override
    public void setTocTutorial() {
        Prefs.setTocTutorialEnabled(false);
        updateTimeSinceLastTutorial();
    }

    @Override
    public boolean isSelectTextTutorialEnabled() {
        return minutesSinceLastTutorial() >= MIN_MINUTES_PER_TUTORIAL
               && !Prefs.isTocTutorialEnabled() && Prefs.isSelectTextTutorialEnabled();
    }

    @Override
    public void setSelectTextTutorial() {
        Prefs.setSelectTextTutorialEnabled(false);
        updateTimeSinceLastTutorial();
    }

    @Override
    public boolean isShareTutorialEnabled() {
        // don't care about time since last tutorial here, since the share tooltip is
        // tied to the highlight action.
        return !Prefs.isTocTutorialEnabled()
                && !isSelectTextTutorialEnabled()
                && Prefs.isShareTutorialEnabled();
    }

    @Override
    public void setShareTutorial() {
        Prefs.setShareTutorialEnabled(false);
        updateTimeSinceLastTutorial();
    }

    private void updateTimeSinceLastTutorial() {
        millisSinceLastTutorial = System.currentTimeMillis();
    }

    private long minutesSinceLastTutorial() {
        return TimeUnit.MILLISECONDS.toMinutes(System.currentTimeMillis() - millisSinceLastTutorial);
    }

    private PrefsOnboardingStateMachine() { }
}
Improve sequencing of ToC and Text-highlight tooltips.
Improve sequencing of ToC and Text-highlight tooltips.

Previously, if the ToC tutorial was shown to the user, then the Highlight
tutorial wouldn't be shown until the app is destroyed and relaunched,
which is not a predictable interval.

This patch guarantees that the Highlight tutorial is shown after the ToC
tutorial, with a minimum time interval of 1 minute between the two
tutorials (so that the user isn't inundated).

Change-Id: I621d179f47c0b606ab24facec06a654e70373c83
