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

Commit 5a1c436f authored by Chaohui Wang's avatar Chaohui Wang
Browse files

Move AppItem into AppListModel

For better unit testings.

The AppItem used to be write with AppListPage, which is a little bit
hard to test, by moving it into AppListModel, AppItem could be tested as
other methods in AppListModel.

Also add more test cases for TogglePermissionAppInfoPage and
TogglePermissionAppListPage.

Bug: 260660819
Test: Unit test
Test: Manually with Settings
Change-Id: I646add1434d435ca3a8dbf7b34f39a705bae8754
parent 61944640
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -4,6 +4,8 @@ import android.content.pm.ApplicationInfo
import android.icu.text.CollationKey
import androidx.compose.runtime.Composable
import androidx.compose.runtime.State
import com.android.settingslib.spaprivileged.template.app.AppListItem
import com.android.settingslib.spaprivileged.template.app.AppListItemModel
import kotlinx.coroutines.flow.Flow

data class AppEntry<T : AppRecord>(
@@ -69,4 +71,9 @@ interface AppListModel<T : AppRecord> {
     */
    @Composable
    fun getSummary(option: Int, record: T): State<String>? = null

    @Composable
    fun AppListItemModel<T>.AppItem() {
        AppListItem {}
    }
}
+6 −9
Original line number Diff line number Diff line
@@ -60,7 +60,6 @@ data class AppListInput<T : AppRecord>(
    val listModel: AppListModel<T>,
    val state: AppListState,
    val header: @Composable () -> Unit,
    val appItem: @Composable AppListItemModel<T>.() -> Unit,
    val bottomPadding: Dp,
)

@@ -80,15 +79,13 @@ internal fun <T : AppRecord> AppListInput<T>.AppListImpl(
) {
    LogCompositions(TAG, config.userId.toString())
    val appListData = appListDataSupplier()
    AppListWidget(appListData, listModel, header, appItem, bottomPadding)
    listModel.AppListWidget(appListData, header, bottomPadding)
}

@Composable
private fun <T : AppRecord> AppListWidget(
private fun <T : AppRecord> AppListModel<T>.AppListWidget(
    appListData: State<AppListData<T>?>,
    listModel: AppListModel<T>,
    header: @Composable () -> Unit,
    appItem: @Composable (itemState: AppListItemModel<T>) -> Unit,
    bottomPadding: Dp,
) {
    val timeMeasurer = rememberTimeMeasurer(TAG)
@@ -108,14 +105,14 @@ private fun <T : AppRecord> AppListWidget(
            }

            items(count = list.size, key = { option to list[it].record.app.packageName }) {
                remember(list) { listModel.getGroupTitleIfFirst(option, list, it) }
                remember(list) { getGroupTitleIfFirst(option, list, it) }
                    ?.let { group -> CategoryTitle(title = group) }

                val appEntry = list[it]
                val summary = listModel.getSummary(option, appEntry.record) ?: "".toState()
                appItem(remember(appEntry) {
                val summary = getSummary(option, appEntry.record) ?: "".toState()
                remember(appEntry) {
                    AppListItemModel(appEntry.record, appEntry.label, summary)
                })
                }.AppItem()
            }
        }
    }
+0 −2
Original line number Diff line number Diff line
@@ -48,7 +48,6 @@ fun <T : AppRecord> AppListPage(
    moreOptions: @Composable MoreOptionsScope.() -> Unit = {},
    header: @Composable () -> Unit = {},
    appList: @Composable AppListInput<T>.() -> Unit = { AppList() },
    appItem: @Composable AppListItemModel<T>.() -> Unit,
) {
    val showSystem = rememberSaveable { mutableStateOf(false) }
    SearchScaffold(
@@ -77,7 +76,6 @@ fun <T : AppRecord> AppListPage(
                        searchQuery = searchQuery,
                    ),
                    header = header,
                    appItem = appItem,
                    bottomPadding = bottomPadding,
                )
                appList(appListInput)
+26 −21
Original line number Diff line number Diff line
@@ -85,37 +85,42 @@ internal class TogglePermissionAppInfoPageProvider(
        fun navigator(permissionType: String, app: ApplicationInfo) =
            navigator(route = "$PAGE_NAME/$permissionType/${app.toRoute()}")

        fun buildPageData(permissionType: String): SettingsPage {
            return SettingsPage.create(
                name = PAGE_NAME,
                parameter = PAGE_PARAMETER,
                arguments = bundleOf(PERMISSION to permissionType)
            )
        }
    }
}

@Composable
        fun <T : AppRecord> EntryItem(
internal fun <T : AppRecord> TogglePermissionAppListModel<T>.TogglePermissionAppInfoPageEntryItem(
    permissionType: String,
    app: ApplicationInfo,
            listModel: TogglePermissionAppListModel<T>,
) {
    val record = remember { transformItem(app) }
    if (!remember { isChangeable(record) }) return
    val context = LocalContext.current
    val internalListModel = remember {
                TogglePermissionInternalAppListModel(context, listModel, ::RestrictionsProviderImpl)
        TogglePermissionInternalAppListModel(
            context = context,
            permissionType = permissionType,
            listModel = this,
            restrictionsProviderFactory = ::RestrictionsProviderImpl,
        )
    }
            val record = remember { listModel.transformItem(app) }
            if (!remember { listModel.isChangeable(record) }) return
    Preference(
        object : PreferenceModel {
                    override val title = stringResource(listModel.pageTitleResId)
            override val title = stringResource(pageTitleResId)
            override val summary = internalListModel.getSummary(record)
                    override val onClick = navigator(permissionType, app)
            override val onClick =
                TogglePermissionAppInfoPageProvider.navigator(permissionType, app)
        }
    )
}

        fun buildPageData(permissionType: String): SettingsPage {
            return SettingsPage.create(
                name = PAGE_NAME,
                parameter = PAGE_PARAMETER,
                arguments = bundleOf(PERMISSION to permissionType)
            )
        }
    }
}

@VisibleForTesting
@Composable
internal fun TogglePermissionAppListModel<out AppRecord>.TogglePermissionAppInfoPage(
+1 −1
Original line number Diff line number Diff line
@@ -96,7 +96,7 @@ interface TogglePermissionAppListProvider {
    @Composable
    fun InfoPageEntryItem(app: ApplicationInfo) {
        val listModel = rememberContext(::createModel)
        TogglePermissionAppInfoPageProvider.EntryItem(permissionType, app, listModel)
        listModel.TogglePermissionAppInfoPageEntryItem(permissionType, app)
    }
}

Loading