From e587a90faf2375e35dcb1ae2ef7701a1c4ce76fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Fri, 6 Oct 2023 13:25:59 +0200 Subject: [PATCH 1/8] Migrate sample main activity to compose for easier testing --- gradle.properties | 1 + .../samples/activities/MainActivity.kt | 232 ++++++++++++++---- .../samples/activities/StringsAdapter.kt | 129 ---------- 3 files changed, 179 insertions(+), 183 deletions(-) delete mode 100644 samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/StringsAdapter.kt diff --git a/gradle.properties b/gradle.properties index 0daadec2a..7f9bdc81e 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,3 +16,4 @@ android.useAndroidX=true # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true org.gradle.jvmargs=-Xmx8192m +android.nonTransitiveRClass=false diff --git a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt index 2efbac1c9..27381bea7 100644 --- a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt +++ b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt @@ -2,58 +2,180 @@ package com.simplemobiletools.commons.samples.activities import android.content.Intent import android.os.Bundle -import com.simplemobiletools.commons.activities.BaseSimpleActivity +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.Info +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.material3.TopAppBar +import androidx.compose.material3.TopAppBarScrollBehavior +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import com.simplemobiletools.commons.activities.AboutActivity +import com.simplemobiletools.commons.activities.CustomizationActivity import com.simplemobiletools.commons.activities.ManageBlockedNumbersActivity +import com.simplemobiletools.commons.compose.alert_dialog.AlertDialogState +import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState import com.simplemobiletools.commons.compose.extensions.DEVELOPER_PLAY_STORE_URL import com.simplemobiletools.commons.compose.extensions.FAKE_VERSION_APP_LABEL -import com.simplemobiletools.commons.dialogs.BottomSheetChooserDialog +import com.simplemobiletools.commons.compose.extensions.appLaunchedCompose +import com.simplemobiletools.commons.compose.extensions.rateStarsRedirectAndThankYou +import com.simplemobiletools.commons.compose.menus.ActionItem +import com.simplemobiletools.commons.compose.menus.ActionMenu +import com.simplemobiletools.commons.compose.menus.OverflowMode +import com.simplemobiletools.commons.compose.settings.scaffold.SettingsLazyScaffold +import com.simplemobiletools.commons.compose.settings.scaffold.topAppBarColors +import com.simplemobiletools.commons.compose.settings.scaffold.topAppBarInsets +import com.simplemobiletools.commons.compose.settings.scaffold.topAppBarPaddings +import com.simplemobiletools.commons.compose.theme.AppThemeSurface import com.simplemobiletools.commons.dialogs.ConfirmationDialog -import com.simplemobiletools.commons.extensions.appLaunched +import com.simplemobiletools.commons.dialogs.DonateAlertDialog +import com.simplemobiletools.commons.dialogs.RateStarsAlertDialog +import com.simplemobiletools.commons.extensions.hideKeyboard +import com.simplemobiletools.commons.extensions.launchMoreAppsFromUsIntent import com.simplemobiletools.commons.extensions.launchViewIntent -import com.simplemobiletools.commons.extensions.toast -import com.simplemobiletools.commons.extensions.viewBinding -import com.simplemobiletools.commons.helpers.LICENSE_AUTOFITTEXTVIEW +import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.FAQItem -import com.simplemobiletools.commons.models.SimpleListItem import com.simplemobiletools.commons.samples.BuildConfig import com.simplemobiletools.commons.samples.R -import com.simplemobiletools.commons.samples.databinding.ActivityMainBinding - -class MainActivity : BaseSimpleActivity() { - override fun getAppLauncherName() = getString(R.string.smtco_app_name) - - override fun getAppIconIDs(): ArrayList { - val ids = ArrayList() - ids.add(R.mipmap.commons_launcher) - return ids - } - - private val binding by viewBinding(ActivityMainBinding::inflate) +import kotlinx.collections.immutable.toImmutableList +class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(binding.root) - appLaunched(BuildConfig.APPLICATION_ID) - updateMaterialActivityViews(binding.mainCoordinator, binding.mainHolder, useTransparentNavigation = true, useTopSearchMenu = false) - setupMaterialScrollListener(binding.mainNestedScrollview, binding.mainToolbar) + setContent { + AppThemeSurface { + SettingsLazyScaffold( + customTopBar = { scrolledColor: Color, _: MutableInteractionSource, scrollBehavior: TopAppBarScrollBehavior, statusBarColor: Int, colorTransitionFraction: Float, contrastColor: Color -> + TopAppBar( + title = {}, + actions = { + val actionMenus = remember { + val about = ActionItem( + R.string.about, + icon = Icons.Outlined.Info, + doAction = ::launchAbout, + overflowMode = OverflowMode.NEVER_OVERFLOW + ) + val moreApps = + ActionItem( + R.string.more_apps_from_us, + doAction = ::launchMoreAppsFromUsIntent, + overflowMode = OverflowMode.ALWAYS_OVERFLOW + ) + listOf(about, moreApps).toImmutableList() + } + var isMenuVisible by remember { mutableStateOf(false) } + ActionMenu( + items = actionMenus, + numIcons = 2, + isMenuVisible = isMenuVisible, + onMenuToggle = { isMenuVisible = it }, + iconsColor = scrolledColor + ) + }, + scrollBehavior = scrollBehavior, + colors = topAppBarColors(statusBarColor, colorTransitionFraction, contrastColor), + modifier = Modifier.topAppBarPaddings(), + windowInsets = topAppBarInsets() + ) + } + ) { + Column( + modifier = Modifier.fillMaxSize(), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + Button( + onClick = ::startCustomizationActivity + ) { + Text(stringResource(id = R.string.color_customization)) + } + Button( + onClick = ::launchAbout + ) { + Text("About") + } + Button( + onClick = { + startActivity(Intent(this@MainActivity, ManageBlockedNumbersActivity::class.java)) + } + ) { + Text("Manage blocked numbers") + } + Button( + onClick = { + startActivity(Intent(this@MainActivity, TestDialogActivity::class.java)) + } + ) { + Text("Compose dialogs") + } + Button( + onClick = { + ConfirmationDialog( + this@MainActivity, + FAKE_VERSION_APP_LABEL, + positive = com.simplemobiletools.commons.R.string.ok, + negative = 0 + ) { + launchViewIntent(DEVELOPER_PLAY_STORE_URL) + } + } + ) { + Text("Test button") + } + } + } - binding.mainColorCustomization.setOnClickListener { - startCustomizationActivity() + AppLaunched() + } } - binding.bottomSheetChooser.setOnClickListener { - launchAbout() + } + + @Composable + private fun AppLaunched( + donateAlertDialogState: AlertDialogState = getDonateAlertDialogState(), + rateStarsAlertDialogState: AlertDialogState = getRateStarsAlertDialogState(), + ) { + LaunchedEffect(Unit) { + appLaunchedCompose( + appId = BuildConfig.APPLICATION_ID, + showDonateDialog = donateAlertDialogState::show, + showRateUsDialog = rateStarsAlertDialogState::show, + showUpgradeDialog = {} + ) } - binding.manageBlockedNumbers.setOnClickListener { - startActivity(Intent(this, ManageBlockedNumbersActivity::class.java)) + } + + @Composable + private fun getDonateAlertDialogState() = + rememberAlertDialogState().apply { + DialogMember { + DonateAlertDialog(alertDialogState = this) + } } - binding.composeDialogs.setOnClickListener { - startActivity(Intent(this, TestDialogActivity::class.java)) + + @Composable + private fun getRateStarsAlertDialogState() = rememberAlertDialogState().apply { + DialogMember { + RateStarsAlertDialog(alertDialogState = this, onRating = ::rateStarsRedirectAndThankYou) } - binding.testButton.setOnClickListener { - ConfirmationDialog(this, FAKE_VERSION_APP_LABEL, positive = com.simplemobiletools.commons.R.string.ok, negative = 0) { - launchViewIntent(DEVELOPER_PLAY_STORE_URL) - } + } + + private fun startCustomizationActivity() { + Intent(applicationContext, CustomizationActivity::class.java).apply { + putExtra(APP_ICON_IDS, getAppIconIDs()) + putExtra(APP_LAUNCHER_NAME, getAppLauncherName()) + startActivity(this) } } @@ -74,27 +196,29 @@ class MainActivity : BaseSimpleActivity() { startAboutActivity(R.string.smtco_app_name, licenses, BuildConfig.VERSION_NAME, faqItems, true) } - private fun launchBottomSheetDemo() { - BottomSheetChooserDialog.createChooser( - fragmentManager = supportFragmentManager, - title = com.simplemobiletools.commons.R.string.please_select_destination, - items = arrayOf( - SimpleListItem(1, com.simplemobiletools.commons.R.string.record_video, com.simplemobiletools.commons.R.drawable.ic_camera_vector), - SimpleListItem( - 2, - com.simplemobiletools.commons.R.string.record_audio, - com.simplemobiletools.commons.R.drawable.ic_microphone_vector, - selected = true - ), - SimpleListItem(4, com.simplemobiletools.commons.R.string.choose_contact, com.simplemobiletools.commons.R.drawable.ic_add_person_vector) - ) - ) { - toast("Clicked ${it.id}") + private fun startAboutActivity( + appNameId: Int, licenseMask: Long, versionName: String, faqItems: ArrayList, showFAQBeforeMail: Boolean, + getAppIconIDs: ArrayList = getAppIconIDs(), + getAppLauncherName: String = getAppLauncherName() + ) { + hideKeyboard() + Intent(applicationContext, AboutActivity::class.java).apply { + putExtra(APP_ICON_IDS, getAppIconIDs) + putExtra(APP_LAUNCHER_NAME, getAppLauncherName) + putExtra(APP_NAME, getString(appNameId)) + putExtra(APP_LICENSES, licenseMask) + putExtra(APP_VERSION_NAME, versionName) + putExtra(APP_FAQ, faqItems) + putExtra(SHOW_FAQ_BEFORE_MAIL, showFAQBeforeMail) + startActivity(this) } } - override fun onResume() { - super.onResume() - setupToolbar(binding.mainToolbar) + private fun getAppLauncherName() = getString(R.string.smtco_app_name) + + private fun getAppIconIDs(): ArrayList { + val ids = ArrayList() + ids.add(R.mipmap.commons_launcher) + return ids } } diff --git a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/StringsAdapter.kt b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/StringsAdapter.kt deleted file mode 100644 index 3a2d040ad..000000000 --- a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/StringsAdapter.kt +++ /dev/null @@ -1,129 +0,0 @@ -package com.simplemobiletools.commons.samples.activities - -import android.annotation.SuppressLint -import android.view.* -import androidx.recyclerview.widget.ItemTouchHelper -import androidx.recyclerview.widget.RecyclerView -import androidx.swiperefreshlayout.widget.SwipeRefreshLayout -import com.simplemobiletools.commons.activities.BaseSimpleActivity -import com.simplemobiletools.commons.adapters.MyRecyclerViewAdapter -import com.simplemobiletools.commons.extensions.beVisibleIf -import com.simplemobiletools.commons.interfaces.ItemMoveCallback -import com.simplemobiletools.commons.interfaces.ItemTouchHelperContract -import com.simplemobiletools.commons.interfaces.StartReorderDragListener -import com.simplemobiletools.commons.samples.databinding.ListItemBinding -import com.simplemobiletools.commons.views.MyRecyclerView -import java.util.* - -class StringsAdapter( - activity: BaseSimpleActivity, var strings: MutableList, recyclerView: MyRecyclerView, val swipeRefreshLayout: SwipeRefreshLayout, - itemClick: (Any) -> Unit -) : MyRecyclerViewAdapter(activity, recyclerView, itemClick), ItemTouchHelperContract { - - private var isChangingOrder = false - private var startReorderDragListener: StartReorderDragListener - - init { - setupDragListener(true) - - val touchHelper = ItemTouchHelper(ItemMoveCallback(this, true)) - touchHelper.attachToRecyclerView(recyclerView) - - startReorderDragListener = object : StartReorderDragListener { - override fun requestDrag(viewHolder: RecyclerView.ViewHolder) { - touchHelper.startDrag(viewHolder) - } - } - } - - override fun getActionMenuId() = com.simplemobiletools.commons.R.menu.cab_delete_only - - override fun prepareActionMode(menu: Menu) {} - - override fun actionItemPressed(id: Int) { - if (selectedKeys.isEmpty()) { - return - } - - when (id) { - com.simplemobiletools.commons.R.id.cab_delete -> changeOrder() - } - } - - override fun getSelectableItemCount() = strings.size - - override fun getIsItemSelectable(position: Int) = true - - override fun getItemSelectionKey(position: Int) = strings.getOrNull(position)?.hashCode() - - override fun getItemKeyPosition(key: Int) = strings.indexOfFirst { it.hashCode() == key } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) = ListItemBinding.inflate(LayoutInflater.from(parent.context), parent, false).run { ViewHolder(root) } - - override fun onActionModeCreated() {} - - override fun onActionModeDestroyed() { - if (isChangingOrder) { - notifyDataSetChanged() - } - - isChangingOrder = false - } - - override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { - val item = strings[position] - holder.bindView(item, true, true) { itemView, layoutPosition -> - setupView(ListItemBinding.bind(itemView), item, holder) - } - bindViewHolder(holder) - } - - override fun getItemCount() = strings.size - - private fun changeOrder() { - isChangingOrder = true - notifyDataSetChanged() - } - - @SuppressLint("ClickableViewAccessibility") - private fun setupView(view: ListItemBinding, string: String, holder: ViewHolder) { - val isSelected = selectedKeys.contains(string.hashCode()) - view.apply { - itemFrame.isSelected = isSelected - itemName.text = string - - dragHandle.beVisibleIf(isChangingOrder) - - if (isChangingOrder) { - dragHandle.setOnTouchListener { v, event -> - if (event.action == MotionEvent.ACTION_DOWN) { - startReorderDragListener.requestDrag(holder) - } - false - } - } - } - } - - override fun onRowMoved(fromPosition: Int, toPosition: Int) { - if (fromPosition < toPosition) { - for (i in fromPosition until toPosition) { - Collections.swap(strings, i, i + 1) - } - } else { - for (i in fromPosition downTo toPosition + 1) { - Collections.swap(strings, i, i - 1) - } - } - - notifyItemMoved(fromPosition, toPosition) - } - - override fun onRowSelected(myViewHolder: ViewHolder?) { - swipeRefreshLayout.isEnabled = false - } - - override fun onRowClear(myViewHolder: ViewHolder?) { - swipeRefreshLayout.isEnabled = true - } -} From 50af9b98b4ca00e756dbb1d98f07ddffc7458a08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 9 Oct 2023 09:04:43 +0200 Subject: [PATCH 2/8] Fix colors for dropdown menus --- .../components/SimpleDropDownMenuItem.kt | 25 +++++++++++-------- .../commons/compose/menus/ActionMenu.kt | 5 ++++ 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/compose/components/SimpleDropDownMenuItem.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/compose/components/SimpleDropDownMenuItem.kt index abe9cc68b..c8617da63 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/compose/components/SimpleDropDownMenuItem.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/compose/components/SimpleDropDownMenuItem.kt @@ -17,6 +17,7 @@ import androidx.compose.ui.unit.dp import com.simplemobiletools.commons.compose.extensions.MyDevices import com.simplemobiletools.commons.compose.extensions.rememberMutableInteractionSource import com.simplemobiletools.commons.compose.theme.AppThemeSurface +import com.simplemobiletools.commons.compose.theme.SimpleTheme private val dropDownPaddings = Modifier.padding(horizontal = 14.dp, vertical = 16.dp) @@ -37,16 +38,20 @@ fun SimpleDropDownMenuItem( indication: Indication? = LocalIndication.current, onClick: () -> Unit ) = - Box(modifier = modifier - .fillMaxWidth() - .clickable(interactionSource = interactionSource, indication = indication, onClick = onClick) - .then(dropDownPaddings)) { - Text( - text = text, - modifier = Modifier - .fillMaxWidth() - ) - } + SimpleDropDownMenuItem( + modifier = modifier, + interactionSource = interactionSource, + indication = indication, + onClick = onClick, + text = { + Text( + text = text, + modifier = Modifier + .fillMaxWidth(), + color = SimpleTheme.colorScheme.onSurface + ) + } + ) @Composable fun SimpleDropDownMenuItem( diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/compose/menus/ActionMenu.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/compose/menus/ActionMenu.kt index c4077e097..bc33a4486 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/compose/menus/ActionMenu.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/compose/menus/ActionMenu.kt @@ -1,6 +1,7 @@ package com.simplemobiletools.commons.compose.menus import androidx.annotation.StringRes +import androidx.compose.foundation.background import androidx.compose.foundation.combinedClickable import androidx.compose.foundation.interaction.MutableInteractionSource import androidx.compose.foundation.layout.* @@ -30,6 +31,7 @@ import com.simplemobiletools.commons.compose.extensions.rememberMutableInteracti import com.simplemobiletools.commons.compose.theme.AppThemeSurface import com.simplemobiletools.commons.compose.theme.Shapes import com.simplemobiletools.commons.compose.theme.SimpleTheme +import com.simplemobiletools.commons.dialogs.dialogBorder import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList @@ -140,6 +142,9 @@ fun ActionMenu( } } DropdownMenu( + modifier = Modifier + .background(SimpleTheme.colorScheme.surface) + .dialogBorder, expanded = isMenuVisible, onDismissRequest = { onMenuToggle(false) }, ) { From b6027442d09b3da549081f2698caaa3b3842474a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 9 Oct 2023 11:58:47 +0200 Subject: [PATCH 3/8] Leave `android.nonTransitiveRClass` at default value --- gradle.properties | 1 - .../commons/samples/activities/MainActivity.kt | 6 +++--- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index 7f9bdc81e..0daadec2a 100644 --- a/gradle.properties +++ b/gradle.properties @@ -16,4 +16,3 @@ android.useAndroidX=true # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects # org.gradle.parallel=true org.gradle.jvmargs=-Xmx8192m -android.nonTransitiveRClass=false diff --git a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt index 27381bea7..5abac52ca 100644 --- a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt +++ b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt @@ -61,14 +61,14 @@ class MainActivity : ComponentActivity() { actions = { val actionMenus = remember { val about = ActionItem( - R.string.about, + com.simplemobiletools.commons.R.string.about, icon = Icons.Outlined.Info, doAction = ::launchAbout, overflowMode = OverflowMode.NEVER_OVERFLOW ) val moreApps = ActionItem( - R.string.more_apps_from_us, + com.simplemobiletools.commons.R.string.more_apps_from_us, doAction = ::launchMoreAppsFromUsIntent, overflowMode = OverflowMode.ALWAYS_OVERFLOW ) @@ -98,7 +98,7 @@ class MainActivity : ComponentActivity() { Button( onClick = ::startCustomizationActivity ) { - Text(stringResource(id = R.string.color_customization)) + Text(stringResource(id = com.simplemobiletools.commons.R.string.color_customization)) } Button( onClick = ::launchAbout From d8fae0db2dc2572b3cb25ca9a58b149ce6cb3073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 9 Oct 2023 11:59:40 +0200 Subject: [PATCH 4/8] Make `MainActivity` go edge to edge --- .../commons/samples/activities/MainActivity.kt | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt index 5abac52ca..2e9669167 100644 --- a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt +++ b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt @@ -24,10 +24,7 @@ import com.simplemobiletools.commons.activities.CustomizationActivity import com.simplemobiletools.commons.activities.ManageBlockedNumbersActivity import com.simplemobiletools.commons.compose.alert_dialog.AlertDialogState import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState -import com.simplemobiletools.commons.compose.extensions.DEVELOPER_PLAY_STORE_URL -import com.simplemobiletools.commons.compose.extensions.FAKE_VERSION_APP_LABEL -import com.simplemobiletools.commons.compose.extensions.appLaunchedCompose -import com.simplemobiletools.commons.compose.extensions.rateStarsRedirectAndThankYou +import com.simplemobiletools.commons.compose.extensions.* import com.simplemobiletools.commons.compose.menus.ActionItem import com.simplemobiletools.commons.compose.menus.ActionMenu import com.simplemobiletools.commons.compose.menus.OverflowMode @@ -52,6 +49,7 @@ class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + enableEdgeToEdgeSimple() setContent { AppThemeSurface { SettingsLazyScaffold( From 7f1f873e3f169576171d1f160822e2a1c6478b53 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 9 Oct 2023 12:07:39 +0200 Subject: [PATCH 5/8] Update sample with latest commons changes --- .../commons/samples/activities/MainActivity.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt index 2e9669167..f89829db6 100644 --- a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt +++ b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt @@ -25,13 +25,13 @@ import com.simplemobiletools.commons.activities.ManageBlockedNumbersActivity import com.simplemobiletools.commons.compose.alert_dialog.AlertDialogState import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState import com.simplemobiletools.commons.compose.extensions.* +import com.simplemobiletools.commons.compose.lists.SimpleLazyListScaffold +import com.simplemobiletools.commons.compose.lists.simpleTopAppBarColors +import com.simplemobiletools.commons.compose.lists.topAppBarInsets +import com.simplemobiletools.commons.compose.lists.topAppBarPaddings import com.simplemobiletools.commons.compose.menus.ActionItem import com.simplemobiletools.commons.compose.menus.ActionMenu import com.simplemobiletools.commons.compose.menus.OverflowMode -import com.simplemobiletools.commons.compose.settings.scaffold.SettingsLazyScaffold -import com.simplemobiletools.commons.compose.settings.scaffold.topAppBarColors -import com.simplemobiletools.commons.compose.settings.scaffold.topAppBarInsets -import com.simplemobiletools.commons.compose.settings.scaffold.topAppBarPaddings import com.simplemobiletools.commons.compose.theme.AppThemeSurface import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.DonateAlertDialog @@ -52,7 +52,7 @@ class MainActivity : ComponentActivity() { enableEdgeToEdgeSimple() setContent { AppThemeSurface { - SettingsLazyScaffold( + SimpleLazyListScaffold( customTopBar = { scrolledColor: Color, _: MutableInteractionSource, scrollBehavior: TopAppBarScrollBehavior, statusBarColor: Int, colorTransitionFraction: Float, contrastColor: Color -> TopAppBar( title = {}, @@ -82,7 +82,7 @@ class MainActivity : ComponentActivity() { ) }, scrollBehavior = scrollBehavior, - colors = topAppBarColors(statusBarColor, colorTransitionFraction, contrastColor), + colors = simpleTopAppBarColors(statusBarColor, colorTransitionFraction, contrastColor), modifier = Modifier.topAppBarPaddings(), windowInsets = topAppBarInsets() ) From c08e09c2ee0d5bcbca0ef781fcbd8de1d0c2d4ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 9 Oct 2023 12:23:38 +0200 Subject: [PATCH 6/8] Extract compose screen from `MainActivity` into `MainScreen` --- .../samples/activities/MainActivity.kt | 130 ++++------------ .../commons/samples/screens/MainScreen.kt | 139 ++++++++++++++++++ 2 files changed, 165 insertions(+), 104 deletions(-) create mode 100644 samples/src/main/kotlin/com/simplemobiletools/commons/samples/screens/MainScreen.kt diff --git a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt index f89829db6..a2e8de071 100644 --- a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt +++ b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/activities/MainActivity.kt @@ -4,34 +4,14 @@ import android.content.Intent import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent -import androidx.compose.foundation.interaction.MutableInteractionSource -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.material.icons.Icons -import androidx.compose.material.icons.outlined.Info -import androidx.compose.material3.Button -import androidx.compose.material3.Text -import androidx.compose.material3.TopAppBar -import androidx.compose.material3.TopAppBarScrollBehavior -import androidx.compose.runtime.* -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.graphics.Color -import androidx.compose.ui.res.stringResource +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect import com.simplemobiletools.commons.activities.AboutActivity import com.simplemobiletools.commons.activities.CustomizationActivity import com.simplemobiletools.commons.activities.ManageBlockedNumbersActivity import com.simplemobiletools.commons.compose.alert_dialog.AlertDialogState import com.simplemobiletools.commons.compose.alert_dialog.rememberAlertDialogState import com.simplemobiletools.commons.compose.extensions.* -import com.simplemobiletools.commons.compose.lists.SimpleLazyListScaffold -import com.simplemobiletools.commons.compose.lists.simpleTopAppBarColors -import com.simplemobiletools.commons.compose.lists.topAppBarInsets -import com.simplemobiletools.commons.compose.lists.topAppBarPaddings -import com.simplemobiletools.commons.compose.menus.ActionItem -import com.simplemobiletools.commons.compose.menus.ActionMenu -import com.simplemobiletools.commons.compose.menus.OverflowMode import com.simplemobiletools.commons.compose.theme.AppThemeSurface import com.simplemobiletools.commons.dialogs.ConfirmationDialog import com.simplemobiletools.commons.dialogs.DonateAlertDialog @@ -43,7 +23,7 @@ import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.models.FAQItem import com.simplemobiletools.commons.samples.BuildConfig import com.simplemobiletools.commons.samples.R -import kotlinx.collections.immutable.toImmutableList +import com.simplemobiletools.commons.samples.screens.MainScreen class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { @@ -52,88 +32,30 @@ class MainActivity : ComponentActivity() { enableEdgeToEdgeSimple() setContent { AppThemeSurface { - SimpleLazyListScaffold( - customTopBar = { scrolledColor: Color, _: MutableInteractionSource, scrollBehavior: TopAppBarScrollBehavior, statusBarColor: Int, colorTransitionFraction: Float, contrastColor: Color -> - TopAppBar( - title = {}, - actions = { - val actionMenus = remember { - val about = ActionItem( - com.simplemobiletools.commons.R.string.about, - icon = Icons.Outlined.Info, - doAction = ::launchAbout, - overflowMode = OverflowMode.NEVER_OVERFLOW - ) - val moreApps = - ActionItem( - com.simplemobiletools.commons.R.string.more_apps_from_us, - doAction = ::launchMoreAppsFromUsIntent, - overflowMode = OverflowMode.ALWAYS_OVERFLOW - ) - listOf(about, moreApps).toImmutableList() - } - var isMenuVisible by remember { mutableStateOf(false) } - ActionMenu( - items = actionMenus, - numIcons = 2, - isMenuVisible = isMenuVisible, - onMenuToggle = { isMenuVisible = it }, - iconsColor = scrolledColor - ) - }, - scrollBehavior = scrollBehavior, - colors = simpleTopAppBarColors(statusBarColor, colorTransitionFraction, contrastColor), - modifier = Modifier.topAppBarPaddings(), - windowInsets = topAppBarInsets() - ) - } - ) { - Column( - modifier = Modifier.fillMaxSize(), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.Center - ) { - Button( - onClick = ::startCustomizationActivity - ) { - Text(stringResource(id = com.simplemobiletools.commons.R.string.color_customization)) - } - Button( - onClick = ::launchAbout - ) { - Text("About") - } - Button( - onClick = { - startActivity(Intent(this@MainActivity, ManageBlockedNumbersActivity::class.java)) - } - ) { - Text("Manage blocked numbers") - } - Button( - onClick = { - startActivity(Intent(this@MainActivity, TestDialogActivity::class.java)) - } - ) { - Text("Compose dialogs") - } - Button( - onClick = { - ConfirmationDialog( - this@MainActivity, - FAKE_VERSION_APP_LABEL, - positive = com.simplemobiletools.commons.R.string.ok, - negative = 0 - ) { - launchViewIntent(DEVELOPER_PLAY_STORE_URL) - } - } - ) { - Text("Test button") - } - } - } + val showMoreApps = onEventValue { !resources.getBoolean(com.simplemobiletools.commons.R.bool.hide_google_relations) } + MainScreen( + openColorCustomization = ::startCustomizationActivity, + manageBlockedNumbers = { + startActivity(Intent(this@MainActivity, ManageBlockedNumbersActivity::class.java)) + }, + showComposeDialogs = { + startActivity(Intent(this@MainActivity, TestDialogActivity::class.java)) + }, + openTestButton = { + ConfirmationDialog( + this@MainActivity, + FAKE_VERSION_APP_LABEL, + positive = com.simplemobiletools.commons.R.string.ok, + negative = 0 + ) { + launchViewIntent(DEVELOPER_PLAY_STORE_URL) + } + }, + showMoreApps = showMoreApps, + openAbout = ::launchAbout, + moreAppsFromUs = ::launchMoreAppsFromUsIntent + ) AppLaunched() } } diff --git a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/screens/MainScreen.kt b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/screens/MainScreen.kt new file mode 100644 index 000000000..ada444188 --- /dev/null +++ b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/screens/MainScreen.kt @@ -0,0 +1,139 @@ +package com.simplemobiletools.commons.samples.screens + +import androidx.compose.foundation.interaction.MutableInteractionSource +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.outlined.Info +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.material3.TopAppBar +import androidx.compose.material3.TopAppBarScrollBehavior +import androidx.compose.runtime.* +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.res.stringResource +import com.simplemobiletools.commons.R +import com.simplemobiletools.commons.compose.extensions.MyDevices +import com.simplemobiletools.commons.compose.lists.SimpleLazyListScaffold +import com.simplemobiletools.commons.compose.lists.simpleTopAppBarColors +import com.simplemobiletools.commons.compose.lists.topAppBarInsets +import com.simplemobiletools.commons.compose.lists.topAppBarPaddings +import com.simplemobiletools.commons.compose.menus.ActionItem +import com.simplemobiletools.commons.compose.menus.ActionMenu +import com.simplemobiletools.commons.compose.menus.OverflowMode +import com.simplemobiletools.commons.compose.theme.AppThemeSurface +import kotlinx.collections.immutable.ImmutableList +import kotlinx.collections.immutable.toImmutableList + +@Composable +fun MainScreen( + openColorCustomization: () -> Unit, + manageBlockedNumbers: () -> Unit, + showComposeDialogs: () -> Unit, + openTestButton: () -> Unit, + showMoreApps: Boolean, + openAbout: () -> Unit, + moreAppsFromUs: () -> Unit, +) { + SimpleLazyListScaffold( + customTopBar = { scrolledColor: Color, _: MutableInteractionSource, scrollBehavior: TopAppBarScrollBehavior, statusBarColor: Int, colorTransitionFraction: Float, contrastColor: Color -> + TopAppBar( + title = {}, + actions = { + val actionMenus = remember { + buildActionMenuItems( + showMoreApps = showMoreApps, + openAbout = openAbout, + moreAppsFromUs = moreAppsFromUs + ) + } + var isMenuVisible by remember { mutableStateOf(false) } + ActionMenu( + items = actionMenus, + numIcons = 2, + isMenuVisible = isMenuVisible, + onMenuToggle = { isMenuVisible = it }, + iconsColor = scrolledColor + ) + }, + scrollBehavior = scrollBehavior, + colors = simpleTopAppBarColors(statusBarColor, colorTransitionFraction, contrastColor), + modifier = Modifier.topAppBarPaddings(), + windowInsets = topAppBarInsets() + ) + } + ) { + Column( + modifier = Modifier.fillMaxSize(), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center + ) { + Button( + onClick = openColorCustomization + ) { + Text(stringResource(id = R.string.color_customization)) + } + Button( + onClick = openAbout + ) { + Text("About") + } + Button( + onClick = manageBlockedNumbers + ) { + Text("Manage blocked numbers") + } + Button( + onClick = showComposeDialogs + ) { + Text("Compose dialogs") + } + Button( + onClick = openTestButton + ) { + Text("Test button") + } + } + } +} + +private fun buildActionMenuItems( + showMoreApps: Boolean, + openAbout: () -> Unit, + moreAppsFromUs: () -> Unit +): ImmutableList { + val list = mutableListOf() + list += ActionItem( + R.string.about, + icon = Icons.Outlined.Info, + doAction = openAbout, + overflowMode = OverflowMode.NEVER_OVERFLOW + ) + if (showMoreApps) { + list += ActionItem( + R.string.more_apps_from_us, + doAction = moreAppsFromUs, + overflowMode = OverflowMode.ALWAYS_OVERFLOW + ) + } + return list.toImmutableList() +} + +@Composable +@MyDevices +private fun MainScreenPreview() { + AppThemeSurface { + MainScreen( + openColorCustomization = {}, + manageBlockedNumbers = {}, + showComposeDialogs = {}, + openTestButton = {}, + showMoreApps = true, + openAbout = {}, + moreAppsFromUs = {} + ) + } +} From 48f2a0e96aa14ad1b6aa5a2dc53705ae61a3fd2f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Mon, 9 Oct 2023 16:27:31 +0200 Subject: [PATCH 7/8] Update `SimpleLazyListScaffold` to `SimpleScaffold` --- .../simplemobiletools/commons/samples/screens/MainScreen.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/screens/MainScreen.kt b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/screens/MainScreen.kt index ada444188..04576fa30 100644 --- a/samples/src/main/kotlin/com/simplemobiletools/commons/samples/screens/MainScreen.kt +++ b/samples/src/main/kotlin/com/simplemobiletools/commons/samples/screens/MainScreen.kt @@ -17,7 +17,7 @@ import androidx.compose.ui.graphics.Color import androidx.compose.ui.res.stringResource import com.simplemobiletools.commons.R import com.simplemobiletools.commons.compose.extensions.MyDevices -import com.simplemobiletools.commons.compose.lists.SimpleLazyListScaffold +import com.simplemobiletools.commons.compose.lists.SimpleScaffold import com.simplemobiletools.commons.compose.lists.simpleTopAppBarColors import com.simplemobiletools.commons.compose.lists.topAppBarInsets import com.simplemobiletools.commons.compose.lists.topAppBarPaddings @@ -38,7 +38,7 @@ fun MainScreen( openAbout: () -> Unit, moreAppsFromUs: () -> Unit, ) { - SimpleLazyListScaffold( + SimpleScaffold( customTopBar = { scrolledColor: Color, _: MutableInteractionSource, scrollBehavior: TopAppBarScrollBehavior, statusBarColor: Int, colorTransitionFraction: Float, contrastColor: Color -> TopAppBar( title = {}, From 6304837242c22f8d41f4838ac354c12f4a84eae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ensar=20Saraj=C4=8Di=C4=87?= Date: Wed, 11 Oct 2023 12:46:21 +0200 Subject: [PATCH 8/8] Use `dialogContainerColor` instead of specific color for `ActionMenu` --- .../com/simplemobiletools/commons/compose/menus/ActionMenu.kt | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/commons/src/main/kotlin/com/simplemobiletools/commons/compose/menus/ActionMenu.kt b/commons/src/main/kotlin/com/simplemobiletools/commons/compose/menus/ActionMenu.kt index f7833b427..a5ffae54a 100644 --- a/commons/src/main/kotlin/com/simplemobiletools/commons/compose/menus/ActionMenu.kt +++ b/commons/src/main/kotlin/com/simplemobiletools/commons/compose/menus/ActionMenu.kt @@ -32,6 +32,7 @@ import com.simplemobiletools.commons.compose.theme.AppThemeSurface import com.simplemobiletools.commons.compose.theme.Shapes import com.simplemobiletools.commons.compose.theme.SimpleTheme import com.simplemobiletools.commons.dialogs.dialogBorder +import com.simplemobiletools.commons.dialogs.dialogContainerColor import kotlinx.collections.immutable.ImmutableList import kotlinx.collections.immutable.toImmutableList @@ -143,7 +144,7 @@ fun ActionMenu( } DropdownMenu( modifier = Modifier - .background(SimpleTheme.colorScheme.surface) + .background(dialogContainerColor) .dialogBorder, expanded = isMenuVisible, onDismissRequest = { onMenuToggle(false) },