@blotch/angular-lynx-native 22.2.25 → 22.2.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (62) hide show
  1. package/README.md +15 -11
  2. package/dist/android/project.d.ts +13 -1
  3. package/dist/android/project.d.ts.map +1 -1
  4. package/dist/android/project.js +40 -11
  5. package/dist/app-links.d.ts +53 -0
  6. package/dist/app-links.d.ts.map +1 -0
  7. package/dist/app-links.js +115 -0
  8. package/dist/generate.d.ts +1 -1
  9. package/dist/generate.js +8 -2
  10. package/dist/index.d.ts +3 -1
  11. package/dist/index.d.ts.map +1 -1
  12. package/dist/index.js +23 -0
  13. package/dist/ios/build.d.ts.map +1 -1
  14. package/dist/ios/build.js +5 -0
  15. package/dist/ios/project.d.ts +16 -2
  16. package/dist/ios/project.d.ts.map +1 -1
  17. package/dist/ios/project.js +57 -6
  18. package/dist/options.d.ts +68 -1
  19. package/dist/options.d.ts.map +1 -1
  20. package/dist/options.js +70 -0
  21. package/dist/permissions.d.ts.map +1 -1
  22. package/dist/permissions.js +7 -0
  23. package/dist/png.d.ts +5 -0
  24. package/dist/png.d.ts.map +1 -1
  25. package/dist/png.js +19 -5
  26. package/dist/splash.d.ts +70 -0
  27. package/dist/splash.d.ts.map +1 -0
  28. package/dist/splash.js +239 -0
  29. package/package.json +1 -1
  30. package/templates/android/README.md +11 -2
  31. package/templates/android/app/build.gradle.kts +19 -9
  32. package/templates/android/app/proguard-rules.pro +67 -55
  33. package/templates/android/app/src/debug/java/dev/angularlynx/shell/DevServer.kt +6 -0
  34. package/templates/android/app/src/main/AndroidManifest.xml +3 -1
  35. package/templates/android/app/src/main/java/dev/angularlynx/shell/AssetsModule.kt +2 -1
  36. package/templates/android/app/src/main/java/dev/angularlynx/shell/CanvasUI.kt +16 -3
  37. package/templates/android/app/src/main/java/dev/angularlynx/shell/LinkingModule.kt +42 -0
  38. package/templates/android/app/src/main/java/dev/angularlynx/shell/MainActivity.kt +96 -7
  39. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +15 -0
  40. package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +24 -0
  41. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLContext.kt +15 -4
  42. package/templates/android/app/src/main/java/dev/angularlynx/shell/WebGLModule.kt +10 -1
  43. package/templates/android/app/src/main/res/values/colors.xml +3 -2
  44. package/templates/android/app/src/main/res/values/splash.xml +9 -0
  45. package/templates/android/app/src/main/res/values/themes.xml +14 -0
  46. package/templates/android/app/src/release/java/dev/angularlynx/shell/DevServer.kt +2 -0
  47. package/templates/android/build.gradle.kts +1 -1
  48. package/templates/android/gradle/wrapper/gradle-wrapper.jar +0 -0
  49. package/templates/android/gradle/wrapper/gradle-wrapper.properties +2 -2
  50. package/templates/android/gradlew.bat +41 -11
  51. package/templates/ios/App/AngularLynxWebGL.h +34 -6
  52. package/templates/ios/App/AngularLynxWebGL.m +58 -23
  53. package/templates/ios/App/AngularLynxWebGLContext.m +104 -26
  54. package/templates/ios/App/AnimationFrameRate.swift +214 -0
  55. package/templates/ios/App/AppDelegate.swift +48 -0
  56. package/templates/ios/App/AssetsModule.swift +3 -1
  57. package/templates/ios/App/DevServer.swift +3 -0
  58. package/templates/ios/App/Info.plist +12 -5
  59. package/templates/ios/App/LinkingModule.swift +62 -1
  60. package/templates/ios/App/LynxViewController.swift +30 -6
  61. package/templates/ios/App/ShellModule.swift +40 -1
  62. package/templates/ios/App/SplashScreen.swift +71 -0
@@ -13,7 +13,21 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
13
13
  _ application: UIApplication,
14
14
  didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?
15
15
  ) -> Bool {
16
+ // Lynx's frameworks are built for release, where Lynx logs at INFO: a
17
+ // line for every touch move, hit test, flush, layout, event listener and
18
+ // main-thread module call, each written with a synchronous NSLog on the
19
+ // thread that logged it. Mostly that's the engine thread, where Angular,
20
+ // layout and Lynx's animator share each frame, and during a touch the
21
+ // UI thread. A release app keeps Lynx's warnings and errors, which carry
22
+ // the app's JavaScript errors; the Debug configuration that
23
+ // `ng serve --platform ios` builds keeps every line for debugging.
24
+ // First, so the lines Lynx logs while it starts are left out too.
25
+ #if !DEBUG
26
+ SetMinimumLoggingLevel(.warning)
27
+ #endif
16
28
  XElementRegistry.register()
29
+ // Before any LynxView, so its first transition already asks for 120 fps.
30
+ AnimationFrameRate.install()
17
31
  let config = LynxConfig(provider: BundleResourceFetcher.shared)
18
32
  LynxEnv.sharedInstance().prepareConfig(config)
19
33
  // LynxService's image service decodes through SDWebImage, which only
@@ -51,12 +65,46 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
51
65
  options connectionOptions: UIScene.ConnectionOptions
52
66
  ) {
53
67
  guard let windowScene = scene as? UIWindowScene else { return }
68
+ // A link that started the app: one with one of the app's schemes, or a
69
+ // Universal Link (an https link to one of angular.json's appLinkDomains),
70
+ // which comes as a web browsing activity. It comes only here, not to
71
+ // scene(_:openURLContexts:) or scene(_:continue:) too, and before the
72
+ // LynxView exists, so it goes out with the first global props
73
+ // (LinkCenter).
74
+ LinkCenter.shared.initialURL =
75
+ (connectionOptions.urlContexts.first?.url
76
+ ?? connectionOptions.userActivities.lazy.compactMap(Self.universalLink).first)?
77
+ .absoluteString
54
78
  let window = UIWindow(windowScene: windowScene)
55
79
  window.rootViewController = LynxViewController()
56
80
  window.makeKeyAndVisible()
57
81
  self.window = window
58
82
  }
59
83
 
84
+ // A link with one of the app's schemes while the app is open, in front or
85
+ // in the background: the runtime's LynxLinking takes it, and the router
86
+ // navigates to it.
87
+ func scene(_ scene: UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) {
88
+ for context in URLContexts {
89
+ LinkCenter.shared.open(context.url)
90
+ }
91
+ }
92
+
93
+ // A Universal Link while the app is open: the same as a link with one of
94
+ // its schemes.
95
+ func scene(_ scene: UIScene, continue userActivity: NSUserActivity) {
96
+ if let url = Self.universalLink(userActivity) {
97
+ LinkCenter.shared.open(url)
98
+ }
99
+ }
100
+
101
+ /// The https link a Universal Link's activity carries. iOS hands the app
102
+ /// one for a link to a domain in its Associated Domains entitlement, once
103
+ /// that domain's apple-app-site-association names the app.
104
+ private static func universalLink(_ activity: NSUserActivity) -> URL? {
105
+ activity.activityType == NSUserActivityTypeBrowsingWeb ? activity.webpageURL : nil
106
+ }
107
+
60
108
  // Out of sight, a collection's pause can't show, and the memory it frees
61
109
  // makes the app less likely to be ended while it's in the background.
62
110
  func sceneDidEnterBackground(_ scene: UIScene) {
@@ -9,7 +9,9 @@ import Lynx
9
9
  /// Lynx's own `<image>`.
10
10
  ///
11
11
  /// Called from the main thread, synchronously (`lynx.module(name)`), like
12
- /// the WebGL module: the runtime turns the answer into a Response.
12
+ /// the WebGL module: the runtime turns the answer into a Response. Lynx calls
13
+ /// it on its engine thread, which LynxViewController runs off the UI thread,
14
+ /// so a dev server download that waits blocks Angular, not UIKit.
13
15
  final class AssetsModule: NSObject, LynxModule {
14
16
  static var name: String { "AngularLynxAssets" }
15
17
 
@@ -185,6 +185,9 @@ final class DevServer {
185
185
  status.text = message
186
186
  statusScreen.isHidden = false
187
187
  statusScreen.superview?.bringSubviewToFront(statusScreen)
188
+ // The launch screen's copy makes way for the message, rather than
189
+ // hide it until it times out (SplashScreen.swift).
190
+ SplashScreen.shared.hide()
188
191
  }
189
192
  #endif
190
193
  }
@@ -2,6 +2,13 @@
2
2
  <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
3
3
  <plist version="1.0">
4
4
  <dict>
5
+ <!-- Lets the app draw at up to 120 fps on a ProMotion iPhone. Without it
6
+ iOS holds an app's CADisplayLink and Core Animation to 60 fps there,
7
+ while the system's own UI runs at 120, and Lynx animates through a
8
+ CADisplayLink: every transition and drag ran at half the phone's
9
+ rate. Lynx asks for 120 itself (the build's `preferredFps`). -->
10
+ <key>CADisableMinimumFrameDurationOnPhone</key>
11
+ <true/>
5
12
  <key>CFBundleDevelopmentRegion</key>
6
13
  <string>$(DEVELOPMENT_LANGUAGE)</string>
7
14
  <key>CFBundleDisplayName</key>
@@ -29,16 +36,16 @@
29
36
  <key>UIApplicationSupportsMultipleScenes</key>
30
37
  <false/>
31
38
  </dict>
32
- <key>UILaunchScreen</key>
33
- <dict>
34
- <key>UIColorName</key>
35
- <string>LaunchBackground</string>
36
- </dict>
37
39
  <key>UIRequiredDeviceCapabilities</key>
38
40
  <array>
39
41
  <string>arm64</string>
40
42
  </array>
41
43
  <!-- angular-lynx:info -->
44
+ <key>UILaunchScreen</key>
45
+ <dict>
46
+ <key>UIColorName</key>
47
+ <string>LaunchBackground</string>
48
+ </dict>
42
49
  <key>UISupportedInterfaceOrientations</key>
43
50
  <array>
44
51
  <string>UIInterfaceOrientationPortrait</string>
@@ -2,6 +2,60 @@ import Lynx
2
2
  import SafariServices
3
3
  import UIKit
4
4
 
5
+ /// The links that opened the app: those with one of angular.json's
6
+ /// `deepLinkSchemes`, and Universal Links, https links to its
7
+ /// `appLinkDomains`.
8
+ ///
9
+ /// The one that starts the app arrives in the scene's connection options,
10
+ /// before there's a LynxView: it becomes the `initialURL` global prop, which
11
+ /// LynxViewController sends before the bundle runs, so the router's first
12
+ /// screen is already the link's. One that arrives while the app is open
13
+ /// (SceneDelegate's `scene(_:openURLContexts:)` and `scene(_:continue:)`)
14
+ /// waits here, and the runtime's
15
+ /// `LynxLinking` takes it with `takeURLs` when it hears the `angularLynxURL`
16
+ /// global event, and once as it starts listening. A link waits for as long
17
+ /// as that takes, so one that arrives while Angular is still starting isn't
18
+ /// lost, and none is handled twice. ShortcutCenter works the same way.
19
+ final class LinkCenter {
20
+ static let shared = LinkCenter()
21
+
22
+ /// The global event that says a link is waiting. It carries nothing: the
23
+ /// listener answers by taking them.
24
+ static let event = "angularLynxURL"
25
+
26
+ /// The link that started the app, if one did. Set before the
27
+ /// LynxViewController is made.
28
+ var initialURL: String?
29
+
30
+ /// Lynx calls modules off the main thread, and the scene delegate leaves
31
+ /// links on it.
32
+ private let lock = NSLock()
33
+ private var waiting: [String] = []
34
+
35
+ /// The app's LynxView, which LynxViewController sets once it has one.
36
+ weak var lynxView: LynxView?
37
+
38
+ /// Leaves a link that arrived while the app is open for the runtime, and
39
+ /// tells it. Called on the main thread.
40
+ func open(_ url: URL) {
41
+ lock.lock()
42
+ waiting.append(url.absoluteString)
43
+ lock.unlock()
44
+ // Before the LynxView exists there's nobody to tell, and the runtime
45
+ // asks when it starts listening anyway.
46
+ lynxView?.sendGlobalEvent(Self.event, withParams: [])
47
+ }
48
+
49
+ /// The waiting links, oldest first, which stop waiting.
50
+ func take() -> [String] {
51
+ lock.lock()
52
+ defer { lock.unlock() }
53
+ let taken = waiting
54
+ waiting = []
55
+ return taken
56
+ }
57
+ }
58
+
5
59
  /// The native module behind the runtime's `LynxLinking`, which calls it as
6
60
  /// `NativeModules.AngularLynxLinking` (from the background thread, where
7
61
  /// NativeModules lives).
@@ -10,7 +64,9 @@ import UIKit
10
64
  /// its own. `openURL` hands it to iOS, which opens it in Safari, Mail or the
11
65
  /// app that claims it, and `openInApp` shows it in SFSafariViewController over
12
66
  /// the app. Each method calls back once: with `{}` when it's done, `{ value }`
13
- /// for `canOpenURL`, or `{ error: "failed", message }`.
67
+ /// for `canOpenURL`, or `{ error: "failed", message }`. `takeURLs` calls back
68
+ /// with `{ urls }`: the links to the app that arrived since the last call
69
+ /// (see LinkCenter).
14
70
  final class LinkingModule: NSObject, LynxModule, SFSafariViewControllerDelegate {
15
71
  static var name: String { "AngularLynxLinking" }
16
72
 
@@ -19,6 +75,7 @@ final class LinkingModule: NSObject, LynxModule, SFSafariViewControllerDelegate
19
75
  "openURL": NSStringFromSelector(#selector(openURL(_:callback:))),
20
76
  "openInApp": NSStringFromSelector(#selector(openInApp(_:options:callback:))),
21
77
  "canOpenURL": NSStringFromSelector(#selector(canOpenURL(_:callback:))),
78
+ "takeURLs": NSStringFromSelector(#selector(takeURLs(_:))),
22
79
  ]
23
80
  }
24
81
 
@@ -67,6 +124,10 @@ final class LinkingModule: NSObject, LynxModule, SFSafariViewControllerDelegate
67
124
  }
68
125
  }
69
126
 
127
+ @objc func takeURLs(_ callback: @escaping LynxCallbackBlock) {
128
+ callback(["urls": LinkCenter.shared.take()])
129
+ }
130
+
70
131
  /// Shows `address` in SFSafariViewController and calls back once the
71
132
  /// person closes it. `options` may hold `toolbarColor` and `controlsColor`,
72
133
  /// each a `#` and eight hex digits: red, green, blue and alpha.
@@ -16,6 +16,8 @@ final class LynxViewController: UIViewController {
16
16
 
17
17
  override func viewDidLoad() {
18
18
  super.viewDidLoad()
19
+ // The launch screen's color (angular.json's splash.backgroundColor),
20
+ // behind the LynxView, so no other color shows between the two.
19
21
  view.backgroundColor = UIColor(named: "LaunchBackground") ?? .systemBackground
20
22
 
21
23
  let fetcher = BundleResourceFetcher.shared
@@ -50,8 +52,19 @@ final class LynxViewController: UIViewController {
50
52
  DevServer.shared.register(in: config)
51
53
  let lynxView = LynxView { builder in
52
54
  builder.config = config
55
+ // Lynx's engine thread, which runs the main thread's JavaScript (the
56
+ // whole Angular app), layout and Lynx's animator, gets a thread of its
57
+ // own. Lynx's default, AllOnUI, runs it on the UI thread, where change
58
+ // detection, a screen being built or a long event handler took frames
59
+ // from what UIKit runs itself: a scroll view's momentum, typing, a web
60
+ // view. Off it, the element tree reaches the screen a step later, once
61
+ // the flush crosses to the UI thread: the build plugin turns on Lynx's
62
+ // pending invokes so UI methods follow it, and the runtime sets
63
+ // enable-async-list on every <list>, which this strategy requires.
64
+ builder.setThreadStrategyForRender(.mostOnTASM)
53
65
  // Lets main-thread code, where Angular runs, call the config's modules
54
- // with lynx.module(name): WebGL needs answers synchronously.
66
+ // with lynx.module(name): WebGL needs answers synchronously. Lynx calls
67
+ // them on its engine thread, not the UI thread (see AngularLynxWebGL.h).
55
68
  builder.enableMTSModule = true
56
69
  builder.screenSize = screenSize
57
70
  builder.fontScale = 1.0
@@ -64,8 +77,9 @@ final class LynxViewController: UIViewController {
64
77
  lynxView.layoutHeightMode = .exact
65
78
  view.addSubview(lynxView)
66
79
  self.lynxView = lynxView
67
- // Who hears that a shortcut ran while the app is open.
80
+ // Who hears that a shortcut ran, or a link arrived, while the app is open.
68
81
  ShortcutCenter.shared.lynxView = lynxView
82
+ LinkCenter.shared.lynxView = lynxView
69
83
 
70
84
  layoutLynxView()
71
85
  lynxView.updateGlobalProps(with: globalProps())
@@ -73,6 +87,11 @@ final class LynxViewController: UIViewController {
73
87
  // server's, which it loads again on every edit.
74
88
  let templateURL = DevServer.shared.templateURL
75
89
  lynxView.loadTemplate(fromURL: templateURL, initData: nil)
90
+ // The launch screen, over the LynxView until the app's first screen is
91
+ // drawn: iOS takes its own down at the first frame, which is the empty
92
+ // LynxView (SplashScreen.swift). The development build's messages go
93
+ // over it.
94
+ SplashScreen.shared.show(over: view)
76
95
  DevServer.shared.attach(to: view) { [weak self] in
77
96
  self?.lynxView?.loadTemplate(fromURL: templateURL, initData: nil)
78
97
  }
@@ -156,10 +175,10 @@ final class LynxViewController: UIViewController {
156
175
  /// The global props the runtime reads: `theme` (LynxTheme), `isNotchScreen`
157
176
  /// (LynxSafeArea), `appLocale` (LynxLocale), `appTimeZone` (the `intl`
158
177
  /// build option's Intl.DateTimeFormat), the accessibility settings
159
- /// (LynxAccessibilitySettings) and `osVersion` (LynxSystemInfo), plus the
160
- /// safe-area insets and `edgeToEdge`, which env(safe-area-inset-*) reads.
161
- /// The Android shell sends the same keys, and Lynx Explorer sends theme and
162
- /// isNotchScreen too.
178
+ /// (LynxAccessibilitySettings), `osVersion` (LynxSystemInfo) and
179
+ /// `initialURL` (LynxLinking and the router), plus the safe-area insets and
180
+ /// `edgeToEdge`, which env(safe-area-inset-*) reads. The Android shell
181
+ /// sends the same keys, and Lynx Explorer sends theme and isNotchScreen too.
163
182
  private func globalProps() -> LynxTemplateData {
164
183
  let insets = view.safeAreaInsets
165
184
  var props: [String: Any] = [
@@ -196,6 +215,11 @@ final class LynxViewController: UIViewController {
196
215
  // option's Intl.DateTimeFormat formats in the device's zone by default,
197
216
  // which it reads from the engine's local time, and names it with this.
198
217
  props["appTimeZone"] = TimeZone.current.identifier
218
+ // The link that started the app, which the router starts at. In every
219
+ // update, since each sends the whole set.
220
+ if let url = LinkCenter.shared.initialURL {
221
+ props["initialURL"] = url
222
+ }
199
223
  // Without useBoolLiterals, Lynx turns a Bool into 1 or 0.
200
224
  return LynxTemplateData(dictionary: props, useBoolLiterals: true)
201
225
  }
@@ -1,5 +1,6 @@
1
1
  import Lynx
2
2
  import UIKit
3
+ import os
3
4
 
4
5
  /// The native module the AngularLynx runtime calls as
5
6
  /// `NativeModules.AngularLynxShell` (from the background thread, where
@@ -14,11 +15,25 @@ import UIKit
14
15
  /// memory pressure, so this reports a moderate one. The runtime asks once it
15
16
  /// has released about a screen's worth of elements and the app has settled
16
17
  /// (`collect-garbage.ts` in `packages/runtime`).
18
+ ///
19
+ /// `hideSplashScreen` fades out the copy of the launch screen the shell keeps
20
+ /// over the app until its first screen is ready (SplashScreen.swift). The
21
+ /// runtime's LynxSplashScreen calls it.
22
+ ///
23
+ /// `reportFullyDrawn` marks the moment the app's first screen is complete
24
+ /// (the runtime's `reportFullyDrawn()`) with an `os_signpost` event named
25
+ /// `Fully Drawn` in the Points of Interest category, which Instruments shows
26
+ /// beside the App Launch template's phases. iOS has no API of its own for it,
27
+ /// as Android has `Activity.reportFullyDrawn()`.
17
28
  final class ShellModule: NSObject, LynxModule {
18
29
  static var name: String { "AngularLynxShell" }
19
30
 
20
31
  static var methodLookup: [String: String] {
21
- ["collectGarbage": NSStringFromSelector(#selector(collectGarbage))]
32
+ [
33
+ "collectGarbage": NSStringFromSelector(#selector(collectGarbage)),
34
+ "hideSplashScreen": NSStringFromSelector(#selector(hideSplashScreen)),
35
+ "reportFullyDrawn": NSStringFromSelector(#selector(reportFullyDrawn)),
36
+ ]
22
37
  }
23
38
 
24
39
  override init() {
@@ -39,4 +54,28 @@ final class ShellModule: NSObject, LynxModule {
39
54
  LynxEnv.sharedInstance().trimMemory(.moderate)
40
55
  }
41
56
  }
57
+
58
+ @objc func hideSplashScreen() {
59
+ DispatchQueue.main.async {
60
+ SplashScreen.shared.hide()
61
+ }
62
+ }
63
+
64
+ private static let pointsOfInterest = OSLog(
65
+ subsystem: Bundle.main.bundleIdentifier ?? "AngularLynx", category: .pointsOfInterest)
66
+ private static let fullyDrawnOnce = NSLock()
67
+ private static var fullyDrawn = false
68
+
69
+ // Emitted on the calling thread: the runtime calls it from Lynx's engine
70
+ // thread, through main-thread modules, and a hop to the main queue would
71
+ // add a frame's wait to the moment it marks. os_signpost is thread-safe.
72
+ @objc func reportFullyDrawn() {
73
+ ShellModule.fullyDrawnOnce.lock()
74
+ defer { ShellModule.fullyDrawnOnce.unlock() }
75
+ // Once per process, as Android's is: the development build loads the app
76
+ // again on every edit, and each load reports.
77
+ guard !ShellModule.fullyDrawn else { return }
78
+ ShellModule.fullyDrawn = true
79
+ os_signpost(.event, log: ShellModule.pointsOfInterest, name: "Fully Drawn")
80
+ }
42
81
  }
@@ -0,0 +1,71 @@
1
+ import UIKit
2
+
3
+ /// Keeps the launch screen up until the app's first screen is ready.
4
+ ///
5
+ /// iOS takes the launch screen down as soon as the app draws its first
6
+ /// frame, which is the empty LynxView: the bundle hasn't run yet, and the
7
+ /// app's first screen may have work to finish first, such as a lazy route or
8
+ /// a guard. So the shell puts a copy of the launch screen over the LynxView
9
+ /// (UILaunchScreen's color and image, from the asset catalog, laid out as iOS
10
+ /// lays them out), and fades it out when the runtime says the screen under
11
+ /// it is drawn: `ShellModule.hideSplashScreen`, which the runtime's
12
+ /// LynxSplashScreen calls after the first frame Angular commits, or when the
13
+ /// app's `provideSplashScreen()` says.
14
+ ///
15
+ /// A host the runtime can't reach never asks, so the copy also goes after
16
+ /// `timeout`, rather than leave the app behind its logo.
17
+ final class SplashScreen {
18
+ static let shared = SplashScreen()
19
+
20
+ /// The longest the copy stays up: enough for a slow first screen, short
21
+ /// enough that one that never settles doesn't look like a hung app.
22
+ static let timeout: TimeInterval = 5
23
+ /// The fade, as long as UIKit's own transitions.
24
+ private static let fadeDuration: TimeInterval = 0.25
25
+
26
+ private weak var overlay: UIView?
27
+
28
+ private init() {}
29
+
30
+ /// Puts the copy over everything in `view`, now and until it's hidden.
31
+ func show(over view: UIView) {
32
+ let overlay = UIView(frame: view.bounds)
33
+ overlay.autoresizingMask = [.flexibleWidth, .flexibleHeight]
34
+ // The same names Info.plist's UILaunchScreen reads, so the two match.
35
+ // Both follow dark mode, as the launch screen does.
36
+ overlay.backgroundColor = UIColor(named: "LaunchBackground") ?? .systemBackground
37
+ // An app without angular.json's splash.image has none: the color alone.
38
+ if let image = UIImage(named: "LaunchImage") {
39
+ let imageView = UIImageView(image: image)
40
+ // At its natural size, centered on the whole screen, as the launch
41
+ // screen draws it (UIImageRespectsSafeAreaInsets is false).
42
+ imageView.center = CGPoint(x: overlay.bounds.midX, y: overlay.bounds.midY)
43
+ imageView.autoresizingMask = [
44
+ .flexibleLeftMargin, .flexibleRightMargin, .flexibleTopMargin, .flexibleBottomMargin,
45
+ ]
46
+ overlay.addSubview(imageView)
47
+ }
48
+ // VoiceOver has nothing to read until the app is there.
49
+ overlay.isAccessibilityElement = false
50
+ view.addSubview(overlay)
51
+ self.overlay = overlay
52
+
53
+ DispatchQueue.main.asyncAfter(deadline: .now() + Self.timeout) { [weak self] in
54
+ self?.hide()
55
+ }
56
+ }
57
+
58
+ /// Fades the copy out. Main thread only. Once it's gone, does nothing: the
59
+ /// development build loads the app again on every edit, and each load
60
+ /// asks.
61
+ func hide() {
62
+ guard let overlay else { return }
63
+ self.overlay = nil
64
+ // Reduce Motion keeps the fade, as UIKit's cross-dissolves do: it moves
65
+ // nothing.
66
+ UIView.animate(
67
+ withDuration: Self.fadeDuration, delay: 0, options: [.curveEaseOut],
68
+ animations: { overlay.alpha = 0 },
69
+ completion: { _ in overlay.removeFromSuperview() })
70
+ }
71
+ }