diff --git a/NotificationExtension/NotificationService.swift b/NotificationExtension/NotificationService.swift index a2754ee4b..6270a3358 100644 --- a/NotificationExtension/NotificationService.swift +++ b/NotificationExtension/NotificationService.swift @@ -371,13 +371,14 @@ private struct HTMLCallbacks: HTMLConversionCallbacks { // Converting WebURL to URL is a small but non-trivial expense (since it works by // serializing the WebURL as a string and then having Foundation parse it again), // so, if available, use the system parser which doesn't require another round trip. - if let url = try? URL.ParseStrategy().parse(string) { + if #available(iOS 16.0, macOS 13.0, *), + let url = try? URL.ParseStrategy().parse(string) { url } else if let web = WebURL(string), let url = URL(web) { url } else { - nil + URL(string: string) } } diff --git a/Packages/ComposeUI/Package.swift b/Packages/ComposeUI/Package.swift index 434658b20..752a018ca 100644 --- a/Packages/ComposeUI/Package.swift +++ b/Packages/ComposeUI/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "ComposeUI", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentRowController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentRowController.swift index c729b26e4..ff11ef043 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentRowController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentRowController.swift @@ -156,7 +156,7 @@ class AttachmentRowController: ViewController { Button(role: .destructive, action: controller.removeAttachment) { Label("Delete", systemImage: "trash") } - } preview: { + } previewIfAvailable: { ControllerView(controller: { controller.thumbnailController }) } @@ -221,3 +221,16 @@ extension AttachmentRowController { case allowEntry, recognizingText } } + +private extension View { + @available(iOS, obsoleted: 16.0) + @available(visionOS 1.0, *) + @ViewBuilder + func contextMenu(@ViewBuilder menuItems: () -> M, @ViewBuilder previewIfAvailable preview: () -> P) -> some View { + if #available(iOS 16.0, *) { + self.contextMenu(menuItems: menuItems, preview: preview) + } else { + self.contextMenu(menuItems: menuItems) + } + } +} diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift index 98af19262..56bae119e 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/AttachmentsListController.swift @@ -214,6 +214,44 @@ fileprivate extension View { self } } + + @available(iOS, obsoleted: 16.0) + @ViewBuilder + func sheetOrPopover(isPresented: Binding, @ViewBuilder content: @escaping () -> some View) -> some View { + if #available(iOS 16.0, *) { + self.modifier(SheetOrPopover(isPresented: isPresented, view: content)) + } else { + self.popover(isPresented: isPresented, content: content) + } + } + + @available(iOS, obsoleted: 16.0) + @ViewBuilder + func withSheetDetentsIfAvailable() -> some View { + if #available(iOS 16.0, *) { + self + .presentationDetents([.medium, .large]) + .presentationDragIndicator(.visible) + } else { + self + } + } +} + +@available(iOS 16.0, *) +fileprivate struct SheetOrPopover: ViewModifier { + @Binding var isPresented: Bool + @ViewBuilder let view: () -> V + + @Environment(\.horizontalSizeClass) var sizeClass + + func body(content: Content) -> some View { + if sizeClass == .compact { + content.sheet(isPresented: $isPresented, content: view) + } else { + content.popover(isPresented: $isPresented, content: view) + } + } } @available(visionOS 1.0, *) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift index d63cd7303..da478b5bb 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ComposeController.swift @@ -125,7 +125,9 @@ public final class ComposeController: ViewController { self.toolbarController = ToolbarController(parent: self) self.attachmentsListController = AttachmentsListController(parent: self) - NotificationCenter.default.addObserver(self, selector: #selector(currentInputModeChanged), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil) + if #available(iOS 16.0, *) { + NotificationCenter.default.addObserver(self, selector: #selector(currentInputModeChanged), name: UITextInputMode.currentInputModeDidChangeNotification, object: nil) + } NotificationCenter.default.addObserver(self, selector: #selector(managedObjectsDidChange), name: .NSManagedObjectContextObjectsDidChange, object: DraftsPersistentContainer.shared.viewContext) } @@ -322,6 +324,10 @@ public final class ComposeController: ViewController { ControllerView(controller: { controller.toolbarController }) #endif } + #if !os(visionOS) + // on iPadOS15, the toolbar ends up below the keyboard's toolbar without this + .padding(.bottom, keyboardInset) + #endif .transition(.move(edge: .bottom)) } } @@ -430,7 +436,7 @@ public final class ComposeController: ViewController { } .listStyle(.plain) #if !os(visionOS) - .scrollDismissesKeyboard(.interactively) + .scrollDismissesKeyboardInteractivelyIfAvailable() #endif .disabled(controller.isPosting) } @@ -481,6 +487,31 @@ public final class ComposeController: ViewController { .keyboardShortcut(.return, modifiers: .command) .disabled(!controller.postButtonEnabled) } + + #if !os(visionOS) + @available(iOS, obsoleted: 16.0) + private var keyboardInset: CGFloat { + if #unavailable(iOS 16.0), + UIDevice.current.userInterfaceIdiom == .pad, + keyboardReader.isVisible { + return ToolbarController.height + } else { + return 0 + } + } + #endif + } +} + +private extension View { + @available(iOS, obsoleted: 16.0) + @ViewBuilder + func scrollDismissesKeyboardInteractivelyIfAvailable() -> some View { + if #available(iOS 16.0, *) { + self.scrollDismissesKeyboard(.interactively) + } else { + self + } } } diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/FocusedAttachmentController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/FocusedAttachmentController.swift index 9369df647..436724d65 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/FocusedAttachmentController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/FocusedAttachmentController.swift @@ -51,11 +51,14 @@ class FocusedAttachmentController: ViewController { .onAppear { player.play() } - } else { + } else if #available(iOS 16.0, *) { ZoomableScrollView { attachmentView .matchedGeometryDestination(id: attachment.id) } + } else { + attachmentView + .matchedGeometryDestination(id: attachment.id) } Spacer(minLength: 0) diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/PollController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/PollController.swift index 18f6b1015..7a1e2cb9e 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/PollController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/PollController.swift @@ -96,7 +96,7 @@ class PollController: ViewController { .onMove(perform: controller.moveOptions) } .listStyle(.plain) - .scrollDisabled(true) + .scrollDisabledIfAvailable(true) .frame(height: 44 * CGFloat(poll.options.count)) Button(action: controller.addOption) { diff --git a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ToolbarController.swift b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ToolbarController.swift index 5528c4a6e..bcef0cf0a 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/Controllers/ToolbarController.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/Controllers/ToolbarController.swift @@ -66,7 +66,7 @@ class ToolbarController: ViewController { } }) } - .scrollDisabled(realWidth ?? 0 <= minWidth ?? 0) + .scrollDisabledIfAvailable(realWidth ?? 0 <= minWidth ?? 0) .frame(height: ToolbarController.height) .frame(maxWidth: .infinity) .background(.regularMaterial, ignoresSafeAreaEdges: [.bottom, .leading, .trailing]) @@ -122,7 +122,8 @@ class ToolbarController: ViewController { Spacer() - if composeController.mastodonController.instanceFeatures.createStatusWithLanguage { + if #available(iOS 16.0, *), + composeController.mastodonController.instanceFeatures.createStatusWithLanguage { LanguagePicker(draftLanguage: $draft.language, hasChangedSelection: $composeController.hasChangedLanguageSelection) } } diff --git a/Packages/ComposeUI/Sources/ComposeUI/KeyboardReader.swift b/Packages/ComposeUI/Sources/ComposeUI/KeyboardReader.swift index 2c049b2aa..11aa3771f 100644 --- a/Packages/ComposeUI/Sources/ComposeUI/KeyboardReader.swift +++ b/Packages/ComposeUI/Sources/ComposeUI/KeyboardReader.swift @@ -10,6 +10,7 @@ import UIKit import Combine +@available(iOS, obsoleted: 16.0) class KeyboardReader: ObservableObject { // @Published var isVisible = false @Published var keyboardHeight: CGFloat = 0 diff --git a/Packages/ComposeUI/Sources/ComposeUI/View+ForwardsCompat.swift b/Packages/ComposeUI/Sources/ComposeUI/View+ForwardsCompat.swift new file mode 100644 index 000000000..7b1dc3c39 --- /dev/null +++ b/Packages/ComposeUI/Sources/ComposeUI/View+ForwardsCompat.swift @@ -0,0 +1,26 @@ +// +// View+ForwardsCompat.swift +// ComposeUI +// +// Created by Shadowfacts on 3/25/23. +// + +import SwiftUI + +extension View { + #if os(visionOS) + func scrollDisabledIfAvailable(_ disabled: Bool) -> some View { + self.scrollDisabled(disabled) + } + #else + @available(iOS, obsoleted: 16.0) + @ViewBuilder + func scrollDisabledIfAvailable(_ disabled: Bool) -> some View { + if #available(iOS 16.0, *) { + self.scrollDisabled(disabled) + } else { + self + } + } + #endif +} diff --git a/Packages/Duckable/Package.swift b/Packages/Duckable/Package.swift index 830960d67..bdff53ed0 100644 --- a/Packages/Duckable/Package.swift +++ b/Packages/Duckable/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "Duckable", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. diff --git a/Packages/GalleryVC/Package.swift b/Packages/GalleryVC/Package.swift index 158fd4c8d..14fd97fff 100644 --- a/Packages/GalleryVC/Package.swift +++ b/Packages/GalleryVC/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "GalleryVC", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, making them visible to other packages. diff --git a/Packages/GalleryVC/Sources/GalleryVC/Content/VideoControlsViewController.swift b/Packages/GalleryVC/Sources/GalleryVC/Content/VideoControlsViewController.swift index f19f13601..6f5046f9d 100644 --- a/Packages/GalleryVC/Sources/GalleryVC/Content/VideoControlsViewController.swift +++ b/Packages/GalleryVC/Sources/GalleryVC/Content/VideoControlsViewController.swift @@ -9,6 +9,15 @@ import UIKit import AVFoundation +@propertyWrapper +final class Box { + var wrappedValue: T + + init(wrappedValue: T) { + self.wrappedValue = wrappedValue + } +} + class VideoControlsViewController: UIViewController { private static let formatter: DateComponentsFormatter = { let f = DateComponentsFormatter() @@ -18,6 +27,9 @@ class VideoControlsViewController: UIViewController { }() private let player: AVPlayer + #if !os(visionOS) + @Box private var playbackSpeed: Float + #endif private lazy var muteButton: MuteButton = { let button = MuteButton() @@ -51,8 +63,13 @@ class VideoControlsViewController: UIViewController { private lazy var optionsButton = MenuButton { [unowned self] in let imageName: String + #if os(visionOS) + let playbackSpeed = player.defaultRate + #else + let playbackSpeed = self.playbackSpeed + #endif if #available(iOS 17.0, *) { - switch player.defaultRate { + switch playbackSpeed { case 0.5: imageName = "gauge.with.dots.needle.0percent" case 1: @@ -68,8 +85,12 @@ class VideoControlsViewController: UIViewController { imageName = "speedometer" } let speedMenu = UIMenu(title: "Playback Speed", image: UIImage(systemName: imageName), children: PlaybackSpeed.allCases.map { speed in - UIAction(title: speed.displayName, state: self.player.defaultRate == speed.rate ? .on : .off) { [unowned self] _ in + UIAction(title: speed.displayName, state: playbackSpeed == speed.rate ? .on : .off) { [unowned self] _ in + #if os(visionOS) self.player.defaultRate = speed.rate + #else + self.playbackSpeed = speed.rate + #endif if self.player.rate > 0 { self.player.rate = speed.rate } @@ -99,11 +120,20 @@ class VideoControlsViewController: UIViewController { private var scrubbingTargetTime: CMTime? private var isSeeking = false + #if os(visionOS) init(player: AVPlayer) { self.player = player super.init(nibName: nil, bundle: nil) } + #else + init(player: AVPlayer, playbackSpeed: Box) { + self.player = player + self._playbackSpeed = playbackSpeed + + super.init(nibName: nil, bundle: nil) + } + #endif required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") @@ -187,7 +217,11 @@ class VideoControlsViewController: UIViewController { @objc private func scrubbingEnded() { scrubbingChanged() if wasPlayingWhenScrubbingStarted { + #if os(visionOS) player.play() + #else + player.rate = playbackSpeed + #endif } } diff --git a/Packages/GalleryVC/Sources/GalleryVC/Content/VideoGalleryContentViewController.swift b/Packages/GalleryVC/Sources/GalleryVC/Content/VideoGalleryContentViewController.swift index 8d5488ee6..a668bb39d 100644 --- a/Packages/GalleryVC/Sources/GalleryVC/Content/VideoGalleryContentViewController.swift +++ b/Packages/GalleryVC/Sources/GalleryVC/Content/VideoGalleryContentViewController.swift @@ -16,6 +16,11 @@ open class VideoGalleryContentViewController: UIViewController, GalleryContentVi public private(set) var item: AVPlayerItem public let player: AVPlayer + #if !os(visionOS) + @available(iOS, obsoleted: 16.0, message: "Use AVPlayer.defaultRate") + @Box private var playbackSpeed: Float = 1 + #endif + private var presentationSizeObservation: NSKeyValueObservation? private var statusObservation: NSKeyValueObservation? private var rateObservation: NSKeyValueObservation? @@ -165,12 +170,20 @@ open class VideoGalleryContentViewController: UIViewController, GalleryContentVi isShowingError ? .fade : .fromSourceViewWithoutSnapshot } + #if os(visionOS) private lazy var overlayVC = VideoOverlayViewController(player: player) + #else + private lazy var overlayVC = VideoOverlayViewController(player: player, playbackSpeed: _playbackSpeed) + #endif public var contentOverlayAccessoryViewController: UIViewController? { overlayVC } + #if os(visionOS) public private(set) lazy var bottomControlsAccessoryViewController: UIViewController? = VideoControlsViewController(player: player) + #else + public private(set) lazy var bottomControlsAccessoryViewController: UIViewController? = VideoControlsViewController(player: player, playbackSpeed: _playbackSpeed) + #endif public func setControlsVisible(_ visible: Bool, animated: Bool, dueToUserInteraction: Bool) { if !isShowingError { diff --git a/Packages/GalleryVC/Sources/GalleryVC/Content/VideoOverlayViewController.swift b/Packages/GalleryVC/Sources/GalleryVC/Content/VideoOverlayViewController.swift index 2877621d8..b86a9af31 100644 --- a/Packages/GalleryVC/Sources/GalleryVC/Content/VideoOverlayViewController.swift +++ b/Packages/GalleryVC/Sources/GalleryVC/Content/VideoOverlayViewController.swift @@ -15,6 +15,9 @@ class VideoOverlayViewController: UIViewController { private static let pauseImage = UIImage(systemName: "pause.fill")! private let player: AVPlayer + #if !os(visionOS) + @Box private var playbackSpeed: Float + #endif private var dimmingView: UIView! private var controlsStack: UIStackView! @@ -23,10 +26,18 @@ class VideoOverlayViewController: UIViewController { private var rateObservation: NSKeyValueObservation? + #if os(visionOS) init(player: AVPlayer) { self.player = player super.init(nibName: nil, bundle: nil) } + #else + init(player: AVPlayer, playbackSpeed: Box) { + self.player = player + self._playbackSpeed = playbackSpeed + super.init(nibName: nil, bundle: nil) + } + #endif required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") @@ -98,7 +109,11 @@ class VideoOverlayViewController: UIViewController { if player.currentTime() >= player.currentItem!.duration { player.seek(to: .zero) } + #if os(visionOS) player.play() + #else + player.rate = playbackSpeed + #endif } } diff --git a/Packages/InstanceFeatures/Package.swift b/Packages/InstanceFeatures/Package.swift index 3da3f9a84..04f8493c5 100644 --- a/Packages/InstanceFeatures/Package.swift +++ b/Packages/InstanceFeatures/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "InstanceFeatures", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. diff --git a/Packages/MatchedGeometryPresentation/Package.swift b/Packages/MatchedGeometryPresentation/Package.swift index a5d53636d..ca4be041d 100644 --- a/Packages/MatchedGeometryPresentation/Package.swift +++ b/Packages/MatchedGeometryPresentation/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "MatchedGeometryPresentation", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, making them visible to other packages. diff --git a/Packages/Pachyderm/Package.swift b/Packages/Pachyderm/Package.swift index a1be7f1d9..aaa51975b 100644 --- a/Packages/Pachyderm/Package.swift +++ b/Packages/Pachyderm/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "Pachyderm", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. diff --git a/Packages/PushNotifications/Package.swift b/Packages/PushNotifications/Package.swift index e4a2c5c92..4fbfe4fa3 100644 --- a/Packages/PushNotifications/Package.swift +++ b/Packages/PushNotifications/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "PushNotifications", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, making them visible to other packages. diff --git a/Packages/TTTKit/Package.swift b/Packages/TTTKit/Package.swift index 64ab3e039..4b8f753ee 100644 --- a/Packages/TTTKit/Package.swift +++ b/Packages/TTTKit/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "TTTKit", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. diff --git a/Packages/TuskerComponents/Package.swift b/Packages/TuskerComponents/Package.swift index 350c14613..077ead8b2 100644 --- a/Packages/TuskerComponents/Package.swift +++ b/Packages/TuskerComponents/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "TuskerComponents", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. diff --git a/Packages/TuskerComponents/Sources/TuskerComponents/AsyncPicker.swift b/Packages/TuskerComponents/Sources/TuskerComponents/AsyncPicker.swift index 5777b6949..d1b78a2ca 100644 --- a/Packages/TuskerComponents/Sources/TuskerComponents/AsyncPicker.swift +++ b/Packages/TuskerComponents/Sources/TuskerComponents/AsyncPicker.swift @@ -9,14 +9,21 @@ import SwiftUI public struct AsyncPicker: View { let titleKey: LocalizedStringKey + #if !os(visionOS) + @available(iOS, obsoleted: 16.0, message: "Switch to LabeledContent") + let labelHidden: Bool + #endif let alignment: Alignment @Binding var value: V let onChange: (V) async -> Bool let content: Content @State private var isLoading = false - public init(_ titleKey: LocalizedStringKey, alignment: Alignment = .center, value: Binding, onChange: @escaping (V) async -> Bool, @ViewBuilder content: () -> Content) { + public init(_ titleKey: LocalizedStringKey, labelHidden: Bool = false, alignment: Alignment = .center, value: Binding, onChange: @escaping (V) async -> Bool, @ViewBuilder content: () -> Content) { self.titleKey = titleKey + #if !os(visionOS) + self.labelHidden = labelHidden + #endif self.alignment = alignment self._value = value self.onChange = onChange @@ -24,9 +31,25 @@ public struct AsyncPicker: View { } public var body: some View { + #if os(visionOS) LabeledContent(titleKey) { picker } + #else + if #available(iOS 16.0, *) { + LabeledContent(titleKey) { + picker + } + } else if labelHidden { + picker + } else { + HStack { + Text(titleKey) + Spacer() + picker + } + } + #endif } private var picker: some View { diff --git a/Packages/TuskerComponents/Sources/TuskerComponents/AsyncToggle.swift b/Packages/TuskerComponents/Sources/TuskerComponents/AsyncToggle.swift index 9352daf68..4e0b00ace 100644 --- a/Packages/TuskerComponents/Sources/TuskerComponents/AsyncToggle.swift +++ b/Packages/TuskerComponents/Sources/TuskerComponents/AsyncToggle.swift @@ -10,19 +10,42 @@ import SwiftUI public struct AsyncToggle: View { let titleKey: LocalizedStringKey + #if !os(visionOS) + @available(iOS, obsoleted: 16.0, message: "Switch to LabeledContent") + let labelHidden: Bool + #endif @Binding var mode: Mode let onChange: (Bool) async -> Bool - public init(_ titleKey: LocalizedStringKey, mode: Binding, onChange: @escaping (Bool) async -> Bool) { + public init(_ titleKey: LocalizedStringKey, labelHidden: Bool = false, mode: Binding, onChange: @escaping (Bool) async -> Bool) { self.titleKey = titleKey + #if !os(visionOS) + self.labelHidden = labelHidden + #endif self._mode = mode self.onChange = onChange } public var body: some View { + #if os(visionOS) LabeledContent(titleKey) { toggleOrSpinner } + #else + if #available(iOS 16.0, *) { + LabeledContent(titleKey) { + toggleOrSpinner + } + } else if labelHidden { + toggleOrSpinner + } else { + HStack { + Text(titleKey) + Spacer() + toggleOrSpinner + } + } + #endif } @ViewBuilder diff --git a/Packages/TuskerComponents/Sources/TuskerComponents/MenuPicker.swift b/Packages/TuskerComponents/Sources/TuskerComponents/MenuPicker.swift index 80bc60723..08f67c1ed 100644 --- a/Packages/TuskerComponents/Sources/TuskerComponents/MenuPicker.swift +++ b/Packages/TuskerComponents/Sources/TuskerComponents/MenuPicker.swift @@ -47,7 +47,9 @@ public struct MenuPicker: UIViewRepresentable { private func makeConfiguration() -> UIButton.Configuration { var config = UIButton.Configuration.borderless() - config.indicator = .popup + if #available(iOS 16.0, *) { + config.indicator = .popup + } if buttonStyle.hasIcon { config.image = selectedOption.image } diff --git a/Packages/TuskerPreferences/Package.swift b/Packages/TuskerPreferences/Package.swift index 58aa2cf84..6624c3eca 100644 --- a/Packages/TuskerPreferences/Package.swift +++ b/Packages/TuskerPreferences/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "TuskerPreferences", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, making them visible to other packages. diff --git a/Packages/UserAccounts/Package.swift b/Packages/UserAccounts/Package.swift index fc1b8c5b4..30fb63f2f 100644 --- a/Packages/UserAccounts/Package.swift +++ b/Packages/UserAccounts/Package.swift @@ -6,7 +6,7 @@ import PackageDescription let package = Package( name: "UserAccounts", platforms: [ - .iOS(.v16), + .iOS(.v15), ], products: [ // Products define the executables and libraries a package produces, and make them visible to other packages. diff --git a/Tusker.xcodeproj/project.pbxproj b/Tusker.xcodeproj/project.pbxproj index 3e451d7c4..d25760d59 100644 --- a/Tusker.xcodeproj/project.pbxproj +++ b/Tusker.xcodeproj/project.pbxproj @@ -141,6 +141,7 @@ D64B96812BC3279D002C8990 /* PrefsAccountView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64B96802BC3279D002C8990 /* PrefsAccountView.swift */; }; D64B96842BC3893C002C8990 /* PushSubscriptionView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64B96832BC3893C002C8990 /* PushSubscriptionView.swift */; }; D64D0AB12128D9AE005A6F37 /* OnboardingViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */; }; + D64D8CA92463B494006B0BAA /* MultiThreadDictionary.swift in Sources */ = {isa = PBXBuildFile; fileRef = D64D8CA82463B494006B0BAA /* MultiThreadDictionary.swift */; }; D651C5B42915B00400236EF6 /* ProfileFieldsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D651C5B32915B00400236EF6 /* ProfileFieldsView.swift */; }; D6531DEE246B81C9000F9538 /* GifvPlayerView.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6531DED246B81C9000F9538 /* GifvPlayerView.swift */; }; D6538945214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */; }; @@ -332,7 +333,7 @@ D6D79F592A13293200AB2315 /* BackgroundManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D79F582A13293200AB2315 /* BackgroundManager.swift */; }; D6D94955298963A900C59229 /* Colors.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D94954298963A900C59229 /* Colors.swift */; }; D6D9498D298CBB4000C59229 /* TrendingStatusCollectionViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D9498C298CBB4000C59229 /* TrendingStatusCollectionViewCell.swift */; }; - D6D9498F298EB79400C59229 /* CopyableLabel.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D9498E298EB79400C59229 /* CopyableLabel.swift */; }; + D6D9498F298EB79400C59229 /* CopyableLable.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6D9498E298EB79400C59229 /* CopyableLable.swift */; }; D6DD8FFD298495A8002AD3FD /* LogoutService.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD8FFC298495A8002AD3FD /* LogoutService.swift */; }; D6DD8FFF2984D327002AD3FD /* BookmarksViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD8FFE2984D327002AD3FD /* BookmarksViewController.swift */; }; D6DD996B2998611A0015C962 /* SuggestedProfilesViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D6DD996A2998611A0015C962 /* SuggestedProfilesViewController.swift */; }; @@ -567,6 +568,7 @@ D64B96802BC3279D002C8990 /* PrefsAccountView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PrefsAccountView.swift; sourceTree = ""; }; D64B96832BC3893C002C8990 /* PushSubscriptionView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PushSubscriptionView.swift; sourceTree = ""; }; D64D0AB02128D9AE005A6F37 /* OnboardingViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OnboardingViewController.swift; sourceTree = ""; }; + D64D8CA82463B494006B0BAA /* MultiThreadDictionary.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiThreadDictionary.swift; sourceTree = ""; }; D651C5B32915B00400236EF6 /* ProfileFieldsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileFieldsView.swift; sourceTree = ""; }; D6531DED246B81C9000F9538 /* GifvPlayerView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GifvPlayerView.swift; sourceTree = ""; }; D6538944214D6D7500E3CEFC /* TableViewSwipeActionProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TableViewSwipeActionProvider.swift; sourceTree = ""; }; @@ -768,7 +770,7 @@ D6D79F582A13293200AB2315 /* BackgroundManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BackgroundManager.swift; sourceTree = ""; }; D6D94954298963A900C59229 /* Colors.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Colors.swift; sourceTree = ""; }; D6D9498C298CBB4000C59229 /* TrendingStatusCollectionViewCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TrendingStatusCollectionViewCell.swift; sourceTree = ""; }; - D6D9498E298EB79400C59229 /* CopyableLabel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CopyableLabel.swift; sourceTree = ""; }; + D6D9498E298EB79400C59229 /* CopyableLable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CopyableLable.swift; sourceTree = ""; }; D6DD8FFC298495A8002AD3FD /* LogoutService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LogoutService.swift; sourceTree = ""; }; D6DD8FFE2984D327002AD3FD /* BookmarksViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BookmarksViewController.swift; sourceTree = ""; }; D6DD996A2998611A0015C962 /* SuggestedProfilesViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SuggestedProfilesViewController.swift; sourceTree = ""; }; @@ -1476,7 +1478,7 @@ D6ADB6EF28ED1F25009924AB /* CachedImageView.swift */, D6895DC328D65342006341DA /* ConfirmReblogStatusPreviewView.swift */, D620483523D38075008A63EF /* ContentTextView.swift */, - D6D9498E298EB79400C59229 /* CopyableLabel.swift */, + D6D9498E298EB79400C59229 /* CopyableLable.swift */, D6E426B225337C7000C02E1C /* CustomEmojiImageView.swift */, D6969E9F240C8384002843CE /* EmojiLabel.swift */, D61F75B8293C15A000C0B37F /* ZeroHeightCollectionViewCell.swift */, @@ -1613,6 +1615,7 @@ D6DEBA8C2B6579830008629A /* MainThreadBox.swift */, D6B81F432560390300F6E31D /* MenuController.swift */, D6CF5B842AC7C56F00F15D83 /* MultiColumnCollectionViewLayout.swift */, + D64D8CA82463B494006B0BAA /* MultiThreadDictionary.swift */, D6945C2E23AC47C3005C403C /* SavedDataManager.swift */, D61F759E29385AD800C0B37F /* SemiCaseSensitiveComparator.swift */, D6895DE828D962C2006341DA /* TimelineLikeController.swift */, @@ -2287,7 +2290,7 @@ D61ABEFE28F1C92600B29151 /* FavoriteService.swift in Sources */, D61F75AB293AF11400C0B37F /* FilterKeywordMO.swift in Sources */, D65B4B5A29720AB000DABDFB /* ReportStatusView.swift in Sources */, - D6D9498F298EB79400C59229 /* CopyableLabel.swift in Sources */, + D6D9498F298EB79400C59229 /* CopyableLable.swift in Sources */, D6333B792139AEFD00CE884A /* Date+TimeAgo.swift in Sources */, D6D706A029466649000827ED /* ScrollingSegmentedControl.swift in Sources */, D686BBE324FBF8110068E6AA /* WrappedProgressView.swift in Sources */, @@ -2371,6 +2374,7 @@ D62E9987279D094F00C26176 /* StatusMetaIndicatorsView.swift in Sources */, D6BEA247291A0F2D002F4D01 /* Duckable+Root.swift in Sources */, D6C1B2082545D1EC00DAAA66 /* StatusCardView.swift in Sources */, + D64D8CA92463B494006B0BAA /* MultiThreadDictionary.swift in Sources */, D630C3CA2BC59FF500208903 /* MastodonController+Push.swift in Sources */, D6F6A554291F0D9600F496A8 /* DeleteListService.swift in Sources */, D68E6F5F253C9B2D001A1B4C /* BaseEmojiLabel.swift in Sources */, @@ -2527,7 +2531,6 @@ INFOPLIST_FILE = NotificationExtension/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = NotificationExtension; INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Shadowfacts. All rights reserved."; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -2560,7 +2563,6 @@ INFOPLIST_FILE = NotificationExtension/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = NotificationExtension; INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Shadowfacts. All rights reserved."; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -2592,7 +2594,6 @@ INFOPLIST_FILE = NotificationExtension/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = NotificationExtension; INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2024 Shadowfacts. All rights reserved."; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -2683,7 +2684,6 @@ CURRENT_PROJECT_VERSION = "$(CURRENT_PROJECT_VERSION)"; INFOPLIST_FILE = Tusker/Info.plist; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking"; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -2727,7 +2727,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; CODE_SIGN_STYLE = Automatic; INFOPLIST_FILE = TuskerUITests/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -2750,7 +2750,6 @@ CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = "$(CURRENT_PROJECT_VERSION)"; INFOPLIST_FILE = OpenInTusker/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -2778,7 +2777,6 @@ INFOPLIST_FILE = ShareExtension/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = ShareExtension; INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2023 Shadowfacts. All rights reserved."; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -2807,7 +2805,6 @@ INFOPLIST_FILE = ShareExtension/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = ShareExtension; INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2023 Shadowfacts. All rights reserved."; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -2836,7 +2833,6 @@ INFOPLIST_FILE = ShareExtension/Info.plist; INFOPLIST_KEY_CFBundleDisplayName = ShareExtension; INFOPLIST_KEY_NSHumanReadableCopyright = "Copyright © 2023 Shadowfacts. All rights reserved."; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -2992,7 +2988,6 @@ CURRENT_PROJECT_VERSION = "$(CURRENT_PROJECT_VERSION)"; INFOPLIST_FILE = Tusker/Info.plist; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking"; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -3025,7 +3020,6 @@ CURRENT_PROJECT_VERSION = "$(CURRENT_PROJECT_VERSION)"; INFOPLIST_FILE = Tusker/Info.plist; INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.social-networking"; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -3090,7 +3084,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; CODE_SIGN_STYLE = Automatic; INFOPLIST_FILE = TuskerUITests/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -3110,7 +3104,7 @@ ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; CODE_SIGN_STYLE = Automatic; INFOPLIST_FILE = TuskerUITests/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; + IPHONEOS_DEPLOYMENT_TARGET = 15.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -3133,7 +3127,6 @@ CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = "$(CURRENT_PROJECT_VERSION)"; INFOPLIST_FILE = OpenInTusker/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", @@ -3158,7 +3151,6 @@ CODE_SIGN_STYLE = Automatic; CURRENT_PROJECT_VERSION = "$(CURRENT_PROJECT_VERSION)"; INFOPLIST_FILE = OpenInTusker/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 16.0; LD_RUNPATH_SEARCH_PATHS = ( "$(inherited)", "@executable_path/Frameworks", diff --git a/Tusker/Caching/DiskCache.swift b/Tusker/Caching/DiskCache.swift index d13b68b4e..aaf8b86f0 100644 --- a/Tusker/Caching/DiskCache.swift +++ b/Tusker/Caching/DiskCache.swift @@ -8,7 +8,6 @@ import Foundation import CryptoKit -import os struct DiskCacheTransformer { let toData: (T) throws -> Data @@ -22,7 +21,7 @@ class DiskCache { let defaultExpiry: CacheExpiry let transformer: DiskCacheTransformer - private var fileStates = OSAllocatedUnfairLock(initialState: [String: FileState]()) + private var fileStates = MultiThreadDictionary() init(name: String, defaultExpiry: CacheExpiry, transformer: DiskCacheTransformer, fileManager: FileManager = .default) throws { self.defaultExpiry = defaultExpiry @@ -60,9 +59,7 @@ class DiskCache { } private func fileState(forKey key: String) -> FileState { - return fileStates.withLock { - $0[key] ?? .unknown - } + return fileStates[key] ?? .unknown } func setObject(_ object: T, forKey key: String) throws { @@ -71,17 +68,13 @@ class DiskCache { guard fileManager.createFile(atPath: path, contents: data, attributes: [.modificationDate: defaultExpiry.date]) else { throw Error.couldNotCreateFile } - fileStates.withLock { - $0[key] = .exists - } + fileStates[key] = .exists } func removeObject(forKey key: String) throws { let path = makeFilePath(for: key) try fileManager.removeItem(atPath: path) - fileStates.withLock { - $0[key] = .doesNotExist - } + fileStates[key] = .doesNotExist } func existsObject(forKey key: String) throws -> Bool { @@ -112,9 +105,7 @@ class DiskCache { } guard date.timeIntervalSinceNow >= 0 else { try fileManager.removeItem(atPath: path) - fileStates.withLock { - $0[key] = .doesNotExist - } + fileStates[key] = .doesNotExist throw Error.expired } diff --git a/Tusker/CoreData/TimelinePosition.swift b/Tusker/CoreData/TimelinePosition.swift index d340d6fcb..ac29440bc 100644 --- a/Tusker/CoreData/TimelinePosition.swift +++ b/Tusker/CoreData/TimelinePosition.swift @@ -76,10 +76,17 @@ func fromTimelineKind(_ kind: String) -> Timeline { } else if kind == "direct" { return .direct } else if kind.starts(with: "hashtag:") { - return .tag(hashtag: String(kind.trimmingPrefix("hashtag:"))) + return .tag(hashtag: String(trimmingPrefix("hashtag:", of: kind))) } else if kind.starts(with: "list:") { - return .list(id: String(kind.trimmingPrefix("list:"))) + return .list(id: String(trimmingPrefix("list:", of: kind))) } else { fatalError("invalid timeline kind \(kind)") } } + +// replace with Collection.trimmingPrefix +@available(iOS, obsoleted: 16.0) +@available(visionOS 1.0, *) +private func trimmingPrefix(_ prefix: String, of str: String) -> Substring { + return str[str.index(str.startIndex, offsetBy: prefix.count)...] +} diff --git a/Tusker/Extensions/View+AppListStyle.swift b/Tusker/Extensions/View+AppListStyle.swift index 2d0e79651..472bfc3c1 100644 --- a/Tusker/Extensions/View+AppListStyle.swift +++ b/Tusker/Extensions/View+AppListStyle.swift @@ -36,12 +36,19 @@ private struct AppGroupedListBackground: ViewModifier { } func body(content: Content) -> some View { - if colorScheme == .dark, !pureBlackDarkMode { - content - .scrollContentBackground(.hidden) - .background(Color.appGroupedBackground.edgesIgnoringSafeArea(.all)) + if #available(iOS 16.0, *) { + if colorScheme == .dark, !pureBlackDarkMode { + content + .scrollContentBackground(.hidden) + .background(Color.appGroupedBackground.edgesIgnoringSafeArea(.all)) + } else { + content + } } else { content + .onAppear { + UITableView.appearance(whenContainedInInstancesOf: [container]).backgroundColor = .appGroupedBackground + } } } } diff --git a/Tusker/HTMLConverter.swift b/Tusker/HTMLConverter.swift index c1975d232..26dd6ccf8 100644 --- a/Tusker/HTMLConverter.swift +++ b/Tusker/HTMLConverter.swift @@ -48,13 +48,14 @@ extension HTMLConverter { // Converting WebURL to URL is a small but non-trivial expense (since it works by // serializing the WebURL as a string and then having Foundation parse it again), // so, if available, use the system parser which doesn't require another round trip. - if let url = try? URL.ParseStrategy().parse(string) { + if #available(iOS 16.0, macOS 13.0, *), + let url = try? URL.ParseStrategy().parse(string) { url } else if let web = WebURL(string), let url = URL(web) { url } else { - nil + URL(string: string) } } diff --git a/Tusker/MultiThreadDictionary.swift b/Tusker/MultiThreadDictionary.swift new file mode 100644 index 000000000..8f015d777 --- /dev/null +++ b/Tusker/MultiThreadDictionary.swift @@ -0,0 +1,104 @@ +// +// MultiThreadDictionary.swift +// Tusker +// +// Created by Shadowfacts on 5/6/20. +// Copyright © 2020 Shadowfacts. All rights reserved. +// + +import Foundation +import os + +// once we target iOS 16, replace uses of this with OSAllocatedUnfairLock<[Key: Value]> +// to make the lock semantics more clear +@available(iOS, obsoleted: 16.0) +@available(visionOS 1.0, *) +final class MultiThreadDictionary: @unchecked Sendable { + #if os(visionOS) + private let lock = OSAllocatedUnfairLock(initialState: [Key: Value]()) + #else + private let lock: any Lock<[Key: Value]> + #endif + + init() { + #if !os(visionOS) + if #available(iOS 16.0, *) { + self.lock = OSAllocatedUnfairLock(initialState: [:]) + } else { + self.lock = UnfairLock(initialState: [:]) + } + #endif + } + + subscript(key: Key) -> Value? { + get { + return lock.withLock { dict in + dict[key] + } + } + set(value) { + #if os(visionOS) + lock.withLock { dict in + dict[key] = value + } + #else + _ = lock.withLock { dict in + dict[key] = value + } + #endif + } + } + + /// If the result of this function is unused, it is preferable to use `removeValueWithoutReturning` as it executes asynchronously and doesn't block the calling thread. + func removeValue(forKey key: Key) -> Value? { + return lock.withLock { dict in + dict.removeValue(forKey: key) + } + } + + func contains(key: Key) -> Bool { + return lock.withLock { dict in + dict.keys.contains(key) + } + } + + // TODO: this should really be throws/rethrows but the stupid type-erased lock makes that not possible + func withLock(_ body: @Sendable (inout [Key: Value]) throws -> R) rethrows -> R where R: Sendable { + return try lock.withLock { dict in + return try body(&dict) + } + } +} + +#if !os(visionOS) +// TODO: replace this only with OSAllocatedUnfairLock +@available(iOS, obsoleted: 16.0) +fileprivate protocol Lock { + associatedtype State + func withLock(_ body: @Sendable (inout State) throws -> R) rethrows -> R where R: Sendable +} + +@available(iOS 16.0, *) +extension OSAllocatedUnfairLock: Lock { +} + +// from http://www.russbishop.net/the-law +fileprivate class UnfairLock: Lock { + private var lock: UnsafeMutablePointer + private var state: State + init(initialState: State) { + self.state = initialState + self.lock = .allocate(capacity: 1) + self.lock.initialize(to: os_unfair_lock()) + } + deinit { + self.lock.deinitialize(count: 1) + self.lock.deallocate() + } + func withLock(_ body: (inout State) throws -> R) rethrows -> R where R: Sendable { + os_unfair_lock_lock(lock) + defer { os_unfair_lock_unlock(lock) } + return try body(&state) + } +} +#endif diff --git a/Tusker/Scenes/MainSceneDelegate.swift b/Tusker/Scenes/MainSceneDelegate.swift index ffb6486c0..31e5b6678 100644 --- a/Tusker/Scenes/MainSceneDelegate.swift +++ b/Tusker/Scenes/MainSceneDelegate.swift @@ -274,7 +274,8 @@ class MainSceneDelegate: UIResponder, UIWindowSceneDelegate, TuskerSceneDelegate } else { mainVC = MainSplitViewController(mastodonController: mastodonController) } - if UIDevice.current.userInterfaceIdiom == .phone { + if UIDevice.current.userInterfaceIdiom == .phone, + #available(iOS 16.0, *) { // TODO: maybe the duckable container should be outside the account switching container return DuckableContainerViewController(child: mainVC) } else { diff --git a/Tusker/Screens/Announcements/AddReactionView.swift b/Tusker/Screens/Announcements/AddReactionView.swift index c5ca7269f..f365ef1be 100644 --- a/Tusker/Screens/Announcements/AddReactionView.swift +++ b/Tusker/Screens/Announcements/AddReactionView.swift @@ -86,7 +86,7 @@ struct AddReactionView: View { } } .navigationViewStyle(.stack) - .presentationDetents([.medium, .large]) + .mediumPresentationDetentIfAvailable() .alertWithData("Error Adding Reaction", data: $error, actions: { _ in Button("OK") {} }, message: { error in @@ -171,6 +171,17 @@ private struct AddReactionButton: View { } private extension View { + @available(iOS, obsoleted: 16.0) + @available(visionOS 1.0, *) + @ViewBuilder + func mediumPresentationDetentIfAvailable() -> some View { + if #available(iOS 16.0, *) { + self.presentationDetents([.medium, .large]) + } else { + self + } + } + @available(iOS, obsoleted: 17.1) @available(visionOS 1.0, *) @ViewBuilder diff --git a/Tusker/Screens/Announcements/AnnouncementListRow.swift b/Tusker/Screens/Announcements/AnnouncementListRow.swift index 13371c20f..412338779 100644 --- a/Tusker/Screens/Announcements/AnnouncementListRow.swift +++ b/Tusker/Screens/Announcements/AnnouncementListRow.swift @@ -20,10 +20,14 @@ struct AnnouncementListRow: View { @State private var isShowingAddReactionSheet = false var body: some View { - mostOfTheBody - .alignmentGuide(.listRowSeparatorLeading, computeValue: { dimension in - dimension[.leading] - }) + if #available(iOS 16.0, *) { + mostOfTheBody + .alignmentGuide(.listRowSeparatorLeading, computeValue: { dimension in + dimension[.leading] + }) + } else { + mostOfTheBody + } } private var mostOfTheBody: some View { @@ -50,7 +54,11 @@ struct AnnouncementListRow: View { Label { Text("Add Reaction") } icon: { - Image("face.smiling.badge.plus") + if #available(iOS 16.0, *) { + Image("face.smiling.badge.plus") + } else { + Image(systemName: "face.smiling") + } } } .labelStyle(.iconOnly) diff --git a/Tusker/Screens/Customize Timelines/AddHashtagPinnedTimelineView.swift b/Tusker/Screens/Customize Timelines/AddHashtagPinnedTimelineView.swift index 1c66346d4..30e4d53cd 100644 --- a/Tusker/Screens/Customize Timelines/AddHashtagPinnedTimelineView.swift +++ b/Tusker/Screens/Customize Timelines/AddHashtagPinnedTimelineView.swift @@ -9,6 +9,20 @@ import SwiftUI import Pachyderm +@available(iOS, obsoleted: 16.0) +struct AddHashtagPinnedTimelineRepresentable: UIViewControllerRepresentable { + typealias UIViewControllerType = UIHostingController + + @Binding var pinnedTimelines: [PinnedTimeline] + + func makeUIViewController(context: Context) -> UIHostingController { + return UIHostingController(rootView: AddHashtagPinnedTimelineView(pinnedTimelines: $pinnedTimelines)) + } + + func updateUIViewController(_ uiViewController: UIHostingController, context: Context) { + } +} + struct AddHashtagPinnedTimelineView: View { @EnvironmentObject private var mastodonController: MastodonController @Environment(\.dismiss) private var dismiss @@ -35,6 +49,9 @@ struct AddHashtagPinnedTimelineView: View { var body: some View { NavigationView { list + #if !os(visionOS) + .appGroupedListBackground(container: AddHashtagPinnedTimelineRepresentable.UIViewControllerType.self) + #endif .listStyle(.grouped) .navigationTitle("Add Hashtag") .navigationBarTitleDisplayMode(.inline) diff --git a/Tusker/Screens/Customize Timelines/CustomizeTimelinesView.swift b/Tusker/Screens/Customize Timelines/CustomizeTimelinesView.swift index 8eabf79b5..e7c32a642 100644 --- a/Tusker/Screens/Customize Timelines/CustomizeTimelinesView.swift +++ b/Tusker/Screens/Customize Timelines/CustomizeTimelinesView.swift @@ -36,8 +36,15 @@ struct CustomizeTimelinesList: View { } var body: some View { - NavigationStack { - navigationBody + if #available(iOS 16.0, *) { + NavigationStack { + navigationBody + } + } else { + NavigationView { + navigationBody + } + .navigationViewStyle(.stack) } } diff --git a/Tusker/Screens/Customize Timelines/EditFilterView.swift b/Tusker/Screens/Customize Timelines/EditFilterView.swift index c6452dffb..acfa70f02 100644 --- a/Tusker/Screens/Customize Timelines/EditFilterView.swift +++ b/Tusker/Screens/Customize Timelines/EditFilterView.swift @@ -149,7 +149,7 @@ struct EditFilterView: View { } .appGroupedListBackground(container: UIHostingController.self) #if !os(visionOS) - .scrollDismissesKeyboard(.interactively) + .scrollDismissesKeyboardInteractivelyIfAvailable() #endif .navigationTitle(create ? "Add Filter" : "Edit Filter") .navigationBarTitleDisplayMode(.inline) @@ -226,6 +226,18 @@ private struct FilterContextToggleStyle: ToggleStyle { } } +private extension View { + @available(iOS, obsoleted: 16.0) + @ViewBuilder + func scrollDismissesKeyboardInteractivelyIfAvailable() -> some View { + if #available(iOS 16.0, *) { + self.scrollDismissesKeyboard(.interactively) + } else { + self + } + } +} + //struct EditFilterView_Previews: PreviewProvider { // static var previews: some View { // EditFilterView() diff --git a/Tusker/Screens/Customize Timelines/PinnedTimelinesView.swift b/Tusker/Screens/Customize Timelines/PinnedTimelinesView.swift index 381cd4f36..c355f285b 100644 --- a/Tusker/Screens/Customize Timelines/PinnedTimelinesView.swift +++ b/Tusker/Screens/Customize Timelines/PinnedTimelinesView.swift @@ -115,8 +115,18 @@ struct PinnedTimelinesModifier: ViewModifier { func body(content: Content) -> some View { content .sheet(isPresented: $isShowingAddHashtagSheet, content: { + #if os(visionOS) AddHashtagPinnedTimelineView(pinnedTimelines: $pinnedTimelines) .edgesIgnoringSafeArea(.bottom) + #else + if #available(iOS 16.0, *) { + AddHashtagPinnedTimelineView(pinnedTimelines: $pinnedTimelines) + .edgesIgnoringSafeArea(.bottom) + } else { + AddHashtagPinnedTimelineRepresentable(pinnedTimelines: $pinnedTimelines) + .edgesIgnoringSafeArea(.bottom) + } + #endif }) .sheet(isPresented: $isShowingAddInstanceSheet, content: { AddInstancePinnedTimelineView(pinnedTimelines: $pinnedTimelines) diff --git a/Tusker/Screens/Explore/InlineTrendsViewController.swift b/Tusker/Screens/Explore/InlineTrendsViewController.swift index 8a4cd6fad..74ab5e36b 100644 --- a/Tusker/Screens/Explore/InlineTrendsViewController.swift +++ b/Tusker/Screens/Explore/InlineTrendsViewController.swift @@ -41,7 +41,9 @@ class InlineTrendsViewController: UIViewController { navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false - navigationItem.preferredSearchBarPlacement = .stacked + if #available(iOS 16.0, *) { + navigationItem.preferredSearchBarPlacement = .stacked + } let trends = TrendsViewController(mastodonController: mastodonController) trends.view.translatesAutoresizingMaskIntoConstraints = false diff --git a/Tusker/Screens/Explore/TrendsViewController.swift b/Tusker/Screens/Explore/TrendsViewController.swift index 511c22b8f..4b09e2e79 100644 --- a/Tusker/Screens/Explore/TrendsViewController.swift +++ b/Tusker/Screens/Explore/TrendsViewController.swift @@ -525,12 +525,12 @@ extension TrendsViewController: UICollectionViewDelegate { } } - func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemsAt indexPaths: [IndexPath], point: CGPoint) -> UIContextMenuConfiguration? { - guard indexPaths.count == 1, - let item = dataSource.itemIdentifier(for: indexPaths[0]) else { + @available(iOS, obsoleted: 16.0) + @available(visionOS 1.0, *) + func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemAt indexPath: IndexPath, point: CGPoint) -> UIContextMenuConfiguration? { + guard let item = dataSource.itemIdentifier(for: indexPath) else { return nil } - let indexPath = indexPaths[0] switch item { case .loadingIndicator, .confirmLoadMoreStatuses(_): @@ -584,6 +584,15 @@ extension TrendsViewController: UICollectionViewDelegate { } } + // implementing the highlightPreviewForItemAt method seems to prevent the old, single IndexPath variant of this method from being called on iOS 16 + @available(iOS 16.0, visionOS 1.0, *) + func collectionView(_ collectionView: UICollectionView, contextMenuConfigurationForItemsAt indexPaths: [IndexPath], point: CGPoint) -> UIContextMenuConfiguration? { + guard indexPaths.count == 1 else { + return nil + } + return self.collectionView(collectionView, contextMenuConfigurationForItemAt: indexPaths[0], point: point) + } + func collectionView(_ collectionView: UICollectionView, willPerformPreviewActionForMenuWith configuration: UIContextMenuConfiguration, animator: UIContextMenuInteractionCommitAnimating) { MenuPreviewHelper.willPerformPreviewAction(animator: animator, presenter: self) } diff --git a/Tusker/Screens/Lists/EditListAccountsViewController.swift b/Tusker/Screens/Lists/EditListAccountsViewController.swift index 31b1e249d..b3a8ed0c6 100644 --- a/Tusker/Screens/Lists/EditListAccountsViewController.swift +++ b/Tusker/Screens/Lists/EditListAccountsViewController.swift @@ -100,13 +100,28 @@ class EditListAccountsViewController: UIViewController, CollectionViewController navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false - navigationItem.preferredSearchBarPlacement = .stacked - - navigationItem.renameDelegate = self - navigationItem.titleMenuProvider = { [unowned self] suggested in - var children = suggested - children.append(contentsOf: self.listSettingsMenuElements()) - return UIMenu(children: children) + if #available(iOS 16.0, *) { + navigationItem.preferredSearchBarPlacement = .stacked + + navigationItem.renameDelegate = self + navigationItem.titleMenuProvider = { [unowned self] suggested in + var children = suggested + children.append(contentsOf: self.listSettingsMenuElements()) + return UIMenu(children: children) + } + } else { + navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Edit", menu: UIMenu(children: [ + // uncached so that menu always reflects the current state of the list + UIDeferredMenuElement.uncached({ [unowned self] elementHandler in + var elements = self.listSettingsMenuElements() + elements.insert(UIAction(title: "Rename…", image: UIImage(systemName: "pencil"), handler: { [unowned self] _ in + RenameListService(list: self.list, mastodonController: self.mastodonController, present: { + self.present($0, animated: true) + }).run() + }), at: 0) + elementHandler(elements) + }) + ])) } } diff --git a/Tusker/Screens/Mute/MuteAccountView.swift b/Tusker/Screens/Mute/MuteAccountView.swift index 8f1168c13..5cdc72a4c 100644 --- a/Tusker/Screens/Mute/MuteAccountView.swift +++ b/Tusker/Screens/Mute/MuteAccountView.swift @@ -41,8 +41,15 @@ struct MuteAccountView: View { @State private var error: Error? var body: some View { - NavigationStack { - navigationViewContent + if #available(iOS 16.0, *) { + NavigationStack { + navigationViewContent + } + } else { + NavigationView { + navigationViewContent + } + .navigationViewStyle(.stack) } } diff --git a/Tusker/Screens/Notifications/FollowRequestNotificationViewController.swift b/Tusker/Screens/Notifications/FollowRequestNotificationViewController.swift index 3d53b7738..522947027 100644 --- a/Tusker/Screens/Notifications/FollowRequestNotificationViewController.swift +++ b/Tusker/Screens/Notifications/FollowRequestNotificationViewController.swift @@ -101,8 +101,14 @@ extension FollowRequestNotificationViewController: UICollectionViewDelegate { UIAction(title: "Accept", image: UIImage(systemName: "checkmark.circle"), handler: { _ in cell.acceptButtonPressed() }), UIAction(title: "Reject", image: UIImage(systemName: "xmark.circle"), handler: { _ in cell.rejectButtonPressed() }), ] + let acceptRejectMenu: UIMenu + if #available(iOS 16.0, *) { + acceptRejectMenu = UIMenu(options: .displayInline, preferredElementSize: .medium, children: acceptRejectChildren) + } else { + acceptRejectMenu = UIMenu(options: .displayInline, children: acceptRejectChildren) + } return UIMenu(children: [ - UIMenu(options: .displayInline, preferredElementSize: .medium, children: acceptRejectChildren), + acceptRejectMenu, UIMenu(options: .displayInline, children: self.actionsForProfile(accountID: accountID, source: .view(cell))), ]) } diff --git a/Tusker/Screens/Notifications/NotificationsCollectionViewController.swift b/Tusker/Screens/Notifications/NotificationsCollectionViewController.swift index acd5b01a9..eb042b724 100644 --- a/Tusker/Screens/Notifications/NotificationsCollectionViewController.swift +++ b/Tusker/Screens/Notifications/NotificationsCollectionViewController.swift @@ -700,8 +700,14 @@ extension NotificationsCollectionViewController: UICollectionViewDelegate { UIAction(title: "Accept", image: UIImage(systemName: "checkmark.circle"), handler: { _ in cell.acceptButtonPressed() }), UIAction(title: "Reject", image: UIImage(systemName: "xmark.circle"), handler: { _ in cell.rejectButtonPressed() }), ] + let acceptRejectMenu: UIMenu + if #available(iOS 16.0, *) { + acceptRejectMenu = UIMenu(options: .displayInline, preferredElementSize: .medium, children: acceptRejectChildren) + } else { + acceptRejectMenu = UIMenu(options: .displayInline, children: acceptRejectChildren) + } return UIMenu(children: [ - UIMenu(options: .displayInline, preferredElementSize: .medium, children: acceptRejectChildren), + acceptRejectMenu, UIMenu(options: .displayInline, children: self.actionsForProfile(accountID: accountID, source: .view(cell))), ]) } diff --git a/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift b/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift index 32d2d018d..17f34f94c 100644 --- a/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift +++ b/Tusker/Screens/Onboarding/InstanceSelectorTableViewController.swift @@ -96,7 +96,9 @@ class InstanceSelectorTableViewController: UITableViewController { searchController.searchBar.placeholder = "Search or enter a URL" navigationItem.searchController = searchController navigationItem.hidesSearchBarWhenScrolling = false - navigationItem.preferredSearchBarPlacement = .stacked + if #available(iOS 16.0, *) { + navigationItem.preferredSearchBarPlacement = .stacked + } definesPresentationContext = true urlHandler = urlCheckerSubject diff --git a/Tusker/Screens/Preferences/About/AboutView.swift b/Tusker/Screens/Preferences/About/AboutView.swift index c3d0dcc52..8f87b236b 100644 --- a/Tusker/Screens/Preferences/About/AboutView.swift +++ b/Tusker/Screens/Preferences/About/AboutView.swift @@ -91,10 +91,14 @@ struct AboutView: View { @ViewBuilder private var iconOrGame: some View { - FlipView { + if #available(iOS 16.0, *) { + FlipView { + appIcon + } back: { + TTTView() + } + } else { appIcon - } back: { - TTTView() } } diff --git a/Tusker/Screens/Preferences/Appearance/AppearancePrefsView.swift b/Tusker/Screens/Preferences/Appearance/AppearancePrefsView.swift index 88e2fd8a5..35e836189 100644 --- a/Tusker/Screens/Preferences/Appearance/AppearancePrefsView.swift +++ b/Tusker/Screens/Preferences/Appearance/AppearancePrefsView.swift @@ -27,7 +27,14 @@ struct AppearancePrefsView: View { private static let accentColorsAndImages: [(AccentColor, UIImage?)] = AccentColor.allCases.map { color in var image: UIImage? if let color = color.color { - image = UIImage(systemName: "circle.fill")!.withTintColor(color, renderingMode: .alwaysTemplate).withRenderingMode(.alwaysOriginal) + if #available(iOS 16.0, *) { + image = UIImage(systemName: "circle.fill")!.withTintColor(color, renderingMode: .alwaysTemplate).withRenderingMode(.alwaysOriginal) + } else { + image = UIGraphicsImageRenderer(size: CGSize(width: 20, height: 20)).image { context in + color.setFill() + context.cgContext.fillEllipse(in: CGRect(x: 0, y: 0, width: 20, height: 20)) + } + } } return (color, image) } diff --git a/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift b/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift index 903395ce3..c8437e261 100644 --- a/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift +++ b/Tusker/Screens/Preferences/Notifications/NotificationsPrefsView.swift @@ -36,7 +36,12 @@ struct NotificationsPrefsView: View { if #available(iOS 15.4, *) { Section { Button { - if let url = URL(string: UIApplication.openNotificationSettingsURLString) { + let str = if #available(iOS 16.0, *) { + UIApplication.openNotificationSettingsURLString + } else { + UIApplicationOpenNotificationSettingsURLString + } + if let url = URL(string: str) { UIApplication.shared.open(url) } } label: { diff --git a/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift b/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift index 9d6a7cb06..98c68d6d8 100644 --- a/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift +++ b/Tusker/Screens/Preferences/Notifications/PushInstanceSettingsView.swift @@ -34,7 +34,7 @@ struct PushInstanceSettingsView: View { HStack { PrefsAccountView(account: account) Spacer() - AsyncToggle("\(account.instanceURL.host!) notifications enabled", mode: $mode, onChange: updateNotificationsEnabled(enabled:)) + AsyncToggle("\(account.instanceURL.host!) notifications enabled", labelHidden: true, mode: $mode, onChange: updateNotificationsEnabled(enabled:)) .labelsHidden() } PushSubscriptionView(account: account, mastodonController: mastodonController, subscription: subscription, updateSubscription: updateSubscription) diff --git a/Tusker/Screens/Preferences/OppositeCollapseKeywordsView.swift b/Tusker/Screens/Preferences/OppositeCollapseKeywordsView.swift index 60a92547a..27e737461 100644 --- a/Tusker/Screens/Preferences/OppositeCollapseKeywordsView.swift +++ b/Tusker/Screens/Preferences/OppositeCollapseKeywordsView.swift @@ -43,9 +43,21 @@ struct OppositeCollapseKeywordsView: View { .listStyle(.grouped) .appGroupedListBackground(container: PreferencesNavigationController.self) } + #if !os(visionOS) + .onAppear(perform: updateAppearance) + #endif .navigationBarTitle(preferences.expandAllContentWarnings ? "Collapse Post CW Keywords" : "Expand Post CW Keywords") } + @available(iOS, obsoleted: 16.0) + private func updateAppearance() { + if #available(iOS 16.0, *) { + // no longer necessary + } else { + UIScrollView.appearance(whenContainedInInstancesOf: [PreferencesNavigationController.self]).keyboardDismissMode = .interactive + } + } + private func commitExisting(at index: Int) -> () -> Void { return { if keywords[index].value.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { diff --git a/Tusker/Screens/Report/ReportAddStatusView.swift b/Tusker/Screens/Report/ReportAddStatusView.swift index a148828eb..e56cc76ba 100644 --- a/Tusker/Screens/Report/ReportAddStatusView.swift +++ b/Tusker/Screens/Report/ReportAddStatusView.swift @@ -69,30 +69,34 @@ private struct ScrollBackgroundModifier: ViewModifier { @Environment(\.colorScheme) private var colorScheme func body(content: Content) -> some View { - content - .scrollContentBackground(.hidden) - .background { - // otherwise the pureBlackDarkMode isn't propagated, for some reason? - // even though it is for ReportSelectRulesView?? - let traits: UITraitCollection = { - var t = UITraitCollection(userInterfaceStyle: colorScheme == .dark ? .dark : .light) - #if os(visionOS) - t = t.modifyingTraits({ mutableTraits in - mutableTraits.pureBlackDarkMode = true - }) - #else - if #available(iOS 17.0, *) { + if #available(iOS 16.0, *) { + content + .scrollContentBackground(.hidden) + .background { + // otherwise the pureBlackDarkMode isn't propagated, for some reason? + // even though it is for ReportSelectRulesView?? + let traits: UITraitCollection = { + var t = UITraitCollection(userInterfaceStyle: colorScheme == .dark ? .dark : .light) + #if os(visionOS) t = t.modifyingTraits({ mutableTraits in mutableTraits.pureBlackDarkMode = true }) - } else { - t.obsoletePureBlackDarkMode = true - } - #endif - return t - }() - Color(uiColor: .appGroupedBackground.resolvedColor(with: traits)) - .edgesIgnoringSafeArea(.all) - } + #else + if #available(iOS 17.0, *) { + t = t.modifyingTraits({ mutableTraits in + mutableTraits.pureBlackDarkMode = true + }) + } else { + t.obsoletePureBlackDarkMode = true + } + #endif + return t + }() + Color(uiColor: .appGroupedBackground.resolvedColor(with: traits)) + .edgesIgnoringSafeArea(.all) + } + } else { + content + } } } diff --git a/Tusker/Screens/Report/ReportSelectRulesView.swift b/Tusker/Screens/Report/ReportSelectRulesView.swift index 4d7d06267..39e6510a8 100644 --- a/Tusker/Screens/Report/ReportSelectRulesView.swift +++ b/Tusker/Screens/Report/ReportSelectRulesView.swift @@ -49,12 +49,26 @@ struct ReportSelectRulesView: View { } .appGroupedListRowBackground() } - .scrollContentBackground(.hidden) - .background(Color.appGroupedBackground) + .withAppBackgroundIfAvailable() .navigationTitle("Rules") } } +private extension View { + @available(iOS, obsoleted: 16.0) + @available(visionOS 1.0, *) + @ViewBuilder + func withAppBackgroundIfAvailable() -> some View { + if #available(iOS 16.0, *) { + self + .scrollContentBackground(.hidden) + .background(Color.appGroupedBackground) + } else { + self + } + } +} + //struct ReportSelectRulesView_Previews: PreviewProvider { // static var previews: some View { // ReportSelectRulesView() diff --git a/Tusker/Screens/Report/ReportView.swift b/Tusker/Screens/Report/ReportView.swift index ec6d3fb79..d51fae177 100644 --- a/Tusker/Screens/Report/ReportView.swift +++ b/Tusker/Screens/Report/ReportView.swift @@ -27,11 +27,18 @@ struct ReportView: View { } var body: some View { - NavigationStack { - navigationViewContent - #if !os(visionOS) - .scrollDismissesKeyboard(.interactively) - #endif + if #available(iOS 16.0, *) { + NavigationStack { + navigationViewContent + #if !os(visionOS) + .scrollDismissesKeyboard(.interactively) + #endif + } + } else { + NavigationView { + navigationViewContent + } + .navigationViewStyle(.stack) } } diff --git a/Tusker/Screens/Search/MastodonSearchController.swift b/Tusker/Screens/Search/MastodonSearchController.swift index 62efb009d..bc6276455 100644 --- a/Tusker/Screens/Search/MastodonSearchController.swift +++ b/Tusker/Screens/Search/MastodonSearchController.swift @@ -39,7 +39,9 @@ class MastodonSearchController: UISearchController { searchResultsUpdater = searchResultsController automaticallyShowsSearchResultsController = false showsSearchResultsController = true - scopeBarActivation = .onSearchActivation + if #available(iOS 16.0, *) { + scopeBarActivation = .onSearchActivation + } searchBar.autocapitalizationType = .none searchBar.delegate = self @@ -76,8 +78,12 @@ class MastodonSearchController: UISearchController { if searchText != defaultLanguage, let match = languageRegex.firstMatch(in: searchText, range: NSRange(location: 0, length: searchText.utf16.count)) { let identifier = (searchText as NSString).substring(with: match.range(at: 1)) - if Locale.LanguageCode.isoLanguageCodes.contains(where: { $0.identifier == identifier }) { - langSuggestions.append("language:\(identifier)") + if #available(iOS 16.0, *) { + if Locale.LanguageCode.isoLanguageCodes.contains(where: { $0.identifier == identifier }) { + langSuggestions.append("language:\(identifier)") + } + } else if searchText != "en" { + langSuggestions.append("language:\(searchText)") } } suggestions.append((.language, langSuggestions)) diff --git a/Tusker/Screens/Utilities/EnhancedNavigationViewController.swift b/Tusker/Screens/Utilities/EnhancedNavigationViewController.swift index a2af712e3..ee74cb135 100644 --- a/Tusker/Screens/Utilities/EnhancedNavigationViewController.swift +++ b/Tusker/Screens/Utilities/EnhancedNavigationViewController.swift @@ -22,7 +22,8 @@ class EnhancedNavigationViewController: UINavigationController { override var viewControllers: [UIViewController] { didSet { poppedViewControllers = [] - if useBrowserStyleNavigation { + if #available(iOS 16.0, *), + useBrowserStyleNavigation { // TODO: this for loop might not be necessary for vc in viewControllers { configureNavItem(vc.navigationItem) @@ -39,7 +40,8 @@ class EnhancedNavigationViewController: UINavigationController { self.interactivePushTransition = InteractivePushTransition(navigationController: self) #endif - if useBrowserStyleNavigation, + if #available(iOS 16.0, *), + useBrowserStyleNavigation, let topViewController { configureNavItem(topViewController.navigationItem) updateTopNavItemState() @@ -50,7 +52,9 @@ class EnhancedNavigationViewController: UINavigationController { let popped = performAfterAnimating(block: { super.popViewController(animated: animated) }, after: { - self.updateTopNavItemState() + if #available(iOS 16.0, *) { + self.updateTopNavItemState() + } }, animated: animated) if let popped { poppedViewControllers.insert(popped, at: 0) @@ -62,7 +66,9 @@ class EnhancedNavigationViewController: UINavigationController { let popped = performAfterAnimating(block: { super.popToRootViewController(animated: animated) }, after: { - self.updateTopNavItemState() + if #available(iOS 16.0, *) { + self.updateTopNavItemState() + } }, animated: animated) if let popped { poppedViewControllers = popped @@ -74,7 +80,9 @@ class EnhancedNavigationViewController: UINavigationController { let popped = performAfterAnimating(block: { super.popToViewController(viewController, animated: animated) }, after: { - self.updateTopNavItemState() + if #available(iOS 16.0, *) { + self.updateTopNavItemState() + } }, animated: animated) if let popped { poppedViewControllers.insert(contentsOf: popped, at: 0) @@ -89,11 +97,15 @@ class EnhancedNavigationViewController: UINavigationController { self.poppedViewControllers = [] } - configureNavItem(viewController.navigationItem) + if #available(iOS 16.0, *) { + configureNavItem(viewController.navigationItem) + } super.pushViewController(viewController, animated: animated) - updateTopNavItemState() + if #available(iOS 16.0, *) { + updateTopNavItemState() + } } func pushPoppedViewController() { @@ -123,7 +135,9 @@ class EnhancedNavigationViewController: UINavigationController { pushViewController(target, animated: true) }, after: { self.viewControllers.insert(contentsOf: toInsert, at: self.viewControllers.count - 1) - self.updateTopNavItemState() + if #available(iOS 16.0, *) { + self.updateTopNavItemState() + } }, animated: true) } diff --git a/Tusker/Screens/Utilities/Previewing.swift b/Tusker/Screens/Utilities/Previewing.swift index 5b9801cb1..32d1cadd1 100644 --- a/Tusker/Screens/Utilities/Previewing.swift +++ b/Tusker/Screens/Utilities/Previewing.swift @@ -204,7 +204,8 @@ extension MenuActionProvider { }), ] - if includeStatusButtonActions { + if #available(iOS 16.0, *), + includeStatusButtonActions { let favorited = status.favourited // TODO: move this color into an asset catalog or something var favImage = UIImage(systemName: favorited ? "star.fill" : "star")! @@ -367,11 +368,19 @@ extension MenuActionProvider { addOpenInNewWindow(actions: &shareSection, activity: UserActivityManager.showConversationActivity(mainStatusID: status.id, accountID: accountID)) - let toggleableAndActions = toggleableSection + actionsSection - return [ - UIMenu(options: .displayInline, preferredElementSize: toggleableAndActions.count == 1 ? .large : .medium, children: toggleableAndActions), - UIMenu(options: .displayInline, children: shareSection), - ] + if #available(iOS 16.0, *) { + let toggleableAndActions = toggleableSection + actionsSection + return [ + UIMenu(options: .displayInline, preferredElementSize: toggleableAndActions.count == 1 ? .large : .medium, children: toggleableAndActions), + UIMenu(options: .displayInline, children: shareSection), + ] + } else { + return [ + UIMenu(options: .displayInline, children: shareSection), + UIMenu(options: .displayInline, children: toggleableSection), + UIMenu(options: .displayInline, children: actionsSection), + ] + } } func actionsForTrendingLink(card: Card, source: PopoverSource) -> [UIMenuElement] { diff --git a/Tusker/Shortcuts/UserActivityHandlingContext.swift b/Tusker/Shortcuts/UserActivityHandlingContext.swift index bfb627b38..5903bb8ef 100644 --- a/Tusker/Shortcuts/UserActivityHandlingContext.swift +++ b/Tusker/Shortcuts/UserActivityHandlingContext.swift @@ -108,7 +108,8 @@ class StateRestorationUserActivityHandlingContext: UserActivityHandlingContext { } func compose(editing draft: Draft) { - if UIDevice.current.userInterfaceIdiom == .phone { + if #available(iOS 16.0, *), + UIDevice.current.userInterfaceIdiom == .phone { self.root.compose(editing: draft, animated: false, isDucked: true, completion: nil) } else { DispatchQueue.main.async { @@ -122,7 +123,8 @@ class StateRestorationUserActivityHandlingContext: UserActivityHandlingContext { precondition(state > .initial) navigation.run() #if !os(visionOS) - if let duckedDraft = UserActivityManager.getDuckedDraft(from: activity) { + if #available(iOS 16.0, *), + let duckedDraft = UserActivityManager.getDuckedDraft(from: activity) { self.root.compose(editing: duckedDraft, animated: false, isDucked: true, completion: nil) } #endif diff --git a/Tusker/TuskerNavigationDelegate.swift b/Tusker/TuskerNavigationDelegate.swift index 18d903e57..ca1e462d5 100644 --- a/Tusker/TuskerNavigationDelegate.swift +++ b/Tusker/TuskerNavigationDelegate.swift @@ -114,7 +114,8 @@ extension TuskerNavigationDelegate { #if os(visionOS) fatalError("unreachable") #else - if presentDuckable(compose, animated: animated, isDucked: isDucked, completion: completion) { + if #available(iOS 16.0, *), + presentDuckable(compose, animated: animated, isDucked: isDucked, completion: completion) { return } else { present(compose, animated: animated, completion: completion) diff --git a/Tusker/Views/AccountDisplayNameView.swift b/Tusker/Views/AccountDisplayNameView.swift index 2cda0c0f7..b089900ba 100644 --- a/Tusker/Views/AccountDisplayNameView.swift +++ b/Tusker/Views/AccountDisplayNameView.swift @@ -9,7 +9,6 @@ import SwiftUI import Pachyderm import WebURLFoundationExtras -import os private let emojiRegex = try! NSRegularExpression(pattern: ":(\\w+):", options: []) @@ -41,7 +40,7 @@ struct AccountDisplayNameView: View { guard !matches.isEmpty else { return } let emojiSize = self.emojiSize - let emojiImages = OSAllocatedUnfairLock(initialState: [String: Image]()) + let emojiImages = MultiThreadDictionary() let group = DispatchGroup() @@ -64,9 +63,7 @@ struct AccountDisplayNameView: View { image.draw(in: CGRect(origin: .zero, size: size)) } - emojiImages.withLock { - $0[emoji.shortcode] = Image(uiImage: resized) - } + emojiImages[emoji.shortcode] = Image(uiImage: resized) } if let request = request { emojiRequests.append(request) @@ -81,7 +78,7 @@ struct AccountDisplayNameView: View { // iterate backwards as to not alter the indices of earlier matches for match in matches.reversed() { let shortcode = (account.displayName as NSString).substring(with: match.range(at: 1)) - guard let image = emojiImages.withLock({ $0[shortcode] }) else { continue } + guard let image = emojiImages[shortcode] else { continue } let afterCurrentMatch = (account.displayName as NSString).substring(with: NSRange(location: match.range.upperBound, length: endIndex - match.range.upperBound)) diff --git a/Tusker/Views/Attachments/AttachmentView.swift b/Tusker/Views/Attachments/AttachmentView.swift index 0db1800dc..2c40283da 100644 --- a/Tusker/Views/Attachments/AttachmentView.swift +++ b/Tusker/Views/Attachments/AttachmentView.swift @@ -263,7 +263,17 @@ class AttachmentView: GIFImageView { let asset = AVURLAsset(url: attachment.url) let generator = AVAssetImageGenerator(asset: asset) generator.appliesPreferredTrackTransform = true - guard let image = try? await generator.image(at: .zero).image, + let image: CGImage? + #if os(visionOS) + image = try? await generator.image(at: .zero).image + #else + if #available(iOS 16.0, *) { + image = try? await generator.image(at: .zero).image + } else { + image = try? generator.copyCGImage(at: .zero, actualTime: nil) + } + #endif + guard let image, let prepared = await UIImage(cgImage: image).byPreparingForDisplay(), !Task.isCancelled else { return diff --git a/Tusker/Views/BaseEmojiLabel.swift b/Tusker/Views/BaseEmojiLabel.swift index 92b0d6e4b..e32aab9a4 100644 --- a/Tusker/Views/BaseEmojiLabel.swift +++ b/Tusker/Views/BaseEmojiLabel.swift @@ -9,7 +9,6 @@ import UIKit import Pachyderm import WebURLFoundationExtras -import os private let emojiRegex = try! NSRegularExpression(pattern: ":(\\w+):", options: []) @@ -57,7 +56,7 @@ extension BaseEmojiLabel { return imageSizeMatchingFontSize } - let emojiImages = OSAllocatedUnfairLock(initialState: [String: UIImage]()) + let emojiImages = MultiThreadDictionary() var foundEmojis = false let group = DispatchGroup() @@ -80,11 +79,9 @@ extension BaseEmojiLabel { // todo: consider caching these somewhere? the cache needs to take into account both the url and the font size, which may vary across labels, so can't just use ImageCache if let thumbnail = image.preparingThumbnail(of: emojiImageSize(image)), let cgImage = thumbnail.cgImage { - emojiImages.withLock { - // the thumbnail API takes a pixel size and returns an image with scale 1, but we want the actual screen scale, so convert - // see FB12187798 - $0[emoji.shortcode] = UIImage(cgImage: cgImage, scale: screenScale, orientation: .up) - } + // the thumbnail API takes a pixel size and returns an image with scale 1, but we want the actual screen scale, so convert + // see FB12187798 + emojiImages[emoji.shortcode] = UIImage(cgImage: cgImage, scale: screenScale, orientation: .up) } } else { // otherwise, perform the network request @@ -102,9 +99,7 @@ extension BaseEmojiLabel { group.leave() return } - emojiImages.withLock { - $0[emoji.shortcode] = transformedImage - } + emojiImages[emoji.shortcode] = transformedImage group.leave() } } diff --git a/Tusker/Views/ContentTextView.swift b/Tusker/Views/ContentTextView.swift index d2a2e5041..f6fc07fc3 100644 --- a/Tusker/Views/ContentTextView.swift +++ b/Tusker/Views/ContentTextView.swift @@ -146,7 +146,8 @@ class ContentTextView: LinkTextView, BaseEmojiLabel { func getLinkAtPoint(_ point: CGPoint) -> (URL, NSRange)? { let locationInTextContainer = CGPoint(x: point.x - textContainerInset.left, y: point.y - textContainerInset.top) - if let textLayoutManager { + if #available(iOS 16.0, *), + let textLayoutManager { guard let fragment = textLayoutManager.textLayoutFragment(for: locationInTextContainer) else { return nil } @@ -304,7 +305,8 @@ extension ContentTextView: UIContextMenuInteractionDelegate { // Determine the line rects that the link takes up in the coordinate space of this view. var rects = [CGRect]() - if let textLayoutManager, + if #available(iOS 16.0, *), + let textLayoutManager, let contentManager = textLayoutManager.textContentManager { // convert from NSRange to NSTextRange // i have no idea under what circumstances any of these calls could fail diff --git a/Tusker/Views/CopyableLabel.swift b/Tusker/Views/CopyableLable.swift similarity index 73% rename from Tusker/Views/CopyableLabel.swift rename to Tusker/Views/CopyableLable.swift index 4c1a2eb4b..23d070e15 100644 --- a/Tusker/Views/CopyableLabel.swift +++ b/Tusker/Views/CopyableLable.swift @@ -1,5 +1,5 @@ // -// CopyableLabel.swift +// CopyableLable.swift // Tusker // // Created by Shadowfacts on 2/4/23. @@ -8,7 +8,7 @@ import UIKit -class CopyableLabel: UILabel { +class CopyableLable: UILabel { private var _editMenuInteraction: Any! @available(iOS 16.0, *) @@ -28,10 +28,12 @@ class CopyableLabel: UILabel { } private func commonInit() { - editMenuInteraction = UIEditMenuInteraction(delegate: nil) - addInteraction(editMenuInteraction) - isUserInteractionEnabled = true - addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(longPressed))) + if #available(iOS 16.0, *) { + editMenuInteraction = UIEditMenuInteraction(delegate: nil) + addInteraction(editMenuInteraction) + isUserInteractionEnabled = true + addGestureRecognizer(UILongPressGestureRecognizer(target: self, action: #selector(longPressed))) + } } override func copy(_ sender: Any?) { diff --git a/Tusker/Views/Profile Header/ProfileHeaderView.xib b/Tusker/Views/Profile Header/ProfileHeaderView.xib index d0663427d..1a5f1cfbe 100644 --- a/Tusker/Views/Profile Header/ProfileHeaderView.xib +++ b/Tusker/Views/Profile Header/ProfileHeaderView.xib @@ -1,8 +1,9 @@ - + - + + @@ -124,16 +125,16 @@ - + -