-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update Interactor and new ViewModelInteractor to support @StateObject
See `Interactor` documentation for details. In summary, this addresses the issue where duplicate interactor instances might be retained as a result of SwiftUI updating a view. The view must declare and reference the interactor as a `@StateObject`. This allows different instances of the view to reference the same interactor instance, therefore maintaining the states of the view's data. This occurs when multiple instances of the same view are instantiated over time by SwiftUI, as the view updates and changes due to data changes or user interactions. And because the interactor is a `@StateObject`, the binding of the interactor via the `bind(observer:)` modifier must be inside the view's `body`.
- Loading branch information
Showing
6 changed files
with
320 additions
and
84 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2022 Yi Wang | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
/// The observer of a SwiftUI view's lifecycle events. | ||
/// | ||
/// This protocol should be used in conjunction with the `bind(observer:)` function of a `View`. This allows the | ||
/// implementation of this protocol to receive the view's various lifecycle events to perform business logic accordingly. | ||
/// | ||
/// This protocol conforms to `ObservableObject` to support retaining this instance as a `@StateObject` in the | ||
/// view that performs the `bind(observer:)` function. | ||
public protocol ViewWithModelLifecycleObserver: ObservableObject { | ||
/// The model of the view that this observer may mutate to provide data to the view. | ||
associatedtype ViewModelType: ViewModel | ||
|
||
/// Notify the observer when the bound `View` has appeared. | ||
/// | ||
/// - Parameters: | ||
/// - viewModel: The model of the view that this observer may mutate to provide data to the view. | ||
func viewDidAppear(viewModel: ViewModelType) | ||
|
||
/// Notify the observer when the bound `View` has disappeared. | ||
/// | ||
/// - Parameters: | ||
/// - viewModel: The model of the view that this observer may mutate to provide data to the view. | ||
func viewDidDisappear(viewModel: ViewModelType) | ||
} | ||
|
||
extension View { | ||
/// Bind the given lifecycle observer to this view. | ||
/// | ||
/// - Parameters: | ||
/// - observer: The observer to be bound and receive this view's lifecycle events. | ||
/// - viewModel: The model of this view that this observer may mutate to provide data to the view. | ||
/// - Returns: This view with the observer bound. | ||
public func bind<Observer: ViewWithModelLifecycleObserver>( | ||
observer: Observer, | ||
viewModel: Observer.ViewModelType | ||
) -> some View { | ||
onAppear { | ||
observer.viewDidAppear(viewModel: viewModel) | ||
} | ||
.onDisappear { | ||
observer.viewDidDisappear(viewModel: viewModel) | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// MIT License | ||
// | ||
// Copyright (c) 2022 Yi Wang | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in all | ||
// copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
// SOFTWARE. | ||
|
||
import Combine | ||
import Foundation | ||
|
||
/// An `Interactor` that has an associated view model used to transform the data from this interactor to presentation | ||
/// data for the corresponding view to display. | ||
/// | ||
/// - Important: Please see `Interactor` documentation for binding requirements. | ||
open class ViewModelInteractor<ViewModelType: ViewModel>: Interactor { | ||
/// Override this function to setup the subscriptions this interactor requires on start. | ||
/// | ||
/// All the created subscriptions returned from this function are bound to the deinit lifecycle of this interactor. | ||
/// | ||
/// class MyInteractor: Interactor { | ||
/// @CancellableBuilder | ||
/// override func onLoad(viewModel: MyViewModel) -> [AnyCancellable] { | ||
/// myDataStream | ||
/// .sink {...} | ||
/// | ||
/// mySecondDataStream | ||
/// .sink {...} | ||
/// } | ||
/// } | ||
/// | ||
/// - Parameters: | ||
/// - viewModel: The view model used to transform and provide presentation data for the corresponding view to | ||
/// display. | ||
/// - Returns: An array of subscription `AnyCancellable`. | ||
@CancellableBuilder | ||
open func onLoad(viewModel: ViewModelType) -> [AnyCancellable] {} | ||
|
||
/// Override this function to perform logic or setup the subscriptions when the view has appeared. | ||
/// | ||
/// All the created subscriptions returned from this function are bound to the disappearance of this interactor's | ||
/// corresponding view. | ||
/// | ||
/// class MyInteractor: Interactor { | ||
/// @CancellableBuilder | ||
/// override func onViewAppear(viewModel: MyViewModel) -> [AnyCancellable] { | ||
/// myDataStream | ||
/// .sink {...} | ||
/// | ||
/// mySecondDataStream | ||
/// .sink {...} | ||
/// } | ||
/// } | ||
/// | ||
/// - Parameters: | ||
/// - viewModel: The view model used to transform and provide presentation data for the corresponding view to | ||
/// display. | ||
/// - Returns: An array of subscription `AnyCancellable`. | ||
@CancellableBuilder | ||
open func onViewAppear(viewModel: ViewModelType) -> [AnyCancellable] {} | ||
|
||
/// Override this function to perform logic when the interactor's view disappears. | ||
/// | ||
/// - Parameters: | ||
/// - viewModel: The view model used to transform and provide presentation data for the corresponding view to | ||
/// display. | ||
open func onViewDisappear(viewModel: ViewModelType) {} | ||
} | ||
|
||
// MARK: - ViewWithModelLifecycleObserver Conformance | ||
|
||
extension ViewModelInteractor: ViewWithModelLifecycleObserver { | ||
public final func viewDidAppear(viewModel: ViewModelType) { | ||
let occurrence = processViewDidAppear() | ||
guard occurrence != .invalid else { | ||
return | ||
} | ||
|
||
if occurrence == .firstTime { | ||
deinitCancelBag.store(onLoad(viewModel: viewModel)) | ||
} | ||
|
||
viewAppearanceCancelBag.store(onViewAppear(viewModel: viewModel)) | ||
} | ||
|
||
public final func viewDidDisappear(viewModel: ViewModelType) { | ||
let occurrence = processViewDidDisappear() | ||
guard occurrence != .invalid else { | ||
return | ||
} | ||
|
||
onViewDisappear(viewModel: viewModel) | ||
} | ||
} |
Oops, something went wrong.