Skip to content

Commit

Permalink
Added view models for help and support
Browse files Browse the repository at this point in the history
  • Loading branch information
D4rK7355608 committed Jul 10, 2024
1 parent 742ad57 commit c81bd8f
Show file tree
Hide file tree
Showing 6 changed files with 159 additions and 115 deletions.
34 changes: 3 additions & 31 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/help/HelpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,16 @@ package com.d4rk.cleaner.ui.help
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.ui.Modifier
import com.d4rk.cleaner.ui.settings.display.theme.style.AppTheme
import com.d4rk.cleaner.utils.IntentUtils
import com.google.android.play.core.review.ReviewManager
import com.google.android.play.core.review.ReviewManagerFactory

class HelpActivity : AppCompatActivity() {
private lateinit var reviewManager : ReviewManager
private val viewModel: HelpViewModel by viewModels()
override fun onCreate(savedInstanceState : Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
Expand All @@ -23,36 +21,10 @@ class HelpActivity : AppCompatActivity() {
Surface(
modifier = Modifier.fillMaxSize() , color = MaterialTheme.colorScheme.background
) {
HelpComposable(this@HelpActivity)
HelpComposable(this@HelpActivity, viewModel)
}
}
}

}

/**
* Initiates the feedback process for the app.
*
* This function uses the Google Play In-App Review API to prompt the user for feedback.
* If the request to launch the in-app review flow is successful, the review dialog is displayed.
* If the request fails, it opens the Google Play Store page for the app's reviews.
*
* @see com.google.android.play.core.review.ReviewManagerFactory
* @see com.google.android.play.core.review.ReviewManager
* @param context The context used to create the ReviewManager instance and launch review flows.
*/
fun feedback() {
reviewManager = ReviewManagerFactory.create(this)
val task = reviewManager.requestReviewFlow()
task.addOnSuccessListener { reviewInfo ->
reviewManager.launchReviewFlow(this , reviewInfo)
}.addOnFailureListener {
IntentUtils.openUrl(
this ,
"https://play.google.com/store/apps/details?id=${this.packageName}&showAllReviews=true"
)
}.addOnFailureListener {
IntentUtils.sendEmailToDeveloper(this)
}
}
}
17 changes: 14 additions & 3 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/help/HelpComposable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberTopAppBarState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -52,11 +53,19 @@ import com.google.android.gms.oss.licenses.OssLicensesMenuActivity

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun HelpComposable(activity : HelpActivity) {
fun HelpComposable(activity : HelpActivity, viewModel: HelpViewModel) {
val scrollBehavior = TopAppBarDefaults.enterAlwaysScrollBehavior(rememberTopAppBarState())
var showMenu by remember { mutableStateOf(false) }
val context = LocalContext.current
val showDialog = remember { mutableStateOf(false) }
val reviewInfo = viewModel.reviewInfo.value

if (reviewInfo != null) {
LaunchedEffect(key1 = reviewInfo) {
viewModel.requestReviewFlow()
}
}

Scaffold(modifier = Modifier.nestedScroll(scrollBehavior.nestedScrollConnection) , topBar = {
LargeTopAppBar(title = { Text(stringResource(R.string.help)) } , navigationIcon = {
IconButton(onClick = {
Expand Down Expand Up @@ -138,8 +147,10 @@ fun HelpComposable(activity : HelpActivity) {
ExtendedFloatingActionButton(
text = { Text(stringResource(id = R.string.feedback)) } ,
onClick = {
activity.feedback()
} ,
viewModel.reviewInfo.value?.let { safeReviewInfo ->
viewModel.launchReviewFlow(activity, safeReviewInfo)
}
},
icon = {
Icon(
Icons.Default.MailOutline , contentDescription = null
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/help/HelpViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.d4rk.cleaner.ui.help

import android.app.Application
import androidx.compose.runtime.*
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.google.android.play.core.review.ReviewInfo
import com.google.android.play.core.review.ReviewManagerFactory
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

class HelpViewModel(application: Application) : AndroidViewModel(application) {

private var _reviewInfo: MutableState<ReviewInfo?> = mutableStateOf(null)
val reviewInfo: State<ReviewInfo?> = _reviewInfo

fun requestReviewFlow() {
viewModelScope.launch(Dispatchers.IO) {
val reviewManager = ReviewManagerFactory.create(getApplication())
val request = reviewManager.requestReviewFlow()
request.addOnCompleteListener { task ->
if (task.isSuccessful) {
_reviewInfo.value = task.result
} else {
task.exception?.printStackTrace()
}
}
}
}

fun launchReviewFlow(activity: HelpActivity, reviewInfo: ReviewInfo) {
val reviewManager = ReviewManagerFactory.create(activity)
reviewManager.launchReviewFlow(activity, reviewInfo)
}
}
28 changes: 7 additions & 21 deletions app/src/main/kotlin/com/d4rk/cleaner/ui/support/SupportActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,30 @@ package com.d4rk.cleaner.ui.support
import android.os.Bundle
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.runtime.snapshots.SnapshotStateMap
import androidx.compose.ui.Modifier
import com.android.billingclient.api.BillingClient
import com.android.billingclient.api.BillingFlowParams
import com.android.billingclient.api.SkuDetails
import com.android.billingclient.api.SkuDetailsParams
import com.d4rk.cleaner.ui.settings.display.theme.style.AppTheme

class SupportActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState : Bundle?) {
private val viewModel: SupportViewModel by viewModels()

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
AppTheme {
Surface(
modifier = Modifier.fillMaxSize() , color = MaterialTheme.colorScheme.background
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
SupportComposable(this@SupportActivity)
SupportComposable(viewModel, this@SupportActivity)
}
}
}
Expand All @@ -41,20 +43,4 @@ class SupportActivity : AppCompatActivity() {
billingClient.launchBillingFlow(this , flowParams)
}
}

fun querySkuDetails(
billingClient : BillingClient , skuDetailsMap : SnapshotStateMap<String , SkuDetails>
) {
val skuList =
listOf("low_donation" , "normal_donation" , "high_donation" , "extreme_donation")
val params = SkuDetailsParams.newBuilder().setSkusList(skuList)
.setType(BillingClient.SkuType.INAPP).build()
billingClient.querySkuDetailsAsync(params) { billingResult , skuDetailsList ->
if (billingResult.responseCode == BillingClient.BillingResponseCode.OK && skuDetailsList != null) {
for (skuDetails in skuDetailsList) {
skuDetailsMap[skuDetails.sku] = skuDetails
}
}
}
}
}
Loading

0 comments on commit c81bd8f

Please sign in to comment.