Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b1089de7 authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Create new widget SettingsAlertDialog

To simplify the usage and unify the UI.

Bug: 259520506
Test: Manually with Gallery
Change-Id: I6c04290d7fbda17ece8065f498d82d4685df2cf6
parent 88068450
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import com.android.settingslib.spa.framework.common.SettingsPageProviderReposito
import com.android.settingslib.spa.framework.common.SpaEnvironment
import com.android.settingslib.spa.framework.common.createSettingsPage
import com.android.settingslib.spa.gallery.button.ActionButtonPageProvider
import com.android.settingslib.spa.gallery.dialog.AlterDialogPageProvider
import com.android.settingslib.spa.gallery.home.HomePageProvider
import com.android.settingslib.spa.gallery.page.ArgumentPageProvider
import com.android.settingslib.spa.gallery.page.ChartPageProvider
@@ -72,6 +73,7 @@ class GallerySpaEnvironment(context: Context) : SpaEnvironment(context) {
                ActionButtonPageProvider,
                ProgressBarPageProvider,
                ChartPageProvider,
                AlterDialogPageProvider,
            ),
            rootPages = listOf(
                HomePageProvider.createSettingsPage(),
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.spa.gallery.dialog

import android.os.Bundle
import androidx.compose.material3.Text
import com.android.settingslib.spa.framework.common.SettingsEntry
import com.android.settingslib.spa.framework.common.SettingsEntryBuilder
import com.android.settingslib.spa.framework.common.SettingsPageProvider
import com.android.settingslib.spa.framework.common.createSettingsPage
import com.android.settingslib.spa.framework.compose.navigator
import com.android.settingslib.spa.widget.dialog.AlertDialogButton
import com.android.settingslib.spa.widget.dialog.rememberAlertDialogPresenter
import com.android.settingslib.spa.widget.preference.Preference
import com.android.settingslib.spa.widget.preference.PreferenceModel

private const val TITLE = "AlterDialogPage"

object AlterDialogPageProvider : SettingsPageProvider {
    override val name = "AlterDialogPage"
    private val owner = createSettingsPage()

    override fun buildEntry(arguments: Bundle?): List<SettingsEntry> = listOf(
        SettingsEntryBuilder.create("AlterDialog", owner).setUiLayoutFn {
            val alertDialogPresenter = rememberAlertDialogPresenter(
                confirmButton = AlertDialogButton("Ok"),
                dismissButton = AlertDialogButton("Cancel"),
                title = "Title",
                text = { Text("Text") },
            )
            Preference(object : PreferenceModel {
                override val title = "Show AlterDialog"
                override val onClick = alertDialogPresenter::open
            })
        }.build(),
    )

    fun buildInjectEntry() = SettingsEntryBuilder.createInject(owner)
        .setIsAllowSearch(true)
        .setUiLayoutFn {
            Preference(object : PreferenceModel {
                override val title = TITLE
                override val onClick = navigator(name)
            })
        }

    override fun getTitle(arguments: Bundle?) = TITLE
}
+2 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import com.android.settingslib.spa.framework.theme.SettingsTheme
import com.android.settingslib.spa.gallery.R
import com.android.settingslib.spa.gallery.SettingsPageProviderEnum
import com.android.settingslib.spa.gallery.button.ActionButtonPageProvider
import com.android.settingslib.spa.gallery.dialog.AlterDialogPageProvider
import com.android.settingslib.spa.gallery.page.ArgumentPageModel
import com.android.settingslib.spa.gallery.page.ArgumentPageProvider
import com.android.settingslib.spa.gallery.page.ChartPageProvider
@@ -58,6 +59,7 @@ object HomePageProvider : SettingsPageProvider {
            ActionButtonPageProvider.buildInjectEntry().setLink(fromPage = owner).build(),
            ProgressBarPageProvider.buildInjectEntry().setLink(fromPage = owner).build(),
            ChartPageProvider.buildInjectEntry().setLink(fromPage = owner).build(),
            AlterDialogPageProvider.buildInjectEntry().setLink(fromPage = owner).build(),
        )
    }

+5 −2
Original line number Diff line number Diff line
@@ -55,6 +55,8 @@ android {
}

dependencies {
    String jetpack_lifecycle_version = "2.6.0-alpha03"

    api "androidx.appcompat:appcompat:1.7.0-alpha01"
    api "androidx.slice:slice-builders:1.1.0-alpha02"
    api "androidx.slice:slice-core:1.1.0-alpha02"
@@ -63,8 +65,9 @@ dependencies {
    api "androidx.compose.material:material-icons-extended:$jetpack_compose_version"
    api "androidx.compose.runtime:runtime-livedata:$jetpack_compose_version"
    api "androidx.compose.ui:ui-tooling-preview:$jetpack_compose_version"
    api "androidx.lifecycle:lifecycle-livedata-ktx:2.6.0-alpha03"
    api "androidx.navigation:navigation-compose:2.6.0-alpha03"
    api "androidx.lifecycle:lifecycle-livedata-ktx:$jetpack_lifecycle_version"
    api "androidx.lifecycle:lifecycle-runtime-compose:$jetpack_lifecycle_version"
    api "androidx.navigation:navigation-compose:2.6.0-alpha04"
    api "com.github.PhilJay:MPAndroidChart:v3.1.0-alpha"
    api "com.google.android.material:material:1.7.0-alpha03"
    debugApi "androidx.compose.ui:ui-tooling:$jetpack_compose_version"
+118 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.settingslib.spa.widget.dialog

import android.content.res.Configuration
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.DialogProperties

data class AlertDialogButton(
    val text: String,
    val onClick: () -> Unit = {},
)

interface AlertDialogPresenter {
    /** Opens the dialog. */
    fun open()

    /** Closes the dialog. */
    fun close()
}

@Composable
fun rememberAlertDialogPresenter(
    confirmButton: AlertDialogButton? = null,
    dismissButton: AlertDialogButton? = null,
    title: String? = null,
    text: @Composable (() -> Unit)? = null,
): AlertDialogPresenter {
    var openDialog by rememberSaveable { mutableStateOf(false) }
    val alertDialogPresenter = remember {
        object : AlertDialogPresenter {
            override fun open() {
                openDialog = true
            }

            override fun close() {
                openDialog = false
            }
        }
    }
    if (openDialog) {
        alertDialogPresenter.SettingsAlertDialog(confirmButton, dismissButton, title, text)
    }
    return alertDialogPresenter
}

@OptIn(ExperimentalComposeUiApi::class)
@Composable
private fun AlertDialogPresenter.SettingsAlertDialog(
    confirmButton: AlertDialogButton?,
    dismissButton: AlertDialogButton?,
    title: String?,
    text: @Composable (() -> Unit)?,
) {
    val configuration = LocalConfiguration.current
    AlertDialog(
        onDismissRequest = ::close,
        modifier = when (configuration.orientation) {
            Configuration.ORIENTATION_LANDSCAPE -> {
                Modifier.width(configuration.screenWidthDp.dp * 0.6f)
            }
            else -> Modifier
        },
        confirmButton = { confirmButton?.let { Button(it) } },
        dismissButton = dismissButton?.let { { Button(it) } },
        title = title?.let { { Text(it) } },
        text = text?.let {
            {
                Column(Modifier.verticalScroll(rememberScrollState())) {
                    text()
                }
            }
        },
        properties = DialogProperties(usePlatformDefaultWidth = false),
    )
}

@Composable
private fun AlertDialogPresenter.Button(button: AlertDialogButton) {
    TextButton(
        onClick = {
            close()
            button.onClick()
        },
    ) {
        Text(button.text)
    }
}
Loading