Skip to content

Instantly share code, notes, and snippets.

@eka
Created April 10, 2018 12:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eka/2622e3e5843b82450959450002ea7b41 to your computer and use it in GitHub Desktop.
Save eka/2622e3e5843b82450959450002ea7b41 to your computer and use it in GitHub Desktop.
using System;
using System.Collections;
using System.Collections.Generic;
using DoozyUI;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
public class RateUsManager : MonoBehaviour
{
public UnityEvent OnRateUsPanelClosed = new UnityEvent();
public Toggle DontAskAgainToggle;
private bool _dontAskAgain = false;
private bool _rated = false;
private int _timesAsked = 0;
private DateTime _lastTimeAsked;
public static readonly string DontAskAgainKey = "DontAskAgain";
public static readonly string RatedKey = "Rated";
public static readonly string LastTimeAskedKey = "LastTimeAsked";
public static readonly string TimesAskedKey = "TimesAsked";
private const int AskAfterDays = 3;
private UIElement _rateUsPanel;
public bool Rated
{
get
{
if (ES3.KeyExists(RatedKey))
{
_rated = ES3.Load<bool>(RatedKey);
}
return _rated;
}
}
public bool DontAskAgain
{
get
{
if (ES3.KeyExists(DontAskAgainKey))
{
Debug.Log("Reading Dont ask again value.");
_dontAskAgain = ES3.Load<bool>(DontAskAgainKey);
}
return _dontAskAgain;
}
}
public int TimesAsked
{
get
{
if (ES3.KeyExists(TimesAskedKey))
{
_timesAsked = ES3.Load<int>(TimesAskedKey);
}
return _timesAsked;
}
}
public DateTime LastTimeAsked
{
get
{
if (ES3.KeyExists(LastTimeAskedKey))
{
_lastTimeAsked = ES3.Load<DateTime>(LastTimeAskedKey);
}
return _lastTimeAsked;
}
}
private void Awake()
{
if (ES3.KeyExists(DontAskAgainKey))
{
_dontAskAgain = ES3.Load<bool>(DontAskAgainKey);
}
if (ES3.KeyExists(RatedKey))
{
_rated = ES3.Load<bool>(RatedKey);
}
if (ES3.KeyExists(LastTimeAskedKey))
{
_lastTimeAsked = ES3.Load<DateTime>(LastTimeAskedKey);
}
if (ES3.KeyExists(TimesAskedKey))
{
_timesAsked = ES3.Load<int>(TimesAskedKey);
}
_rateUsPanel = GetComponent<UIElement>();
}
// Use this for initialization
void Start()
{
DontAskAgainToggle.isOn = DontAskAgain;
}
// Update is called once per frame
void Update()
{
}
public void ShowRateUsPanel()
{
var now = DateTime.Now;
ES3.Save<DateTime>(LastTimeAskedKey, now);
_rateUsPanel.Show(false);
}
public void OnClosePanel()
{
_rateUsPanel.Hide(false);
if (OnRateUsPanelClosed != null)
{
OnRateUsPanelClosed.Invoke();
}
}
public void OnDontAskAgainToggle(bool dontAskAgain)
{
Debug.Log("Dont ask again toggle: " + dontAskAgain);
ES3.Save<bool>(DontAskAgainKey, dontAskAgain);
}
public void OnRateNowButtonClicked()
{
//#if UNITY_EDITOR
Application.OpenURL("https://play.google.com/store/apps/details?id=dog.Yak.CatchTheMantraMahaMantra&rdid=dog.Yak.CatchTheMantraMahaMantra");
//#else
// Application.OpenURL("market://details?id=dog.Yak.CatchTheMantraMahaMantra/");
//#endif
ES3.Save<bool>(RatedKey, true);
_rateUsPanel.Hide(false);
if (OnRateUsPanelClosed != null)
{
OnRateUsPanelClosed.Invoke();
}
}
public bool CanShowRatePanel()
{
// can we be sure they rated?
// if (Rated) return false;
if (DontAskAgain) return false;
if (!ES3.KeyExists(LastTimeAskedKey)) return true;
DateTime lastTime = ES3.Load<DateTime>(LastTimeAskedKey);
var now = DateTime.Now;
var elapsed = now - lastTime;
var elapsedDays = new TimeSpan(elapsed.Ticks).Days;
return elapsedDays >= 3;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment