Skip to content

Commit

Permalink
feat: add In-App Review Functionality for playstore flavor (#4860)
Browse files Browse the repository at this point in the history

Co-authored-by: lailajs <lailajs@umich.edu>
  • Loading branch information
VaiTon and lailajs committed Oct 6, 2022
1 parent 977c5ed commit eb967ad
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 85 deletions.
8 changes: 8 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ dependencies {


resourcePlaceholders { files = listOf("xml/shortcuts.xml") }


}

android {
Expand Down Expand Up @@ -302,6 +304,12 @@ android {
dimension = "platform"

buildConfigField("boolean", "USE_MLKIT", "true")

dependencies {
// Google Play Core Dependencies - In-app review system
implementation("com.google.android.play:core:1.10.3")
implementation("com.google.android.play:core-ktx:1.8.1")
}
}

create("fdroid") {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package openfoodfacts.github.scrachx.openfood.features.scan

import android.app.Activity
import android.content.Intent
import android.net.Uri
import androidx.browser.customtabs.CustomTabsIntent
import androidx.core.net.toUri
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import openfoodfacts.github.scrachx.openfood.R
import openfoodfacts.github.scrachx.openfood.customtabs.CustomTabActivityHelper
import openfoodfacts.github.scrachx.openfood.customtabs.WebViewFallback
import openfoodfacts.github.scrachx.openfood.utils.PreferencesService
import javax.inject.Inject


class MainActivityHelper @Inject constructor(
private val prefManager: PreferencesService,
) {

/** Dialog for rating the app on play store */
fun showReviewDialog(activity: Activity, customTabsIntent: CustomTabsIntent) {
val rateDialog = MaterialAlertDialogBuilder(activity).setTitle(R.string.app_name)
.setMessage(R.string.user_ask_rate_app)
.setPositiveButton(R.string.rate_app) { dialog, _ ->
//open app page in play store
activity.startActivity(Intent(
Intent.ACTION_VIEW,
Uri.parse("market://details?id=${activity.packageName}"),
))
dialog.dismiss()
}
.setNegativeButton(R.string.no_thx) { dialog, _ -> dialog.dismiss() }

//dialog for giving feedback
val feedbackDialog = MaterialAlertDialogBuilder(activity)
.setTitle(R.string.app_name)
.setMessage(R.string.user_ask_show_feedback_form)
.setPositiveButton(android.R.string.ok) { dialog, _ ->
//show feedback form
CustomTabActivityHelper.openCustomTab(
activity,
customTabsIntent,
activity.getString(R.string.feedback_form_url).toUri(),
WebViewFallback(),
)
dialog.dismiss()
}.setNegativeButton(R.string.txtNo) { dialog, _ -> dialog.dismiss() }


MaterialAlertDialogBuilder(activity)
.setTitle(R.string.app_name)
.setMessage(R.string.user_enjoying_app)
.setPositiveButton(R.string.txtYes) { dialog, _ ->
prefManager.userAskedToRate = true
rateDialog.show()
dialog.dismiss()
}
.setNegativeButton(R.string.txtNo) { dialog, _ ->
prefManager.userAskedToRate = true
feedbackDialog.show()
dialog.dismiss()
}.show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,7 @@ import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.google.zxing.BinaryBitmap
import com.google.zxing.DecodeHintType
import com.google.zxing.FormatException
import com.google.zxing.MultiFormatReader
import com.google.zxing.RGBLuminanceSource
import com.google.zxing.*
import com.google.zxing.common.HybridBinarizer
import com.mikepenz.iconics.typeface.library.googlematerial.GoogleMaterial
import com.mikepenz.materialdrawer.AccountHeader
Expand Down Expand Up @@ -92,6 +88,7 @@ import openfoodfacts.github.scrachx.openfood.features.login.LoginActivity.Compan
import openfoodfacts.github.scrachx.openfood.features.preferences.PreferencesFragment
import openfoodfacts.github.scrachx.openfood.features.product.edit.ProductEditActivity
import openfoodfacts.github.scrachx.openfood.features.productlists.ProductListsActivity
import openfoodfacts.github.scrachx.openfood.features.scan.MainActivityHelper
import openfoodfacts.github.scrachx.openfood.features.scanhistory.ScanHistoryActivity
import openfoodfacts.github.scrachx.openfood.features.searchbycode.SearchByCodeFragment
import openfoodfacts.github.scrachx.openfood.features.shared.BaseActivity
Expand All @@ -104,9 +101,7 @@ import openfoodfacts.github.scrachx.openfood.listeners.CommonBottomListenerInsta
import openfoodfacts.github.scrachx.openfood.listeners.CommonBottomListenerInstaller.selectNavigationItem
import openfoodfacts.github.scrachx.openfood.models.Product
import openfoodfacts.github.scrachx.openfood.models.ProductImageField
import openfoodfacts.github.scrachx.openfood.utils.Intent
import openfoodfacts.github.scrachx.openfood.utils.LocaleManager
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener
import openfoodfacts.github.scrachx.openfood.utils.*
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.Companion.ITEM_ABOUT
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.Companion.ITEM_ADDITIVES
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.Companion.ITEM_ADVANCED_SEARCH
Expand All @@ -128,34 +123,12 @@ import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.Comp
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.Companion.ITEM_USER
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.Companion.ITEM_YOUR_LISTS
import openfoodfacts.github.scrachx.openfood.utils.NavigationDrawerListener.NavigationDrawerType
import openfoodfacts.github.scrachx.openfood.utils.OnKeyboardVisibilityChanged
import openfoodfacts.github.scrachx.openfood.utils.PreferencesService
import openfoodfacts.github.scrachx.openfood.utils.SearchSuggestionProvider
import openfoodfacts.github.scrachx.openfood.utils.SearchType
import openfoodfacts.github.scrachx.openfood.utils.buildAccountHeader
import openfoodfacts.github.scrachx.openfood.utils.buildDrawer
import openfoodfacts.github.scrachx.openfood.utils.dividerItem
import openfoodfacts.github.scrachx.openfood.utils.getAppPreferences
import openfoodfacts.github.scrachx.openfood.utils.getLoginPreferences
import openfoodfacts.github.scrachx.openfood.utils.getLoginUsername
import openfoodfacts.github.scrachx.openfood.utils.getUserSession
import openfoodfacts.github.scrachx.openfood.utils.hideKeyboard
import openfoodfacts.github.scrachx.openfood.utils.isApplicationInstalled
import openfoodfacts.github.scrachx.openfood.utils.isGranted
import openfoodfacts.github.scrachx.openfood.utils.isHardwareCameraInstalled
import openfoodfacts.github.scrachx.openfood.utils.isNetworkConnected
import openfoodfacts.github.scrachx.openfood.utils.isUserSet
import openfoodfacts.github.scrachx.openfood.utils.listenToKeyboardVisibilityChanges
import openfoodfacts.github.scrachx.openfood.utils.primaryItem
import openfoodfacts.github.scrachx.openfood.utils.profileItem
import openfoodfacts.github.scrachx.openfood.utils.profileSettingItem
import openfoodfacts.github.scrachx.openfood.utils.sectionItem
import openfoodfacts.github.scrachx.openfood.utils.stopListeningToKeyboardVisibilityChanges
import java.io.FileNotFoundException
import java.io.IOException
import javax.inject.Inject
import openfoodfacts.github.scrachx.openfood.features.search.ProductSearchActivity.Companion.start as startSearch


@AndroidEntryPoint
class MainActivity : BaseActivity(), NavigationDrawerListener, NavigationDrawerHost {
private var _binding: ActivityMainBinding? = null
Expand Down Expand Up @@ -296,8 +269,10 @@ class MainActivity : BaseActivity(), NavigationDrawerListener, NavigationDrawerH
}
}
if (!isApplicationInstalled(this@MainActivity, BuildConfig.OFOTHERLINKAPP)) {
drawerResult.updateName(ITEM_OBF.toLong(),
StringHolder("${getString(R.string.install)} ${getString(R.string.open_other_flavor_drawer)}"))
drawerResult.updateName(
ITEM_OBF.toLong(),
StringHolder("${getString(R.string.install)} ${getString(R.string.open_other_flavor_drawer)}")
)
} else {
drawerResult.updateName(ITEM_OBF.toLong(), StringHolder(getString(R.string.open_other_flavor_drawer)))
}
Expand Down Expand Up @@ -497,14 +472,18 @@ class MainActivity : BaseActivity(), NavigationDrawerListener, NavigationDrawerH
ITEM_LOGIN -> loginThenUpdate.launch(Unit)
ITEM_ALERT -> newFragment = AllergensAlertFragment.newInstance()
ITEM_PREFERENCES -> newFragment = PreferencesFragment.newInstance()
ITEM_ABOUT -> CustomTabActivityHelper.openCustomTab(this@MainActivity,
ITEM_ABOUT -> CustomTabActivityHelper.openCustomTab(
this@MainActivity,
customTabsIntent,
discoverUri,
WebViewFallback())
ITEM_CONTRIBUTE -> CustomTabActivityHelper.openCustomTab(this@MainActivity,
WebViewFallback()
)
ITEM_CONTRIBUTE -> CustomTabActivityHelper.openCustomTab(
this@MainActivity,
customTabsIntent,
contributeUri,
WebViewFallback())
WebViewFallback()
)
ITEM_INCOMPLETE_PRODUCTS -> startSearch(
this@MainActivity,
SearchType.INCOMPLETE_PRODUCT,
Expand All @@ -530,8 +509,12 @@ class MainActivity : BaseActivity(), NavigationDrawerListener, NavigationDrawerH
}
} else {
try {
startActivity(Intent(Intent.ACTION_VIEW,
"market://details?id=${BuildConfig.OFOTHERLINKAPP}".toUri()))
startActivity(
Intent(
Intent.ACTION_VIEW,
"market://details?id=${BuildConfig.OFOTHERLINKAPP}".toUri()
)
)
} catch (anfe: ActivityNotFoundException) {
startActivity(
Intent(
Expand Down Expand Up @@ -767,58 +750,18 @@ class MainActivity : BaseActivity(), NavigationDrawerListener, NavigationDrawerH
) {
val firstTimeLaunchTime = prefManager.firstTimeLaunchTime

// Check if it has been a week since first launch
// Check if it has been a week since first launch
if (firstTimeLaunchTime + System.currentTimeMillis() >= 7 * 24 * 60 * 60 * 1000)
showFeedbackDialog()
}
}

/**
* show dialog to ask the user to rate the app/give feedback
*/
private fun showFeedbackDialog() {
//dialog for rating the app on play store
val rateDialog = MaterialAlertDialogBuilder(this)
.setTitle(R.string.app_name)
.setMessage(R.string.user_ask_rate_app)
.setPositiveButton(R.string.rate_app) { dialog, _ ->
//open app page in play store
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=$packageName")))
dialog.dismiss()
}
.setNegativeButton(R.string.no_thx) { dialog, _ -> dialog.dismiss() }

//dialog for giving feedback
val feedbackDialog = MaterialAlertDialogBuilder(this)
.setTitle(R.string.app_name)
.setMessage(R.string.user_ask_show_feedback_form)
.setPositiveButton(android.R.string.ok) { dialog, _ ->
//show feedback form
CustomTabActivityHelper.openCustomTab(
this@MainActivity,
customTabsIntent,
getString(R.string.feedback_form_url).toUri(),
WebViewFallback(),
)
dialog.dismiss()
}
.setNegativeButton(R.string.txtNo) { dialog, _ -> dialog.dismiss() }

@Inject
lateinit var mainActivityHelper: MainActivityHelper

MaterialAlertDialogBuilder(this)
.setTitle(R.string.app_name)
.setMessage(R.string.user_enjoying_app)
.setPositiveButton(R.string.txtYes) { dialog, _ ->
prefManager.userAskedToRate = true
rateDialog.show()
dialog.dismiss()
}
.setNegativeButton(R.string.txtNo) { dialog, _ ->
prefManager.userAskedToRate = true
feedbackDialog.show()
dialog.dismiss()
}
.show()
/** show dialog to ask the user to rate the app/give feedback */
private fun showFeedbackDialog() {
mainActivityHelper.showReviewDialog(this, customTabsIntent)
}

override fun onStop() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package openfoodfacts.github.scrachx.openfood.features.scan

import android.app.Activity
import androidx.browser.customtabs.CustomTabsIntent
import com.google.android.play.core.review.ReviewManagerFactory
import javax.inject.Inject

class MainActivityHelper @Inject constructor() {
/** Dialog for rating the app on play store */
fun showReviewDialog(
activity: Activity,
@Suppress("UNUSED_PARAMETER") customTabsIntent: CustomTabsIntent,
) {
//dialog for rating the app on play store
val manager = ReviewManagerFactory.create(activity)
val request = manager.requestReviewFlow()
request.addOnCompleteListener { task ->
if (task.isSuccessful) {
val reviewInfo = task.result
manager.launchReviewFlow(activity, reviewInfo)
}
}
}
}

0 comments on commit eb967ad

Please sign in to comment.