Skip to content

Commit

Permalink
Update to Swift6
Browse files Browse the repository at this point in the history
  • Loading branch information
danielsaidi committed Oct 6, 2024
1 parent 95d154c commit e177ea9
Show file tree
Hide file tree
Showing 55 changed files with 275 additions and 2,037 deletions.
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.9
// swift-tools-version:6.0

import PackageDescription

Expand Down
21 changes: 21 additions & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ SwiftUIKit makes its best effort to honor semver, but breaking changes can occur



## 5.0

This verison makes SwiftUIKit use Swift 6.

Due to the strict concurrency enforcement, this involves some breaking changes.

If some of the changes made are too strict for you, feel free to open a GitHub issue to discuss it.

### 🚨 Breaking Changes

* All previously deprecated code has been removed.
* All static style and configurations are now computed properties.
* Some classes apply `@MainActor` to properly handle mutable state.

* `ImageCache.shared` has been removed.
* `ImageService.default` has been removed.
* `KeychainWrapper.shared` has been removed.
* `URLOpener` has been removed, since there now are native alternatives.
* `View+Geometry` has been removed.


## 4.4

This version adds a new storage value type and moves the gesture buttons to `https://github.com/danielsaidi/GestureButton`.
Expand Down
7 changes: 4 additions & 3 deletions Sources/SwiftUIKit/Buttons/Button+Standard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,14 @@ public extension Button.StandardType {
}
}
}

/*
#Preview {
@ViewBuilder
func buttons() -> some View {
Section {
ForEach(Button.StandardType.allCases) {
Button($0) {}
ForEach(Button.StandardType.allCases) { type in
Button(type) { print("Tapped \(type.title)") }
}
}
}
Expand All @@ -124,3 +124,4 @@ public extension Button.StandardType {
}
}
}
*/
18 changes: 8 additions & 10 deletions Sources/SwiftUIKit/Errors/ErrorAlerter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,19 @@ public extension ErrorAlerter {
typealias BlockOperation<ErrorType: Error> = (BlockCompletion<ErrorType>) -> Void

/// Alert the provided error asynchronously.
func alertAsync(
@MainActor func alertAsync(
error: Error,
okButtonText: String = "OK"
) {
DispatchQueue.main.async {
alert(
error: error,
okButtonText: okButtonText
)
}
alert(
error: error,
okButtonText: okButtonText
)
}

/// Try to perform a block-based operation, and alert if
/// this operation fails in any way.
func tryWithErrorAlert<ErrorType: Error>(
@MainActor func tryWithErrorAlert<ErrorType: Error>(
_ operation: @escaping BlockOperation<ErrorType>,
completion: @escaping BlockCompletion<ErrorType>
) {
Expand All @@ -98,12 +96,12 @@ public extension ErrorAlerter {
///
/// This function wraps an async operation in a task and
/// alerts any errors that are thrown.
func tryWithErrorAlert(_ operation: @escaping AsyncOperation) {
@MainActor func tryWithErrorAlert(_ operation: @escaping AsyncOperation) {
Task {
do {
try await operation()
} catch {
await alert(error: error)
alert(error: error)
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftUIKit/Extensions/DispatchQueue+Async.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ public extension DispatchQueue {
/// Perform an operation after a certain time interval.
func asyncAfter(
_ interval: DispatchTimeInterval,
execute: @escaping () -> Void
execute: @escaping @Sendable () -> Void
) {
asyncAfter(
deadline: .now() + interval,
execute: execute)
asyncAfter(deadline: .now() + interval) {
execute()
}
}
}
70 changes: 0 additions & 70 deletions Sources/SwiftUIKit/Extensions/View+Geometry.swift

This file was deleted.

10 changes: 5 additions & 5 deletions Sources/SwiftUIKit/Files/DirectoryObservable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import Foundation
///
/// The view uses an internal ``DirectoryMonitor`` instance,
/// to keep the ``files`` property in sync.
@MainActor
public class DirectoryObservable: ObservableObject {

/// Create an instance that observes the provided `url`.
Expand All @@ -40,13 +41,12 @@ public class DirectoryObservable: ObservableObject {

private lazy var folderMonitor = DirectoryMonitor(
url: url,
onChange: handleChanges
onChange: {
self.handleChanges()
}
)
}

private extension DirectoryObservable {

func handleChanges() {
private func handleChanges() {
let files = try? fileManager.contentsOfDirectory(
at: url,
includingPropertiesForKeys: nil,
Expand Down
3 changes: 0 additions & 3 deletions Sources/SwiftUIKit/Images/ImageCache.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@ public class ImageCache {
/// The cache dictionary that is used to store images.
public var cache = [String: ImageRepresentable]()

/// A shared cache instance.
public static let shared = ImageCache()

/// Store an image into the cache.
public func cache(image: ImageRepresentable, for key: String) {
cache[key] = image
Expand Down
4 changes: 1 addition & 3 deletions Sources/SwiftUIKit/Images/UIImage+Photos.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,14 @@ public extension UIImage {
/// Failing to add these permissions before calling this
/// function will crash the app.
func saveToPhotos(completion: @escaping (Error?) -> Void) {
ImageService.default.saveImageToPhotos(self, completion: completion)
ImageService().saveImageToPhotos(self, completion: completion)
}
}

private class ImageService: NSObject {

public typealias Completion = (Error?) -> Void

public static private(set) var `default` = ImageService()

private var completions = [Completion]()

public func saveImageToPhotos(_ image: UIImage, completion: @escaping (Error?) -> Void) {
Expand Down
25 changes: 13 additions & 12 deletions Sources/SwiftUIKit/Keychain/KeychainItemAccessibility.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
// Created by James Blair on 4/24/16.
// Copyright © 2016 Jason Rendel. All rights reserved.

import Foundation
@preconcurrency import Foundation

protocol KeychainAttrRepresentable {

Expand Down Expand Up @@ -53,22 +53,23 @@ public enum KeychainItemAccessibility {
case whenUnlockedThisDeviceOnly

static func accessibilityForAttributeValue(_ keychainAttrValue: CFString) -> KeychainItemAccessibility? {
keychainItemAccessibilityLookup.first { $0.value == keychainAttrValue }?.key
lookupTable.first { $0.value == keychainAttrValue }?.key
}
}


private let keychainItemAccessibilityLookup: [KeychainItemAccessibility: CFString] = [
.afterFirstUnlock: kSecAttrAccessibleAfterFirstUnlock,
.afterFirstUnlockThisDeviceOnly: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
.whenPasscodeSetThisDeviceOnly: kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
.whenUnlocked: kSecAttrAccessibleWhenUnlocked,
.whenUnlockedThisDeviceOnly: kSecAttrAccessibleWhenUnlockedThisDeviceOnly
]
static var lookupTable: [KeychainItemAccessibility: CFString] {
[
.afterFirstUnlock: kSecAttrAccessibleAfterFirstUnlock,
.afterFirstUnlockThisDeviceOnly: kSecAttrAccessibleAfterFirstUnlockThisDeviceOnly,
.whenPasscodeSetThisDeviceOnly: kSecAttrAccessibleWhenPasscodeSetThisDeviceOnly,
.whenUnlocked: kSecAttrAccessibleWhenUnlocked,
.whenUnlockedThisDeviceOnly: kSecAttrAccessibleWhenUnlockedThisDeviceOnly
]
}
}

extension KeychainItemAccessibility: KeychainAttrRepresentable {

public var keychainAttrValue: CFString {
keychainItemAccessibilityLookup[self]!
Self.lookupTable[self]!
}
}
2 changes: 1 addition & 1 deletion Sources/SwiftUIKit/Keychain/KeychainService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import Foundation
open class KeychainService {

public init(
wrapper: KeychainWrapper = .standard
wrapper: KeychainWrapper = .init()
) {
self.wrapper = wrapper
}
Expand Down
5 changes: 1 addition & 4 deletions Sources/SwiftUIKit/Keychain/KeychainWrapper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ open class KeychainWrapper {
// MARK: - Initialization

/// Create a standard instance of this class.
private convenience init() {
public convenience init() {
let id = Bundle.main.bundleIdentifier
let fallback = "com.swiftkit.keychain"
self.init(serviceName: id ?? fallback)
Expand Down Expand Up @@ -77,9 +77,6 @@ open class KeychainWrapper {

// MARK: - Properties

/// A standard keychain wrapper instance.
public static let standard = KeychainWrapper()

/// The service name to use for this instance.
private let serviceName: String

Expand Down
1 change: 1 addition & 0 deletions Sources/SwiftUIKit/Lists/ListButtonGroup.swift
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ private extension ButtonStyle where Self == ListButtonGroupStyle {
}
}

@MainActor
private extension String {

func previewButton(_ icon: Image) -> some View {
Expand Down
12 changes: 6 additions & 6 deletions Sources/SwiftUIKit/Lists/ListButtonGroupStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -177,10 +177,8 @@ public extension ListButtonGroupStyle {

public extension ListButtonGroupStyle.LabelStyle {

/// A standard label style.
///
/// You can set this style to affect the global default.
static var standard = Self()
/// The standard list button group label style.
static var standard: Self { .init() }
}

public extension LabelStyle where Self == ListButtonGroupStyle.LabelStyle {
Expand Down Expand Up @@ -214,9 +212,11 @@ public extension View {

private extension ListButtonGroupStyle {

struct Key: EnvironmentKey {
@MainActor struct Key: @preconcurrency EnvironmentKey {

public static var defaultValue: ListButtonGroupStyle = .standard
public static var defaultValue: ListButtonGroupStyle {
.init()
}
}
}

Expand Down
18 changes: 9 additions & 9 deletions Sources/SwiftUIKit/Lists/ListCard.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,15 @@ public struct ListCard<Content: View, ContextMenuView: View>: View {

public extension ViewShadowStyle {

/**
The standard list card shadow style.
*/
static var listCard = ViewShadowStyle(
color: .black.opacity(0.2),
radius: 2,
x: 0,
y: 2
)
/// The standard list card shadow style.
static var listCard: Self {
.init(
color: .black.opacity(0.2),
radius: 2,
x: 0,
y: 2
)
}
}

#Preview {
Expand Down
8 changes: 4 additions & 4 deletions Sources/SwiftUIKit/Lists/ListCardStyle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,7 @@ public struct ListCardStyle {
public extension ListCardStyle {

/// The standard list card style.
///
/// You can set this style to affect the global default.
static var standard = ListCardStyle()
static var standard: Self { .init() }
}

public extension View {
Expand All @@ -53,7 +51,9 @@ private extension ListCardStyle {

struct Key: EnvironmentKey {

public static var defaultValue: ListCardStyle = .standard
public static var defaultValue: ListCardStyle {
.standard
}
}
}

Expand Down
Loading

0 comments on commit e177ea9

Please sign in to comment.