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

Commit f46d0d64 authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Refactor for AppListItem"

parents 08866838 4aaca3ac
Loading
Loading
Loading
Loading
+10 −7
Original line number Diff line number Diff line
@@ -55,19 +55,22 @@ internal data class AppListState(
    val searchQuery: State<String>,
)

internal data class AppListInput<T : AppRecord>(
    val config: AppListConfig,
    val listModel: AppListModel<T>,
    val state: AppListState,
    val header: @Composable () -> Unit,
    val appItem: @Composable AppListItemModel<T>.() -> Unit,
    val bottomPadding: Dp,
)

/**
 * The template to render an App List.
 *
 * This UI element will take the remaining space on the screen to show the App List.
 */
@Composable
internal fun <T : AppRecord> AppList(
    config: AppListConfig,
    listModel: AppListModel<T>,
    state: AppListState,
    header: @Composable () -> Unit,
    appItem: @Composable (itemState: AppListItemModel<T>) -> Unit,
    bottomPadding: Dp,
internal fun <T : AppRecord> AppListInput<T>.AppList(
    appListDataSupplier: @Composable () -> State<AppListData<T>?> = {
        loadAppListData(config, listModel, state)
    },
+5 −9
Original line number Diff line number Diff line
@@ -35,16 +35,13 @@ class AppListItemModel<T : AppRecord>(
)

@Composable
fun <T : AppRecord> AppListItem(
    itemModel: AppListItemModel<T>,
    onClick: () -> Unit,
) {
fun <T : AppRecord> AppListItemModel<T>.AppListItem(onClick: () -> Unit) {
    Preference(remember {
        object : PreferenceModel {
            override val title = itemModel.label
            override val summary = itemModel.summary
            override val title = label
            override val summary = this@AppListItem.summary
            override val icon = @Composable {
                AppIcon(app = itemModel.record.app, size = SettingsDimension.appIconItemSize)
                AppIcon(app = record.app, size = SettingsDimension.appIconItemSize)
            }
            override val onClick = onClick
        }
@@ -58,7 +55,6 @@ private fun AppListItemPreview() {
        val record = object : AppRecord {
            override val app = LocalContext.current.applicationInfo
        }
        val itemModel = AppListItemModel<AppRecord>(record, "Chrome", "Allowed".toState())
        AppListItem(itemModel) {}
        AppListItemModel<AppRecord>(record, "Chrome", "Allowed".toState()).AppListItem {}
    }
}
+19 −2
Original line number Diff line number Diff line
@@ -47,7 +47,23 @@ fun <T : AppRecord> AppListPage(
    primaryUserOnly: Boolean = false,
    moreOptions: @Composable MoreOptionsScope.() -> Unit = {},
    header: @Composable () -> Unit = {},
    appItem: @Composable (itemState: AppListItemModel<T>) -> Unit,
    appItem: @Composable AppListItemModel<T>.() -> Unit,
) {
    AppListPageImpl(
        title, listModel, showInstantApps, primaryUserOnly, moreOptions, header, appItem,
    ) { it.AppList() }
}

@Composable
internal fun <T : AppRecord> AppListPageImpl(
    title: String,
    listModel: AppListModel<T>,
    showInstantApps: Boolean = false,
    primaryUserOnly: Boolean = false,
    moreOptions: @Composable MoreOptionsScope.() -> Unit = {},
    header: @Composable () -> Unit = {},
    appItem: @Composable AppListItemModel<T>.() -> Unit,
    appList: @Composable (input: AppListInput<T>) -> Unit,
) {
    val showSystem = rememberSaveable { mutableStateOf(false) }
    SearchScaffold(
@@ -64,7 +80,7 @@ fun <T : AppRecord> AppListPage(
                val options = remember { listModel.getSpinnerOptions() }
                val selectedOption = rememberSaveable { mutableStateOf(0) }
                Spinner(options, selectedOption.value) { selectedOption.value = it }
                AppList(
                val appListInput = AppListInput(
                    config = AppListConfig(
                        userId = userInfo.id,
                        showInstantApps = showInstantApps,
@@ -79,6 +95,7 @@ fun <T : AppRecord> AppListPage(
                    appItem = appItem,
                    bottomPadding = bottomPadding,
                )
                appList(appListInput)
            }
        }
    }
+4 −5
Original line number Diff line number Diff line
@@ -9,8 +9,7 @@ import com.android.settingslib.spa.widget.preference.TwoTargetSwitchPreference
import com.android.settingslib.spaprivileged.model.app.AppRecord

@Composable
fun <T : AppRecord> AppListSwitchItem(
    itemModel: AppListItemModel<T>,
fun <T : AppRecord> AppListItemModel<T>.AppListSwitchItem(
    onClick: () -> Unit,
    checked: State<Boolean?>,
    changeable: State<Boolean>,
@@ -19,14 +18,14 @@ fun <T : AppRecord> AppListSwitchItem(
    TwoTargetSwitchPreference(
        model = remember {
            object : SwitchPreferenceModel {
                override val title = itemModel.label
                override val summary = itemModel.summary
                override val title = label
                override val summary = this@AppListSwitchItem.summary
                override val checked = checked
                override val changeable = changeable
                override val onCheckedChange = onCheckedChange
            }
        },
        icon = { AppIcon(itemModel.record.app, SettingsDimension.appIconItemSize) },
        icon = { AppIcon(record.app, SettingsDimension.appIconItemSize) },
        onClick = onClick,
    )
}
+2 −3
Original line number Diff line number Diff line
@@ -91,12 +91,11 @@ internal class TogglePermissionAppListPageProvider(
        AppListPage(
            title = stringResource(listModel.pageTitleResId),
            listModel = internalListModel,
        ) { itemModel ->
        ) {
            AppListItem(
                itemModel = itemModel,
                onClick = TogglePermissionAppInfoPageProvider.navigator(
                    permissionType = permissionType,
                    app = itemModel.record.app,
                    app = record.app,
                ),
            )
        }
Loading