From d3f11a73b2bdf0adb26647c3900e4309b217fca7 Mon Sep 17 00:00:00 2001 From: rustamsafarovrs Date: Tue, 25 Jun 2024 18:21:51 +0500 Subject: [PATCH] [feat] in app review --- app/build.gradle.kts | 2 ++ .../inmuslim/data/preferences/Preferences.kt | 9 +++++++++ .../data/repositories/UserRepository.kt | 8 ++++++++ .../rsdevteam/inmuslim/ui/home/HomeScreens.kt | 12 +++++++++++ .../inmuslim/ui/home/HomeViewModel.kt | 6 ++++++ .../tj/rsdevteam/inmuslim/utils/AndroidExt.kt | 20 +++++++++++++++++++ gradle/libs.versions.toml | 3 +++ 7 files changed, 60 insertions(+) create mode 100644 app/src/main/java/tj/rsdevteam/inmuslim/utils/AndroidExt.kt diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 4178cb4..9490b2f 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -138,4 +138,6 @@ dependencies { implementation(libs.firebase.messaging.ktx) implementation(libs.firebase.analytics.ktx) implementation(libs.firebase.crashlytics.ktx) + + implementation(libs.google.play.review.ktx) } diff --git a/app/src/main/java/tj/rsdevteam/inmuslim/data/preferences/Preferences.kt b/app/src/main/java/tj/rsdevteam/inmuslim/data/preferences/Preferences.kt index f08ace7..9efe56b 100644 --- a/app/src/main/java/tj/rsdevteam/inmuslim/data/preferences/Preferences.kt +++ b/app/src/main/java/tj/rsdevteam/inmuslim/data/preferences/Preferences.kt @@ -20,6 +20,7 @@ class Preferences(context: Context) { const val PREFS_REGION_ID = "region_id" const val PREFS_USER_ID = "user_id" const val PREFS_FIREBASE_TOKEN = "firebase_token" + const val PREFS_REVIEW_SHOWN = "review_shown" } fun saveRegionId(id: Long) { @@ -41,4 +42,12 @@ class Preferences(context: Context) { fun getFirebaseToken(): String { return prefs.getString(PREFS_FIREBASE_TOKEN, null) ?: "" } + + fun saveReviewShown() { + editor.putBoolean(PREFS_REVIEW_SHOWN, true).commit() + } + + fun isReviewShown(): Boolean { + return prefs.getBoolean(PREFS_REVIEW_SHOWN, false) + } } diff --git a/app/src/main/java/tj/rsdevteam/inmuslim/data/repositories/UserRepository.kt b/app/src/main/java/tj/rsdevteam/inmuslim/data/repositories/UserRepository.kt index 0d61821..aadf5bd 100644 --- a/app/src/main/java/tj/rsdevteam/inmuslim/data/repositories/UserRepository.kt +++ b/app/src/main/java/tj/rsdevteam/inmuslim/data/repositories/UserRepository.kt @@ -74,4 +74,12 @@ class UserRepository fun getFirebaseToken(): String { return preferences.getFirebaseToken() } + + fun isReviewShown(): Boolean { + return preferences.isReviewShown() + } + + fun saveReviewShown() { + preferences.saveReviewShown() + } } diff --git a/app/src/main/java/tj/rsdevteam/inmuslim/ui/home/HomeScreens.kt b/app/src/main/java/tj/rsdevteam/inmuslim/ui/home/HomeScreens.kt index 40eaa9d..9c01d12 100644 --- a/app/src/main/java/tj/rsdevteam/inmuslim/ui/home/HomeScreens.kt +++ b/app/src/main/java/tj/rsdevteam/inmuslim/ui/home/HomeScreens.kt @@ -18,9 +18,11 @@ import androidx.compose.material3.Scaffold import androidx.compose.material3.Text import androidx.compose.material3.TopAppBar import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.draw.clip +import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.res.stringResource import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp @@ -32,6 +34,8 @@ import tj.rsdevteam.inmuslim.ui.common.ProgressIndicator import tj.rsdevteam.inmuslim.ui.theme.InmuslimShapes import tj.rsdevteam.inmuslim.ui.theme.InmuslimTheme import tj.rsdevteam.inmuslim.ui.theme.InmuslimTypography +import tj.rsdevteam.inmuslim.utils.findActivity +import tj.rsdevteam.inmuslim.utils.launchInAppReview /** * Created by Rustam Safarov on 8/13/23. @@ -60,6 +64,14 @@ fun HomeScreen( viewModel: HomeViewModel = hiltViewModel(), navigateToSettings: () -> Unit ) { + val context = LocalContext.current + + LaunchedEffect(key1 = Unit) { + if (!viewModel.isReviewShown) { + context.findActivity().launchInAppReview { viewModel.reviewShowed() } + } + } + Scaffold( topBar = { TopAppBar( diff --git a/app/src/main/java/tj/rsdevteam/inmuslim/ui/home/HomeViewModel.kt b/app/src/main/java/tj/rsdevteam/inmuslim/ui/home/HomeViewModel.kt index 8f1e279..178ba3f 100644 --- a/app/src/main/java/tj/rsdevteam/inmuslim/ui/home/HomeViewModel.kt +++ b/app/src/main/java/tj/rsdevteam/inmuslim/ui/home/HomeViewModel.kt @@ -32,6 +32,8 @@ class HomeViewModel private set var timing = mutableStateOf(null) private set + val isReviewShown: Boolean + get() = userRepository.isReviewShown() // endregion init { @@ -91,4 +93,8 @@ class HomeViewModel registerUser() } } + + fun reviewShowed() { + userRepository.saveReviewShown() + } } diff --git a/app/src/main/java/tj/rsdevteam/inmuslim/utils/AndroidExt.kt b/app/src/main/java/tj/rsdevteam/inmuslim/utils/AndroidExt.kt new file mode 100644 index 0000000..cce4fc7 --- /dev/null +++ b/app/src/main/java/tj/rsdevteam/inmuslim/utils/AndroidExt.kt @@ -0,0 +1,20 @@ +package tj.rsdevteam.inmuslim.utils + +import android.app.Activity +import com.google.android.play.core.review.ReviewManagerFactory + +/** + * Created by Rustam Safarov on 6/25/24. + * github.com/rustamsafarovrs + */ + +fun Activity.launchInAppReview( + onComplete: (() -> Unit)? = null, +) { + val reviewManager = ReviewManagerFactory.create(this) + reviewManager.requestReviewFlow().addOnCompleteListener { task -> + if (task.isSuccessful) { + reviewManager.launchReviewFlow(this, task.result).addOnCompleteListener { onComplete?.invoke() } + } + } +} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 78f5f4d..c8ef9df 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -22,6 +22,7 @@ retrofit = "2.11.0" retrofitAdapters = "1.0.11" moshi = "1.15.1" firebaseBom = "33.1.1" +googlePlayReview = "2.0.1" junit = "4.13.2" junitAndroid = "1.1.5" @@ -72,6 +73,8 @@ firebase-analytics-ktx = { group = "com.google.firebase", name = "firebase-analy firebase-crashlytics-ktx = { group = "com.google.firebase", name = "firebase-crashlytics-ktx" } firebase-messaging-ktx = { group = "com.google.firebase", name = "firebase-messaging-ktx" } +google-play-review-ktx = { module = "com.google.android.play:review-ktx", version.ref = "googlePlayReview" } + [bundles] [plugins]