Skip to content

simonberner/ladi-simulator

Repository files navigation

Live Activities & Dynamic Island Simulator

mastodon.green/@simonberner Gitmoji Conventional Commits

A little basketball game simulator which generates some mock data to show how that live data can be displayed with live activities on the lock screen and in the dynamic island.


Contents


Functionality

This App shows Live Activity (live data updates):

  • On the lock screen and on devices that don't support the Dynamic Island, a banner on top of the screen.
  • in the compact version of the dynamic island
  • in the expanded version of the dynamic island Dynamic Island: On devices that support the Dynamic Island (see list below), the App displays Live Activities on the leading and trailing side of the TrueDepth camera.

Definitions

Tech Stack

  • Xcode 14.2
  • Swift 5.7.2

Frameworks

Device and OS Compatibility

LAs are iPhone only.

  • For LA: iPhone with iOS 16.1+
  • For DI: iPhone 14 Pro/Pro Max with iOS 16.1+

Screenshots

App View Banner iPhone 14 Banner iPhone 14 Pro
LADI iPhone14Pro LADI expanded iPhone14Pro

Architecture

The App Architecture is built using the following patterns:

I am using the Model-View architectural pattern in order that the GameView can observe/listen to state changes in the GameModel and rerender its UI accordingly.

I am using the following design patterns to solve common challenges:

How does it work?

  • By pressing the Start Game Simulation, the GameSimulator "factory" spits out a new GameState every 2 seconds. It is restricted to be started only once.
  • By pressing Start Live Activity, the Live Activity can be started. It is restricted to be started only once.
  • The Live Activity updates while the App is running in the background. This is only possible because the background mode 'Audio, AirPlay,...' is enabled.
  • The GameView and the Live Activity updates with every (new) GameState change
  • The GameModel is (the glue) between the GameView and the GameSimulator
  • The GameSimulator stops automatically after 120 ball possessions in total

Learnings

Live Activities

  • Apple doc: Displaying live data
  • Live activities can live for up to 8 hours at max.
  • Completed (final state) Live Activity will stay on the Lock Screen for 4 hours.
  • Live Activity has to be launched while the App is in the foreground.
  • Live Activity can be updated while the App is running in the background.
  • Live Activities are not Widgets but we need WidgetKit to build them with Widget Extension.
  • It is highly likely that one is going to have also a Widget in combination with Live Activities.
  • Live Activity vs Widget: a Widget can update itself, as a LA have to be updated from within the App.
  • As soon you create a Widget Extension including LA, Xcode will generate some boilerplate code for you.
  • When more than one LA is active the system chooses which LAs are visible and displays two using the minimal presentation
  • IMPORTANT: In the App Target in the Info.plist we have to add the Key: Supports Live Activities and set its value to True.
  • Use the new (iOS 16.2+) struct ActivityContent<State> to describe the current state and config of a LA.
  • UI Banner on the Always-On display (iPhone 14 only) doesn't show any animations
  • UI Banner on the regular lock screen shows animations

Live Activities - Background Updates

Any, AnyObject, any

AnyObject

  • All classes, class types, or class-only protocols can use AnyObject as their concrete type.
  • If possible try to use concrete types as it is more readable to your friends.
  • By using it as our destination, we always need to cast and consider casting failures using the default implementation.
  • Try to use concrete protocol instead, see here

Any

any

Type Erasure

Nice to know

  • Architecture vs Architectural Patterns vs Design Patterns
  • Architecture: An architecture shows how we organize our code, or how the system will look like from 10000 meters above from the highest level of abstraction of our system design.
  • Architecture: Software architecture refers to the fundamental structure underlying a system. It is the general conceptual design that informs the development and maintenance of software and defines what it can β€” and cannot β€” do.
  • Architecture: A poor architecture can make changes to the software much harder than they need to be.
  • Architectural Patterns: Are broad solutions how we can implement the chosen architecture.
  • Design Patterns: Are accumulative best practices and experiences that developers used over the years to solve general problems.
  • GitHub: We can use one of the following keywords to close an issue via a commit message: close, closes, closed, fix, fixes, fixed, resolve, resolves, resolved. The message MUST contain a string matching the following pattern: KEYWORD #ISSUENO. For example: Closes/closes #1 or multiple issues Closes #1, Resolves #2, Fixes #3. credits

Contribution Guidelines

Unit-Testing

Follow the Arrange, Act and Assert Pattern for Unit Testing.

  • Arrange inputs and targets:
    • Does the test require any objects or special settings?
    • Does it need to prep a database?
    • Does it need to log into a web app?
  • Act on target behavior:
    • This step should call the function/method/API, or whatever needs to be tested.
    • It should focus on the target behavior.
  • Assert expected outcomes:
    • This step should elicit some sort of response.
    • Then the response should be judged for correctness.

UI-Testing

Follow the Given-When-Then style to write the UI-Tests from a user perspective.

Code Comments

  • Include "self documenting code."
  • Additionally, include descriptive comments in order to improve readability. -Aim for comments that allow other programmers to understand your code without having to work through your entire classes/methods.

Pull Requests

  • Pull requests should be kept to a reasonable size.
  • Pull requests should be descriptive, answer the following questions:
    • What was the problem?
    • What did you do to improve it?
    • How do you know it is a working solution?/What tests did you do?
    • Why do you believe this is the right solution?
    • Why is this the best solution for the problem?
  • Review/proofread your pull request before submission. Following this guide.

References

Credits

πŸ™πŸ½ Sean Allen

Extra: Xcode shortcuts

  • CMD + B: build the project
  • CMD + R: build and run the App in the simulator
  • CMD + SHIFT + K: clean build folder
  • CMD + SHIFT + O: open quick search
  • CMD + SHIFT + J: highlight the selected open file in the navigator
  • CMD + SHIFT + L: open up the library (Snippets, Media, Colors, SF Symbols)

Made with a πŸ™‚ Simon Berner