Skip to content

Commit

Permalink
reproduce ios version issue on rickclephas#80
Browse files Browse the repository at this point in the history
  • Loading branch information
osrl committed Aug 24, 2024
1 parent 46f11e3 commit edc55ad
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Content>: View where Content: View {

let useNavigationStack: Bool
@ViewBuilder var content: () -> Content

var body: some View {
Expand Down Expand Up @@ -68,7 +107,7 @@ fileprivate struct NavigationDestinationModifier<Destination: View>: ViewModifie
}

extension View {
func navigationDestinationCompat<Destination: View>(useNavigationStack: Bool, isPresented: Binding<Bool>, @ViewBuilder destination: () -> Destination) -> some View {
func navigationDestinationCompat<Destination: View>(isPresented: Binding<Bool>, @ViewBuilder destination: () -> Destination) -> some View {
modifier(NavigationDestinationModifier(useNavigationStack: useNavigationStack, isPresented: isPresented, destination: destination()))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import kotlinx.coroutines.flow.*
import kotlin.random.Random

open class TimeTravelViewModel: ViewModel() {

init {
Logger.i("TimeTravelViewModel") { "init $this" }
}
Expand Down

0 comments on commit edc55ad

Please sign in to comment.