diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/MainActivity.kt b/app/src/main/java/de/jeanlucmakiola/agendula/MainActivity.kt index b69efa6..4eefc0b 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/MainActivity.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/MainActivity.kt @@ -26,6 +26,7 @@ import androidx.lifecycle.lifecycleScope import dagger.hilt.android.AndroidEntryPoint import de.jeanlucmakiola.agendula.data.di.LaunchHook import de.jeanlucmakiola.agendula.domain.SmartList +import de.jeanlucmakiola.agendula.domain.TaskFilter import de.jeanlucmakiola.agendula.domain.smartListNamed import de.jeanlucmakiola.agendula.ui.imports.importIntentUri import de.jeanlucmakiola.agendula.ui.navigation.AppShortcuts @@ -35,6 +36,7 @@ import de.jeanlucmakiola.agendula.ui.navigation.NavRequest import de.jeanlucmakiola.agendula.ui.settings.SettingsViewModel import de.jeanlucmakiola.agendula.ui.theme.AgendulaTheme import de.jeanlucmakiola.agendula.ui.theme.SystemTheme +import de.jeanlucmakiola.agendula.ui.views.ViewSession import de.jeanlucmakiola.floret.crash.CrashReportDialog import de.jeanlucmakiola.floret.crash.CrashReporter import de.jeanlucmakiola.floret.crash.submitCrashReport @@ -55,6 +57,8 @@ class MainActivity : ComponentActivity() { @Inject lateinit var settingsPrefs: SettingsPrefs + @Inject lateinit var viewSession: ViewSession + // A captured crash report awaiting the user's decision, surfaced as a dialog // over the app on the next launch (the single-crash path). A startup // crash-loop is handled out of band, before setContent — see below. @@ -76,7 +80,10 @@ class MainActivity : ComponentActivity() { } enableEdgeToEdge() - AppShortcuts.publish(this) + // Every view is a launcher shortcut, kept up with the views as they change. + lifecycleScope.launch { + settingsPrefs.views.collect { views -> AppShortcuts.publish(this@MainActivity, views) } + } // The stored setting, never the view model's placeholder: a night mode // that flips back and forth would recreate the activity each time. @@ -88,7 +95,21 @@ class MainActivity : ComponentActivity() { // Only on a fresh launch: after a rotation or process restore the intent // is the same old one and the back stack already holds its destination. val fromHistory = intent.flags and Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY != 0 - if (savedInstanceState == null && !fromHistory) navRequest = navRequestOf(intent) + if (savedInstanceState == null && !fromHistory) { + navRequest = navRequestOf(intent)?.also(::switchViewFor) + // A plain launch opens the start destination (#141); anything else opens what it asked for. + if (navRequest == null) { + viewSession.beginStart() + lifecycleScope.launch { + val push = when (val filter = viewSession.start()) { + is TaskFilter.OfList -> NavRequest.OpenList(filter.listId) + is TaskFilter.Saved -> NavRequest.OpenFilter(filter.id) + else -> null + } + if (push != null && navRequest == null) navRequest = push + } + } + } // Surface a single captured crash as a dialog on the next launch. if (CrashReporter.shouldPrompt(this)) pendingCrashReport = CrashReporter.pendingReport(this) @@ -141,7 +162,17 @@ class MainActivity : ComponentActivity() { override fun onNewIntent(intent: Intent) { super.onNewIntent(intent) setIntent(intent) - navRequestOf(intent)?.let { navRequest = it } + navRequestOf(intent)?.let { + switchViewFor(it) + navRequest = it + } + } + + /** A view's shortcut switches to it at once, so home never shows the one before. */ + private fun switchViewFor(request: NavRequest) { + if (request !is NavRequest.OpenView) return + viewSession.switchTo(request.viewId) + AppShortcuts.reportUsed(this, request.viewId) } override fun onResume() { @@ -164,6 +195,8 @@ class MainActivity : ComponentActivity() { private const val ACTION_OPEN_SMART = "de.jeanlucmakiola.agendula.action.OPEN_SMART" private const val ACTION_OPEN_LIST = "de.jeanlucmakiola.agendula.action.OPEN_LIST" private const val ACTION_OPEN_FILTER = "de.jeanlucmakiola.agendula.action.OPEN_FILTER" + private const val ACTION_OPEN_VIEW = "de.jeanlucmakiola.agendula.action.OPEN_VIEW" + private const val EXTRA_VIEW_ID = "de.jeanlucmakiola.agendula.extra.VIEW_ID" private const val EXTRA_FILTER_ID = "de.jeanlucmakiola.agendula.extra.FILTER_ID" private const val EXTRA_SMART_LIST = "de.jeanlucmakiola.agendula.extra.SMART_LIST" private const val EXTRA_LIST_ID = "de.jeanlucmakiola.agendula.extra.LIST_ID" @@ -247,6 +280,12 @@ class MainActivity : ComponentActivity() { .putExtra(EXTRA_FILTER_ID, filterId) .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + /** Opens one view — its launcher shortcut. */ + fun viewIntent(context: Context, viewId: Long): Intent = + Intent(ACTION_OPEN_VIEW, "agendula://view/$viewId".toUri(), context, MainActivity::class.java) + .putExtra(EXTRA_VIEW_ID, viewId) + .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK) + internal fun navRequestOf(intent: Intent?): NavRequest? { if (intent == null) return null if (BuildConfig.SYNC_ENABLED) { @@ -271,6 +310,8 @@ class MainActivity : ComponentActivity() { ?.let { return NavRequest.OpenList(it) } ACTION_OPEN_FILTER -> intent.getLongExtra(EXTRA_FILTER_ID, -1L).takeIf { it > 0L } ?.let { return NavRequest.OpenFilter(it) } + ACTION_OPEN_VIEW -> intent.getLongExtra(EXTRA_VIEW_ID, -1L).takeIf { it > 0L } + ?.let { return NavRequest.OpenView(it) } } importIntentUri(intent)?.let { return NavRequest.Import(it) } if (intent.action == Intent.ACTION_SEND && intent.type?.startsWith("text/plain") == true) { diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AppShortcuts.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AppShortcuts.kt index 5c8ed14..ab53acf 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AppShortcuts.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/navigation/AppShortcuts.kt @@ -1,33 +1,105 @@ package de.jeanlucmakiola.agendula.ui.navigation import android.content.Context +import android.graphics.Bitmap +import android.graphics.Canvas +import android.graphics.Paint +import androidx.compose.ui.graphics.PathFillType +import androidx.compose.ui.graphics.asAndroidPath +import androidx.compose.ui.graphics.vector.ImageVector +import androidx.compose.ui.graphics.vector.VectorGroup +import androidx.compose.ui.graphics.vector.VectorPath +import androidx.compose.ui.graphics.vector.toPath +import androidx.core.content.ContextCompat import androidx.core.content.pm.ShortcutInfoCompat import androidx.core.content.pm.ShortcutManagerCompat +import androidx.core.graphics.createBitmap +import androidx.core.graphics.withSave import androidx.core.graphics.drawable.IconCompat import de.jeanlucmakiola.agendula.MainActivity import de.jeanlucmakiola.agendula.R +import de.jeanlucmakiola.agendula.domain.View +import de.jeanlucmakiola.agendula.ui.views.vector /** - * The launcher's long-press shortcuts. Published at runtime rather than from a - * static `shortcuts.xml`, whose intents would have to name the release package - * and so break in the suffixed debug and releaseTest builds — and so the labels - * follow the app language. + * The launcher's long-press shortcuts: New task, Today, then every view (#59), + * as many as the launcher takes. Published at runtime rather than from a static + * `shortcuts.xml`, whose intents would have to name the release package and so + * break in the suffixed debug and releaseTest builds — and so the labels follow + * the app language. */ object AppShortcuts { - fun publish(context: Context) { - val shortcuts = listOf( + /** The views last published, for a republish that doesn't come with them. */ + @Volatile private var views: List = emptyList() + + fun publish(context: Context, views: List = this.views) { + this.views = views + val fixed = listOf( ShortcutInfoCompat.Builder(context, "new_task") .setShortLabel(context.getString(R.string.shortcut_new_task)) .setIcon(IconCompat.createWithResource(context, R.drawable.ic_shortcut_new_task)) .setIntent(MainActivity.newTaskIntent(context)) + .setRank(0) .build(), ShortcutInfoCompat.Builder(context, "today") .setShortLabel(context.getString(R.string.shortcut_today)) .setIcon(IconCompat.createWithResource(context, R.drawable.ic_shortcut_today)) .setIntent(MainActivity.todayIntent(context)) + .setRank(1) .build(), ) - runCatching { ShortcutManagerCompat.setDynamicShortcuts(context, shortcuts) } + val room = runCatching { ShortcutManagerCompat.getMaxShortcutCountPerActivity(context) }.getOrDefault(fixed.size) + val viewShortcuts = views.take((room - fixed.size).coerceAtLeast(0)).mapIndexed { index, view -> + val label = view.name.ifBlank { + context.getString(if (view.id == View.HOME_ID) R.string.view_home else R.string.view_unnamed) + } + ShortcutInfoCompat.Builder(context, "view_${view.id}") + .setShortLabel(label) + .setIcon(IconCompat.createWithBitmap(shortcutBitmap(context, view.icon.vector))) + .setIntent(MainActivity.viewIntent(context, view.id)) + .setRank(fixed.size + index) + .build() + } + runCatching { ShortcutManagerCompat.setDynamicShortcuts(context, fixed + viewShortcuts) } + } + + /** Tells the launcher a view's shortcut was used, so it can rank it. */ + fun reportUsed(context: Context, viewId: Long) { + runCatching { ShortcutManagerCompat.reportShortcutUsed(context, "view_$viewId") } + } + + /** [icon] white on the brand circle, as the drawn shortcut icons are; launchers don't tint them. */ + private fun shortcutBitmap(context: Context, icon: ImageVector): Bitmap { + val size = (48 * context.resources.displayMetrics.density).toInt() + val bitmap = createBitmap(size, size) + val canvas = Canvas(bitmap) + val paint = Paint(Paint.ANTI_ALIAS_FLAG) + paint.color = ContextCompat.getColor(context, R.color.ic_launcher_background) + canvas.drawCircle(size / 2f, size / 2f, size / 2f, paint) + paint.color = android.graphics.Color.WHITE + // The glyph takes 58% of the circle, as in the drawn icons. + val glyph = size * 0.58f + canvas.translate((size - glyph) / 2f, (size - glyph) / 2f) + canvas.scale(glyph / icon.viewportWidth, glyph / icon.viewportHeight) + drawGroup(canvas, icon.root, paint) + return bitmap + } + + private fun drawGroup(canvas: Canvas, group: VectorGroup, paint: Paint): Unit = canvas.withSave { + translate(group.translationX + group.pivotX, group.translationY + group.pivotY) + rotate(group.rotation) + scale(group.scaleX, group.scaleY) + translate(-group.pivotX, -group.pivotY) + for (node in group) { + when (node) { + is VectorGroup -> drawGroup(this, node, paint) + is VectorPath -> { + val path = node.pathData.toPath().asAndroidPath() + if (node.pathFillType == PathFillType.EvenOdd) path.fillType = android.graphics.Path.FillType.EVEN_ODD + drawPath(path, paint) + } + } + } } } diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/settings/SettingsScreen.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/settings/SettingsScreen.kt index 35509ff..fe7b1f4 100644 --- a/app/src/main/java/de/jeanlucmakiola/agendula/ui/settings/SettingsScreen.kt +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/settings/SettingsScreen.kt @@ -53,6 +53,7 @@ import de.jeanlucmakiola.floret.components.GroupedSectionHeader import de.jeanlucmakiola.floret.components.LanguagePickerRow import de.jeanlucmakiola.floret.components.OptionPicker import de.jeanlucmakiola.floret.components.Position +import de.jeanlucmakiola.agendula.ui.views.StartDestinationRow import de.jeanlucmakiola.floret.crash.CrashReportDialog import de.jeanlucmakiola.floret.crash.CrashReporter import de.jeanlucmakiola.floret.crash.openIssueTracker @@ -237,10 +238,11 @@ private fun SettingsHub( GroupedRow( title = stringResource(R.string.settings_section_reminders), summary = stringResource(R.string.settings_reminders_subtitle), - position = Position.Bottom, + position = Position.Middle, leading = { CategoryIcon(Icons.Default.Notifications, ChipAccent.Primary) }, onClick = { onOpenSection(SettingsSection.Notifications) }, ) + StartDestinationRow(position = Position.Bottom) Spacer(Modifier.height(8.dp)) GroupedSectionHeader(stringResource(R.string.settings_group_data)) diff --git a/app/src/main/java/de/jeanlucmakiola/agendula/ui/views/StartDestinationRow.kt b/app/src/main/java/de/jeanlucmakiola/agendula/ui/views/StartDestinationRow.kt new file mode 100644 index 0000000..aaa6b46 --- /dev/null +++ b/app/src/main/java/de/jeanlucmakiola/agendula/ui/views/StartDestinationRow.kt @@ -0,0 +1,161 @@ +package de.jeanlucmakiola.agendula.ui.views + +import androidx.compose.foundation.layout.Spacer +import androidx.compose.foundation.layout.height +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.rounded.FilterList +import androidx.compose.material.icons.rounded.History +import androidx.compose.material.icons.rounded.RocketLaunch +import androidx.compose.material3.Icon +import androidx.compose.runtime.Composable +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.saveable.rememberSaveable +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import androidx.compose.ui.unit.dp +import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel +import androidx.lifecycle.ViewModel +import androidx.lifecycle.compose.collectAsStateWithLifecycle +import androidx.lifecycle.viewModelScope +import dagger.hilt.android.lifecycle.HiltViewModel +import de.jeanlucmakiola.agendula.R +import de.jeanlucmakiola.agendula.data.prefs.SettingsPrefs +import de.jeanlucmakiola.agendula.data.tasks.TasksRepository +import de.jeanlucmakiola.agendula.domain.DefaultStartDestination +import de.jeanlucmakiola.agendula.domain.SavedFilter +import de.jeanlucmakiola.agendula.domain.StartDestination +import de.jeanlucmakiola.agendula.domain.TaskList +import de.jeanlucmakiola.agendula.domain.View +import de.jeanlucmakiola.agendula.ui.common.ListColorChip +import de.jeanlucmakiola.agendula.ui.settings.CategoryIcon +import de.jeanlucmakiola.agendula.ui.settings.ChipAccent +import de.jeanlucmakiola.floret.components.FullScreenPicker +import de.jeanlucmakiola.floret.components.GroupedRow +import de.jeanlucmakiola.floret.components.GroupedSectionHeader +import de.jeanlucmakiola.floret.components.Position +import de.jeanlucmakiola.floret.components.positionOf +import kotlinx.coroutines.flow.SharingStarted +import kotlinx.coroutines.flow.StateFlow +import kotlinx.coroutines.flow.catch +import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.stateIn +import kotlinx.coroutines.launch +import javax.inject.Inject + +/** What the start destination can be, and what it is. */ +data class StartChoices( + val selected: StartDestination = DefaultStartDestination, + val views: List = emptyList(), + val lists: List = emptyList(), + val filters: List = emptyList(), +) + +@HiltViewModel +class StartDestinationViewModel @Inject constructor( + private val settingsPrefs: SettingsPrefs, + repository: TasksRepository, +) : ViewModel() { + + val choices: StateFlow = combine( + settingsPrefs.startDestination, + settingsPrefs.views, + repository.taskLists().catch { emit(emptyList()) }, + settingsPrefs.savedFilters, + ::StartChoices, + ).stateIn(viewModelScope, SharingStarted.WhileSubscribed(5_000), StartChoices()) + + fun select(destination: StartDestination) = viewModelScope.launch { settingsPrefs.setStartDestination(destination) } +} + +/** + * The settings row for where the app opens (#141), and its full-screen picker: + * *Last used view*, then the views, lists and saved filters. + */ +@Composable +fun StartDestinationRow(position: Position, viewModel: StartDestinationViewModel = hiltViewModel()) { + val choices by viewModel.choices.collectAsStateWithLifecycle() + var picking by rememberSaveable { mutableStateOf(false) } + GroupedRow( + title = stringResource(R.string.start_destination), + summary = startLabel(choices.selected, choices), + position = position, + leading = { CategoryIcon(Icons.Rounded.RocketLaunch, ChipAccent.Tertiary) }, + onClick = { picking = true }, + ) + if (picking) { + StartDestinationPicker(choices, onSelect = viewModel::select, onDismiss = { picking = false }) + } +} + +@Composable +private fun StartDestinationPicker( + choices: StartChoices, + onSelect: (StartDestination) -> Unit, + onDismiss: () -> Unit, +) { + val pick = { destination: StartDestination -> + onSelect(destination) + onDismiss() + } + FullScreenPicker(title = stringResource(R.string.start_destination), onDismiss = onDismiss) { + GroupedRow( + title = stringResource(R.string.start_last_used), + summary = stringResource(R.string.start_last_used_hint), + position = Position.Alone, + leading = { Icon(Icons.Rounded.History, contentDescription = null) }, + checked = choices.selected == StartDestination.LastUsed, + onClick = { pick(StartDestination.LastUsed) }, + ) + GroupedSectionHeader(stringResource(R.string.start_section_views)) + choices.views.forEachIndexed { index, view -> + val destination = StartDestination.View(view.id) + GroupedRow( + title = viewName(view.name, view.id), + position = positionOf(index, choices.views.size), + leading = { Icon(view.icon.vector, contentDescription = null) }, + checked = choices.selected == destination, + onClick = { pick(destination) }, + ) + } + if (choices.lists.isNotEmpty()) { + GroupedSectionHeader(stringResource(R.string.start_section_lists)) + val showAccount = choices.lists.mapTo(HashSet()) { it.accountName }.size > 1 + choices.lists.forEachIndexed { index, list -> + val destination = StartDestination.List(list.id) + GroupedRow( + title = list.name, + summary = list.accountName.takeIf { showAccount }, + position = positionOf(index, choices.lists.size), + leading = { ListColorChip(list.color) }, + checked = choices.selected == destination, + onClick = { pick(destination) }, + ) + } + } + if (choices.filters.isNotEmpty()) { + GroupedSectionHeader(stringResource(R.string.start_section_filters)) + choices.filters.forEachIndexed { index, filter -> + val destination = StartDestination.Filter(filter.id) + GroupedRow( + title = filter.name, + position = positionOf(index, choices.filters.size), + leading = { Icon(Icons.Rounded.FilterList, contentDescription = null) }, + checked = choices.selected == destination, + onClick = { pick(destination) }, + ) + } + } + Spacer(Modifier.height(16.dp)) + } +} + +/** What [destination] is called; one since deleted reads as Home, where the app then opens. */ +@Composable +private fun startLabel(destination: StartDestination, choices: StartChoices): String = when (destination) { + StartDestination.LastUsed -> stringResource(R.string.start_last_used) + is StartDestination.View -> choices.views.firstOrNull { it.id == destination.id }?.let { viewName(it.name, it.id) } + is StartDestination.List -> choices.lists.firstOrNull { it.id == destination.id }?.name + is StartDestination.Filter -> choices.filters.firstOrNull { it.id == destination.id }?.name +} ?: choices.views.firstOrNull()?.let { viewName(it.name, it.id) } ?: stringResource(R.string.view_home)