Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #16

Merged
merged 4 commits into from
May 2, 2024
Merged

Dev #16

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions composeApp/src/androidMain/kotlin/SystemNavigation.android.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ui.navigation.Screen

class AndroidSystemNavigation : SystemNavigation {
override fun navigateTo(screen: Screen) {}
override fun navigateBack() {}
}

actual fun systemNavigation(): SystemNavigation = AndroidSystemNavigation()
8 changes: 8 additions & 0 deletions composeApp/src/commonMain/kotlin/SystemNavigation.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ui.navigation.Screen

interface SystemNavigation {
fun navigateTo(screen: Screen)
fun navigateBack()
}

expect fun systemNavigation(): SystemNavigation
4 changes: 3 additions & 1 deletion composeApp/src/commonMain/kotlin/di/AppModule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ivy.di.Di
import ivy.di.Di.register
import ivy.di.Di.singleton
import ivy.di.DiModule
import systemNavigation
import ui.navigation.Navigation
import util.DispatchersProvider
import util.DispatchersProviderImpl
Expand All @@ -12,7 +13,8 @@ object AppModule : DiModule {

override fun init() {
Di.appScope {
singleton { Navigation() }
register { systemNavigation() }
singleton { Navigation(Di.get()) }
register<DispatchersProvider> { DispatchersProviderImpl() }
}
}
Expand Down
9 changes: 7 additions & 2 deletions composeApp/src/commonMain/kotlin/ui/navigation/Navigation.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package ui.navigation

import SystemNavigation
import androidx.compose.runtime.*
import kotlinx.collections.immutable.persistentListOf
import kotlinx.collections.immutable.plus
import kotlinx.collections.immutable.toPersistentList

class Navigation {
class Navigation(private val systemNavigation: SystemNavigation) {
private var backstack by mutableStateOf(persistentListOf<Screen>())

@Composable
Expand All @@ -18,6 +19,7 @@ class Navigation {

fun navigate(screen: Screen) {
backstack = backstack.plus(screen.also(Screen::initialize))
systemNavigation.navigateTo(screen)
}

fun backUntil(predicate: (Screen) -> Boolean) {
Expand All @@ -30,6 +32,9 @@ class Navigation {
fun back(): Screen? {
val lastScreen = backstack.lastOrNull()
backstack = backstack.dropLast(1).toPersistentList()
return lastScreen?.also(Screen::destroy)
return lastScreen?.also {
it.destroy()
systemNavigation.navigateBack()
}
}
}
2 changes: 2 additions & 0 deletions composeApp/src/commonMain/kotlin/ui/navigation/Screen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import kotlinx.coroutines.SupervisorJob

abstract class Screen {

abstract val path: String

private lateinit var job: CompletableJob
protected lateinit var screenScope: CoroutineScope

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
package ui.screen.debug

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyListScope
import androidx.compose.material.Text
Expand All @@ -18,15 +14,11 @@ import component.LearnScaffold
import ivy.di.Di
import ui.navigation.Navigation
import ui.navigation.Screen
import ui.theme.Blue
import ui.theme.BlueVariant
import ui.theme.Gray
import ui.theme.Green
import ui.theme.Orange
import ui.theme.OrangeVariant
import ui.theme.Red
import ui.theme.*

class ColorDemoScreen : Screen() {
override val path: String = "colorDemo"

private val navigation: Navigation = Di.get()

override fun onDi(): Di.ScreenScope.() -> Unit = {}
Expand Down
2 changes: 2 additions & 0 deletions composeApp/src/commonMain/kotlin/ui/screen/home/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import ui.navigation.Screen
import ui.screen.home.composable.HomeContent

class HomeScreen : Screen() {
override val path: String = "home"

override fun onDi(): Di.ScreenScope.() -> Unit = {
register { HomeViewModel(Di.get()) }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import ui.navigation.Screen
import ui.screen.intro.composable.IntroContent

class IntroScreen : Screen() {
override val path: String = "intro"

override fun onDi(): Di.ScreenScope.() -> Unit = {
register { IntroViewModel(Di.get()) }
}
Expand Down
8 changes: 8 additions & 0 deletions composeApp/src/desktopMain/kotlin/SystemNavigation.desktop.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import ui.navigation.Screen

class DesktopSystemNavigation : SystemNavigation {
override fun navigateTo(screen: Screen) {}
override fun navigateBack() {}
}

actual fun systemNavigation(): SystemNavigation = DesktopSystemNavigation()
9 changes: 9 additions & 0 deletions composeApp/src/iosMain/kotlin/SystemNavigation.ios.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import ui.navigation.Screen

class IOSSystemNavigation : SystemNavigation {
override fun navigateTo(screen: Screen) {}

override fun navigateBack() {}
}

actual fun systemNavigation(): SystemNavigation = IOSSystemNavigation()
20 changes: 20 additions & 0 deletions composeApp/src/jsMain/kotlin/SystemNavigation.js.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import kotlinx.browser.window
import ui.navigation.Screen

class WebSystemNavigation : SystemNavigation {
override fun navigateTo(screen: Screen) {
// TODO: Temporary workaround until we support deep links
val path = "" // "/${screen.path}"

val stateObject = js("({})")
// TODO: Temporary workaround until we support deep links
stateObject.screen = "" //screen.path
window.history.pushState(stateObject, "", path)
}

override fun navigateBack() {
window.history.back()
}
}

actual fun systemNavigation(): SystemNavigation = WebSystemNavigation()
13 changes: 13 additions & 0 deletions composeApp/src/jsMain/kotlin/main.js.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import androidx.compose.ui.ExperimentalComposeUiApi
import androidx.compose.ui.window.CanvasBasedWindow
import ivy.di.Di
import kotlinx.browser.window
import org.jetbrains.skiko.wasm.onWasmReady
import ui.navigation.Navigation

@OptIn(ExperimentalComposeUiApi::class)
fun main() {
Expand All @@ -9,4 +12,14 @@ fun main() {
App()
}
}
setupBackNavigationHandler()
}

fun setupBackNavigationHandler() {
window.addEventListener("popstate", {
val navigation = Di.get<Navigation>()
if (navigation.backstack().size > 1) {
navigation.back()
}
})
}
7 changes: 6 additions & 1 deletion server/src/main/kotlin/ivy/learn/api/LessonsApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package ivy.learn.api

import arrow.core.raise.ensureNotNull
import io.ktor.server.routing.*
import io.ktor.util.*
import ivy.learn.api.common.Api
import ivy.learn.api.common.endpoint
import ivy.learn.api.common.model.ServerError
Expand All @@ -13,7 +14,11 @@ class LessonsApi(
private val repository: LessonsRepository
) : Api {
override fun Routing.endpoints() {
// Endpoint that gets a lesson by ID
lessonById()
}

@KtorDsl
private fun Routing.lessonById() {
get("/lessons/{id}", endpoint<Lesson> { params ->
val lessonId = params["id"]?.let(::LessonId)
ensureNotNull(lessonId) { ServerError.BadRequest("Lesson id is missing!") }
Expand Down
1 change: 0 additions & 1 deletion shared/src/androidMain/kotlin/Platform.android.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import io.ktor.client.engine.android.*

class AndroidPlatform : Platform {
override val name: String = "Android ${Build.VERSION.SDK_INT}"
override val debug: Boolean = false

override fun log(level: LogLevel, msg: String) {
val tag = ""
Expand Down
1 change: 0 additions & 1 deletion shared/src/commonMain/kotlin/Platform.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import io.ktor.client.*

interface Platform {
val name: String
val debug: Boolean
fun log(level: LogLevel, msg: String)
fun httpClient(config: HttpClientConfig<*>.() -> Unit = {}): HttpClient
}
Expand Down
1 change: 0 additions & 1 deletion shared/src/iosMain/kotlin/Platform.ios.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import platform.UIKit.UIDevice

class IOSPlatform : Platform {
override val name: String = UIDevice.currentDevice.systemName() + " " + UIDevice.currentDevice.systemVersion
override val debug: Boolean = false

override fun log(level: LogLevel, msg: String) {
println("${level.name}: $msg")
Expand Down
1 change: 0 additions & 1 deletion shared/src/jsMain/kotlin/Platform.js.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import io.ktor.client.engine.js.*

class JsPlatform : Platform {
override val name: String = "Web with Kotlin/JS"
override val debug: Boolean = false

override fun log(level: LogLevel, msg: String) {
console.log("${level.name}: $msg")
Expand Down
1 change: 0 additions & 1 deletion shared/src/jvmMain/kotlin/Platform.jvm.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import io.ktor.client.engine.java.*

class JVMPlatform: Platform {
override val name: String = "Java ${System.getProperty("java.version")}"
override val debug: Boolean = false

override fun log(level: LogLevel, msg: String) {
println("${level.name}: $msg")
Expand Down