feat(shell): swipe between tabs
The four tabs are now pages of one HorizontalPager on a single NavHost route, so a sideways swipe moves between Alarms, Timers, Stopwatch and World clock as well as tapping the bar. The pager is the source of truth for the selected tab; editors, the timer setup page and Settings stay NavHost destinations pushed over it. A tab tap from one of them pops back to the tabs, and a deep link always lands on its tab rather than on whatever nested screen was last open there. Swiping is on only under a navigation bar: beside a rail the tabs are stacked vertically, and a horizontal swipe has no counterpart there. Closes #2
This commit is contained in:
1 parent
45e5a51c98
commit
be526711d7
6 files changed
+358
-216
No files matched your search
@@ -1,6 +1,7 @@
|
||||
package de.jeanlucmakiola.clockula.ui.alarms
|
||||
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.clockula.ui.shell.ClockulaDestination
|
||||
import de.jeanlucmakiola.clockula.ui.shell.ShellBackAction
|
||||
import de.jeanlucmakiola.clockula.ui.shell.ShellNavigation
|
||||
import org.junit.jupiter.api.Test
|
||||
@@ -23,12 +24,13 @@ class AlarmRoutesTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the editor is no tab, so the alarms tab stays selected`() {
|
||||
assertThat(ShellNavigation.destinationOf(AlarmRoutes.editor(7L))).isNull()
|
||||
fun `the editor is a screen nested over the tabs`() {
|
||||
assertThat(ShellNavigation.isNested(AlarmRoutes.EDITOR_PATTERN)).isTrue()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `the shell stands aside on back, leaving the editor to pop itself`() {
|
||||
assertThat(ShellNavigation.onBack(AlarmRoutes.editor(7L))).isEqualTo(ShellBackAction.Exit)
|
||||
assertThat(ShellNavigation.onBack(AlarmRoutes.EDITOR_PATTERN, ClockulaDestination.ALARMS))
|
||||
.isEqualTo(ShellBackAction.Exit)
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package de.jeanlucmakiola.clockula.ui.shell
|
||||
|
||||
import androidx.compose.material3.adaptive.navigationsuite.NavigationSuiteType
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import de.jeanlucmakiola.clockula.stopwatch.StopwatchIntents
|
||||
import de.jeanlucmakiola.clockula.timer.TimerIntents
|
||||
@@ -10,8 +11,9 @@ import org.junit.jupiter.api.Test
|
||||
|
||||
/**
|
||||
* The shell's navigation policy as pure data: which tab is the root, what a tab
|
||||
* tap does to the back stack, and where Back goes. The four lines that replay a
|
||||
* [TabNavCommand] onto a `NavController` are not worth testing; this is.
|
||||
* tap does to the back stack and the pager, and where Back goes. The lines that
|
||||
* replay a [TabSelection] onto a `NavController` and a `PagerState` are not
|
||||
* worth testing; this is.
|
||||
*/
|
||||
class ShellNavigationTest {
|
||||
|
||||
@@ -28,10 +30,10 @@ class ShellNavigationTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every tab has its own non-blank route`() {
|
||||
val routes = ShellNavigation.topLevel.map { it.route }
|
||||
fun `every tab has its own non-blank page key`() {
|
||||
val keys = ShellNavigation.topLevel.map { it.key }
|
||||
|
||||
assertThat(routes.filter { it.isNotBlank() }.toSet()).hasSize(4)
|
||||
assertThat(keys.filter { it.isNotBlank() }.toSet()).hasSize(4)
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -46,133 +48,156 @@ class ShellNavigationTest {
|
||||
assertThat(ShellNavigation.start).isEqualTo(ClockulaDestination.ALARMS)
|
||||
}
|
||||
|
||||
// --- #2: the tabs are pages of one pager ---
|
||||
|
||||
@Test
|
||||
fun `each tab's own route resolves back to it`() {
|
||||
val resolved = ClockulaDestination.entries.map { ShellNavigation.destinationOf(it.route) }
|
||||
fun `each tab's own page resolves back to it`() {
|
||||
val resolved = ClockulaDestination.entries.map {
|
||||
ShellNavigation.destinationAt(ShellNavigation.pageOf(it))
|
||||
}
|
||||
|
||||
assertThat(resolved).isEqualTo(ClockulaDestination.entries.toList())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a null route resolves to no destination`() {
|
||||
assertThat(ShellNavigation.destinationOf(null)).isNull()
|
||||
fun `pages follow tab order`() {
|
||||
val pages = ShellNavigation.topLevel.map(ShellNavigation::pageOf)
|
||||
|
||||
assertThat(pages).containsExactly(0, 1, 2, 3).inOrder()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `an unknown route resolves to no destination`() {
|
||||
assertThat(ShellNavigation.destinationOf("nope")).isNull()
|
||||
fun `an out-of-range page clamps instead of throwing`() {
|
||||
assertThat(ShellNavigation.destinationAt(-1)).isEqualTo(ClockulaDestination.ALARMS)
|
||||
assertThat(ShellNavigation.destinationAt(9)).isEqualTo(ClockulaDestination.WORLD_CLOCK)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a nested route does not resolve by prefix`() {
|
||||
assertThat(ShellNavigation.destinationOf("alarms/12")).isNull()
|
||||
fun `the tabs route and an unknown host are not nested`() {
|
||||
assertThat(ShellNavigation.isNested(ShellNavigation.TABS_ROUTE)).isFalse()
|
||||
assertThat(ShellNavigation.isNested(null)).isFalse()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `selecting another tab saves and restores the tab's own stack`() {
|
||||
val command = ShellNavigation.onTabSelected("alarms", ClockulaDestination.TIMERS)
|
||||
fun `editors, the timer setup page and settings are nested over the tabs`() {
|
||||
val routes = listOf(
|
||||
AlarmRoutes.EDITOR_PATTERN,
|
||||
TimerRoutes.EDITOR_PATTERN,
|
||||
TimerRoutes.NEW,
|
||||
SettingsRoutes.MAIN,
|
||||
SettingsRoutes.SELF_CHECK,
|
||||
SettingsRoutes.BACKUP,
|
||||
)
|
||||
|
||||
assertThat(command).isEqualTo(
|
||||
TabNavCommand(
|
||||
route = "timers",
|
||||
popUpTo = "alarms",
|
||||
popUpToInclusive = false,
|
||||
saveState = true,
|
||||
restoreState = true,
|
||||
launchSingleTop = true,
|
||||
),
|
||||
assertThat(routes.map(ShellNavigation::isNested).toSet()).containsExactly(true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a tap on the tabs slides there without touching the back stack`() {
|
||||
val selection = ShellNavigation.onTabSelected(ShellNavigation.TABS_ROUTE, ClockulaDestination.TIMERS)
|
||||
|
||||
assertThat(selection).isEqualTo(
|
||||
TabSelection(ClockulaDestination.TIMERS, popToTabs = false, animate = true),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `re-selecting the current tab pops its own stack instead of restoring it`() {
|
||||
val command = ShellNavigation.onTabSelected("timers", ClockulaDestination.TIMERS)
|
||||
fun `a tap over a nested screen pops back to the tabs and jumps`() {
|
||||
val selection = ShellNavigation.onTabSelected(SettingsRoutes.MAIN, ClockulaDestination.STOPWATCH)
|
||||
|
||||
assertThat(command).isEqualTo(
|
||||
TabNavCommand(
|
||||
route = "timers",
|
||||
popUpTo = "alarms",
|
||||
popUpToInclusive = false,
|
||||
saveState = true,
|
||||
restoreState = false,
|
||||
launchSingleTop = true,
|
||||
),
|
||||
assertThat(selection).isEqualTo(
|
||||
TabSelection(ClockulaDestination.STOPWATCH, popToTabs = true, animate = false),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `selecting a tab before the first destination is known restores its stack`() {
|
||||
val command = ShellNavigation.onTabSelected(null, ClockulaDestination.TIMERS)
|
||||
fun `re-selecting the tab under its editor pops the editor`() {
|
||||
val selection = ShellNavigation.onTabSelected(TimerRoutes.EDITOR_PATTERN, ClockulaDestination.TIMERS)
|
||||
|
||||
assertThat(command.restoreState).isTrue()
|
||||
assertThat(selection.popToTabs).isTrue()
|
||||
assertThat(selection.destination).isEqualTo(ClockulaDestination.TIMERS)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `every tab tap pops up to the start destination`() {
|
||||
val popUpTos = ClockulaDestination.entries.flatMap { current ->
|
||||
ClockulaDestination.entries.map { target ->
|
||||
ShellNavigation.onTabSelected(current.route, target).popUpTo
|
||||
}
|
||||
}
|
||||
fun `a tab asked for by an intent never slides, wherever the app was`() {
|
||||
val routes = listOf(null, ShellNavigation.TABS_ROUTE, AlarmRoutes.EDITOR_PATTERN, SettingsRoutes.BACKUP)
|
||||
|
||||
assertThat(popUpTos.toSet()).containsExactly(ShellNavigation.start.route)
|
||||
val animated = routes.map { ShellNavigation.onTabRequested(it, ClockulaDestination.WORLD_CLOCK).animate }
|
||||
|
||||
assertThat(animated.toSet()).containsExactly(false)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `a tab asked for by an intent pops any nested screen, so it lands on the tab itself`() {
|
||||
val selection = ShellNavigation.onTabRequested(SettingsRoutes.MAIN, ClockulaDestination.ALARMS)
|
||||
|
||||
assertThat(selection).isEqualTo(
|
||||
TabSelection(ClockulaDestination.ALARMS, popToTabs = true, animate = false),
|
||||
)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `swiping pages the tabs under a navigation bar`() {
|
||||
val bars = listOf(
|
||||
NavigationSuiteType.ShortNavigationBarCompact,
|
||||
NavigationSuiteType.ShortNavigationBarMedium,
|
||||
NavigationSuiteType.NavigationBar,
|
||||
)
|
||||
|
||||
assertThat(bars.map(ShellNavigation::swipesBetweenTabs).toSet()).containsExactly(true)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `swiping is off beside a rail, a drawer, or no navigation at all`() {
|
||||
val others = listOf(
|
||||
NavigationSuiteType.WideNavigationRailCollapsed,
|
||||
NavigationSuiteType.WideNavigationRailExpanded,
|
||||
NavigationSuiteType.NavigationRail,
|
||||
NavigationSuiteType.NavigationDrawer,
|
||||
NavigationSuiteType.None,
|
||||
)
|
||||
|
||||
assertThat(others.map(ShellNavigation::swipesBetweenTabs).toSet()).containsExactly(false)
|
||||
}
|
||||
|
||||
// --- back ---
|
||||
|
||||
@Test
|
||||
fun `back from alarms leaves the app`() {
|
||||
assertThat(ShellNavigation.onBack("alarms")).isEqualTo(ShellBackAction.Exit)
|
||||
assertThat(ShellNavigation.onBack(ShellNavigation.TABS_ROUTE, ClockulaDestination.ALARMS))
|
||||
.isEqualTo(ShellBackAction.Exit)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `back from any other tab returns to alarms`() {
|
||||
val actions = listOf("timers", "stopwatch", "world_clock").map(ShellNavigation::onBack)
|
||||
val actions = listOf(
|
||||
ClockulaDestination.TIMERS,
|
||||
ClockulaDestination.STOPWATCH,
|
||||
ClockulaDestination.WORLD_CLOCK,
|
||||
).map { ShellNavigation.onBack(ShellNavigation.TABS_ROUTE, it) }
|
||||
|
||||
assertThat(actions).containsExactly(
|
||||
ShellBackAction.GoTo(ClockulaDestination.ALARMS),
|
||||
ShellBackAction.GoTo(ClockulaDestination.ALARMS),
|
||||
ShellBackAction.GoTo(ClockulaDestination.ALARMS),
|
||||
)
|
||||
assertThat(actions.toSet()).containsExactly(ShellBackAction.GoTo(ClockulaDestination.ALARMS))
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `back before the first destination is known leaves the app`() {
|
||||
assertThat(ShellNavigation.onBack(null)).isEqualTo(ShellBackAction.Exit)
|
||||
assertThat(ShellNavigation.onBack(null, ClockulaDestination.TIMERS)).isEqualTo(ShellBackAction.Exit)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `back from a nested destination is not the shell's to intercept`() {
|
||||
assertThat(ShellNavigation.onBack("alarms/12")).isEqualTo(ShellBackAction.Exit)
|
||||
fun `back from a nested screen is not the shell's to intercept, whatever tab is under it`() {
|
||||
val actions = ClockulaDestination.entries.map { ShellNavigation.onBack(SettingsRoutes.MAIN, it) }
|
||||
|
||||
assertThat(actions.toSet()).containsExactly(ShellBackAction.Exit)
|
||||
}
|
||||
|
||||
// --- M6 §5.15: the timer editor's route, and the notification's deep link ---
|
||||
|
||||
/** §5.15 #1 */
|
||||
@Test
|
||||
fun `the timer editor is no tab, so the timers tab stays selected`() {
|
||||
assertThat(ShellNavigation.destinationOf(TimerRoutes.editor(7L))).isNull()
|
||||
}
|
||||
|
||||
// --- M10: settings has no single owning tab ---
|
||||
|
||||
@Test
|
||||
fun `settings has no owning tab, so the caller falls back to the start destination`() {
|
||||
assertThat(ShellNavigation.selectedDestinationOf(SettingsRoutes.MAIN)).isNull()
|
||||
assertThat(ShellNavigation.selectedDestinationOf(SettingsRoutes.SELF_CHECK)).isNull()
|
||||
assertThat(ShellNavigation.selectedDestinationOf(SettingsRoutes.BACKUP)).isNull()
|
||||
}
|
||||
|
||||
@Test
|
||||
fun `editors highlight their parent tab`() {
|
||||
assertThat(ShellNavigation.selectedDestinationOf(AlarmRoutes.EDITOR_PATTERN))
|
||||
.isEqualTo(ClockulaDestination.ALARMS)
|
||||
assertThat(ShellNavigation.selectedDestinationOf(TimerRoutes.EDITOR_PATTERN))
|
||||
.isEqualTo(ClockulaDestination.TIMERS)
|
||||
}
|
||||
|
||||
/** §5.15 #2 */
|
||||
@Test
|
||||
fun `the shell stands aside on back, leaving the timer editor to pop itself`() {
|
||||
assertThat(ShellNavigation.onBack(TimerRoutes.editor(7L))).isEqualTo(ShellBackAction.Exit)
|
||||
assertThat(ShellNavigation.onBack(TimerRoutes.EDITOR_PATTERN, ClockulaDestination.TIMERS))
|
||||
.isEqualTo(ShellBackAction.Exit)
|
||||
}
|
||||
|
||||
/** §5.15 #3 */
|
||||
|
||||
Reference in new issue
Block a user