Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,18 @@ class DIContainer {
)
}()

// MARK: - Store

lazy var storeFactory: StoreFactory = {
return DefaultStoreFactory(version: "1.0.0")
}()

lazy var storeManager: StoreManager = {
return DefaultStoreManager(storeFactory: storeFactory)
}()

// MARK: SDK

lazy var componentRegistry: ComponentRegistry = {
let registry = ComponentRegistry()
let renderers: [Renderer] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import Supabase
// Public interface for the SDK
class RenderSDK {
static let shared = RenderSDK()

private let client = DIContainer.shared.supabaseClient
private let componentRegistry = DIContainer.shared.componentRegistry
private let scenarioFetcher = DIContainer.shared.scenarioService
private let storeManager = DIContainer.shared.storeManager

private init() {}

Expand Down Expand Up @@ -69,4 +70,41 @@ class RenderSDK {
)
return vc
}

// MARK: - Store API

/// Get a store for app-scoped data
public func getAppStore(storage: Storage = .userPrefs()) -> Store {
storeManager.getStore(scope: .app, storage: storage)
}

/// Get a store for scenario-scoped data
public func getScenarioStore(scenarioID: String, storage: Storage = .memory) -> Store {
storeManager.getStore(scope: .scenario(id: scenarioID), storage: storage)
}

/// Configure stores for a scenario session
public func configureScenarioStores(scenarioID: String) {
storeManager.configureScenarioStores(scenarioID: scenarioID)
}

/// Clean up scenario stores when scenario ends
public func cleanupScenarioStores(scenarioID: String) {
storeManager.cleanupScenarioStores(scenarioID: scenarioID)
}

/// Reset all stores for a specific scope
public func resetStores(for scope: Scope) {
storeManager.resetStores(for: scope)
}

/// Reset all stores
public func resetAllStores() {
storeManager.resetAllStores()
}

/// Handle version changes
public func handleVersionChange(from oldVersion: SemanticVersion, to newVersion: SemanticVersion) {
storeManager.handleVersionChange(from: oldVersion, to: newVersion)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,17 @@ public protocol RenderViewControllerDelegate: AnyObject {

public class RenderViewController: UIViewController, ScenarioObserver {
weak public var delegate: RenderViewControllerDelegate?

let scenarioID: String
private var scenario: Scenario?
private let repository = DIContainer.shared.scenarioRepository
private let service = DIContainer.shared.scenarioService
private let registry = DIContainer.shared.componentRegistry
private let storeManager = DIContainer.shared.storeManager

// Store instances for this scenario
private var appStore: Store?
private var scenarioStore: Store?

// Root flex container
private let rootFlexContainer = UIView()
Expand All @@ -38,7 +43,8 @@ public class RenderViewController: UIViewController, ScenarioObserver {
super.viewDidLoad()
view.backgroundColor = .white
setupFlexContainer()

setupStores()

if let scenario = scenario {
buildViewHierarchy(from: scenario.mainComponent)
} else {
Expand All @@ -56,10 +62,18 @@ public class RenderViewController: UIViewController, ScenarioObserver {

public override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)

Task {
await repository.unsubscribeFromScenario(self)
}

// Clean up stores when view disappears
cleanupStores()
}

deinit {
// Final cleanup
cleanupStores()
}

override public func viewDidLayoutSubviews() {
Expand Down Expand Up @@ -144,4 +158,35 @@ public class RenderViewController: UIViewController, ScenarioObserver {

return view
}

// MARK: - Store Management

private func setupStores() {
// Initialize app store (shared across all scenarios)
appStore = storeManager.getStore(scope: .app, storage: .userPrefs())

// Initialize scenario store (scoped to this scenario)
scenarioStore = storeManager.getStore(scope: .scenario(id: scenarioID), storage: .memory)

// Configure scenario stores
storeManager.configureScenarioStores(scenarioID: scenarioID)
}

private func cleanupStores() {
// Clean up scenario-specific stores
storeManager.cleanupScenarioStores(scenarioID: scenarioID)

// Clear references
scenarioStore = nil
}

/// Get the app-scoped store for global data
public func getAppStore() -> Store? {
appStore
}

/// Get the scenario-scoped store for scenario-specific data
public func getScenarioStore() -> Store? {
scenarioStore
}
}
Loading