Skip to main content

The iOS app

The app is native SwiftUI for iOS 17 and later, in ios/. It signs in with Amazon Cognito (through the Amplify library) and talks to the API through a Swift client generated from the contract.

This page is written for someone who knows programming but not SwiftUI. It starts with the few SwiftUI ideas you need, then follows the app from launch to a screen on the phone.

SwiftUI in five ideas​

  • A View is a struct that describes UI. Its body returns other views. SwiftUI calls body again whenever data the view reads changes, and redraws only what changed. You never "update the screen" yourself.
  • @State is a value a view owns, like a local variable that survives redraws.
  • @Observable classes are models. A view that reads a model's property redraws when that property changes. In this app, each screen's logic and loading lives in a model class, and the view only draws it.
  • @Environment passes values down the view tree without threading them through every initializer. The app uses it for app-wide actions such as "push this screen" or "open Quick log".
  • .task { … } runs async work when a view appears and cancels it when the view goes away. This is where screens load their data.

From launch to a screen​

  1. CaregiverApp.swift is the entry point (@main). It configures Amplify from amplifyconfiguration-dev.json in Debug builds or -prod.json in Release, creates the one Session, and shows RootView.

  2. Session.swift decides where the caregiver is. refresh() checks for a Cognito session and calls GET /v1/me, then sets one of four states: checking, signedOut, noTeam(account) or ready(account).

  3. RootView.swift switches on that state: the auth screens, Join a care team, or the signed-in app. It also offers Face ID after a password sign-in.

  4. TabShell.swift is the signed-in app: the tab bar (Home, Insights placeholder, Team, Settings) and the ⊕ button. It owns the models shared across tabs, such as the active care receiver, and publishes app-wide actions through the environment:

    Environment valueWhat it does
    pushRoutePush a screen onto Home's stack.
    popToHome, popToTrackersGo back to the root, or to the Trackers list.
    quickLogOpen Quick log; quickLogRuns tells screens when it saved.
    homeWrote, homeWritesTell Home an entry or note was edited or deleted.
    receiverChangedA care receiver was renamed, archived or restored.

Home's tab holds one NavigationStack, driven by an array of Route values. Route is an enum with one case per pushable screen: trackers, tracker, schedules, careReceiver, entry, journalNote and comingUp. Pushing a screen means appending a Route; going back means removing it. routeDestinations(...) maps each case to its view, in one place.

The stack is kept to one path of values on purpose. SwiftUI misorders a stack that mixes this style with other ways of pushing, so a tap would flash and Back would show the wrong screen. Sheets (Add tracker, Quick log, confirmations) are presented with .sheet on the screen that opens them, and are not part of the path. Team and Settings each have a simpler stack of their own.

A screen: View, Model, Service​

Every area folder follows the same three-part shape:

PartExampleJob
ViewTrackerDetailViewDraws the screen from the model, and turns taps into model calls or pushes.
ModelTrackerDetailModel (@Observable)Holds the screen's state (phase: .loading / .loaded / .failed) and does the async work.
ServiceTrackersServiceA struct of async closures, one per API call. .live(client) calls the API; tests and previews pass fakes.

Services being plain closures is what makes the app testable: a test builds a model with a service whose closures return canned data, so there are no network calls and no mocking framework. Previews do the same, with data from each area's …Fixtures.swift (the people and trackers are described in ios/fixtures.md).

Worked example: opening a tracker​

What happens when a caregiver taps a tracker on the Trackers list:

  1. The tap. In TrackersView.swift each row is a NavigationLink(value: Route.tracker(id:receiver:)), which appends that route to the stack's path.
  2. The destination. routeDestinations in Route.swift maps .tracker to TrackerDetailView(trackerID:receiver:service:).
  3. Loading. The view's .task creates a TrackerDetailModel and calls load(). The model starts in .loading, so the view shows a spinner.
  4. The API call. load() calls service.trackerDetail(trackerID). The live closure in TrackersService.swift calls the generated client, client.getTrackerDetail(path:), which is GET /v1/trackers/{trackerId}. The API side is the same request from the server's point of view.
  5. The response. The generated client returns one case per documented response (.ok, .notFound, .forbidden and so on). The service turns .ok into the app's TrackerProfile, and any other case into an AppError.
  6. Drawing. The model sets phase = .loaded(profile). Because the view reads phase, SwiftUI redraws it with the tracker's details.

Talking to the API​

  • The generated client. CaregiverAPI is a local Swift package in shared/types-swift. Apple's swift-openapi-generator builds it from a copy of the contract during the Xcode build, so there is no generated Swift in the repo. Each operationId in the contract becomes a method, like getTrackerDetail.
  • APIClient.swift builds the client with the base URL and a chain of middleware.
  • AuthMiddleware.swift adds Authorization: Bearer <ID token> to every request, using CognitoTokenProvider.
  • The base URL comes from API_BASE_URL in Info.plist, which is set per build from ios/Config/Dev.xcconfig and Prod.xcconfig (read by AppConfig.swift).

Folder map​

FolderWhat is in it
App/The entry point and RootView.
Auth/Landing, Sign in, Sign up, confirm code, reset password, Face ID.
GettingIn/Join a care team and Create your first care team, after sign-in.
Home/Home, the tab shell, Quick log, Entry detail, journal notes, Coming up.
Trackers/The Trackers list, Tracker detail, Add and Edit tracker, schedules, and Route.
Team/Team, Care team, Care receiver, invitations, coverage.
Settings/Settings, notifications, close account, sign out, about.
Support/Session, the API client and its middleware, errors, Keychain, Face ID, server clock.
DesignSystem/Shared Stride… components and Theme (colors, type, spacing), cataloged in ios/design-system.md.
Resources/Assets and the Amplify configuration files.

Tests are in ios/CaregiverTests/, one file per model or area (XCTest).

Building and running​

The Xcode project is generated from ios/project.yml by XcodeGen, and the .xcodeproj is not committed. After pulling, or after editing project.yml:

cd ios && xcodegen generate && open Caregiver.xcodeproj

Run the tests from the command line:

cd ios && xcodegen generate && xcodebuild test \
-scheme Caregiver -destination 'platform=iOS Simulator,name=iPhone 17' \
-skipPackagePluginValidation

-skipPackagePluginValidation is needed on every xcodebuild, because the generated client is built by a package plugin. A Debug build talks to the dev API; a Release build talks to prod. CI builds and tests the app on pull requests that touch ios/.