Skip to content

Commit

Permalink
New initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
fermoyadrop authored and fermoya committed Jan 6, 2021
1 parent 28e1626 commit 8c5c248
Show file tree
Hide file tree
Showing 17 changed files with 248 additions and 258 deletions.
17 changes: 10 additions & 7 deletions Documentation/Usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
## Initialization

Creating a `Pager` is very simple. You just need to pass:
- `Binding` to the current page
- `PagerModel`, a wrapper to the current page index
- `Array` of items
- `KeyPath` to an identifier
- `ViewBuilder` factory method to create each page

```swift
@State var page: Int = 0
@StateObject var pagerModel = PagerModel(page: 0)
// @ObservedObject var pagerModel = PagerModel(page: 0)
var items = Array(0..<10)

var body: some View {
Pager(page: $page,
Pager(page: pagerModel,
data: items,
id: \.identifier,
content: { index in
Expand Down Expand Up @@ -244,7 +245,8 @@ You can use `onPageChanged` to add new items on demand whenever the user is gett

```swift

@State var page: Int = 0
@StateObject var pagerModel = PagerModel(page: 0)
// @ObservedObject var pagerModel = PagerModel(page: 0)
@State var data = Array(0..<5)

var body: some View {
Expand All @@ -265,10 +267,11 @@ At the same time, items can be added at the start. Notice you'll need to update
```swift

@State var count: Int = -1
@State var page: Int = 3
@StateObject var pagerModel = PagerModel(page: 0)
// @ObservedObject var pagerModel = PagerModel(page: 0)
@State var data = Array(0..<5)

Pager(page: self.$page,
Pager(page: self.pagerModel,
data: self.data,
id: \.self) {
self.pageView($0)
Expand All @@ -278,7 +281,7 @@ Pager(page: self.$page,
let newData = (1...5).map { $0 * self.count }
withAnimation {
self.data1.insert(contentsOf: newData, at: 0)
self.page1 += 5
self.pagerModel.page += 5
self.count -= 1
}
})
Expand Down
8 changes: 2 additions & 6 deletions Example/SwiftUIPagerExample.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

/* 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 @@ -53,7 +52,6 @@

/* 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 @@ -172,7 +170,6 @@
6BD3828124C97DE3007B1CF6 /* CGPoint+Angle.swift */,
6B9C4A8924B45F66004C06C5 /* OnDeactivateModifier.swift */,
6BC5EDF724866D9500E1E78C /* View+Helper.swift */,
176F05942598BB380002024B /* ViewProxy.swift */,
);
name = Helpers;
path = ../../Sources/SwiftUIPager/Helpers;
Expand Down Expand Up @@ -281,7 +278,6 @@
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 Expand Up @@ -434,7 +430,7 @@
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = SwiftUIPagerExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand All @@ -455,7 +451,7 @@
DEVELOPMENT_TEAM = "";
ENABLE_PREVIEWS = YES;
INFOPLIST_FILE = SwiftUIPagerExample/Info.plist;
IPHONEOS_DEPLOYMENT_TARGET = 13.0;
IPHONEOS_DEPLOYMENT_TARGET = 14.0;
LD_RUNPATH_SEARCH_PATHS = (
"$(inherited)",
"@executable_path/Frameworks",
Expand Down
4 changes: 1 addition & 3 deletions Example/SwiftUIPagerExample/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ extension Color {
}

struct ContentView: View {

@State var tab: Int = 0


var body: some View {
TabView {
InfiniteExampleView()
Expand Down
8 changes: 4 additions & 4 deletions Example/SwiftUIPagerExample/Examples/BizarreExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@
import SwiftUI

struct BizarreExampleView: View {
@State var page1: Int = 0
@State var page2: Int = 0
@StateObject var page1 = PagerModel(page: 0)
@StateObject var page2 = PagerModel(page: 0)
var data = Array(0..<10)

var body: some View {
GeometryReader { proxy in
VStack(spacing: 10) {
Text("Vertical, alignment start").bold()
Pager(page: self.$page1,
Pager(page: self.page1,
data: self.data,
id: \.self) {
self.pageView($0)
Expand All @@ -31,7 +31,7 @@ struct BizarreExampleView: View {
Spacer()

Text("Right to left, interactive").bold()
Pager(page: self.$page2,
Pager(page: self.page2,
data: self.data,
id: \.self) {
self.pageView($0)
Expand Down
31 changes: 13 additions & 18 deletions Example/SwiftUIPagerExample/Examples/ColorsExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import SwiftUI

struct ColorsExampleView: View {

@State var pageIndex = 0
@StateObject var pageIndex = PagerModel(page: 0)

var colors: [Color] = [
.red, .blue, .black, .gray, .purple, .green, .orange, .pink, .yellow, .white
Expand All @@ -20,7 +20,7 @@ struct ColorsExampleView: View {
NavigationView {
GeometryReader { proxy in
VStack(spacing: 10) {
Pager(page: self.$pageIndex,
Pager(page: self.pageIndex,
data: self.colors,
id: \.self) {
self.pageView($0)
Expand All @@ -29,11 +29,6 @@ struct ColorsExampleView: View {
.disableDragging()
.itemSpacing(10)
.padding(20)
.onPageChanged({ page in
withAnimation {
self.pageIndex = page
}
})
.frame(width: min(proxy.size.height, proxy.size.width),
height: min(proxy.size.height, proxy.size.width))
.background(Color.gray.opacity(0.3))
Expand All @@ -44,11 +39,11 @@ struct ColorsExampleView: View {
HStack {
Spacer()
Circle()
.fill(self.colors[self.pageIndex])
.fill(self.colors[self.pageIndex.page])
.frame(width: 80)
.overlay(Circle().stroke(self.pageIndex < 4 ? Color.gray.opacity(0.5) : Color.black, lineWidth: 5))
.overlay(Circle().stroke(self.pageIndex.page < 4 ? Color.gray.opacity(0.5) : Color.black, lineWidth: 5))
Spacer()
Text("\(self.colors[self.pageIndex].rgb)")
Text("\(self.colors[self.pageIndex.page].rgb)")
Spacer()
}

Expand All @@ -58,7 +53,7 @@ struct ColorsExampleView: View {
Spacer()
Button(action: {
withAnimation {
self.pageIndex = 0
self.pageIndex.page = 0
}
}, label: {
VStack(spacing: 4) {
Expand All @@ -67,10 +62,10 @@ struct ColorsExampleView: View {
Text("Start")
.font(.subheadline)
}
}).disabled(self.pageIndex <= 0)
}).disabled(self.pageIndex.page <= 0)
Button(action: {
withAnimation {
self.pageIndex = max(0, self.pageIndex - 1)
self.pageIndex.page = max(0, self.pageIndex.page - 1)
}
}, label: {
VStack(spacing: 4) {
Expand All @@ -79,11 +74,11 @@ struct ColorsExampleView: View {
Text("Previous")
.font(.subheadline)
}
}).disabled(self.pageIndex <= 0)
}).disabled(self.pageIndex.page <= 0)
Spacer()
Button(action: {
withAnimation {
self.pageIndex = min(self.colors.count - 1, self.pageIndex + 1)
self.pageIndex.page = min(self.colors.count - 1, self.pageIndex.page + 1)
}
}, label: {
VStack(spacing: 4) {
Expand All @@ -92,10 +87,10 @@ struct ColorsExampleView: View {
Text("Next")
.font(.subheadline)
}
}).disabled(self.pageIndex >= self.colors.count - 1)
}).disabled(self.pageIndex.page >= self.colors.count - 1)
Button(action: {
withAnimation {
self.pageIndex = self.colors.count - 1
self.pageIndex.page = self.colors.count - 1
}
}, label: {
VStack(spacing: 4) {
Expand All @@ -104,7 +99,7 @@ struct ColorsExampleView: View {
Text("End")
.font(.subheadline)
}
}).disabled(self.pageIndex >= self.colors.count - 1)
}).disabled(self.pageIndex.page >= self.colors.count - 1)
Spacer()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
import SwiftUI

struct EmbeddedExampleView: View {
@State var page: Int = 0
@State var page2: Int = 0
@StateObject var page1 = PagerModel(page: 0)
@StateObject var page2 = PagerModel(page: 0)
var data = Array(0..<10)


Expand All @@ -21,7 +21,7 @@ struct EmbeddedExampleView: View {
GeometryReader { proxy in
ScrollView {
VStack {
Pager(page: self.$page,
Pager(page: self.page1,
data: self.data,
id: \.self) { page in
self.pageView(page)
Expand All @@ -46,7 +46,7 @@ struct EmbeddedExampleView: View {
.pickerStyle(SegmentedPickerStyle())
.padding(EdgeInsets(top: 0, leading: 12, bottom: 0, trailing: 12))

Pager(page: self.$page2,
Pager(page: self.page2,
data: self.data,
id: \.self) { page in
self.pageView(page)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import SwiftUI

struct InfiniteExampleView: View {

@State var page1: Int = 0
@State var page2: Int = 0
@StateObject var page1 = PagerModel(page: 0)
@StateObject var page2 = PagerModel(page: 0)
@State var data1 = Array(0..<20)
@State var isPresented: Bool = false
var data2 = Array(0..<20)
Expand All @@ -23,7 +23,7 @@ struct InfiniteExampleView: View {
Text("Appending on the fly")
.bold()
.padding(.top)
Pager(page: self.$page1,
Pager(page: self.page1,
data: self.data1,
id: \.self) {
self.pageView($0)
Expand Down Expand Up @@ -52,7 +52,7 @@ struct InfiniteExampleView: View {

Text("Looping Pager")
.bold()
Pager(page: self.$page2,
Pager(page: self.page2,
data: self.data2,
id: \.self) {
self.pageView($0)
Expand Down
29 changes: 13 additions & 16 deletions Example/SwiftUIPagerExample/Examples/NestedExampleView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,20 @@ import SwiftUI

struct NestedExampleView: View {

@State var page: Int = 0
@StateObject var pagerModel = PagerModel(page: 0)
@State var pageOffset: Double = 0
@State var nestedPages: [Int] = [0, 0, 0, 0]
@State var nestedPagerModels: [PagerModel] = [
PagerModel(page: 0),
PagerModel(page: 0),
PagerModel(page: 0),
PagerModel(page: 0)
]

var data = Array(0..<4)
var nestedData = Array(0..<10)

var body: some View {
Pager(page: self.$page,
Pager(page: self.pagerModel,
data: self.data,
id: \.self) { page in
self.nestedPager(page)
Expand All @@ -29,22 +34,14 @@ struct NestedExampleView: View {
}

func nestedPager(_ index: Int) -> some View {
let binding = Binding<Int>(
get: {
self.nestedPages[index]
}, set: { newValue in
var newNestedPages = self.nestedPages
newNestedPages[index] = newValue
self.nestedPages = newNestedPages
})

let nestedPagerModel = nestedPagerModels[index]
return VStack {
Text("Parent page: \(index)")
.font(.system(size: 22))
.bold()
.padding(20)
Spacer()
Pager(page: binding,
Pager(page: nestedPagerModel,
data: self.nestedData,
id: \.self) { page in
self.pageView(page)
Expand All @@ -55,9 +52,9 @@ struct NestedExampleView: View {
})
.onDraggingChanged { increment in
withAnimation {
if binding.wrappedValue == self.nestedData.count - 1, increment > 0 {
if nestedPagerModel.page == self.nestedData.count - 1, increment > 0 {
pageOffset = increment
} else if binding.wrappedValue == 0, increment < 0 {
} else if nestedPagerModel.page == 0, increment < 0 {
pageOffset = increment
}
}
Expand All @@ -66,7 +63,7 @@ struct NestedExampleView: View {
guard pageOffset != 0 else { return }
withAnimation {
pageOffset = 0
page += Int(increment.rounded())
pagerModel.page += Int(increment.rounded())
}
}
.itemSpacing(10)
Expand Down
Loading

0 comments on commit 8c5c248

Please sign in to comment.