Skip to content

Commit

Permalink
remove Observation support
Browse files Browse the repository at this point in the history
  • Loading branch information
fatbobman committed Oct 28, 2024
1 parent b1bf655 commit 8f32a45
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 84 deletions.
16 changes: 0 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ A Swift library that monitors the iCloud account status and responds to synchron
- **Account Status Monitoring**: Check if the iCloud account is available and handle unavailable states.
- **Synchronization Event Handling**: Monitor importing, exporting, setup, and idle states during data synchronization.
- **Error Handling**: Handle specific CloudKit errors, such as `quotaExceeded`.
- **Compatibility**: Uses the Observation framework on iOS 17 and above; compatible with `ObservableObject` on older versions.
- **Logging Support**: Optional logging of synchronization events for debugging purposes.

## Requirements
Expand Down Expand Up @@ -44,20 +43,10 @@ import SyncStatusManager

### Initialize SyncStatusManager

You can initialize `SyncStatusManager` using either `@StateObject` or `@State`, depending on your needs. Even on iOS 17 and above, using `@StateObject` does not affect the Observation features.

#### Using @StateObject

```swift
@StateObject var syncManager = SyncStatusManager()
```

#### Using @State (iOS 17 and above)

```swift
@State var syncManager = SyncStatusManager()
```

### Observing Sync Events

You can observe `syncEvent` to monitor the synchronization status.
Expand Down Expand Up @@ -133,11 +122,6 @@ let syncManager = SyncStatusManager(
)
```

## Notes

- **Observation Framework**: Even though `SyncStatusManager` uses the Observation framework on iOS 17 and above, you can safely use `@StateObject` without affecting its functionality.
- **Backward Compatibility**: The library is designed to work seamlessly across different iOS versions, abstracting away the differences between the Observation framework and `ObservableObject`.

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
Expand Down
32 changes: 6 additions & 26 deletions Sources/iCloudSyncStatusKit/SyncStatusKit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,38 +14,18 @@ import Combine
import CoreData
import Foundation
import SimpleLogger
#if canImport(Observation)
import Observation
#endif

/// Synchronization status manager
#if canImport(Observation)
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, visionOS 1.0, *)
@Observable
#endif
@MainActor
public final class SyncStatusManager: SyncStatusManagerProtocol {
#if canImport(Observation)
/// Synchronization event state
public var syncEvent: SyncEvent = .idle
#else
@Published public var syncEvent: SyncEvent = .idle
#endif
/// Synchronization event state
@Published public var syncEvent: SyncEvent = .idle

#if canImport(Observation)
@ObservationIgnored
var cancellables: Set<AnyCancellable> = []
#else
var cancellables: Set<AnyCancellable> = []
#endif
var cancellables: Set<AnyCancellable> = []

/// Whether the quota has been exceeded, only alert once during the instance lifecycle
var hasRunQuotaExceeded = false

#if canImport(Observation)
@ObservationIgnored
/// Whether the quota has been exceeded, only alert once during the instance lifecycle
var hasRunQuotaExceeded = false
#else
var hasRunQuotaExceeded = false
#endif
/// Quota exceeded callback method
let quotaExceededHandler: (@Sendable () async -> Void)?
/// Logger manager
Expand Down
42 changes: 12 additions & 30 deletions Sources/iCloudSyncStatusKit/SyncStatusProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,15 @@ import CloudKit
import Combine
import Foundation

#if canImport(Observation)
import Observation
/// Sync Status Manager Protocol
@available(iOS 17.0, macOS 14.0, tvOS 17.0, watchOS 10.0, visionOS 1.0, *)
@MainActor
public protocol SyncStatusManagerProtocol: Observable, ObservableObject, Sendable {
/// Sync status, primarily observing importing and exporting
var syncEvent: SyncEvent { get }
/// Checks the iCloud account status
/// If the status is not obtained, returns nil
/// Regardless of the status obtained, as long as it is not available, onUnavailable will be called
@discardableResult
func validateICloudAvailability(
onUnavailable: @Sendable (AccountStatus, Error?) async -> Void
) async -> AccountStatus?
}
#else
@MainActor
public protocol SyncStatusManagerProtocol: ObservableObject, Sendable {
/// Sync status, primarily observing importing and exporting
var syncEvent: SyncEvent { get }
/// Checks the iCloud account status
/// If the status is not obtained, returns nil
/// Regardless of the status obtained, as long as it is not available, onUnavailable will be called
@discardableResult
func validateICloudAvailability(
onUnavailable: @Sendable (AccountStatus, Error?) async -> Void
) async -> AccountStatus?
}
#endif
@MainActor
public protocol SyncStatusManagerProtocol: ObservableObject, Sendable {
/// Sync status, primarily observing importing and exporting
var syncEvent: SyncEvent { get }
/// Checks the iCloud account status
/// If the status is not obtained, returns nil
/// Regardless of the status obtained, as long as it is not available, onUnavailable will be called
@discardableResult
func validateICloudAvailability(
onUnavailable: @Sendable (AccountStatus, Error?) async -> Void
) async -> AccountStatus?
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ A Swift library that monitors the iCloud account status and responds to synchron
- **Account Status Monitoring**: Check if the iCloud account is available and handle unavailable states.
- **Synchronization Event Handling**: Monitor importing, exporting, setup, and idle states during data synchronization.
- **Error Handling**: Handle specific CloudKit errors, such as `quotaExceeded`.
- **Compatibility**: Uses the Observation framework on iOS 17 and above; compatible with `ObservableObject` on older versions.
- **Logging Support**: Optional logging of synchronization events for debugging purposes.

## Usage
Expand All @@ -20,20 +19,10 @@ import SyncStatusManager

### Initialize SyncStatusManager

You can initialize `SyncStatusManager` using either `@StateObject` or `@State`, depending on your needs. Even on iOS 17 and above, using `@StateObject` does not affect the Observation features.

#### Using @StateObject

```swift
@StateObject var syncManager = SyncStatusManager()
```

#### Using @State (iOS 17 and above)

```swift
@State var syncManager = SyncStatusManager()
```

### Observing Sync Events

You can observe `syncEvent` to monitor the synchronization status.
Expand Down Expand Up @@ -111,3 +100,4 @@ let syncManager = SyncStatusManager(




Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Testing
@testable import iCloudSyncStatusKit

@MainActor
@Test func example() async throws {
// Write your test here and use APIs like `#expect(...)` to check expected conditions.
// let statusKit = SyncStatusManager()
}

0 comments on commit 8f32a45

Please sign in to comment.