Skip to content

Commit

Permalink
Use StateObject if available
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoya committed Dec 27, 2020
1 parent 2e3b566 commit bad983f
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 12 deletions.
4 changes: 4 additions & 0 deletions Example/SwiftUIPagerExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

/* Begin PBXBuildFile section */
1751176A2573D93F00D809CF /* PagerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 175117692573D93F00D809CF /* PagerModel.swift */; };
176F05952598BB380002024B /* ViewProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 176F05942598BB380002024B /* ViewProxy.swift */; };
17D9E0F423D4CF6700C5AE93 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17D9E0F323D4CF6700C5AE93 /* AppDelegate.swift */; };
17D9E0F623D4CF6700C5AE93 /* SceneDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17D9E0F523D4CF6700C5AE93 /* SceneDelegate.swift */; };
17D9E0F823D4CF6700C5AE93 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17D9E0F723D4CF6700C5AE93 /* ContentView.swift */; };
Expand Down Expand Up @@ -52,6 +53,7 @@

/* Begin PBXFileReference section */
175117692573D93F00D809CF /* PagerModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PagerModel.swift; path = ../../Sources/SwiftUIPager/PagerModel.swift; sourceTree = "<group>"; };
176F05942598BB380002024B /* ViewProxy.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewProxy.swift; sourceTree = "<group>"; };
17D9E0F023D4CF6700C5AE93 /* SwiftUIPagerExample.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = SwiftUIPagerExample.app; sourceTree = BUILT_PRODUCTS_DIR; };
17D9E0F323D4CF6700C5AE93 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
17D9E0F523D4CF6700C5AE93 /* SceneDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SceneDelegate.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -170,6 +172,7 @@
6BD3828124C97DE3007B1CF6 /* CGPoint+Angle.swift */,
6B9C4A8924B45F66004C06C5 /* OnDeactivateModifier.swift */,
6BC5EDF724866D9500E1E78C /* View+Helper.swift */,
176F05942598BB380002024B /* ViewProxy.swift */,
);
name = Helpers;
path = ../../Sources/SwiftUIPager/Helpers;
Expand Down Expand Up @@ -278,6 +281,7 @@
6BEA731624ACF8D7007EA8DC /* GesturePriority.swift in Sources */,
6B35B6C125346610000D618F /* PaginationSensitivity.swift in Sources */,
1751176A2573D93F00D809CF /* PagerModel.swift in Sources */,
176F05952598BB380002024B /* ViewProxy.swift in Sources */,
6BEA731324ACF8D7007EA8DC /* PositionAlignment.swift in Sources */,
6BEA731424ACF8D7007EA8DC /* SwipeDirection.swift in Sources */,
6BC5EE0224866D9500E1E78C /* PagerContent+Helper.swift in Sources */,
Expand Down
45 changes: 45 additions & 0 deletions Sources/SwiftUIPager/Helpers/ViewProxy.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ViewProxy.swift
// SwiftUIPagerExample
//
// Created by Fernando Moya de Rivas on 27/12/20.
// Copyright © 2020 Fernando Moya de Rivas. All rights reserved.
//

import SwiftUI

/// Proxy to inject an `ObservableObject` as a `StateObject`
@available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *)
struct StateObjectViewProxy<Property: ObservableObject, Content: View>: View {

@StateObject private var observableProperty: Property
let content: (Property) -> Content

init(property: Property, @ViewBuilder content: @escaping (Property) -> Content) {
self._observableProperty = StateObject(wrappedValue: property)
self.content = content
}

var body: some View {
content(observableProperty)
}

}

/// Proxy to inject an `ObservableObject` as a `ObservedObject`
@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
struct ObservedObjectViewProxy<Property: ObservableObject, Content: View>: View {

@ObservedObject private var observableProperty: Property
let content: (Property) -> Content

init(property: Property, @ViewBuilder content: @escaping (Property) -> Content) {
self.observableProperty = property
self.content = content
}

var body: some View {
content(observableProperty)
}

}
18 changes: 12 additions & 6 deletions Sources/SwiftUIPager/Pager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,6 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:

/*** State and Binding properties ***/

/// Size of the view
@State var size: CGSize = .zero

/// `swipeGesture` translation on the X-Axis
@State var draggingOffset: CGFloat = 0

Expand All @@ -174,7 +171,7 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
}
}

@ObservedObject var pagerModel: PagerModel
let pagerModel: PagerModel

/// Initializes a new `Pager`.
///
Expand All @@ -193,7 +190,6 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
public var body: some View {
GeometryReader { proxy in
self.content(for: proxy.size)
.environmentObject(pagerModel)
.onReceive(pagerModel.$page) { (page) in
guard self.page != page else { return }
self.page = page
Expand All @@ -202,7 +198,7 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
.clipped()
}

func content(for size: CGSize) -> PagerContent {
func pagerContent(_ pagerModel: PagerModel, size: CGSize) -> PagerContent {
var pagerContent =
PagerContent(size: size,
pagerModel: pagerModel,
Expand Down Expand Up @@ -245,6 +241,16 @@ public struct Pager<Element, ID, PageView>: View where PageView: View, Element:
return pagerContent
}

func content(for size: CGSize) -> some View {
Group {
if #available(iOS 14.0, macOS 11.0, tvOS 14.0, watchOS 7.0, *) {
StateObjectViewProxy(property: pagerModel) { pagerContent($0, size: size) }
} else {
ObservedObjectViewProxy(property: pagerModel) { pagerContent($0, size: size) }
}
}
}

}

@available(iOS 13.0, macOS 10.15, tvOS 13.0, watchOS 6.0, *)
Expand Down
12 changes: 12 additions & 0 deletions SwiftUIPager.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
175117822573E2D700D809CF /* PagerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 175117792573E2D200D809CF /* PagerModel.swift */; };
1751178A2573E2D900D809CF /* PagerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 175117792573E2D200D809CF /* PagerModel.swift */; };
175117912573E2DA00D809CF /* PagerModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 175117792573E2D200D809CF /* PagerModel.swift */; };
176F05972598C1720002024B /* ViewProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 176F05962598C1720002024B /* ViewProxy.swift */; };
176F05982598C1720002024B /* ViewProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 176F05962598C1720002024B /* ViewProxy.swift */; };
176F05992598C1720002024B /* ViewProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 176F05962598C1720002024B /* ViewProxy.swift */; };
176F059A2598C1720002024B /* ViewProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 176F05962598C1720002024B /* ViewProxy.swift */; };
176F059B2598C1720002024B /* ViewProxy.swift in Sources */ = {isa = PBXBuildFile; fileRef = 176F05962598C1720002024B /* ViewProxy.swift */; };
6B0F33DE24B4A50A006031BE /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B0F33DD24B4A4DE006031BE /* OnDeactivateModifier.swift */; };
6B0F33DF24B4A50B006031BE /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B0F33DD24B4A4DE006031BE /* OnDeactivateModifier.swift */; };
6B0F33E024B4A50B006031BE /* OnDeactivateModifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = 6B0F33DD24B4A4DE006031BE /* OnDeactivateModifier.swift */; };
Expand Down Expand Up @@ -132,6 +137,7 @@
172F4D7823DF8A6400FD2F15 /* SwiftUIPager.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = SwiftUIPager.framework; sourceTree = BUILT_PRODUCTS_DIR; };
172F4D7B23DF8A6400FD2F15 /* Info-watchOS.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "Info-watchOS.plist"; sourceTree = "<group>"; };
175117792573E2D200D809CF /* PagerModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = PagerModel.swift; path = Sources/SwiftUIPager/PagerModel.swift; sourceTree = SOURCE_ROOT; };
176F05962598C1720002024B /* ViewProxy.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ViewProxy.swift; sourceTree = "<group>"; };
6B0F33DD24B4A4DE006031BE /* OnDeactivateModifier.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = OnDeactivateModifier.swift; sourceTree = "<group>"; };
6B2C2FDE248D379D00E528F9 /* SwiftUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftUI.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.15.sdk/System/Library/Frameworks/SwiftUI.framework; sourceTree = DEVELOPER_DIR; };
6B2C2FE2248D37C100E528F9 /* SwiftUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = SwiftUI.framework; path = Platforms/WatchOS.platform/Developer/SDKs/WatchOS6.2.sdk/System/Library/Frameworks/SwiftUI.framework; sourceTree = DEVELOPER_DIR; };
Expand Down Expand Up @@ -271,6 +277,7 @@
6BEF677724C98C12008533FE /* CGPoint+Angle.swift */,
6B0F33DD24B4A4DE006031BE /* OnDeactivateModifier.swift */,
6BDE442523DE12480022A2F7 /* View+Helper.swift */,
176F05962598C1720002024B /* ViewProxy.swift */,
);
name = Helpers;
path = Sources/SwiftUIPager/Helpers;
Expand Down Expand Up @@ -552,6 +559,7 @@
6BEA731D24ACF9F9007EA8DC /* GesturePriority.swift in Sources */,
6BEF677A24C98C17008533FE /* CGPoint+Angle.swift in Sources */,
6BEF677424C98BFB008533FE /* PageWrapper.swift in Sources */,
176F05992598C1720002024B /* ViewProxy.swift in Sources */,
6BEF519A24C9ECF8000DF66B /* PagerContent.swift in Sources */,
6BEA732C24ACFA08007EA8DC /* SwipeInteractionArea.swift in Sources */,
172F4D5623DF830600FD2F15 /* Pager+Buildable.swift in Sources */,
Expand All @@ -577,6 +585,7 @@
6BEA731E24ACF9FA007EA8DC /* GesturePriority.swift in Sources */,
172F4D8123DF8B3D00FD2F15 /* Buildable.swift in Sources */,
6BEF677C24C98C18008533FE /* CGPoint+Angle.swift in Sources */,
176F059A2598C1720002024B /* ViewProxy.swift in Sources */,
6BEF677524C98BFB008533FE /* PageWrapper.swift in Sources */,
6BEF519B24C9ECF8000DF66B /* PagerContent.swift in Sources */,
6BEA732D24ACFA08007EA8DC /* SwipeInteractionArea.swift in Sources */,
Expand All @@ -602,6 +611,7 @@
6BEA731C24ACF9F9007EA8DC /* GesturePriority.swift in Sources */,
6BEF677924C98C17008533FE /* CGPoint+Angle.swift in Sources */,
6BEF677324C98BFA008533FE /* PageWrapper.swift in Sources */,
176F05982598C1720002024B /* ViewProxy.swift in Sources */,
6BEF519924C9ECF7000DF66B /* PagerContent.swift in Sources */,
6BEA732B24ACFA07007EA8DC /* SwipeInteractionArea.swift in Sources */,
6B2C305A248D740800E528F9 /* Pager+Buildable.swift in Sources */,
Expand All @@ -627,6 +637,7 @@
6BEA731F24ACF9FB007EA8DC /* GesturePriority.swift in Sources */,
6BBC3D752488DC04004194BD /* Buildable.swift in Sources */,
6BEF677D24C98C18008533FE /* CGPoint+Angle.swift in Sources */,
176F059B2598C1720002024B /* ViewProxy.swift in Sources */,
6BEF677624C98BFB008533FE /* PageWrapper.swift in Sources */,
6BEF519C24C9ECF9000DF66B /* PagerContent.swift in Sources */,
6BEA732E24ACFA09007EA8DC /* SwipeInteractionArea.swift in Sources */,
Expand All @@ -652,6 +663,7 @@
6BEA731B24ACF9F8007EA8DC /* GesturePriority.swift in Sources */,
6BEF677824C98C16008533FE /* CGPoint+Angle.swift in Sources */,
6BEF677224C98BFA008533FE /* PageWrapper.swift in Sources */,
176F05972598C1720002024B /* ViewProxy.swift in Sources */,
6BEF519824C9ECF7000DF66B /* PagerContent.swift in Sources */,
6BEA732A24ACFA07007EA8DC /* SwipeInteractionArea.swift in Sources */,
6BDE442A23DE12480022A2F7 /* Pager+Buildable.swift in Sources */,
Expand Down
7 changes: 1 addition & 6 deletions release_description.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
### Features
- New modifier to switch back to `singlePagination` and provide a reveal `ratio`
- New modifiers to keep track of the dragging `onDraggingChanged` and `onDraggingEnded`
- New modifier to disable `bounces`

### Fixes
- Smoother scroll
- Workaround to use `StateObject` if possible

0 comments on commit bad983f

Please sign in to comment.