From edc55ad736875557a10e33de0b77d5e062858a89 Mon Sep 17 00:00:00 2001 From: Osman Saral Date: Sat, 24 Aug 2024 16:43:26 +0300 Subject: [PATCH] reproduce ios version issue on #80 --- .../KMPObservableViewModelSampleApp.swift | 55 ++++++++++++++++--- .../sample/shared/TimeTravelViewModel.kt | 2 +- 2 files changed, 48 insertions(+), 9 deletions(-) diff --git a/sample/iosApp/KMPObservableViewModelSample/KMPObservableViewModelSampleApp.swift b/sample/iosApp/KMPObservableViewModelSample/KMPObservableViewModelSampleApp.swift index ff9a19e..4edd3ee 100644 --- a/sample/iosApp/KMPObservableViewModelSample/KMPObservableViewModelSampleApp.swift +++ b/sample/iosApp/KMPObservableViewModelSample/KMPObservableViewModelSampleApp.swift @@ -7,34 +7,73 @@ import SwiftUI -let useNavigationStack = false -let useScrollView = true -let useTextInput = true +let useNavigationStack = true +let setDeeplinkHandler = true +let useScrollView = false +let useTextInput = false + +class DeeplinkHandler: ObservableObject { + @Published var chatId: String? = nil +} @main struct KMPObservableViewModelSampleApp: App { @State private var shouldPresentContent: Bool = false + @StateObject var deeplinkHandler = DeeplinkHandler() var body: some Scene { WindowGroup { - NavigationStackCompat(useNavigationStack: useNavigationStack) { + NavigationStackCompat { Button { shouldPresentContent = true } label: { Text("Next Screen") } - .navigationDestinationCompat(useNavigationStack: useNavigationStack, isPresented: $shouldPresentContent) { - ContentView() + .navigationDestinationCompat(isPresented: $shouldPresentContent) { + ChatList() + } + } + .environmentObject(deeplinkHandler) + } + } +} + +struct ChatList: View { + @EnvironmentObject var deeplinkHandler: DeeplinkHandler + @State private var shouldPresentChat: Bool = false + @State private var selectedChat: String? = nil { + didSet { + shouldPresentChat = selectedChat != nil + } + } + + var body: some View { + VStack { + Text("Chat List") + ForEach(["chat1", "chat2", "chat3"], id: \.self) { chat in + Button { + selectedChat = chat + } label: { + Text(chat) } } } + .onAppear { + if setDeeplinkHandler { + selectedChat = deeplinkHandler.chatId + } + } + .navigationDestinationCompat(isPresented: $shouldPresentChat) { + if let chat = selectedChat { + ContentView() + } + } } } struct NavigationStackCompat: View where Content: View { - let useNavigationStack: Bool @ViewBuilder var content: () -> Content var body: some View { @@ -68,7 +107,7 @@ fileprivate struct NavigationDestinationModifier: ViewModifie } extension View { - func navigationDestinationCompat(useNavigationStack: Bool, isPresented: Binding, @ViewBuilder destination: () -> Destination) -> some View { + func navigationDestinationCompat(isPresented: Binding, @ViewBuilder destination: () -> Destination) -> some View { modifier(NavigationDestinationModifier(useNavigationStack: useNavigationStack, isPresented: isPresented, destination: destination())) } } diff --git a/sample/shared/src/commonMain/kotlin/com/rickclephas/kmp/observableviewmodel/sample/shared/TimeTravelViewModel.kt b/sample/shared/src/commonMain/kotlin/com/rickclephas/kmp/observableviewmodel/sample/shared/TimeTravelViewModel.kt index 3562fb1..183114a 100644 --- a/sample/shared/src/commonMain/kotlin/com/rickclephas/kmp/observableviewmodel/sample/shared/TimeTravelViewModel.kt +++ b/sample/shared/src/commonMain/kotlin/com/rickclephas/kmp/observableviewmodel/sample/shared/TimeTravelViewModel.kt @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.* import kotlin.random.Random open class TimeTravelViewModel: ViewModel() { - + init { Logger.i("TimeTravelViewModel") { "init $this" } }