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

StackPath #160

Draft
wants to merge 5 commits into
base: main
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
24 changes: 0 additions & 24 deletions Development/FluidInterfaceKit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,6 @@
4B01AFE0281939CB00A44537 /* DemoSheetViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoSheetViewController.swift; sourceTree = "<group>"; };
4B2AFC4B2A7EC243001B5BEF /* DemoDragViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoDragViewController.swift; sourceTree = "<group>"; };
4B2F619427B818E800363580 /* DemoListContainerViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DemoListContainerViewController.swift; sourceTree = "<group>"; };
4B2F9E9F27FC080000F74ADB /* UIKitPresentationTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIKitPresentationTests.swift; sourceTree = "<group>"; };
4B3BE33B27EDF6370060BAD3 /* FluidLocalEnvironmentTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FluidLocalEnvironmentTests.swift; sourceTree = "<group>"; };
4B46804B279C58E800625FCB /* TranslationVelocityPlaygroundViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TranslationVelocityPlaygroundViewController.swift; sourceTree = "<group>"; };
4B46804C279C58E800625FCB /* DemoTransitionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DemoTransitionViewController.swift; sourceTree = "<group>"; };
4B46804D279C58E800625FCB /* AnimatorPlaygroundViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AnimatorPlaygroundViewController.swift; sourceTree = "<group>"; };
Expand All @@ -96,10 +94,7 @@
4B46805F279C58E800625FCB /* DemoApplicationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DemoApplicationViewController.swift; sourceTree = "<group>"; };
4B468060279C58E800625FCB /* ScalingVelocityPlaygroundViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScalingVelocityPlaygroundViewController.swift; sourceTree = "<group>"; };
4B4E93DE29810AAC0060C775 /* DemoStageViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = DemoStageViewController.swift; sourceTree = "<group>"; };
4B69049527B6685F0088FDDD /* FluidViewControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FluidViewControllerTests.swift; sourceTree = "<group>"; };
4B6F87C927B599B100216F4E /* UINavigationItemTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UINavigationItemTests.swift; sourceTree = "<group>"; };
4B7410B827ACE975003D8AB5 /* DemoCompositionOrderViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoCompositionOrderViewController.swift; sourceTree = "<group>"; };
4B79ABBC27E7CB9A008DE44F /* UIViewControllerExtentionTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UIViewControllerExtentionTests.swift; sourceTree = "<group>"; };
4B853AB229D9940100F62504 /* DemoPopoverSwiftUIViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoPopoverSwiftUIViewController.swift; sourceTree = "<group>"; };
4B853AB429D9A32A00F62504 /* DemoPopoverViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoPopoverViewController.swift; sourceTree = "<group>"; };
4B959B062764F77E0003FFB6 /* FluidInterfaceKit-Demo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "FluidInterfaceKit-Demo.app"; sourceTree = BUILT_PRODUCTS_DIR; };
Expand All @@ -112,10 +107,7 @@
4BC0A15727C178A300CD3A46 /* FirstViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FirstViewController.swift; sourceTree = "<group>"; };
4BC0A15927C178C000CD3A46 /* SecondViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SecondViewController.swift; sourceTree = "<group>"; };
4BC57A3B29D70A9400CA32A9 /* DemoPortalStackViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoPortalStackViewController.swift; sourceTree = "<group>"; };
4BC667B927B2E69C00182E6C /* FluidInterfaceKitTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FluidInterfaceKitTests.swift; sourceTree = "<group>"; };
4BC7B20C284E3C7F0087BC7A /* DemoPictureInPictureCoolController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DemoPictureInPictureCoolController.swift; sourceTree = "<group>"; };
4BC8F5F727B6B1DA00095412 /* TreeBuilder.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TreeBuilder.swift; sourceTree = "<group>"; };
4BD2A2B627B6A9AC00E20F07 /* FluidStackControllerTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FluidStackControllerTests.swift; sourceTree = "<group>"; };
4BE0AAC4277FD5620086FD7E /* */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = ""; sourceTree = "<group>"; };
4BF417742AD910E700F6E561 /* FluidInterfaceKit-work1 */ = {isa = PBXFileReference; lastKnownFileType = wrapper; name = "FluidInterfaceKit-work1"; path = ..; sourceTree = "<group>"; };
4BFC696127A7F3BA0011238E /* FluidInterfaceKit-Demo-Bridging-Header.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = "FluidInterfaceKit-Demo-Bridging-Header.h"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -171,7 +163,6 @@
children = (
4BC0A14727C1731D00CD3A46 /* ExampleFluidApp */,
4B468049279C58E800625FCB /* FluidInterfaceKit-Demo */,
4BC667B827B2E69C00182E6C /* FluidInterfaceKitTests */,
);
path = Sources;
sourceTree = "<group>";
Expand Down Expand Up @@ -266,21 +257,6 @@
path = ExampleFluidApp;
sourceTree = "<group>";
};
4BC667B827B2E69C00182E6C /* FluidInterfaceKitTests */ = {
isa = PBXGroup;
children = (
4BC8F5F727B6B1DA00095412 /* TreeBuilder.swift */,
4BC667B927B2E69C00182E6C /* FluidInterfaceKitTests.swift */,
4B6F87C927B599B100216F4E /* UINavigationItemTests.swift */,
4B69049527B6685F0088FDDD /* FluidViewControllerTests.swift */,
4BD2A2B627B6A9AC00E20F07 /* FluidStackControllerTests.swift */,
4B79ABBC27E7CB9A008DE44F /* UIViewControllerExtentionTests.swift */,
4B2F9E9F27FC080000F74ADB /* UIKitPresentationTests.swift */,
4B3BE33B27EDF6370060BAD3 /* FluidLocalEnvironmentTests.swift */,
);
path = FluidInterfaceKitTests;
sourceTree = "<group>";
};
FCEA76EB29349CA600E8B0E6 /* DemoFloatingDisplayKit */ = {
isa = PBXGroup;
children = (
Expand Down
17 changes: 15 additions & 2 deletions Sources/FluidStack/ViewController/FluidStackController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ public struct FluidStackContentConfiguration {

}


/// A container view controller that manages view controller and view as child view controllers.
/// It provides transitions when adding and removing.
///
Expand All @@ -55,7 +56,9 @@ open class FluidStackController: UIViewController {
}

// MARK: - Properties


public private(set) var path: FluidStackPath = .init()

/// A closure that receives ``Action``
public final var stackActionHandler: (Action) -> Void = { _ in }

Expand Down Expand Up @@ -87,7 +90,17 @@ open class FluidStackController: UIViewController {
didSet {

if stackingItems != oldValue {


let components = stackingItems.map { item -> FluidStackPath.Component in
if let identifiable = item.viewController as? (any FluidIdentifiableViewController) {
return .identifiable(.init(identifiable))
} else {
return .volatile(.init(objectIdentifier: .init(item.viewController), ref: item.viewController))
}
}

self.path = .init(components: components)

// TODO: Update with animation
UIViewPropertyAnimator(duration: 0.4, dampingRatio: 1) {
self.setNeedsStatusBarAppearanceUpdate()
Expand Down
75 changes: 75 additions & 0 deletions Sources/FluidStack/ViewController/FluidStackPath.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import UIKit

public struct FluidStackPath: Equatable {

public enum Component: Equatable {

public struct Volatile: Equatable {
public let objectIdentifier: ObjectIdentifier
public private(set) weak var ref: UIViewController?
}

public struct Identifiable: Equatable {
public static func == (lhs: Self, rhs: Self) -> Bool {
guard lhs.id == rhs.id else { return false }
guard lhs.viewControllerType == rhs.viewControllerType else { return false }
return true
}

public let id: AnyHashable
public let viewControllerType: UIViewController.Type

@MainActor
public init(_ viewController: some FluidIdentifiableViewController) {
self.id = viewController.fluidIdentifier as AnyHashable
self.viewControllerType = type(of: viewController)
}

public func restore<Target: FluidIdentifiableViewController>(_ target: Target.Type) -> Target.FluidID? {
guard viewControllerType == target else { return nil }
guard let id = id as? Target.FluidID else { return nil }
return id
}
}

case volatile(Volatile)
case identifiable(AnyFluidIdentifiableViewController)
}

public private(set) var components: [Component] = []

public init() {

}

public init(components: [Component]) {
self.components = components
}

public mutating func append(_ component: Component) {
components.append(component)
}

public mutating func removeLast(_ k: Int = 1) {
components.removeLast(k)
}
}

public struct AnyFluidIdentifiableViewController: Equatable {

public var base: AnyHashable

public init(_ base: some FluidIdentifiableViewController) {
self.base = base as AnyHashable
}

}

@MainActor
public protocol FluidIdentifiableViewController: UIViewController {

associatedtype FluidID: Hashable

var fluidIdentifier: FluidID { get }

}
26 changes: 26 additions & 0 deletions Tests/FluidStackTests/FluidStackPathTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import FluidStack
import XCTest

@MainActor
final class FluidStackPathTests: XCTestCase {

func test_identifiable() {

class A: UIViewController, FluidIdentifiableViewController {
var fluidIdentifier: String { "1" }
}

class B: UIViewController, FluidIdentifiableViewController {
var fluidIdentifier: String { "1" }
}

XCTAssertNotEqual(
FluidStackPath.Component.Identifiable(A()),
FluidStackPath.Component.Identifiable(B())
)

XCTAssertNotNil(FluidStackPath.Component.Identifiable(A()).restore(A.self))
XCTAssertNil(FluidStackPath.Component.Identifiable(A()).restore(B.self))

}
}