Skip to content

Commit

Permalink
Adds further support for ShareLink
Browse files Browse the repository at this point in the history
  • Loading branch information
shaps80 committed Sep 29, 2022
1 parent c036065 commit e05c689
Show file tree
Hide file tree
Showing 21 changed files with 665 additions and 436 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ Environment:
- `requestReview`
- `Refreshable` – includes pull-to-refresh 
- `ScaledMetric`
- `ShareLink`
- `StateObject`
- `scrollDisabled`
- `scrollDismissesKeyboard`
Expand Down
8 changes: 0 additions & 8 deletions Sources/SwiftUIBackports/Internal/Inspect+UIKit.swift
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import SwiftUI

#if os(iOS)
internal typealias PlatformView = UIView
internal typealias PlatformViewController = UIViewController
#elseif os(macOS)
internal typealias PlatformView = NSView
internal typealias PlatformViewController = NSViewController
#endif

#if os(iOS) || os(macOS)
internal extension PlatformView {
func ancestor<ViewType: PlatformView>(ofType type: ViewType.Type) -> ViewType? {
Expand Down
4 changes: 2 additions & 2 deletions Sources/SwiftUIBackports/Internal/OwningController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ import AppKit
public extension NSView {

var parentController: NSViewController? {
if let responder = self.next as? NSViewController {
if let responder = self.nextResponder as? NSViewController {
return responder
} else if let responder = self.next as? NSView {
} else if let responder = self.nextResponder as? NSView {
return responder.parentController
} else {
return nil
Expand Down
19 changes: 19 additions & 0 deletions Sources/SwiftUIBackports/Internal/Platforms.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#if os(iOS)

import UIKit

public typealias PlatformImage = UIImage

internal typealias PlatformView = UIView
internal typealias PlatformViewController = UIViewController

#elseif os(macOS)

import AppKit

public typealias PlatformImage = NSImage

internal typealias PlatformView = NSView
internal typealias PlatformViewController = NSViewController

#endif
12 changes: 12 additions & 0 deletions Sources/SwiftUIBackports/Internal/String+LocalizationKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import SwiftUI

extension String {
internal init?(_ stringKey: LocalizedStringKey) {
guard let key = Mirror(reflecting: stringKey).children
.first(where: { $0.label == "key" })?.value as? String else {
return nil
}

self = NSLocalizedString(key, comment: "")
}
}
2 changes: 1 addition & 1 deletion Sources/SwiftUIBackports/Shared/Label/Label.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ extension Backport where Wrapped == Any {
}

@MainActor public var body: some View {
if #available(iOS 14, *) {
if #available(iOS 14, macOS 11, *) {
SwiftUI.Label {
config.title
} icon: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ extension Backport where Wrapped == Any {
IconOnlyLabelStyle().makeBody(configuration: configuration)
} else {
TitleAndIconLabelStyle().makeBody(configuration: configuration)
#if os(iOS)
.inspect { inspector in
inspector.ancestor(ofType: UINavigationBar.self)
} customize: { _ in
isToolbarElement = true
}
#endif
}
}
}
Expand Down
115 changes: 0 additions & 115 deletions Sources/SwiftUIBackports/Shared/ShareLink/ActivityView.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import SwiftUI

public struct DefaultShareLinkLabel: View {
let text: Text
private static let shareIcon = "square.and.arrow.up"

init() {
text = .init("Share")
Expand All @@ -20,10 +21,25 @@ public struct DefaultShareLinkLabel: View {
}

public var body: some View {
Backport.Label {
text
} icon: {
Image(systemName: "square.and.arrow.up")
if #available(iOS 14, macOS 11, watchOS 7, tvOS 14, *) {
Label {
text
} icon: {
Image(systemName: Self.shareIcon)
}
} else {
Backport.Label {
text
} icon: {
#if os(macOS)
// no icon on earlier macOS versions
if #available(macOS 11, *) {
Image(systemName: Self.shareIcon)
}
#else
Image(systemName: Self.shareIcon)
#endif
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import SwiftUI

@available(iOS, deprecated: 16)
@available(macOS, deprecated: 13)
@available(watchOS, deprecated: 9)
@available(tvOS, unavailable)
public extension Backport.ShareLink where Wrapped == Any {
init<S: StringProtocol>(_ title: S, items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
where Data.Element: BackportTransferable, Label == DefaultShareLinkLabel
{
self.label = .init(title)
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}

init(_ titleKey: LocalizedStringKey, items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
where Data.Element: BackportTransferable, Label == DefaultShareLinkLabel
{
self.label = .init(titleKey)
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}

init(_ title: Text, items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
where Data.Element: BackportTransferable, Label == DefaultShareLinkLabel
{
self.label = .init(title)
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import SwiftUI

@available(iOS, deprecated: 16)
@available(macOS, deprecated: 13)
@available(watchOS, deprecated: 9)
@available(tvOS, unavailable)
public extension Backport.ShareLink where Wrapped == Any {
init<S: StringProtocol>(_ title: S, items: Data, subject: String? = nil, message: String? = nil)
where PreviewImage == Never, PreviewIcon == Never, Data.Element == String, Label == DefaultShareLinkLabel {
self.label = .init(title)
self.data = items
self.subject = subject
self.message = message
self.preview = { .init($0) }
}

init(_ titleKey: LocalizedStringKey, items: Data, subject: String? = nil, message: String? = nil)
where PreviewImage == Never, PreviewIcon == Never, Data.Element == String, Label == DefaultShareLinkLabel {
self.label = .init(titleKey)
self.data = items
self.subject = subject
self.message = message
self.preview = { .init($0) }
}

init(_ title: Text, items: Data, subject: String? = nil, message: String? = nil)
where PreviewImage == Never, PreviewIcon == Never, Data.Element == String, Label == DefaultShareLinkLabel {
self.label = .init(title)
self.data = items
self.subject = subject
self.message = message
self.preview = { .init($0) }
}

init<S: StringProtocol>(_ title: S, items: Data, subject: String? = nil, message: String? = nil)
where PreviewImage == Never, PreviewIcon == Never, Data.Element == URL, Label == DefaultShareLinkLabel {
self.label = .init(title)
self.data = items
self.subject = subject
self.message = message
self.preview = { .init($0.absoluteString) }
}

init(_ titleKey: LocalizedStringKey, items: Data, subject: String? = nil, message: String? = nil)
where PreviewImage == Never, PreviewIcon == Never, Data.Element == URL, Label == DefaultShareLinkLabel {
self.label = .init(titleKey)
self.data = items
self.subject = subject
self.message = message
self.preview = { .init($0.absoluteString) }
}

init(_ title: Text, items: Data, subject: String? = nil, message: String? = nil)
where PreviewImage == Never, PreviewIcon == Never, Data.Element == URL, Label == DefaultShareLinkLabel {
self.label = .init(title)
self.data = items
self.subject = subject
self.message = message
self.preview = { .init($0.absoluteString) }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import SwiftUI

@available(iOS, deprecated: 16)
@available(macOS, deprecated: 13)
@available(watchOS, deprecated: 9)
@available(tvOS, unavailable)
public extension Backport.ShareLink where Wrapped == Any {
init(items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>)
where Data.Element: BackportTransferable, Label == DefaultShareLinkLabel {
self.label = .init()
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}

init(items: Data, subject: String? = nil, message: String? = nil, preview: @escaping (Data.Element) -> SharePreview<PreviewImage, PreviewIcon>, @ViewBuilder label: () -> Label)
where Data.Element: BackportTransferable {
self.label = label()
self.data = items
self.subject = subject
self.message = message
self.preview = preview
}
}
Loading

0 comments on commit e05c689

Please sign in to comment.