-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Reimplement navigation system
feat: Back handler (gui esc)
- Loading branch information
Showing
14 changed files
with
219 additions
and
80 deletions.
There are no files selected for viewing
71 changes: 71 additions & 0 deletions
71
guiy-example/src/main/kotlin/com/mineinabyss/guiy/example/gui/NavigationExample.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package com.mineinabyss.guiy.example.gui | ||
|
||
import com.mineinabyss.guiy.viewmodel.GuiyViewModel | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import org.bukkit.OfflinePlayer | ||
import java.util.* | ||
|
||
interface Routes { | ||
object Home : Routes | ||
|
||
object Search : Routes | ||
|
||
data class Profile(val player: UUID) : Routes | ||
} | ||
|
||
class ProfileViewModel: GuiyViewModel() { | ||
val profile = MutableStateFlow<OfflinePlayer?>(null) | ||
} | ||
|
||
//@Composable | ||
//fun NavigationExample() { | ||
// val navController = rememberNavController<Routes>() | ||
// val viewModel = viewModel { ProfileViewModel() } | ||
// NavHost(navController, startDestination = Home) { | ||
// when (it) { | ||
// Home -> HomeScreen( | ||
// search = { navController.navigate(Search) }, | ||
// openProfile = { navController.navigate(Profile(it))} | ||
// ) | ||
// Search -> TextInput(selectPlayer = { player -> | ||
// viewModel.profile.update { player } | ||
// navController.popBackStack() | ||
// }) | ||
// | ||
// is Profile -> ProfileScreen() | ||
// } | ||
// } | ||
//} | ||
// | ||
//@Composable | ||
//fun HomeScreen( | ||
// profileViewModel: ProfileViewModel = viewModel(), | ||
// search: () -> Unit, | ||
// openProfile: (UUID) -> Unit, | ||
//) = Chest("Home Screen") { | ||
// val current = profileViewModel.profile.collectAsState().value | ||
// if(current == null) Button(search) { | ||
// Text("Find a player") | ||
// } | ||
// else Button(onClick = { openProfile(current) }) { Text("Current player: $current") } | ||
//} | ||
// | ||
//@Composable | ||
//fun TextInput(selectPlayer: (OfflinePlayer) -> Unit) { | ||
// var input by remember { mutableStateOf("") } | ||
// Anvil( | ||
// "Search by player name", | ||
// onTextChanged = { input = it }, | ||
// inputLeft = { Text("") }, | ||
// output = { Text("Search for $input") }, | ||
// onSubmit = { | ||
// val player = Bukkit.getOfflinePlayerIfCached(input) ?: return@Anvil | ||
// selectPlayer(player) | ||
// } | ||
// ) | ||
//} | ||
// | ||
//@Composable | ||
//fun ProfileScreen() = Chest("Profile Screen") { | ||
// | ||
//} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,4 +7,5 @@ import org.bukkit.entity.Player | |
interface InventoryCloseScope { | ||
val player: Player | ||
fun exit() | ||
fun back() | ||
} |
38 changes: 38 additions & 0 deletions
38
src/main/kotlin/com/mineinabyss/guiy/navigation/BackHandler.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package com.mineinabyss.guiy.navigation | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.DisposableEffect | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
|
||
class BackGestureDispatcher { | ||
val listeners = mutableListOf<() -> Unit>() | ||
val activeListener get() = listeners.lastOrNull() | ||
|
||
fun addListener(listener: () -> Unit) { | ||
listeners.add(listener) | ||
} | ||
|
||
fun removeListener(listener: () -> Unit) { | ||
listeners.remove(listener) | ||
} | ||
|
||
fun onBack() { | ||
activeListener?.invoke() | ||
} | ||
} | ||
|
||
val LocalBackGestureDispatcher = staticCompositionLocalOf<BackGestureDispatcher?> { null } | ||
|
||
/** | ||
* A listener to back events (usually a menu being closed with `esc`). | ||
*/ | ||
@Composable | ||
fun BackHandler(onBack: () -> Unit) { | ||
val dispatcher = LocalBackGestureDispatcher.current ?: return | ||
DisposableEffect(dispatcher) { | ||
dispatcher.addListener(onBack) | ||
onDispose { | ||
dispatcher.removeListener(onBack) | ||
} | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/kotlin/com/mineinabyss/guiy/navigation/NavController.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.mineinabyss.guiy.navigation | ||
|
||
import androidx.compose.runtime.Composable | ||
import com.mineinabyss.guiy.viewmodel.GuiyViewModel | ||
import com.mineinabyss.guiy.viewmodel.viewModel | ||
import kotlinx.coroutines.flow.* | ||
import kotlinx.coroutines.flow.SharingStarted.Companion.WhileSubscribed | ||
|
||
class NavController() : GuiyViewModel() { | ||
private val screens = MutableStateFlow(listOf<NavRoute>()) | ||
val route: StateFlow<NavRoute?> = screens | ||
.map { it.lastOrNull() } | ||
.stateIn(viewModelScope, WhileSubscribed(stopTimeoutMillis = 5000), null) | ||
|
||
fun popBackStack() = screens.update { it.dropLast(1) } | ||
|
||
fun navigate(route: NavRoute) = screens.update { it + route } | ||
|
||
fun <T: Any> navigate(route: T) = navigate(NavRoute.of(route)) | ||
|
||
fun reset() { | ||
screens.update { listOf() } | ||
} | ||
|
||
// fun refresh() { | ||
// val screen = screen | ||
// screens.remove(screen) | ||
// open(screen ?: default()) | ||
// } | ||
} | ||
|
||
@Composable | ||
fun rememberNavController() = viewModel { NavController() } |
10 changes: 10 additions & 0 deletions
10
src/main/kotlin/com/mineinabyss/guiy/navigation/NavGraph.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.mineinabyss.guiy.navigation | ||
|
||
import androidx.compose.runtime.Composable | ||
|
||
data class NavGraph( | ||
val destinations: Map<String, @Composable (NavRoute) -> Unit>, | ||
val start: NavRoute, | ||
) { | ||
fun getDestination(route: NavRoute) = destinations[route.route] | ||
} |
16 changes: 16 additions & 0 deletions
16
src/main/kotlin/com/mineinabyss/guiy/navigation/NavGraphBuilder.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.mineinabyss.guiy.navigation | ||
|
||
import androidx.compose.runtime.Composable | ||
|
||
class NavGraphBuilder { | ||
val destinations = mutableMapOf<String, @Composable (NavRoute) -> Unit>() | ||
fun build(startDestination: NavRoute): NavGraph = NavGraph( | ||
destinations, startDestination | ||
) | ||
} | ||
|
||
inline fun <reified T> NavGraphBuilder.composable(crossinline content: @Composable (T) -> Unit) { | ||
destinations[NavRoute.routeFor(T::class)] = { | ||
content(it.data as T) | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
src/main/kotlin/com/mineinabyss/guiy/navigation/NavHost.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.mineinabyss.guiy.navigation | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.collectAsState | ||
import androidx.compose.runtime.remember | ||
|
||
@Composable | ||
fun <T: Any> NavHost( | ||
navController: NavController, | ||
startDestination: T, | ||
builder: NavGraphBuilder.() -> Unit, | ||
) { | ||
BackHandler { navController.popBackStack() } | ||
val graph = remember(builder, navController, startDestination) { | ||
NavGraphBuilder().apply(builder).build(NavRoute.of(startDestination)) | ||
} | ||
val route = navController.route.collectAsState().value ?: graph.start | ||
graph.getDestination(route)?.invoke(route) ?: error("No navigation destination for ${route.route} was found") | ||
} |
15 changes: 15 additions & 0 deletions
15
src/main/kotlin/com/mineinabyss/guiy/navigation/NavRoute.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
package com.mineinabyss.guiy.navigation | ||
|
||
import androidx.compose.runtime.Immutable | ||
import kotlin.reflect.KClass | ||
|
||
@Immutable | ||
data class NavRoute( | ||
val route: String, | ||
val data: Any?, | ||
) { | ||
companion object { | ||
fun routeFor(kClass: KClass<*>) = "/${kClass.qualifiedName}" | ||
fun of(data: Any) = NavRoute(routeFor(data::class), data) | ||
} | ||
} |
77 changes: 0 additions & 77 deletions
77
src/main/kotlin/com/mineinabyss/guiy/navigation/Navigator.kt
This file was deleted.
Oops, something went wrong.