feat: add ability to test compose's dialogs

This commit is contained in:
FunkyMuse 2023-09-20 22:32:48 +02:00
parent 65b366a388
commit c900309502
7 changed files with 175 additions and 4 deletions

View file

@ -57,6 +57,11 @@
android:exported="false"
android:label="@string/manage_blocked_numbers" />
<activity
android:name="com.simplemobiletools.commons.activities.TestDialogActivity"
android:enableOnBackInvokedCallback="true"
android:exported="false" />
<receiver
android:name=".receivers.SharedThemeReceiver"
android:enabled="true"

View file

@ -0,0 +1,64 @@
package com.simplemobiletools.commons.activities
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.simplemobiletools.commons.compose.alert_dialog.AlertDialogState
import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState
import com.simplemobiletools.commons.compose.theme.AppThemeSurface
import com.simplemobiletools.commons.dialogs.AddOrEditBlockedNumberAlertDialog
import com.simplemobiletools.commons.dialogs.AppSideLoadedAlertDialog
class TestDialogActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
AppThemeSurface {
val appSideLoadedDialogState = rememberAlertDialogState().apply {
DialogMember {
AppSideLoadedAlertDialog(onDownloadClick = {}, onCancelClick = {}, alertDialogState = this)
}
}
val addBlockedNumberDialogState = rememberAlertDialogState().apply {
DialogMember {
AddOrEditBlockedNumberAlertDialog(blockedNumber = null, deleteBlockedNumber = {}, addBlockedNumber = {}, alertDialogState = this)
}
}
Column(
Modifier
.fillMaxSize()
.padding(horizontal = 16.dp)
.verticalScroll(rememberScrollState()),
verticalArrangement = Arrangement.spacedBy(16.dp)
) {
Spacer(modifier = Modifier.padding(top = 16.dp))
ShowButton(appSideLoadedDialogState, text = "App side loaded dialog")
ShowButton(addBlockedNumberDialogState, text = "Add blocked number")
Spacer(modifier = Modifier.padding(bottom = 16.dp))
}
}
}
}
@Composable
private fun ShowButton(appSideLoadedDialogState: AlertDialogState, text: String) {
Button(onClick = appSideLoadedDialogState::show) {
Text(
text = text,
modifier = Modifier
.fillMaxWidth(),
textAlign = TextAlign.Center
)
}
}
}

View file

@ -94,7 +94,12 @@ fun stringFromHTML(source: String): Spanned {
}
@Composable
fun LinkifyText(modifier: Modifier = Modifier, fontSize: TextUnit = 14.sp, text: () -> Spanned) {
fun LinkifyText(
modifier: Modifier = Modifier,
fontSize: TextUnit = 14.sp,
removeUnderlines: Boolean = true,
text: () -> Spanned
) {
val context = LocalContext.current
val customLinkifyTextView = remember {
TextView(context)
@ -107,7 +112,9 @@ fun LinkifyText(modifier: Modifier = Modifier, fontSize: TextUnit = 14.sp, text:
textView.text = text()
textView.textSize = fontSize.value
textView.movementMethod = LinkMovementMethod.getInstance()
customLinkifyTextView.removeUnderlines()
if (removeUnderlines) {
customLinkifyTextView.removeUnderlines()
}
}
}

View file

@ -26,7 +26,7 @@ import kotlinx.coroutines.android.awaitFrame
@Composable
fun AddOrEditBlockedNumberAlertDialog(
blockedNumber: BlockedNumber?,
alertDialogState: AlertDialogState = rememberAlertDialogState(),
alertDialogState: AlertDialogState,
deleteBlockedNumber: (String) -> Unit,
addBlockedNumber: (String) -> Unit
) {
@ -99,7 +99,8 @@ private fun AddOrEditBlockedNumberAlertDialogPreview() {
AddOrEditBlockedNumberAlertDialog(
blockedNumber = null,
deleteBlockedNumber = {},
addBlockedNumber = {}
addBlockedNumber = {},
alertDialogState = rememberAlertDialogState()
)
}
}

View file

@ -4,7 +4,27 @@ import android.app.Activity
import android.text.Html
import android.text.method.LinkMovementMethod
import androidx.appcompat.app.AlertDialog
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.sp
import androidx.compose.ui.window.DialogProperties
import com.simplemobiletools.commons.R
import com.simplemobiletools.commons.compose.alert_dialog.AlertDialogState
import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState
import com.simplemobiletools.commons.compose.extensions.MyDevices
import com.simplemobiletools.commons.compose.screens.LinkifyText
import com.simplemobiletools.commons.compose.screens.stringFromHTML
import com.simplemobiletools.commons.compose.theme.AppThemeSurface
import com.simplemobiletools.commons.compose.theme.Shapes
import com.simplemobiletools.commons.databinding.DialogTextviewBinding
import com.simplemobiletools.commons.extensions.getAlertDialogBuilder
import com.simplemobiletools.commons.extensions.getStringsPackageName
@ -45,3 +65,62 @@ class AppSideloadedDialog(val activity: Activity, val callback: () -> Unit) {
callback()
}
}
@Composable
fun AppSideLoadedAlertDialog(
alertDialogState: AlertDialogState = rememberAlertDialogState(),
onDownloadClick: (url: String) -> Unit,
onCancelClick: () -> Unit
) {
val context = LocalContext.current
val url = remember { "https://play.google.com/store/apps/details?id=${context.getStringsPackageName()}" }
AlertDialog(
modifier = Modifier.fillMaxWidth(0.9f),
properties = DialogProperties(usePlatformDefaultWidth = false),
onDismissRequest = alertDialogState::hide,
confirmButton = {
TextButton(onClick = {
alertDialogState.hide()
onDownloadClick(url)
}) {
Text(text = stringResource(id = R.string.download))
}
},
dismissButton = {
TextButton(onClick = {
alertDialogState.hide()
onCancelClick()
}) {
Text(text = stringResource(id = R.string.cancel))
}
},
shape = Shapes.medium,
text = {
val source = stringResource(id = R.string.sideloaded_app, url)
LinkifyText(fontSize = 16.sp, removeUnderlines = false) {
stringFromHTML(source)
}
},
title = {
Text(
stringResource(id = R.string.app_corrupt),
color = MaterialTheme.colorScheme.primary,
fontSize = 21.sp,
fontWeight = FontWeight.Bold,
)
},
)
}
@Composable
@MyDevices
private fun AppSideLoadedAlertDialogPreview() {
AppThemeSurface {
AddOrEditBlockedNumberAlertDialog(
blockedNumber = null,
deleteBlockedNumber = {},
addBlockedNumber = {},
alertDialogState = rememberAlertDialogState()
)
}
}

View file

@ -4,6 +4,8 @@ import android.content.Intent
import android.os.Bundle
import com.simplemobiletools.commons.activities.BaseSimpleActivity
import com.simplemobiletools.commons.activities.ManageBlockedNumbersActivity
import com.simplemobiletools.commons.activities.TestDialogActivity
import com.simplemobiletools.commons.dialogs.AppSideloadedDialog
import com.simplemobiletools.commons.dialogs.BottomSheetChooserDialog
import com.simplemobiletools.commons.extensions.appLaunched
import com.simplemobiletools.commons.extensions.toast
@ -43,6 +45,9 @@ class MainActivity : BaseSimpleActivity() {
binding.manageBlockedNumbers.setOnClickListener {
startActivity(Intent(this, ManageBlockedNumbersActivity::class.java))
}
binding.composeDialogs.setOnClickListener {
startActivity(Intent(this, TestDialogActivity::class.java))
}
}
private fun launchAbout() {
@ -84,5 +89,8 @@ class MainActivity : BaseSimpleActivity() {
override fun onResume() {
super.onResume()
setupToolbar(binding.mainToolbar)
AppSideloadedDialog(this) {
}
}
}

View file

@ -35,11 +35,18 @@
android:layout_height="wrap_content"
android:text="@string/color_customization" />
<Button
android:id="@+id/compose_dialogs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Compose dialogs" />
<Button
android:id="@+id/bottom_sheet_chooser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About" />
<Button
android:id="@+id/manage_blocked_numbers"
android:layout_width="wrap_content"