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

Support custom indicator images for PageControl (Closes #474) #475

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
98 changes: 98 additions & 0 deletions Sources/SwiftUIX/Intramodular/Pagination/PageControl.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public struct PageControl {
var defersCurrentPageDisplay: Bool?
@usableFromInline
var hidesForSinglePage: Bool?
@usableFromInline
var indicatorImages: [Int: UIImage]?
@usableFromInline
var currentPageIndicatorImages: [Int: UIImage]?

@inlinable
public init(numberOfPages: Int, currentPage: Binding<Int>) {
Expand Down Expand Up @@ -72,6 +76,24 @@ extension PageControl: UIViewRepresentable {
if let backgroundStyle = context.environment.pageControlBackgroundStyle {
uiView.backgroundStyle = backgroundStyle
}

if let indicators = indicatorImages {
for (page, image) in indicators {
uiView.setIndicatorImage(image, forPage: page)
}
}

uiView.preferredIndicatorImage = context.environment.preferredIndicatorImage
}

if #available(iOS 16.0, tvOS 16.0, *) {
if let currentPageIndicators = currentPageIndicatorImages {
for (page, image) in currentPageIndicators {
uiView.setCurrentPageIndicatorImage(image, forPage: page)
}
}

uiView.preferredCurrentPageIndicatorImage = context.environment.preferredCurrentPageIndicatorImage
}

if let hidesForSinglePage = hidesForSinglePage {
Expand Down Expand Up @@ -103,6 +125,36 @@ extension PageControl {
public func hidesForSinglePage(_ hidesForSinglePage: Bool) -> Self {
then({ $0.hidesForSinglePage = hidesForSinglePage })
}

@available(iOS 14.0, tvOS 14.0, *)
@inlinable
public func indicatorImage(forPage page: Int) -> UIImage? {
indicatorImages?[page]
}

@available(iOS 14.0, tvOS 14.0, *)
@inlinable
public func setIndicatorImage(_ image: UIImage?, forPage page: Int) -> Self {
then({
if $0.indicatorImages == nil { $0.indicatorImages = [:] }
$0.indicatorImages?[page] = image
})
}

@available(iOS 16.0, tvOS 16.0, *)
@inlinable
public func currentPageIndicatorImage(forPage page: Int) -> UIImage? {
currentPageIndicatorImages?[page]
}

@available(iOS 16.0, tvOS 16.0, *)
@inlinable
public func setCurrentPageIndicatorImage(_ image: UIImage?, forPage page: Int) -> Self {
then({
if $0.currentPageIndicatorImages == nil { $0.currentPageIndicatorImages = [:] }
$0.currentPageIndicatorImages?[page] = image
})
}
}

extension View {
Expand All @@ -121,6 +173,18 @@ extension View {
public func currentPageIndicatorTintColor(_ color: Color) -> some View {
environment(\.currentPageIndicatorTintColor, color)
}

@available(iOS 14.0, tvOS 14.0, *)
@inlinable
public func preferredIndicatorImage(_ image: UIImage) -> some View {
environment(\.preferredIndicatorImage, image)
}

@available(iOS 16.0, tvOS 16.0, *)
@inlinable
public func preferredCurrentPageIndicatorImage(_ image: UIImage) -> some View {
environment(\.preferredCurrentPageIndicatorImage, image)
}
}

// MARK: - Auxiliary
Expand All @@ -144,6 +208,20 @@ extension PageControl {
@usableFromInline
static let defaultValue: Color? = nil
}

@available(iOS 14.0, tvOS 14.0, *)
@usableFromInline
struct PreferredIndicatorImageEnvironmentKey: EnvironmentKey {
@usableFromInline
static let defaultValue: UIImage? = nil
}

@available(iOS 16.0, tvOS 16.0, *)
@usableFromInline
struct PreferredCurrentPageIndicatorImage: EnvironmentKey {
@usableFromInline
static let defaultValue: UIImage? = nil
}
}

extension EnvironmentValues {
Expand Down Expand Up @@ -174,6 +252,26 @@ extension EnvironmentValues {
self[PageControl.CurrentTintColorEnvironmentKey.self] = newValue
}
}

@available(iOS 14.0, tvOS 14.0, *)
@inlinable
public var preferredIndicatorImage: UIImage? {
get {
self[PageControl.PreferredIndicatorImageEnvironmentKey.self]
} set {
self[PageControl.PreferredIndicatorImageEnvironmentKey.self] = newValue
}
}

@available(iOS 16.0, tvOS 16.0, *)
@inlinable
public var preferredCurrentPageIndicatorImage: UIImage? {
get {
self[PageControl.PreferredCurrentPageIndicatorImage.self]
} set {
self[PageControl.PreferredCurrentPageIndicatorImage.self] = newValue
}
}
}

#endif