Add missing permission banner to app shortcut results

This commit is contained in:
MM20 2022-03-21 19:04:55 +01:00
parent f17336b615
commit d080d71e0f
No known key found for this signature in database
GPG Key ID: 0B61A8F2DEAFA389
2 changed files with 55 additions and 3 deletions

View File

@ -195,4 +195,23 @@ class SearchVM : ViewModel(), KoinComponent {
} }
} }
val missingAppShortcutPermission = combine(
permissionsManager.hasPermission(PermissionGroup.AppShortcuts),
dataStore.data.map { it.appShortcutSearch.enabled }.distinctUntilChanged()
) { perm, enabled -> !perm && enabled }
fun requestAppShortcutPermission(context: AppCompatActivity) {
permissionsManager.requestPermission(context, PermissionGroup.AppShortcuts)
}
fun disableAppShortcutSearch() {
viewModelScope.launch {
dataStore.updateData {
it.toBuilder()
.setAppShortcutSearch(it.appShortcutSearch.toBuilder().setEnabled(false))
.build()
}
}
}
} }

View File

@ -1,30 +1,63 @@
package de.mm20.launcher2.ui.launcher.search.appshortcuts package de.mm20.launcher2.ui.launcher.search.appshortcuts
import androidx.appcompat.app.AppCompatActivity
import androidx.compose.animation.AnimatedVisibility import androidx.compose.animation.AnimatedVisibility
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.ui.Modifier import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import de.mm20.launcher2.ui.R
import de.mm20.launcher2.ui.component.LauncherCard import de.mm20.launcher2.ui.component.LauncherCard
import de.mm20.launcher2.ui.component.MissingPermissionBanner
import de.mm20.launcher2.ui.launcher.search.SearchVM import de.mm20.launcher2.ui.launcher.search.SearchVM
import de.mm20.launcher2.ui.launcher.search.common.list.SearchResultList import de.mm20.launcher2.ui.launcher.search.common.list.SearchResultList
@Composable @Composable
fun ColumnScope.AppShortcutResults() { fun ColumnScope.AppShortcutResults() {
val viewModel: SearchVM = viewModel() val viewModel: SearchVM = viewModel()
val apps by viewModel.appShortcutResults.observeAsState(emptyList()) val shortcuts by viewModel.appShortcutResults.observeAsState(emptyList())
val context = LocalContext.current
AnimatedVisibility(apps.isNotEmpty()) { val isSearchEmpty by viewModel.isSearchEmpty.observeAsState(true)
val missingPermission by viewModel.missingAppShortcutPermission.collectAsState(false)
AnimatedVisibility(shortcuts.isNotEmpty() || (!isSearchEmpty && missingPermission)) {
LauncherCard( LauncherCard(
modifier = Modifier.padding(bottom = 8.dp) modifier = Modifier.padding(bottom = 8.dp)
) { ) {
Column {
AnimatedVisibility(!isSearchEmpty && missingPermission) {
MissingPermissionBanner(
text = stringResource(R.string.missing_permission_appshortcuts_search, stringResource(R.string.app_name)),
onClick = { viewModel.requestAppShortcutPermission(context as AppCompatActivity) },
modifier = Modifier.padding(16.dp),
secondaryAction = {
TextButton(onClick = {
viewModel.disableAppShortcutSearch()
}) {
Text(
stringResource(R.string.turn_off),
style = MaterialTheme.typography.labelLarge
)
}
}
)
}
}
SearchResultList( SearchResultList(
items = apps, items = shortcuts,
modifier = Modifier modifier = Modifier
.fillMaxWidth() .fillMaxWidth()
.padding(12.dp) .padding(12.dp)