Creating a collaborative photo gallery with SharePlay
At a glance
| Item | Summary |
|---|---|
| Purpose | Build a shared photo gallery by using SharePlay to synchronize images among participants. |
| App architecture | A Swift sample with the source-visible chain GroupActivities_PhotoLibraryApp → ContentView → AppModel → GroupActivities APIs. |
| Main patterns | View-controller organization, Binding-based state propagation |
| Project style | 11 scanned source file(s) across Swift, organized around ranked entry, type, and file boundaries. |
| Execution model | Source-visible boundaries: @MainActor, Task, await suspension point; none alone proves a background thread. |
| State/event model | Source-visible mechanisms: SwiftUI state property wrapper, @Observable. |
| Key frameworks/packages | SwiftUI, OSLog, GroupActivities, PhotosUI, Combine; these are source dependencies, not architecture labels. |
Project structure
Source bundle/
├── GroupActivities-PhotoLibrary/
│ ├── GroupActivities_PhotoLibraryApp.swift
│ ├── Models/
│ │ ├── AppModel.swift
│ │ ├── SessionController.swift
│ │ ├── DisplayImage.swift
│ │ ├── PhotoShareActivity.swift
│ │ └── PhotosPickerItemExtension.swift
│ └── Views/
│ ├── ContentView.swift
│ ├── ImageCellView.swift
│ ├── ImageView.swift
│ ├── PhotoGalleryView.swift
│ └── SharePlayButton.swift
└── Configuration/
└── SampleCode.xcconfig
Structure observations
- Architecturally prominent files are ranked from entry points and role-named declarations; resource-only paths are omitted.
- Primary languages: Swift.
- The verified tree contains 5 project/configuration file(s) and 10 source declaration(s).
Overall architecture
flowchart LR
N1["GroupActivities_PhotoLibraryApp"]
N2["ContentView"]
N3["AppModel"]
N4["GroupActivities APIs"]
N1 --> N2
N2 --> N3
N3 --> N4
Reference code
GroupActivities-PhotoLibrary/GroupActivities_PhotoLibraryApp.swift:10 — architecture anchor
@main
struct PhotoLibraryApp: App {
@State private var appModel = AppModel()
var body: some Scene {
WindowGroup {
VStack {
ContentView()
.environment(appModel)
// Add a hidden `ShareLink` to start the activity from the window bar.
ShareLink(item: PhotoShareActivity(), preview: SharePreview("Share Images"))
.hidden()
}
}
.windowResizability(.contentMinSize)
}
}Interpretation
The arrows summarize the source-visible entry, role-named types or folders, and framework direction; when nodes come from structural folders, the sequence is a high-level interpretation rather than proof that every adjacent node calls the next. Ownership is claimed only where the next section cites a stored property or assignment. The diagram is intentionally limited to the dominant path into Group Activities.
Ownership and state
classDiagram
PhotoLibraryApp *-- AppModel : appModel
AppModel *-- Logger : logger
AppModel *-- String : name
AppModel o-- SessionController : sessionController
Ownership evidence
GroupActivities-PhotoLibrary/GroupActivities_PhotoLibraryApp.swift:12 — stored dependency or nearest verified ownership anchor
@main
struct PhotoLibraryApp: App {
@State private var appModel = AppModel()
// ...
}| Owner | Object or state | Relationship | Mutation authority |
|---|---|---|---|
PhotoLibraryApp |
AppModel (appModel) |
owns wrapper-managed state | Owning lexical scope |
AppModel |
Logger (logger) |
creates and retains | Initialized by the owner; the binding is immutable |
AppModel |
String (name) |
owns value state | App/module collaborators |
AppModel |
SessionController (sessionController) |
stores or receives | Owning lexical scope |
Composition arrows indicate a source-visible construction expression or locally owned value state; aggregation means the owner stores or receives a dependency without proving exclusive lifetime ownership.
Concurrency, scheduling, and thread safety
Evidence limit: actor isolation, async/await, or Task creation does not by itself prove background-thread execution; Sendable conformance alone does not prove thread-safe mutation.
| Concern | Source mechanism | Verified placement or handoff | Evidence |
|---|---|---|---|
| Main isolation | @MainActor |
The cited annotation marks its attached declaration or closure as main-actor isolated. | GroupActivities-PhotoLibrary/Models/AppModel.swift:14 |
| Task creation | Task |
The source creates an unstructured task; surrounding context determines inherited actor isolation. | GroupActivities-PhotoLibrary/Models/AppModel.swift:68 |
| Suspension boundary | await suspension point |
The source declares or crosses an asynchronous boundary; it does not by itself establish background execution. | GroupActivities-PhotoLibrary/Models/AppModel.swift:69 |
@MainActor/MainActor.run, DispatchQueue.main, and RunLoop.main are reported as distinct isolation, queue, and event-loop mechanisms. A plain Task is kept separate from Task.detached; neither is labeled as a background thread.
Reference code
GroupActivities-PhotoLibrary/Models/AppModel.swift:14 — representative execution boundary
@MainActor @Observable final class AppModel {
let logger = Logger(subsystem: "com.example.apple-samplecode.GroupActivities-PhotoLibrary", category: "AppModel")
// ...
}State propagation, frameworks, and dependencies
Evidence limit: an import proves a source-level compilation dependency at the cited line; it does not prove runtime use, architectural adoption, or whether a Swift package is a direct application dependency.
| Category | Mechanism or module | Verified role | Evidence |
|---|---|---|---|
| State propagation | SwiftUI state property wrapper |
A SwiftUI property wrapper supplies or observes UI state. | GroupActivities-PhotoLibrary/GroupActivities_PhotoLibraryApp.swift:12 |
| State propagation | @Observable |
Observation macro publishes source-visible changes. | GroupActivities-PhotoLibrary/Models/AppModel.swift:14 |
| Source import | SwiftUI |
The cited file imports this module; runtime use and architectural role are not inferred. | GroupActivities-PhotoLibrary/GroupActivities_PhotoLibraryApp.swift:8 |
| Source import | OSLog |
The cited file imports this module; runtime use and architectural role are not inferred. | GroupActivities-PhotoLibrary/Models/AppModel.swift:10 |
| Source import | GroupActivities |
The cited file imports this module; runtime use and architectural role are not inferred. | GroupActivities-PhotoLibrary/Models/AppModel.swift:9 |
| Source import | PhotosUI |
The cited file imports this module; runtime use and architectural role are not inferred. | GroupActivities-PhotoLibrary/Models/AppModel.swift:11 |
| Source import | Combine |
The cited file imports this module; runtime use and architectural role are not inferred. | GroupActivities-PhotoLibrary/Models/AppModel.swift:8 |
| Source import | QuickLook |
The cited file imports this module; runtime use and architectural role are not inferred. | GroupActivities-PhotoLibrary/Views/ImageCellView.swift:9 |
receive(on:) describes downstream delivery scheduling, while subscribe(on:) describes upstream subscription/request/cancel scheduling. An import Combine alone establishes neither behavior nor a Store, reducer, Redux, or other application architecture.
Class and protocol design
GroupActivities-PhotoLibrary/GroupActivities_PhotoLibraryApp.swift:11 — representative type boundary
@main
struct PhotoLibraryApp: App {
@State private var appModel = AppModel()
// ...
}| Type | Responsibility | Depends on or conforms to |
|---|---|---|
PhotoLibraryApp |
Application entry and top-level composition | App |
AppModel |
Feature data or observable state | Concrete collaborators/imported frameworks |
SessionController |
View lifecycle, callbacks, and feature coordination | Concrete collaborators/imported frameworks |
ContentView |
User-interface presentation and input forwarding | View |
ImageCellView |
User-interface presentation and input forwarding | View |
ImageView |
User-interface presentation and input forwarding | View |
PhotoGalleryView |
User-interface presentation and input forwarding | View |
DisplayImage |
Represents a feature value or composable behavior | Identifiable, Codable, Transferable, Equatable |
PhotoShareActivity |
Represents a feature value or composable behavior | GroupActivity, Transferable |
SharePlayButton |
Represents a feature value or composable behavior | View |
No local protocol conformance is claimed as protocol-oriented design; external framework conformances are listed only as dependencies.
Access control
| Symbol | Access | Verified effect | Likely rationale |
|---|---|---|---|
appModel (GroupActivities-PhotoLibrary/GroupActivities_PhotoLibraryApp.swift:12) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
sessionController (GroupActivities-PhotoLibrary/Models/AppModel.swift:29) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
sharedImages (GroupActivities-PhotoLibrary/Models/AppModel.swift:32) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
photoPickerSelectionObservationTask (GroupActivities-PhotoLibrary/Models/AppModel.swift:52) |
private |
Use is restricted to the lexical declaration and same-file extensions allowed by Swift. | Inference: keep state mutation or dependency lifetime inside the owning implementation. |
Reference code
GroupActivities-PhotoLibrary/GroupActivities_PhotoLibraryApp.swift:12 — representative boundary
@main
struct PhotoLibraryApp: App {
@State private var appModel = AppModel()
// ...
}Swift declarations without a modifier are internal; explicit private, fileprivate, private(set), public, or open entries above are interpreted by language semantics. Objective-C/C samples instead rely on header and implementation boundaries, which are not equivalent to Swift lexical privacy.
Logic ownership and placement
| Logic | Owning type or file | Placement rationale |
|---|---|---|
| Application entry and top-level composition | PhotoLibraryApp |
The source’s App suffix makes this role explicit. |
| View lifecycle, callbacks, and feature coordination | SessionController |
The source’s Controller suffix makes this role explicit. |
| Feature data or observable state | AppModel |
The source’s Model suffix makes this role explicit. |
| User-interface presentation and input forwarding | ContentView, ImageCellView, ImageView, PhotoGalleryView |
The source’s View suffix makes this role explicit. |
Design patterns
| Pattern | Source evidence | Purpose or tradeoff |
|---|---|---|
| View-controller organization | GroupActivities-PhotoLibrary/Models/SessionController.swift:17 |
A controller is the verified coordination boundary; this is MVC-style only where a separate model is present. |
| Binding-based state propagation | GroupActivities-PhotoLibrary/Views/ImageView.swift:20 |
A binding exposes controlled read/write access while the upstream owner remains authoritative. |
Main application flow
sequenceDiagram
participant AppModel
participant PhotoShareActivity
participant SessionController
PhotoShareActivity-->>AppModel: session() stream
AppModel->>SessionController: SessionController()
AppModel-->>AppModel: state() stream
AppModel->>AppModel: shareExistingImages()
Reference code
GroupActivities-PhotoLibrary/Models/AppModel.swift:140 — observeGroupSessions()
private func observeGroupSessions() async {
// When a new `GroupSession` of `PhotoShareActivity` is available, set up a `SessionController`
// to manage the session and join it.
for await session in PhotoShareActivity.sessions() {
let sessionController = await SessionController(session, appModel: self)
self.sessionController = sessionController
// Create a task on the same actor to observe the group session state and clear the
// session controller when the group session invalidates.
Task.immediate(name: "session state observation") {
for await state in session.$state.values {
guard self.sessionController?.session.id == session.id else {
return
}
if case .invalidated = state {
self.sessionController = nil
return
}
}
}
// Share existing finished images when the session starts.
await shareExistingImages(to: sessionController)
}
}Naming conventions
- Types: App: PhotoLibraryApp; Controller: SessionController; Model: AppModel; View: ContentView, ImageCellView, ImageView, PhotoGalleryView.
- Protocols: no local protocol declaration in the scanned source.
- Methods:
endSession,removeItem,shareExistingImages,observeGroupSessions,updateImagesFromSelection,addImagesFromPhotoPicker,removeImagesFromPhotoPicker,loadImage. - Files:
GroupActivities-PhotoLibrary/Models/AppModel.swift,GroupActivities-PhotoLibrary/Models/SessionController.swift,GroupActivities-PhotoLibrary/Views/ContentView.swift,GroupActivities-PhotoLibrary/Views/ImageCellView.swift,GroupActivities-PhotoLibrary/Views/ImageView.swift,GroupActivities-PhotoLibrary/Views/PhotoGalleryView.swift.
Architecture takeaways
GroupActivities_PhotoLibraryAppis the main source-visible entry or composition anchor for this sample.- Framework work reaches SwiftUI, GroupActivities, PhotosUI, QuickLook through a deliberately small high-level chain; the detailed API graph remains inside the cited implementation files.
- Stored-property evidence identifies lifecycle collaboration; it does not by itself prove exclusive object ownership.
- Access-control conclusions separate verified language visibility from the likely design rationale.
- The source does not justify labeling the design protocol-oriented.
Source map
| Source file | Relevant symbols |
|---|---|
GroupActivities-PhotoLibrary/GroupActivities_PhotoLibraryApp.swift |
Cited implementation, PhotoLibraryApp, SwiftUI state property wrapper, SwiftUI |
GroupActivities-PhotoLibrary/Models/AppModel.swift |
Cited implementation, @MainActor, Task, await suspension point, @Observable, OSLog, GroupActivities, PhotosUI, Combine, AppModel |
GroupActivities-PhotoLibrary/Models/SessionController.swift |
SessionController |
GroupActivities-PhotoLibrary/Views/ImageView.swift |
Cited implementation, ImageView |
GroupActivities-PhotoLibrary/Views/ImageCellView.swift |
QuickLook, ImageCellView |
GroupActivities-PhotoLibrary/Views/ContentView.swift |
ContentView |
GroupActivities-PhotoLibrary/Views/PhotoGalleryView.swift |
PhotoGalleryView |
GroupActivities-PhotoLibrary/Models/DisplayImage.swift |
DisplayImage |
GroupActivities-PhotoLibrary/Models/PhotoShareActivity.swift |
PhotoShareActivity |
GroupActivities-PhotoLibrary/Models/PhotosPickerItemExtension.swift |
Feature implementation |
GroupActivities-PhotoLibrary/Views/SharePlayButton.swift |
SharePlayButton |