// // Copyright 2022 New Vector Ltd // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // import Foundation /// A CoordinatorProtocol wrapper and type erasing component that allows /// dynamically presenting arbitrary screens @MainActor class NavigationModule: Identifiable, Hashable { let id = UUID() /// The NavigationStack has a tendency to hold on to path items for longer than needed. We work around that by manually nilling the coordinator /// when a NavigationModule is dismissed, reason why this is an optional property /// As the NavigationModule is just a wrapper multiple instances of it continuing living is of no consequence /// https://stackoverflow.com/questions/73885353/found-a-strange-behaviour-of-state-when-combined-to-the-new-navigation-stack/ var coordinator: (any CoordinatorProtocol)? var dismissalCallback: (() -> Void)? init(_ coordinator: any CoordinatorProtocol, dismissalCallback: (() -> Void)? = nil) { self.coordinator = coordinator self.dismissalCallback = dismissalCallback } func tearDown() { coordinator?.stop() coordinator = nil let callback = dismissalCallback dismissalCallback = nil callback?() } nonisolated static func == (lhs: NavigationModule, rhs: NavigationModule) -> Bool { lhs.id == rhs.id } nonisolated func hash(into hasher: inout Hasher) { hasher.combine(id) } }