Android In App Review

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

Part 1: Implementing In App Review

04. Store Review Preferences

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: 03. Build An In App Review Module Next episode: 05. Provide Dependencies

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.

Notes: 04. Store Review Preferences

The UI code for the In App Review feature is predefined for you, as you’ll focus on the technical aspects of IAR. You’re free to explore the UI logic and code to learn more.

Make sure you use the starting project from this episode and that you set it up in the same way you did in the ‘Set Up The Project’ episode.

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 the module set up, you can start working on the In App Review feature. Before you dive into writing code, take a moment to look at the dialog package and its contents. The UI part of the code is predefined for you, as it’s less important than the business logic and the Review flow code you’ll work on.

interface InAppReviewPreferences {

  fun hasUserRatedApp(): Boolean

  fun setUserRatedApp(hasRated: Boolean)

  fun hasUserChosenRateLater(): Boolean

  fun setUserChosenRateLater(hasChosenRateLater: Boolean)

  fun getRateLaterTime(): Long

  fun setRateLater(time: Long)
}
class InAppReviewPreferencesImpl @Inject constructor(
  private val sharedPreferences: SharedPreferences
) : InAppReviewPreferences {

  companion object {
    private const val KEY_HAS_RATED_APP = "hasRatedApp"
    private const val KEY_CHOSEN_RATE_LATER = "rateLater"
    private const val KEY_RATE_LATER_TIME = "rateLaterTime"
  }
}
  override fun hasUserRatedApp(): Boolean =
    sharedPreferences.getBoolean(KEY_HAS_RATED_APP, false)

  override fun setUserRatedApp(hasRated: Boolean): Unit =
    sharedPreferences.edit { putBoolean(KEY_HAS_RATED_APP, hasRated) }
    
  override fun hasUserChosenRateLater(): Boolean =
    sharedPreferences.getBoolean(KEY_CHOSEN_RATE_LATER, false)

  override fun setUserChosenRateLater(hasChosenRateLater: Boolean): Unit =
    sharedPreferences.edit { putBoolean(KEY_CHOSEN_RATE_LATER, hasChosenRateLater) }

  override fun getRateLaterTime(): Long =
    sharedPreferences.getLong(KEY_RATE_LATER_TIME, System.currentTimeMillis())

  override fun setRateLater(time: Long): Unit =
    sharedPreferences.edit { putLong(KEY_RATE_LATER_TIME, time) }
  @Inject
  lateinit var preferences: InAppReviewPreferences
  private fun onLeaveReviewTapped() {
    preferences.setUserRatedApp(true)
    // TODO start review
    dismissAllowingStateLoss()
  }
  private fun onRateLaterTapped() {
    preferences.setUserChosenRateLater(true)
    preferences.setRateLater(getLaterTime())
    dismissAllowingStateLoss()
  }
  override fun onCancel(dialog: DialogInterface) {
    preferences.setUserChosenRateLater(true)
    preferences.setRateLater(getLaterTime())
    super.onCancel(dialog)
  }

  private fun getLaterTime(): Long {
    return System.currentTimeMillis() + TimeUnit.DAYS.toMillis(14)
  }