Skip to content

overview architecture

Jason Holt edited this page Oct 27, 2026 · 1 revision

Architecture

The plugin has a Dart facade, a token-verified platform interface, a Flutter channel adapter, and two native implementations. Swift document APIs coordinate local access while Apple manages the iCloud Drive lifecycle (lib/icloud_storage.dart, lib/icloud_storage_platform_interface.dart, AGENTS.md).

Components

graph TD
    App["Consuming Flutter app"] -->|validated API calls| Facade["ICloudStorage"]
    Facade -->|platform delegation| Interface["ICloudStoragePlatform"]
    Interface -->|default implementation| Channel["MethodChannelICloudStorage"]
    Channel -->|method and event channels| IOS["iOS plugin"]
    Channel -->|method and event channels| Mac["macOS plugin"]
    IOS -->|local coordinated operations| Foundation["Foundation helpers"]
    Mac -->|local coordinated operations| Foundation
    Foundation -->|filesystem access| Container["Local ubiquity container"]
    Container -->|Apple-managed propagation| Drive["iCloud Drive"]
Loading
Layer Source Responsibility
Public API lib/icloud_storage.dart Static methods, path/name validation, exports
Platform interface lib/icloud_storage_platform_interface.dart Overridable typed methods, implementation-token verification
Channel adapter lib/icloud_storage_method_channel.dart Method calls, dynamic event channels, strict result decoding
iOS registration ios/icloud_storage_plus/Sources/icloud_storage_plus/iOSICloudStoragePlugin.swift Flutter dispatch, native operation routing, stream registration
macOS registration macos/icloud_storage_plus/Sources/icloud_storage_plus/macOSICloudStoragePlugin.swift macOS routing and presenter integration
Documents ios/icloud_storage_plus/Sources/icloud_storage_plus/ICloudDocument.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus/ICloudDocument.swift Path-only transfers, in-memory document content, observation
Foundation helpers ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/CoordinatedIO.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus_foundation/CoordinatedIO.swift Async bridges around synchronous file coordination

Dart and Swift contain the main implementation: 3,142 Dart physical lines and 10,481 Swift physical lines at the recorded revision, excluding tests/manifests and counting mirrored platform sources separately. Ruby podspecs and Swift package manifests define native distribution; YAML configures Flutter and automation. By the numbers gives the full measured language breakdown and counting rules.

Local operation flow

The facade validates container-relative paths before delegating. Native container resolution uses a single off-main-thread lookup in ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/UbiquityContainerResolver.swift and its macOS counterpart. Write preflight creates parent directories; the document or file helper then performs the local operation.

Mutation lanes serialize operations that target the same standardized path. Cross-path mutations acquire both lanes in sorted order. Reads do not acquire a mutation lane. Blocking coordination is dispatched outside the Swift cooperative pool (ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/PerPathMutationLane.swift, ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/CoordinatedIO.swift).

In-place text and byte reads use plain NSFileCoordinator access rather than opening document objects. Batch byte reads use one reading intent per file in a coordinated batch; missing files are omitted from the returned map, while other failures fail the call (lib/icloud_storage.dart, ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/CoordinatedIO.swift).

Container moves use FileManager.setUbiquitous on a dispatch worker, outside any coordinator accessor. Moving in creates missing parent directories with a separate coordinated write. Moving out removes the item from iCloud; copy-out retains it (ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/UbiquitousItemMover.swift, lib/icloud_storage.dart).

Existing-file content replacement stages complete contents and installs them through coordinated FileManager.replaceItemAt. It rejects existing directory targets and does not resolve conflict versions (ios/icloud_storage_plus/Sources/icloud_storage_plus_foundation/CoordinatedReplaceWriter.swift).

Streams and failures

The fixed method channel is icloud_storage_plus. Gather updates, document events, and transfer progress use independently named event channels. Callbacks must attach listeners synchronously before native allocation (lib/icloud_storage_method_channel.dart, lib/src/synchronous_stream_bridge.dart).

Native failures become typed operation exceptions through lib/src/platform_exception_decoder.dart. Malformed channel results become plugin-contract errors. Download state is metadata, not a manufactured operation failure.

Platform differences

iOS requests a Flutter background task queue when available. Transfers, writes, and observation use UIDocument on iOS and NSDocument on macOS; in-place reads use Foundation coordination directly. Both plugin entry points dispatch filesystem existence, item metadata, and conflict-version work off the main thread and return results on the main queue. macOS watched-file replacement also records modification dates to suppress matching passive callbacks. The two source trees are related implementations, not one shared compiled source directory (ios/icloud_storage_plus/Sources/icloud_storage_plus/iOSICloudStoragePlugin.swift, macos/icloud_storage_plus/Sources/icloud_storage_plus/macOSICloudStoragePlugin.swift).

See Dart boundary, Native files, and Document observation.

Clone this wiki locally