refactor: migrate dialogs to Compose

This commit is contained in:
FunkyMuse 2023-10-03 16:15:20 +02:00
parent 2d350e14cd
commit 51bd8a736c
5 changed files with 190 additions and 24 deletions

View File

@ -4,18 +4,29 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.activity.ComponentActivity import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent import androidx.activity.compose.setContent
import androidx.compose.runtime.Composable import androidx.compose.runtime.*
import androidx.compose.runtime.snapshots.SnapshotStateList
import com.simplemobiletools.commons.compose.alert_dialog.AlertDialogState
import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState
import com.simplemobiletools.commons.compose.extensions.enableEdgeToEdgeSimple import com.simplemobiletools.commons.compose.extensions.enableEdgeToEdgeSimple
import com.simplemobiletools.commons.compose.extensions.linkColor
import com.simplemobiletools.commons.compose.extensions.onEventValue import com.simplemobiletools.commons.compose.extensions.onEventValue
import com.simplemobiletools.commons.compose.theme.AppThemeSurface import com.simplemobiletools.commons.compose.theme.AppThemeSurface
import com.simplemobiletools.commons.dialogs.DonateAlertDialog
import com.simplemobiletools.commons.dialogs.RateStarsAlertDialog
import com.simplemobiletools.commons.dialogs.UpgradeToProAlertDialog
import com.simplemobiletools.commons.dialogs.WhatsNewAlertDialog
import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.commons.models.FAQItem
import com.simplemobiletools.commons.models.Release import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.thankyou.BuildConfig import com.simplemobiletools.thankyou.BuildConfig
import com.simplemobiletools.thankyou.R import com.simplemobiletools.thankyou.R
import com.simplemobiletools.thankyou.extensions.checkWhatsNew
import com.simplemobiletools.thankyou.extensions.startAboutActivity import com.simplemobiletools.thankyou.extensions.startAboutActivity
import com.simplemobiletools.thankyou.helpers.appLaunchedCompose
import com.simplemobiletools.thankyou.helpers.checkWhatsNewCompose
import com.simplemobiletools.thankyou.helpers.upgradeToPro
import com.simplemobiletools.thankyou.screens.MainScreen import com.simplemobiletools.thankyou.screens.MainScreen
import kotlinx.collections.immutable.toImmutableList
class MainActivity : ComponentActivity() { class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -23,6 +34,8 @@ class MainActivity : ComponentActivity() {
enableEdgeToEdgeSimple() enableEdgeToEdgeSimple()
setContent { setContent {
AppThemeSurface { AppThemeSurface {
val releasesList = remember { mutableStateListOf<Release>() }
val checkWhatsNewAlertDialogState = getCheckWhatsNewAlertDialogState(releasesList)
val linkColor = linkColor() val linkColor = linkColor()
val showMoreApps = onEventValue { !resources.getBoolean(R.bool.hide_google_relations) } val showMoreApps = onEventValue { !resources.getBoolean(R.bool.hide_google_relations) }
MainScreen( MainScreen(
@ -32,17 +45,86 @@ class MainActivity : ComponentActivity() {
openAbout = ::launchAbout, openAbout = ::launchAbout,
moreAppsFromUs = ::launchMoreAppsFromUsIntent moreAppsFromUs = ::launchMoreAppsFromUsIntent
) )
AppLaunched()
CheckWhatsNew(releasesList, checkWhatsNewAlertDialogState)
} }
} }
appLaunched(BuildConfig.APPLICATION_ID)
checkWhatsNewDialog()
} }
@Composable @Composable
private fun linkColor() = onEventValue { private fun AppLaunched(
when { donateAlertDialogState: AlertDialogState = getDonateAlertDialogState(),
isWhiteTheme() || isBlackAndWhiteTheme() -> baseConfig.accentColor rateStarsAlertDialogState: AlertDialogState = getRateStarsAlertDialogState(),
else -> getProperPrimaryColor() upgradeToProAlertDialogState: AlertDialogState = getUpgradeToProAlertDialogState()
) {
LaunchedEffect(Unit) {
appLaunchedCompose(
appId = BuildConfig.APPLICATION_ID,
showDonateDialog = donateAlertDialogState::show,
showRateUsDialog = rateStarsAlertDialogState::show,
showUpgradeDialog = upgradeToProAlertDialogState::show
)
}
}
@Composable
private fun CheckWhatsNew(
releasesList: SnapshotStateList<Release>,
checkWhatsNewAlertDialogState: AlertDialogState
) {
DisposableEffect(Unit) {
checkWhatsNewCompose(
releases = listOf(
Release(14, R.string.release_14),
Release(3, R.string.release_3)
),
currVersion = BuildConfig.VERSION_CODE,
showWhatsNewDialog = { releases ->
releasesList.addAll(releases)
checkWhatsNewAlertDialogState.show()
}
)
onDispose {
releasesList.clear()
}
}
}
@Composable
private fun getUpgradeToProAlertDialogState() = rememberAlertDialogState().apply {
DialogMember {
UpgradeToProAlertDialog(
alertDialogState = this, onMoreInfoClick = ::upgradeToPro, onUpgradeClick = ::launchUpgradeToProIntent
)
}
}
@Composable
private fun getCheckWhatsNewAlertDialogState(releasesList: SnapshotStateList<Release>) = rememberAlertDialogState().apply {
DialogMember {
WhatsNewAlertDialog(alertDialogState = this, releases = releasesList.toImmutableList())
}
}
@Composable
private fun getDonateAlertDialogState() =
rememberAlertDialogState().apply {
DialogMember {
DonateAlertDialog(alertDialogState = this)
}
}
@Composable
private fun getRateStarsAlertDialogState() = rememberAlertDialogState().apply {
DialogMember {
RateStarsAlertDialog(alertDialogState = this) { stars ->
if (stars == 5) {
redirectToRateUs()
}
toast(com.simplemobiletools.commons.R.string.thank_you)
baseConfig.wasAppRated = true
}
} }
} }
@ -61,12 +143,4 @@ class MainActivity : ComponentActivity() {
startAboutActivity(R.string.app_name, 0, BuildConfig.VERSION_NAME, faqItems, false) startAboutActivity(R.string.app_name, 0, BuildConfig.VERSION_NAME, faqItems, false)
} }
private fun checkWhatsNewDialog() {
arrayListOf<Release>().apply {
add(Release(14, R.string.release_14))
add(Release(3, R.string.release_3))
checkWhatsNew(this, BuildConfig.VERSION_CODE)
}
}
} }

View File

@ -1,4 +1,10 @@
package com.simplemobiletools.thankyou.helpers package com.simplemobiletools.thankyou.helpers
import androidx.compose.material3.Typography
// Shared Preferences // Shared Preferences
const val HIDE_LAUNCHER_ICON = "hide_launcher_icon" const val HIDE_LAUNCHER_ICON = "hide_launcher_icon"
val typography = Typography(
)

View File

@ -0,0 +1,86 @@
package com.simplemobiletools.thankyou.helpers
import android.content.ComponentName
import android.content.Context
import android.content.pm.PackageManager
import androidx.activity.ComponentActivity
import com.simplemobiletools.commons.extensions.*
import com.simplemobiletools.commons.models.Release
import com.simplemobiletools.thankyou.R
fun ComponentActivity.appLaunchedCompose(
appId: String,
showUpgradeDialog: () -> Unit,
showDonateDialog: () -> Unit,
showRateUsDialog: () -> Unit
) {
baseConfig.internalStoragePath = getInternalStoragePath()
updateSDCardPath()
baseConfig.appId = appId
if (baseConfig.appRunCount == 0) {
baseConfig.wasOrangeIconChecked = true
checkAppIconColor()
} else if (!baseConfig.wasOrangeIconChecked) {
baseConfig.wasOrangeIconChecked = true
val primaryColor = resources.getColor(R.color.color_primary)
if (baseConfig.appIconColor != primaryColor) {
getAppIconColors().forEachIndexed { index, color ->
toggleAppIconColor(appId, index, color, false)
}
val defaultClassName = "${baseConfig.appId.removeSuffix(".debug")}.activities.SplashActivity"
packageManager.setComponentEnabledSetting(
ComponentName(baseConfig.appId, defaultClassName),
PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
PackageManager.DONT_KILL_APP
)
val orangeClassName = "${baseConfig.appId.removeSuffix(".debug")}.activities.SplashActivity.Orange"
packageManager.setComponentEnabledSetting(
ComponentName(baseConfig.appId, orangeClassName),
PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
PackageManager.DONT_KILL_APP
)
baseConfig.appIconColor = primaryColor
baseConfig.lastIconColor = primaryColor
}
}
baseConfig.appRunCount++
if (baseConfig.appRunCount % 30 == 0 && !isAProApp()) {
if (!resources.getBoolean(R.bool.hide_google_relations)) {
if (getCanAppBeUpgraded()) {
showUpgradeDialog()
} else if (!isOrWasThankYouInstalled()) {
showDonateDialog()
}
}
}
if (baseConfig.appRunCount % 40 == 0 && !baseConfig.wasAppRated) {
if (!resources.getBoolean(R.bool.hide_google_relations)) {
showRateUsDialog()
}
}
}
fun ComponentActivity.checkWhatsNewCompose(releases: List<Release>, currVersion: Int, showWhatsNewDialog: (List<Release>) -> Unit) {
if (baseConfig.lastVersion == 0) {
baseConfig.lastVersion = currVersion
return
}
val newReleases = arrayListOf<Release>()
releases.filterTo(newReleases) { it.id > baseConfig.lastVersion }
if (newReleases.isNotEmpty()) {
showWhatsNewDialog(newReleases)
}
baseConfig.lastVersion = currVersion
}
fun ComponentActivity.upgradeToPro() {
launchViewIntent("https://simplemobiletools.com/upgrade_to_pro")
}

View File

@ -33,7 +33,7 @@ internal fun MainScreen(
openSettings: () -> Unit, openSettings: () -> Unit,
openAbout: () -> Unit, openAbout: () -> Unit,
moreAppsFromUs: () -> Unit, moreAppsFromUs: () -> Unit,
linkColor: Int, linkColor: Color,
) { ) {
SettingsLazyScaffold(customTopBar = { scrolledColor: Color, _: MutableInteractionSource, scrollBehavior: TopAppBarScrollBehavior, statusBarColor: Int, colorTransitionFraction: Float, contrastColor: Color -> SettingsLazyScaffold(customTopBar = { scrolledColor: Color, _: MutableInteractionSource, scrollBehavior: TopAppBarScrollBehavior, statusBarColor: Int, colorTransitionFraction: Float, contrastColor: Color ->
TopAppBar( TopAppBar(
@ -77,7 +77,7 @@ internal fun MainScreen(
.padding(bottom = paddingValues.calculateBottomPadding()) .padding(bottom = paddingValues.calculateBottomPadding())
.padding(40.dp), .padding(40.dp),
update = { textView -> update = { textView ->
textView.setLinkTextColor(linkColor) textView.setLinkTextColor(linkColor.toArgb())
textView.setTextColor(textColor) textView.setTextColor(textColor)
} }
) )
@ -88,6 +88,6 @@ internal fun MainScreen(
@MyDevices @MyDevices
private fun MainScreenPreview() { private fun MainScreenPreview() {
AppThemeSurface { AppThemeSurface {
MainScreen(showMoreApps = true, openSettings = {}, openAbout = {}, moreAppsFromUs = {}, linkColor = MaterialTheme.colorScheme.onSurface.toArgb()) MainScreen(showMoreApps = true, openSettings = {}, openAbout = {}, moreAppsFromUs = {}, linkColor = MaterialTheme.colorScheme.onSurface)
} }
} }

View File

@ -2,18 +2,18 @@
#jetbrains #jetbrains
kotlin = "1.9.10" kotlin = "1.9.10"
#Simple tools #Simple tools
simple-commons = "b7dd6ad428" simple-commons = "a32b3b41a5"
#Compose #Compose
composeActivity = "1.8.0-beta01" composeActivity = "1.8.0-rc01"
compose = "1.6.0-alpha05" compose = "1.6.0-alpha06"
composeCompiler = "1.5.3" composeCompiler = "1.5.3"
composeMaterial3 = "1.2.0-alpha07" composeMaterial3 = "1.2.0-alpha08"
#Androidx #Androidx
androidx-customView = "1.2.0-alpha02" androidx-customView = "1.2.0-alpha02"
androidx-customViewPooling = "1.0.0" androidx-customViewPooling = "1.0.0"
androidx-lifecycle = "2.7.0-alpha02" androidx-lifecycle = "2.7.0-alpha02"
#Gradle #Gradle
gradlePlugins-agp = "8.1.1" gradlePlugins-agp = "8.1.2"
app-build-compileSDKVersion = "34" app-build-compileSDKVersion = "34"
app-build-targetSDK = "34" app-build-targetSDK = "34"
app-build-minimumSDK = "23" app-build-minimumSDK = "23"