Skip to content

Instantly share code, notes, and snippets.

@KryptKode
Last active May 29, 2020 06:36
Show Gist options
  • Save KryptKode/a5944e3571e2e8f7e887844e2b8e9413 to your computer and use it in GitHub Desktop.
Save KryptKode/a5944e3571e2e8f7e887844e2b8e9413 to your computer and use it in GitHub Desktop.
Rating manager on androd
/**
* Created by kryptkode on 2/11/2020.
*/
interface RatingDataProvider {
fun setAgreeShowDialog(isAgree: Boolean)
fun getIsAgreeShowDialog(): Boolean
fun setRemindInterval()
fun getRemindInterval(): Long
fun setInstallDate()
fun getInstallDate(): Long
fun setLaunchTimes(launchTimes: Int)
fun getLaunchTimes(): Int
fun isFirstLaunch(): Boolean
}
import java.util.*
class RatingManager (private val prefsHelper: RatingDataProvider) {
private var installDate = 10
private var launchTimes = 10
private var remindInterval = 1
private var isDebug = false
fun showRateIfMeetsConditions(): Boolean {
return isDebug || shouldShowRateDialog()
}
fun monitor() {
if (prefsHelper.isFirstLaunch()) {
prefsHelper.setInstallDate()
}
prefsHelper.setLaunchTimes(prefsHelper.getLaunchTimes() + 1)
}
fun setLaunchTimes(launchTimes: Int): RatingManager {
this.launchTimes = launchTimes
return this
}
fun setInstallDays(installDate: Int): RatingManager {
this.installDate = installDate
return this
}
fun setRemindInterval(remindInterval: Int): RatingManager {
this.remindInterval = remindInterval
return this
}
fun setAgreeShowDialog(agreed:Boolean): RatingManager {
prefsHelper.setAgreeShowDialog(agreed)
return this
}
private fun shouldShowRateDialog(): Boolean {
return prefsHelper.getIsAgreeShowDialog() &&
isOverLaunchTimes() &&
isOverInstallDate() &&
isOverRemindDate()
}
private fun isOverLaunchTimes(): Boolean {
return prefsHelper.getLaunchTimes() >= launchTimes
}
private fun isOverInstallDate(): Boolean {
return isOverDate(prefsHelper.getInstallDate(), installDate)
}
private fun isOverRemindDate(): Boolean {
return isOverDate(prefsHelper.getRemindInterval(), remindInterval)
}
private fun isOverDate(targetDate: Long, threshold: Int): Boolean {
return Date().getTime() - targetDate >= threshold * 24 * 60 * 60 * 1000
}
fun isDebug(): Boolean {
return isDebug
}
fun setDebug(isDebug: Boolean): RatingManager {
this.isDebug = isDebug
return this
}
}
<string name="rate_dialog_title">Rate this app</string>
<string name="rate_dialog_message">If you enjoy playing this app, would you mind taking a moment to rate it? It won\'t take more than a minute. Thanks for your support!</string>
<string name="rate_dialog_ok">Rate now</string>
<string name="rate_dialog_cancel">Later</string>
import android.content.Context
import android.net.Uri
object UriHelper {
private const val GOOGLE_PLAY = "https://play.google.com/store/apps/details?id="
private const val AMAZON_APPSTORE = "amzn://apps/android?p="
fun getGooglePlay(packageName: String?): Uri? {
return if (packageName == null) null else Uri.parse(GOOGLE_PLAY + packageName)
}
fun getAmazonAppstore(packageName: String?): Uri? {
return if (packageName == null) null else Uri.parse(AMAZON_APPSTORE + packageName)
}
fun isPackageExists(context: Context, targetPackage: String): Boolean {
val pm = context.packageManager
val packages = pm.getInstalledApplications(0)
for (packageInfo in packages) {
if (packageInfo.packageName == targetPackage) return true
}
return false
}
}
class MainActivity : AppCompatActivity(){
val ratingManager = RatingManager()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
showRatingDialogIfConditionsAreMet()
}
protected fun showRatingDialogIfConditionsAreMet() {
if (ratingManager.setDebug(BuildConfig.DEBUG)
.showRateIfMeetsConditions()) {
val dialogFragment = InfoDialog.newInstance(
title = getString(R.string.rate_dialog_title),
message = getString(R.string.rate_dialog_message),
buttonText = getString(R.string.rate_dialog_ok),
hasNeutralButton = true,
neutralButtonText = getString(R.string.rate_dialog_cancel),
neutralListener = object : InfoDialog.NeutralListener {
override fun onNeutralClick() {
}
},
listener = object : InfoDialog.InfoListener {
override fun onConfirm() {
rateApp()
}
}
)
dialogFragment.show(supportFragmentManager, dialogFragment.javaClass.name)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment