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

SavedStateHandle support #56

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
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
144 changes: 144 additions & 0 deletions KMMViewModel.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
1D43F3EC2ABAFCA600EB3DFE /* ObservableViewModelPublisher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D43F3EB2ABAFCA600EB3DFE /* ObservableViewModelPublisher.swift */; };
1D43F3EE2ABAFD7D00EB3DFE /* ObservableViewModelPublishers.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D43F3ED2ABAFD7D00EB3DFE /* ObservableViewModelPublishers.swift */; };
1D6641DD2A5175C3000180D7 /* ChildViewModels.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D6641DC2A5175C3000180D7 /* ChildViewModels.swift */; };
1D9474232B5084690064A727 /* SavedStateHandle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D9474222B5084690064A727 /* SavedStateHandle.swift */; };
1D9474272B5084BC0064A727 /* SavedStateObserver.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D9474262B5084BC0064A727 /* SavedStateObserver.swift */; };
1D9E9A8A2B51DB7C009F4D43 /* SavedStateManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1D9E9A892B51DB7C009F4D43 /* SavedStateManager.swift */; };
1DDAF21E293545DD0049C114 /* KMMViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1DDAF21D293545DD0049C114 /* KMMViewModel.swift */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -55,6 +58,10 @@
1D43F3EB2ABAFCA600EB3DFE /* ObservableViewModelPublisher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservableViewModelPublisher.swift; sourceTree = "<group>"; };
1D43F3ED2ABAFD7D00EB3DFE /* ObservableViewModelPublishers.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ObservableViewModelPublishers.swift; sourceTree = "<group>"; };
1D6641DC2A5175C3000180D7 /* ChildViewModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChildViewModels.swift; sourceTree = "<group>"; };
1D9474202B5083310064A727 /* KMMViewModelSavedState.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = KMMViewModelSavedState.framework; sourceTree = BUILT_PRODUCTS_DIR; };
1D9474222B5084690064A727 /* SavedStateHandle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SavedStateHandle.swift; sourceTree = "<group>"; };
1D9474262B5084BC0064A727 /* SavedStateObserver.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SavedStateObserver.swift; sourceTree = "<group>"; };
1D9E9A892B51DB7C009F4D43 /* SavedStateManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SavedStateManager.swift; sourceTree = "<group>"; };
1DDAF21D293545DD0049C114 /* KMMViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = KMMViewModel.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -82,6 +89,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
1D94741A2B5083310064A727 /* Frameworks */ = {
isa = PBXFrameworksBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
Expand All @@ -91,6 +105,7 @@
1D0DA7F9293369A80057DDAD /* KMMViewModelCore */,
1D198B282933BFD900EF778D /* KMMViewModelCoreObjC */,
1D0DA809293370E60057DDAD /* KMMViewModelSwiftUI */,
1D9474212B5083D10064A727 /* KMMViewModelSavedState */,
1D0DA7DE293367DE0057DDAD /* Products */,
1D0DA8112933715A0057DDAD /* Frameworks */,
);
Expand All @@ -102,6 +117,7 @@
1D0DA7F8293369A80057DDAD /* KMMViewModelCore.framework */,
1D0DA808293370E60057DDAD /* KMMViewModelSwiftUI.framework */,
1D198B272933BFD900EF778D /* KMMViewModelCoreObjC.framework */,
1D9474202B5083310064A727 /* KMMViewModelSavedState.framework */,
);
name = Products;
sourceTree = "<group>";
Expand Down Expand Up @@ -146,6 +162,16 @@
path = KMMViewModelCoreObjC;
sourceTree = "<group>";
};
1D9474212B5083D10064A727 /* KMMViewModelSavedState */ = {
isa = PBXGroup;
children = (
1D9474222B5084690064A727 /* SavedStateHandle.swift */,
1D9474262B5084BC0064A727 /* SavedStateObserver.swift */,
1D9E9A892B51DB7C009F4D43 /* SavedStateManager.swift */,
);
path = KMMViewModelSavedState;
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXHeadersBuildPhase section */
Expand All @@ -172,6 +198,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
1D9474142B5083310064A727 /* Headers */ = {
isa = PBXHeadersBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXHeadersBuildPhase section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -231,6 +264,24 @@
productReference = 1D198B272933BFD900EF778D /* KMMViewModelCoreObjC.framework */;
productType = "com.apple.product-type.framework";
};
1D9474112B5083310064A727 /* KMMViewModelSavedState */ = {
isa = PBXNativeTarget;
buildConfigurationList = 1D94741D2B5083310064A727 /* Build configuration list for PBXNativeTarget "KMMViewModelSavedState" */;
buildPhases = (
1D9474142B5083310064A727 /* Headers */,
1D9474152B5083310064A727 /* Sources */,
1D94741A2B5083310064A727 /* Frameworks */,
1D94741C2B5083310064A727 /* Resources */,
);
buildRules = (
);
dependencies = (
);
name = KMMViewModelSavedState;
productName = KMMViewModelSwiftUI;
productReference = 1D9474202B5083310064A727 /* KMMViewModelSavedState.framework */;
productType = "com.apple.product-type.framework";
};
/* End PBXNativeTarget section */

/* Begin PBXProject section */
Expand All @@ -251,6 +302,9 @@
1D198B262933BFD900EF778D = {
CreatedOnToolsVersion = 14.0.1;
};
1D9474112B5083310064A727 = {
LastSwiftMigration = 1500;
};
};
};
buildConfigurationList = 1D0DA7D7293367DE0057DDAD /* Build configuration list for PBXProject "KMMViewModel" */;
Expand All @@ -269,6 +323,7 @@
1D0DA7F7293369A80057DDAD /* KMMViewModelCore */,
1D198B262933BFD900EF778D /* KMMViewModelCoreObjC */,
1D0DA807293370E60057DDAD /* KMMViewModelSwiftUI */,
1D9474112B5083310064A727 /* KMMViewModelSavedState */,
);
};
/* End PBXProject section */
Expand All @@ -295,6 +350,13 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
1D94741C2B5083310064A727 /* Resources */ = {
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXResourcesBuildPhase section */

/* Begin PBXSourcesBuildPhase section */
Expand Down Expand Up @@ -329,6 +391,16 @@
);
runOnlyForDeploymentPostprocessing = 0;
};
1D9474152B5083310064A727 /* Sources */ = {
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1D9474272B5084BC0064A727 /* SavedStateObserver.swift in Sources */,
1D9E9A8A2B51DB7C009F4D43 /* SavedStateManager.swift in Sources */,
1D9474232B5084690064A727 /* SavedStateHandle.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
/* End PBXSourcesBuildPhase section */

/* Begin PBXTargetDependency section */
Expand Down Expand Up @@ -658,6 +730,69 @@
};
name = Release;
};
1D94741E2B5083310064A727 /* Debug */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = (
"@executable_path/../Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.rickclephas.kmm.KMMViewModelSavedState;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_OPTIMIZATION_LEVEL = "-Onone";
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Debug;
};
1D94741F2B5083310064A727 /* Release */ = {
isa = XCBuildConfiguration;
buildSettings = {
CLANG_ENABLE_MODULES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEFINES_MODULE = YES;
DYLIB_COMPATIBILITY_VERSION = 1;
DYLIB_CURRENT_VERSION = 1;
DYLIB_INSTALL_NAME_BASE = "@rpath";
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_NSHumanReadableCopyright = "";
INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks";
LD_RUNPATH_SEARCH_PATHS = (
"@executable_path/Frameworks",
"@loader_path/Frameworks",
);
"LD_RUNPATH_SEARCH_PATHS[sdk=macosx*]" = (
"@executable_path/../Frameworks",
"@loader_path/Frameworks",
);
MARKETING_VERSION = 1.0;
PRODUCT_BUNDLE_IDENTIFIER = com.rickclephas.kmm.KMMViewModelSavedState;
PRODUCT_NAME = "$(TARGET_NAME)";
SKIP_INSTALL = YES;
SWIFT_EMIT_LOC_STRINGS = YES;
SWIFT_VERSION = 5.0;
TARGETED_DEVICE_FAMILY = "1,2";
};
name = Release;
};
/* End XCBuildConfiguration section */

/* Begin XCConfigurationList section */
Expand Down Expand Up @@ -697,6 +832,15 @@
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
1D94741D2B5083310064A727 /* Build configuration list for PBXNativeTarget "KMMViewModelSavedState" */ = {
isa = XCConfigurationList;
buildConfigurations = (
1D94741E2B5083310064A727 /* Debug */,
1D94741F2B5083310064A727 /* Release */,
);
defaultConfigurationIsVisible = 0;
defaultConfigurationName = Release;
};
/* End XCConfigurationList section */
};
rootObject = 1D0DA7D4293367DE0057DDAD /* Project object */;
Expand Down
13 changes: 13 additions & 0 deletions KMMViewModelSavedState/SavedStateHandle.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//
// SavedStateHandle.swift
// KMMViewModelSavedState
//
// Created by Rick Clephas on 11/01/2024.
//

import Foundation

public protocol SavedStateHandle: AnyObject {
var data: Data? { get set }
func setStateChangedListener(listener: @escaping (Data?) -> Void)
}
63 changes: 63 additions & 0 deletions KMMViewModelSavedState/SavedStateManager.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// SavedStateManager.swift
// KMMViewModelSavedState
//
// Created by Rick Clephas on 12/01/2024.
//

import Combine
import SwiftUI

public extension View {

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
func savedStateManager() -> some View {
modifier(SavedStateManagerViewModifier())
}
}

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
private struct SavedStateManagerViewModifier: ViewModifier {

@SceneStorage("KMMViewModelSavedState") private var state: Data?
@StateObject private var manager = SavedStateManager()

func body(content: Content) -> some View {
content.environmentObject(manager).onReceive(manager.stateChanged) {
state = manager.getState()
}.onAppear {
manager.setState(state)
}
}
}

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
internal class SavedStateManager: ObservableObject {

let stateChanged = ObservableObjectPublisher()

private var states: [String:Data] = [:]
private var state: Data? = nil

func getState(for key: String) -> Data? { states[key] }

func setState(_ state: Data?, for key: String) {
if let state {
states[key] = state
} else {
states.removeValue(forKey: key)
}
self.state = try! NSKeyedArchiver.archivedData(withRootObject: states, requiringSecureCoding: true)
stateChanged.send()
}

func getState() -> Data? { state }

func setState(_ state: Data?) {
guard let state, self.state == nil else { return }
self.state = state
states = try! NSKeyedUnarchiver.unarchivedDictionary(
ofKeyClass: NSString.self, objectClass: NSData.self, from: state
) as [String : Data]? ?? [:]
}
}
63 changes: 63 additions & 0 deletions KMMViewModelSavedState/SavedStateObserver.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
//
// SavedStateObserver.swift
// KMMViewModelSavedState
//
// Created by Rick Clephas on 11/01/2024.
//

import Combine
import SwiftUI

public extension View {

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
func savedState(_ key: String, _ savedStateHandle: SavedStateHandle) -> some View {
modifier(SavedStateObserverViewModifier(key, savedStateHandle))
}
}

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
internal struct SavedStateObserverViewModifier: ViewModifier {

@EnvironmentObject private var manager: SavedStateManager
@StateObject private var observer: SavedStateObserver

init(_ key: String, _ savedStateHandle: SavedStateHandle) {
self._observer = StateObject(wrappedValue: {
SavedStateObserver(key, savedStateHandle)
}())
}

func body(content: Content) -> some View {
content.onAppear {
observer.setManager(manager)
}
}
}

@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
private class SavedStateObserver: ObservableObject {

private let key: String
private let savedStateHandle: SavedStateHandle
private var manager: SavedStateManager? = nil

init(_ key: String, _ savedStateHandle: SavedStateHandle) {
self.key = key
self.savedStateHandle = savedStateHandle
savedStateHandle.setStateChangedListener { [weak self] data in
guard let self else { return }
self.manager?.setState(data, for: key)
}
}

func setManager(_ manager: SavedStateManager) {
guard self.manager == nil else { return }
self.manager = manager
savedStateHandle.data = manager.getState(for: key)
}

deinit {
manager?.setState(nil, for: key)
}
}
Loading