The final step you need to take before testing the In App Review feature is to connect the manager to the rest of your app. Start off by creating an interface in the inappreview module, called InAppReviewView:
interface InAppReviewView {
fun showReviewFlow()
}
This is an interface that you’ll implement in places you want to expose and call the IAR feature. Now open the InAppReviewPromptDialog.kt file and change the code like so:
@Inject
lateinit var inAppReviewManager: InAppReviewManager
private fun onLeaveReviewTapped() {
preferences.setUserRatedApp(true)
inAppReviewManager.startReview(requireActivity())
dismissAllowingStateLoss()
}
You first inject the manager, using Hilt. Then you fill in the onLeaveReviewTapped() function, to start the review flow when the user taps the review option in the dialog. Now open the CollectionFragment.kt file and add the manager, as well:
@Inject
lateinit var reviewManager: InAppReviewManager
The manager holds the information to check if the app is eligible or not to show a review dialog. This is why you need it here. Change the following code, to set up the view in the ViewModel:
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.setInAppReviewView(this) // here
initUi()
initObservers()
loadCollection()
checkAndShowOnboarding()
}
This function doesn’t yet exist and you haven’t implemented the InAppReviewView yet. Let’s implement the interface next:
override fun showReviewFlow() {
if (reviewManager.isEligibleForReview()) {
val dialog = InAppReviewPromptDialog()
dialog.show(childFragmentManager, null)
}
}
Good job. If the app is eligible, you’ll create a dialog and show it using this Fragment. Now add the following code to the CollectionViewModel to add the InAppReviewView property:
private lateinit var inAppReviewView: InAppReviewView
fun setInAppReviewView(inAppReviewView: InAppReviewView) {
this.inAppReviewView = inAppReviewView
}
This code will set up the InAppReviewView, allowing you to tell the Fragment to show a review prompt dialog. Finally, add the following code and call it within the loadCollection() function:
fun checkIfNeedsReviewPrompt() {
val value = collection.value ?: return
if (value.isProgressionFinished()) {
inAppReviewView.showReviewFlow()
}
}
Awesome! The last piece of code will check if the video course you’re browsing is completed or not. If it is, you’ll show the review flow and ask the user for a review.
Build & Run the app just to check that everything works. Don’t trigger the review flow yet, as testing In App Reviews is a bit more complicated, so you’ll do it in the next episode! See you there! :]