Android In App Review

Jan 28 2021 · Kotlin 1.4, Android 5, Android Studio 4

Part 1: Implementing In App Review

06. Implement The In App Review Manager

Episode complete

Play next episode

Next
About this episode
Leave a rating/review
See forum comments
Cinema mode Mark complete Download course materials
Previous episode: 05. Provide Dependencies Next episode: 07. Connect The Manager To UI

Get immediate access to this and 4,000+ other videos and books.

Take your career further with a Kodeco Personal Plan. With unlimited access to over 40+ books and 4,000+ professional videos in a single subscription, it's simply the best investment you can make in your development career.

Learn more Already a subscriber? Sign in.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Heads up... You’re accessing parts of this content for free, with some sections shown as obfuscated text.

Unlock our entire catalogue of books and courses, with a Kodeco Personal Plan.

Unlock now

Now that you have nearly everything in place in terms of dependencies, project setup and data, it’s time to build the InAppReviewManager. To start doing that, create a new interface named InAppReviewManager:

interface InAppReviewManager {

  fun startReview(activity: Activity)

  fun isEligibleForReview(): Boolean
}
class InAppReviewManagerImpl @Inject constructor(
  @ApplicationContext private val context: Context,
  private val reviewManager: ReviewManager,
  private val inAppReviewPreferences: InAppReviewPreferences
) : InAppReviewManager {

}
@Binds
@Singleton
abstract fun bindInAppReviewManager(
  inAppReviewManagerImpl: InAppReviewManagerImpl
): InAppReviewManager
  companion object {
    private const val KEY_REVIEW = "reviewFlow"
  }

  private var reviewInfo: ReviewInfo? = null

  init {
    if (isEligibleForReview()) {
      reviewManager.requestReviewFlow().addOnCompleteListener {
        if (it.isComplete && it.isSuccessful) {
          this.reviewInfo = it.result
        }
      }
    }
  }
override fun isEligibleForReview(): Boolean {
  return (!inAppReviewPreferences.hasUserRatedApp()
    && !inAppReviewPreferences.hasUserChosenRateLater())
    || (inAppReviewPreferences.hasUserChosenRateLater() && enoughTimePassed())
}

private fun enoughTimePassed(): Boolean {
  val rateLaterTimestamp = inAppReviewPreferences.getRateLaterTime()

  return abs(rateLaterTimestamp - System.currentTimeMillis()) >= TimeUnit.DAYS.toMillis(14)
}
override fun startReview(activity: Activity) {
  if (reviewInfo != null) {
    reviewManager.launchReviewFlow(activity, reviewInfo).addOnCompleteListener { reviewFlow ->
      onReviewFlowLaunchCompleted(reviewFlow)
    }
  }
}

private fun onReviewFlowLaunchCompleted(reviewFlow: Task<Void>) {
  if (reviewFlow.isSuccessful) {
    logSuccess()
  }
}

private fun logSuccess() {
  if (BuildConfig.DEBUG) {
    Log.d(KEY_REVIEW, "Review complete!")
  }
}