Skip to content

Commit

Permalink
Fixed cross-platform compilation issues
Browse files Browse the repository at this point in the history
  • Loading branch information
bdrelling committed Mar 1, 2024
1 parent d747218 commit e50505a
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 27 deletions.
6 changes: 3 additions & 3 deletions Examples/StormView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import SwiftUI

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
private struct StormView: View {
@State private var storm = Storm()
let rainColor = Color(red: 0.25, green: 0.5, blue: 0.75)
Expand Down Expand Up @@ -31,7 +31,7 @@ private struct Raindrop: Hashable, Equatable {
var speed: Double
}

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Observable
private class Storm {
var drops = Set<Raindrop>()
Expand All @@ -44,7 +44,7 @@ private class Storm {

// MARK: - Previews

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
struct StormView_Previews: PreviewProvider {
static var previews: some View {
StormView()
Expand Down
12 changes: 12 additions & 0 deletions Sources/KippleFont/Extensions/Font+Convenience.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ public extension UXFont {
static func printNamesAndVariations() {
familyNamesAndVariations.forEach { print($0) }
}

/// A wrapper around `familyName` that converts to a `String` on all platforms.
///
/// This is necessary because `NSFont.familyName` is optional, but `UIFont.familyName` is not.
/// On macOS, if a family name is not found, a value of `"Unknown"` is returned.
var safeFamilyName: String {
#if canImport(UIKit)
self.familyName
#else
self.familyName ?? "Unknown"
#endif
}
}

// MARK: - Platform Conformance
Expand Down
2 changes: 1 addition & 1 deletion Sources/KippleFont/Views/FontIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public struct FontIterator<Content>: View where Content: View {

public var body: some View {
ForEach(self.fonts, id: \.self) { font in
self.content(Font(font), font.familyName ?? "Unknown")
self.content(Font(font), font.safeFamilyName)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@

import SwiftUI

#if os(macOS)

#if os(macOS) || os(tvOS) || os(watchOS)
public enum PlatformSafeTitleDisplayMode {
case automatic
case inline

@available(tvOS, unavailable)
case large
}
#endif

#if os(macOS)

@available(iOS, unavailable)
@available(watchOS, unavailable)
@available(tvOS, unavailable)
public extension View {
func navigationBarTitleDisplayMode(_: PlatformSafeTitleDisplayMode) -> some View {
self
}
}

#endif

#if os(tvOS) || os(watchOS)

extension View {
public func navigationBarTitle(_ titleKey: LocalizedStringKey, displayMode: NavigationBarItem.TitleDisplayMode) -> some View {
self.navigationTitle(titleKey)
}
}

#endif
10 changes: 9 additions & 1 deletion Sources/KippleUI/Kipple/String+CopyToClipboard.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright © 2024 Brian Drelling. All rights reserved.

#if canImport(UIKit)
#if os(iOS) || os(visionOS)

import UIKit

Expand All @@ -23,4 +23,12 @@ public extension String {
}
}

#else

public extension String {
func copyToClipboard() {
print("WARNING: String.copyToClipboard is only available on iOS, macOS, and visionOS.")
}
}

#endif
7 changes: 5 additions & 2 deletions Sources/KippleUI/Kipple/View+Copying.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ private struct CopyingModifier: ViewModifier {

private let text: String

#if canImport(UIKit)
#if os(iOS)
private let backgroundColor: Color = .init(uiColor: .secondarySystemBackground)
#else
#elseif os(macOS)
private let backgroundColor: Color = .init(nsColor: .windowBackgroundColor)
#else
#warning("backgroundColor is not defined on tvOS and watchOS!")
private let backgroundColor: Color = .init(uiColor: .clear)
#endif

func body(content: Content) -> some View {
Expand Down
2 changes: 1 addition & 1 deletion Sources/KippleUI/Kipple/View+PreventDimming.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public extension View {
/// When the `View` disappears, the screen is able to dim again.
@ViewBuilder
func preventScreenDimming() -> some View {
#if canImport(UIKit)
#if canImport(UIKit) && (os(iOS) || os(tvOS) || os(visionOS))
self.onAppear {
UIApplication.shared.isIdleTimerDisabled = true
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright © 2024 Brian Drelling. All rights reserved.

#if canImport(UIKit)
#if canImport(UIKit) && (os(iOS) || os(tvOS))

import SwiftUI

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ public extension View {
#if canImport(UIKit)

struct NavigationBarBackground_Previews: PreviewProvider {
#if os(iOS)
private static let displayModes: [NavigationBarItem.TitleDisplayMode] = [
.inline,
.large,
]
#else
private static let displayModes: [NavigationBarItem.TitleDisplayMode] = [
.inline,
]
#endif

static var previews: some View {
ForEach(displayModes, id: \.self) { displayMode in
Expand Down
19 changes: 14 additions & 5 deletions Sources/KippleUI/SwiftUI/Core/ViewModifiers/View+Snapshot.swift
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
// Copyright © 2024 Brian Drelling. All rights reserved.

#if canImport(UIKit)

import SwiftUI

#if canImport(UIKit) && (os(iOS) || os(tvOS))

public extension View {
func snapshot() -> Image {
.init(uiImage: self.uiImage())
}

func uiImage() -> UIImage {
private func uiImage() -> UIImage {
let controller = UIHostingController(rootView: self)
let view = controller.view

Expand All @@ -25,6 +25,17 @@ public extension View {
}
}

#else

public extension View {
func snapshot() -> Image {
print("WARNING: View.snapshot() is only available on iOS and tvOS.")
return .init(systemName: "photo")
}
}

#endif

// MARK: - Previews

struct ViewSnapshot_Previews: PreviewProvider {
Expand All @@ -37,5 +48,3 @@ struct ViewSnapshot_Previews: PreviewProvider {
.snapshot()
}
}

#endif
4 changes: 3 additions & 1 deletion Sources/KippleUI/SwiftUI/Core/Views/BackButton.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ public struct BackButtonPreviewer<Content>: View where Content: View {

// MARK: - Extensions

@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
@available(iOS 16, macOS 13, tvOS 16, watchOS 9, *)
@available(watchOS, unavailable)
public extension View {
func withBackButton<Content: View>(@ViewBuilder content: @escaping () -> Content) -> some View {
navigationBarBackButtonHidden(true)
Expand All @@ -89,6 +90,7 @@ public extension View {
// MARK: - Previews

@available(iOS 15, macOS 12, tvOS 15, watchOS 8, *)
@available(watchOS, unavailable)
struct BackButton_Previews: PreviewProvider {
static var previews: some View {
BackButtonPreviewer {
Expand Down
6 changes: 6 additions & 0 deletions Sources/KippleUI/SwiftUI/Core/Views/Modal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import SwiftUI

@available(watchOS, unavailable)
public struct Modal<Content>: View where Content: View {
private let content: () -> Content

Expand All @@ -16,7 +17,11 @@ public struct Modal<Content>: View where Content: View {
self.content()
.withNavigationBarBackground {
Rectangle()
#if os(watchOS)
.fill(.background)
#else
.fill(.regularMaterial)
#endif
}
// .navigationBarTitleDisplayMode(.inline)
.withNavigationBarDoneButton()
Expand All @@ -35,6 +40,7 @@ public struct Modal<Content>: View where Content: View {

// MARK: - Extensions

@available(watchOS, unavailable)
public extension View {
func inModal() -> some View {
Modal {
Expand Down
14 changes: 7 additions & 7 deletions Sources/KippleUI/SwiftUI/Core/Views/NavigatorStack.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import SwiftUI

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
public struct NavigatorStack<Content>: View where Content: View {
@Bindable private var navigator: Navigator
private let content: (Navigator) -> Content
Expand Down Expand Up @@ -46,7 +46,7 @@ public struct NavigatorStack<Content>: View where Content: View {

// MARK: - Supporting Types

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
@Observable
public final class Navigator {
public var path: NavigationPath
Expand Down Expand Up @@ -74,35 +74,35 @@ public final class Navigator {
}
}

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
private struct NavigatorKey: EnvironmentKey {
static let defaultValue: Navigator = .init()
}

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
public extension EnvironmentValues {
var navigator: Navigator {
get { self[NavigatorKey.self] }
set { self[NavigatorKey.self] = newValue }
}
}

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
public protocol Navigable: Hashable {
associatedtype View: SwiftUI.View
@ViewBuilder var view: View { get }
}

// MARK: - Extensions

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
public extension Navigator {
convenience init<PathComponent>(path: [PathComponent]) where PathComponent: Hashable {
self.init(path: .init(path))
}
}

@available(iOS 17, macOS 14, tvOS 14, watchOS 10, *)
@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
public extension View {
func navigationDestination<N>(for _: N.Type) -> some View where N: Navigable {
self.navigationDestination(for: N.self) { $0.view }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// Copyright © 2024 Brian Drelling. All rights reserved.

#if canImport(CoreHaptics)

import Combine
import CoreHaptics

Expand Down Expand Up @@ -127,3 +129,5 @@ public extension CHHapticEvent {
)
}
}

#endif

0 comments on commit e50505a

Please sign in to comment.