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

Downgrade platform requirements #122

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import PackageDescription

let package = Package(
name: "PagerTabStripView",
platforms: [.iOS(.v16), .macOS(.v13)],
platforms: [.iOS(.v15), .macOS(.v12)],
products: [
.library(name: "PagerTabStripView", targets: ["PagerTabStripView"])
],
Expand Down
6 changes: 3 additions & 3 deletions Sources/NavBarItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
import SwiftUI

struct NavBarItem<SelectionType>: View, Identifiable where SelectionType: Hashable {

var id: SelectionType
@Binding private var selection: SelectionType
@EnvironmentObject private var pagerSettings: PagerSettings<SelectionType>

public init(id: SelectionType, selection: Binding<SelectionType>) {
self.id = id
self._selection = selection
}

@MainActor var body: some View {
if let dataItem = pagerSettings.items[id] {
dataItem.view
Expand Down
20 changes: 13 additions & 7 deletions Sources/NavBarModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Copyright © 2022 Xmartlabs SRL. All rights reserved.
//

#if os(iOS)
import SwiftUI

struct NavBarModifier<SelectionType>: ViewModifier where SelectionType: Hashable {
Expand All @@ -17,12 +18,12 @@ struct NavBarModifier<SelectionType>: ViewModifier where SelectionType: Hashable
@MainActor func body(content: Content) -> some View {
VStack(alignment: .leading, spacing: 0) {
if !style.placedInToolbar {
NavBarWrapperView(selection: $selection)
NavBarWrapperView<SelectionType>(selection: $selection)
content
} else {
content.toolbar(content: {
ToolbarItem(placement: .principal) {
NavBarWrapperView(selection: $selection)
NavBarWrapperView<SelectionType>(selection: $selection)
}
})
}
Expand All @@ -40,17 +41,22 @@ private struct NavBarWrapperView<SelectionType>: View where SelectionType: Hasha
case let barStyle as BarStyle:
IndicatorBarView<SelectionType, AnyView>(selection: $selection, indicator: barStyle.indicatorView)
case is SegmentedControlStyle:
SegmentedNavBarView(selection: $selection)
SegmentedNavBarView<SelectionType>(selection: $selection)
case let indicatorStyle as BarButtonStyle:
if indicatorStyle.scrollable {
ScrollableNavBarView(selection: $selection)
if #available(iOS 16, *) {
if indicatorStyle.scrollable {
ScrollableNavBarView<SelectionType>(selection: $selection)
} else {
FixedSizeNavBarView<SelectionType>(selection: $selection)
}
} else {
FixedSizeNavBarView(selection: $selection)
SegmentedNavBarView<SelectionType>(selection: $selection)
}
default:
SegmentedNavBarView(selection: $selection)
SegmentedNavBarView<SelectionType>(selection: $selection)
}
}

@Environment(\.pagerStyle) var style: PagerStyle
}
#endif
9 changes: 7 additions & 2 deletions Sources/NavigationBarStyles/FixedSizeNavBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import SwiftUI

@available(iOS 16.0, macOS 13.0, *)
internal struct FixedSizeNavBarView<SelectionType>: View where SelectionType: Hashable {

@Binding private var selection: SelectionType
Expand All @@ -24,8 +25,11 @@ internal struct FixedSizeNavBarView<SelectionType>: View where SelectionType: Ha
Group {
FixedSizeNavBarViewLayout(spacing: internalStyle.tabItemSpacing) {
ForEach(pagerSettings.itemsOrderedByIndex, id: \.self) { tag in
NavBarItem(id: tag, selection: $selection)
.tag(tag)
if let dataItem = pagerSettings.items[tag] {
NavBarItem(id: tag, selection: $selection)
.tag(tag)
.id(dataItem.id)
}
}
internalStyle.indicatorView()
.frame(height: internalStyle.indicatorViewHeight)
Expand All @@ -50,6 +54,7 @@ internal struct FixedSizeNavBarView<SelectionType>: View where SelectionType: Ha

}

@available(iOS 16.0, macOS 13.0, *)
struct FixedSizeNavBarViewLayout: Layout {

let spacing: CGFloat
Expand Down
9 changes: 7 additions & 2 deletions Sources/NavigationBarStyles/ScrollableNavBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Foundation
import SwiftUI

@available(iOS 16.0, macOS 13.0, *)
internal struct ScrollableNavBarView<SelectionType>: View where SelectionType: Hashable {

@Binding var selection: SelectionType
Expand All @@ -25,8 +26,11 @@ internal struct ScrollableNavBarView<SelectionType>: View where SelectionType: H
ScrollView(.horizontal, showsIndicators: false) {
ScrollableNavBarViewLayout(spacing: internalStyle.tabItemSpacing) {
ForEach(pagerSettings.itemsOrderedByIndex, id: \.self) { tag in
NavBarItem(id: tag, selection: $selection)
.tag(tag)
if let dataItem = pagerSettings.items[tag] {
NavBarItem(id: tag, selection: $selection)
.tag(tag)
.id(dataItem.id)
}
}
internalStyle.indicatorView()
.frame(height: internalStyle.indicatorViewHeight)
Expand Down Expand Up @@ -66,6 +70,7 @@ internal struct ScrollableNavBarView<SelectionType>: View where SelectionType: H
}
}

@available(iOS 16.0, macOS 13.0, *)
struct ScrollableNavBarViewLayout: Layout {

private let spacing: CGFloat
Expand Down
10 changes: 8 additions & 2 deletions Sources/NavigationBarStyles/SegmentedNavBarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@ internal struct SegmentedNavBarView<SelectionType>: View where SelectionType: Ha
Picker("SegmentedNavBarView", selection: $selection) {
if pagerSettings.items.count > 0 && pagerSettings.width > 0 {
ForEach(pagerSettings.itemsOrderedByIndex, id: \.self) { tag in
NavBarItem(id: tag, selection: $selection)
.tag(tag)
if let dataItem = pagerSettings.items[tag] {
NavBarItem(id: tag, selection: $selection)
.tag(tag)
.id(dataItem.id)
} else {
NavBarItem(id: tag, selection: $selection)
.tag(tag)
}
}
}
}
Expand Down
18 changes: 11 additions & 7 deletions Sources/PagerSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,20 @@
import SwiftUI

struct DataItem<SelectedType>: Identifiable, Equatable where SelectedType: Hashable {

static func == (lhs: DataItem<SelectedType>, rhs: DataItem<SelectedType>) -> Bool {
return lhs.tag == rhs.tag
return lhs.tag == rhs.tag && lhs.id == rhs.id
}
private(set) var tag: SelectedType
fileprivate(set) var id: String?
fileprivate(set) var view: AnyView
fileprivate(set) var index: Int

var id: SelectedType { tag }
// var id: SelectedType { tag }
// var id: SelectedType { tag }

fileprivate init(tag: SelectedType, index: Int, view: AnyView) {
fileprivate init(tag: SelectedType, id: String?, index: Int, view: AnyView) {
self.tag = tag
self.id = id
self.index = index
self.view = view
}
Expand Down Expand Up @@ -127,19 +129,21 @@ public class PagerSettings<SelectionType>: ObservableObject where SelectionType:
@Published private(set) var itemsOrderedByIndex = [SelectionType]()

private func recalculateTransition() {
let indexAndPercentage = -contentOffset / width
guard width > 0 else { return }
let indexAndPercentage = width == 0 ? 0 : -contentOffset / width
let percentage = (indexAndPercentage + 1).truncatingRemainder(dividingBy: 1)
let lowIndex = Int(floor(indexAndPercentage))
transition = TransitionProgress(from: itemsOrderedByIndex[safe: lowIndex], to: itemsOrderedByIndex[safe: lowIndex+1], percentage: percentage)
}

func createOrUpdate<TabView: View>(tag: SelectionType, index: Int, view: TabView) {
func createOrUpdate<TabView: View>(tag: SelectionType, id: String?, index: Int, view: TabView) {
if var dataItem = items[tag] {
dataItem.id = id
dataItem.index = index
dataItem.view = AnyView(view)
items[tag] = dataItem
} else {
items[tag] = DataItem(tag: tag, index: index, view: AnyView(view))
items[tag] = DataItem(tag: tag, id: id, index: index, view: AnyView(view))
}
}

Expand Down
6 changes: 5 additions & 1 deletion Sources/PagerStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ extension PagerStyle where Self == SegmentedControlStyle {
public static func segmentedControl(placedInToolbar: Bool = false,
pagerAnimationOnTap: Animation? = DefaultPagerAnimation.onTap,
pagerAnimationOnSwipe: Animation? = DefaultPagerAnimation.onSwipe,
navBarID: String? = nil,
backgroundColor: Color = .white,
padding: EdgeInsets = EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10)) -> SegmentedControlStyle {
return SegmentedControlStyle(placedInToolbar: placedInToolbar,
pagerAnimationOnTap: pagerAnimationOnTap,
pagerAnimationOnSwipe: pagerAnimationOnSwipe,
navBarID: navBarID,
backgroundColor: backgroundColor,
padding: padding)
}
Expand Down Expand Up @@ -101,21 +103,23 @@ public struct SegmentedControlStyle: PagerStyle {
public var placedInToolbar: Bool
public var pagerAnimationOnTap: Animation?
public var pagerAnimationOnSwipe: Animation?
public var navBarID: String? = nil
public var backgroundColor: Color
public var padding: EdgeInsets = EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)

public init(placedInToolbar: Bool = false,
pagerAnimationOnTap: Animation? = DefaultPagerAnimation.onTap,
pagerAnimationOnSwipe: Animation? = DefaultPagerAnimation.onSwipe,
navBarID: String? = nil,
backgroundColor: Color = .white,
padding: EdgeInsets = EdgeInsets(top: 0, leading: 0, bottom: 0, trailing: 0)) {
self.backgroundColor = backgroundColor
self.navBarID = navBarID
self.placedInToolbar = placedInToolbar
self.padding = padding
self.pagerAnimationOnTap = pagerAnimationOnTap
self.pagerAnimationOnSwipe = pagerAnimationOnSwipe
}

}

public struct BarStyle: PagerWithIndicatorStyle {
Expand Down
20 changes: 15 additions & 5 deletions Sources/PagerTabItemModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ struct PagerTabItemModifier<SelectionType, NavTabView>: ViewModifier where Selec

let navTabView: () -> NavTabView
let tag: SelectionType

init(tag: SelectionType, navTabView: @escaping () -> NavTabView) {
var id: String?

init(tag: SelectionType, id: String? = nil, navTabView: @escaping () -> NavTabView) {
self.tag = tag
self.id = id
self.navTabView = navTabView
}

Expand All @@ -23,16 +25,24 @@ struct PagerTabItemModifier<SelectionType, NavTabView>: ViewModifier where Selec
.onAppear {
let frame = geometryProxy.frame(in: .named("PagerViewScrollView"))
index = Int(round(frame.minX / frame.width))
pagerSettings.createOrUpdate(tag: tag, index: index, view: navTabView())
}.onDisappear {
pagerSettings.createOrUpdate(tag: tag, id: id, index: index, view: navTabView())
}
.onDisappear {
pagerSettings.remove(tag: tag)
}
.onChange(of: geometryProxy.frame(in: .named("PagerViewScrollView"))) { newFrame in
index = Int(round(newFrame.minX / newFrame.width))
}
.onChange(of: index) { newIndex in
pagerSettings.createOrUpdate(tag: tag, index: newIndex, view: navTabView())
pagerSettings.createOrUpdate(tag: tag, id: id, index: newIndex, view: navTabView())
}
.onChange(of: id) { newID in
pagerSettings.createOrUpdate(tag: tag, id: newID, index: index, view: navTabView())
}
.task {
pagerSettings.createOrUpdate(tag: tag, id: id, index: index, view: navTabView())
}
.id(id)
}
}

Expand Down
Loading