@apexnative/navigation 1.0.0-alpha.3 → 1.0.0-alpha.4

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 (193) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/android/CLAUDE.md +17 -7
  3. package/android/CORE.md +8 -3
  4. package/android/RECIPES.md +9 -3
  5. package/android/src/main/java/dev/apexnative/navigation/NavigationApplication.kt +34 -13
  6. package/android/src/main/java/dev/apexnative/navigation/layout/LayoutChecks.kt +5 -1
  7. package/android/src/main/java/dev/apexnative/navigation/layout/LayoutNode.kt +6 -2
  8. package/android/src/main/java/dev/apexnative/navigation/nativescreen/NativeScreenContext.kt +2 -2
  9. package/android/src/main/java/dev/apexnative/navigation/options/ButtonMenuOptions.kt +2 -2
  10. package/android/src/main/java/dev/apexnative/navigation/options/ButtonOptions.kt +2 -2
  11. package/android/src/main/java/dev/apexnative/navigation/options/OptionsCache.kt +8 -4
  12. package/android/src/main/java/dev/apexnative/navigation/options/UserChoices.kt +159 -0
  13. package/android/src/main/java/dev/apexnative/navigation/react/CommandAnswer.kt +2 -24
  14. package/android/src/main/java/dev/apexnative/navigation/react/CommandContract.kt +59 -0
  15. package/android/src/main/java/dev/apexnative/navigation/react/Hinge.kt +27 -19
  16. package/android/src/main/java/dev/apexnative/navigation/react/NavigationTurboModule.kt +5 -4
  17. package/android/src/main/java/dev/apexnative/navigation/react/PendingCalls.kt +48 -0
  18. package/android/src/main/java/dev/apexnative/navigation/react/events/AppearanceDispatcher.kt +17 -7
  19. package/android/src/main/java/dev/apexnative/navigation/slots/SlotContainers.kt +10 -7
  20. package/android/src/main/java/dev/apexnative/navigation/slots/SlotView.kt +4 -3
  21. package/android/src/main/java/dev/apexnative/navigation/slots/SlotViews.kt +40 -0
  22. package/android/src/main/java/dev/apexnative/navigation/utils/ImageKey.kt +66 -0
  23. package/android/src/main/java/dev/apexnative/navigation/utils/ImageLoader.kt +7 -89
  24. package/android/src/main/java/dev/apexnative/navigation/utils/ImageRequests.kt +76 -0
  25. package/android/src/main/java/dev/apexnative/navigation/utils/OptionImages.kt +262 -0
  26. package/android/src/main/java/dev/apexnative/navigation/utils/ViewController.kt +3 -1
  27. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/bottomtabs/AppliedTabCalls.kt +49 -0
  28. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/bottomtabs/BottomTabsController.kt +16 -6
  29. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/bottomtabs/BottomTabsPresenter.kt +25 -24
  30. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/component/ContentReadiness.kt +1 -1
  31. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/component/RenderWait.kt +27 -7
  32. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/modal/ModalCoordinator.kt +5 -5
  33. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/navigator/Navigator.kt +4 -3
  34. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/navigator/RootCoordinator.kt +6 -3
  35. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/overlay/OverlayCoordinator.kt +8 -8
  36. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/parent/Capabilities.kt +11 -4
  37. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/sidemenu/SideMenuController.kt +1 -1
  38. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/splitview/SplitPanes.kt +23 -0
  39. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/splitview/SplitViewController.kt +58 -15
  40. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/stack/StackCommands.kt +28 -18
  41. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/stack/StackController.kt +16 -7
  42. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/stack/StackDisplay.kt +2 -1
  43. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/stack/StackQueue.kt +85 -29
  44. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/stack/StackViews.kt +2 -2
  45. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/stack/fab/FabPresenter.kt +7 -5
  46. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/stack/search/StackSearchPresenter.kt +3 -3
  47. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/stack/topbar/MenuChecks.kt +13 -56
  48. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/stack/topbar/TopBarPresenter.kt +10 -2
  49. package/android/src/main/java/dev/apexnative/navigation/viewcontrollers/viewcontroller/ViewController.kt +56 -21
  50. package/android/src/main/java/dev/apexnative/navigation/views/bottomtabs/BottomTabs.kt +4 -2
  51. package/android/src/main/java/dev/apexnative/navigation/views/stack/fab/Fab.kt +14 -10
  52. package/cli/doctor/checks/app.js +61 -13
  53. package/cli/migrate/native.js +18 -0
  54. package/cli/setup/source.js +2 -1
  55. package/cli/setup/steps/mainApplication.js +134 -19
  56. package/docs-md/architecture/transitions.md +6 -7
  57. package/docs-md/help/glossary.md +1 -1
  58. package/docs-md/help/troubleshooting.md +20 -3
  59. package/docs-md/index.json +4 -0
  60. package/docs-md/migration/from-react-native-navigation.md +8 -2
  61. package/docs-md/screen-ui/back-and-gestures.md +2 -2
  62. package/docs-md/start/installation.md +3 -1
  63. package/ios/CLAUDE.md +17 -5
  64. package/ios/CORE.md +13 -8
  65. package/ios/Contract/ANNCommandAnswer.mm +5 -20
  66. package/ios/Contract/ANNCommandContract.h +15 -0
  67. package/ios/Contract/ANNCommandContract.mm +78 -0
  68. package/ios/Core/ANNAppearanceDispatcher.mm +1 -1
  69. package/ios/Core/ANNErrorHandler.h +1 -1
  70. package/ios/Core/ANNErrorHandler.mm +3 -1
  71. package/ios/Core/ANNEventEmitter.h +1 -1
  72. package/ios/Core/ANNEventEmitter.mm +2 -2
  73. package/ios/Core/ANNJavaScriptCalls.h +34 -0
  74. package/ios/Core/ANNJavaScriptCalls.mm +61 -0
  75. package/ios/Core/ANNNavigationState.h +7 -3
  76. package/ios/Core/ANNNavigationState.mm +10 -5
  77. package/ios/Core/ANNNodeHelper.h +32 -12
  78. package/ios/Core/ANNNodeHelper.mm +100 -3
  79. package/ios/Core/ANNRenderWait.h +15 -1
  80. package/ios/Core/ANNRenderWait.mm +119 -17
  81. package/ios/Core/UIViewController+LayoutProtocol.h +9 -2
  82. package/ios/Core/UIViewController+LayoutProtocol.mm +8 -0
  83. package/ios/Layouts/Modal/ANNModalCoordinator.h +1 -1
  84. package/ios/Layouts/Modal/ANNModalCoordinator.mm +55 -14
  85. package/ios/Layouts/Screen/ANNComponentViewController.h +8 -0
  86. package/ios/Layouts/Screen/ANNComponentViewController.mm +32 -5
  87. package/ios/Layouts/Screen/ANNScreenPlace.h +41 -0
  88. package/ios/Layouts/Screen/ANNScreenPlace.mm +175 -0
  89. package/ios/Layouts/SplitView/ANNSplitViewCommands.h +3 -2
  90. package/ios/Layouts/SplitView/ANNSplitViewCommands.mm +105 -39
  91. package/ios/Layouts/SplitView/ANNSplitViewController.mm +4 -2
  92. package/ios/Layouts/Stack/ANNStackCommands.mm +1 -0
  93. package/ios/Layouts/Stack/ANNStackController.mm +26 -3
  94. package/ios/Layouts/Stack/ANNStackQueue.h +3 -1
  95. package/ios/Layouts/Stack/ANNStackQueue.mm +56 -43
  96. package/ios/Layouts/Stack/UINavigationController+ANNCommands.h +4 -2
  97. package/ios/Layouts/Stack/UINavigationController+ANNCommands.mm +2 -3
  98. package/ios/Layouts/Tabs/ANNBottomTabItemBuilder.h +4 -0
  99. package/ios/Layouts/Tabs/ANNBottomTabItemBuilder.mm +11 -0
  100. package/ios/Layouts/Tabs/ANNBottomTabsController.mm +13 -5
  101. package/ios/Layouts/Tabs/ANNBottomTabsPresenter.h +2 -2
  102. package/ios/Layouts/Tabs/ANNBottomTabsPresenter.mm +109 -7
  103. package/ios/Options/ANNOptionsCache.h +7 -6
  104. package/ios/Options/ANNSearchBarOptions.h +4 -3
  105. package/ios/Options/ANNUserChoice.h +57 -0
  106. package/ios/Options/ANNUserChoice.mm +44 -0
  107. package/ios/Options/Params/ANNImageSource.h +61 -0
  108. package/ios/Options/Params/ANNImageSource.mm +101 -0
  109. package/ios/Options/Params/ANNOptionImages.h +37 -0
  110. package/ios/Options/Params/ANNOptionImages.mm +284 -0
  111. package/ios/Options/Params/Image.h +11 -2
  112. package/ios/Options/Params/Image.mm +15 -2
  113. package/ios/Options/Params/ImageParser.h +5 -5
  114. package/ios/Options/Params/ImageParser.mm +28 -46
  115. package/ios/Public/ApexNavigation/ApexNavigation.h +1 -1
  116. package/ios/RECIPES.md +11 -5
  117. package/ios/React/Slots/ANNSlotComponentView.mm +24 -17
  118. package/ios/React/Slots/ANNSlotContainers.h +14 -6
  119. package/ios/React/Slots/ANNSlotContainers.mm +75 -2
  120. package/ios/TopBar/ANNBarButtons.h +9 -5
  121. package/ios/TopBar/ANNBarButtons.mm +211 -63
  122. package/ios/TopBar/ANNIconButtonImage.h +1 -0
  123. package/ios/TopBar/ANNIconButtonImage.mm +34 -1
  124. package/ios/TopBar/ANNLargeTitlePresenter.h +13 -0
  125. package/ios/TopBar/ANNLargeTitlePresenter.mm +26 -3
  126. package/ios/TopBar/ANNSearchPresenter.mm +170 -19
  127. package/ios/TopBar/ANNTopBarPresenter.h +5 -0
  128. package/ios/TopBar/ANNTopBarPresenter.mm +60 -11
  129. package/ios/TurboModules/ANNTurboModule.mm +39 -21
  130. package/lib/module/ARCHITECTURE.md +18 -18
  131. package/lib/module/adapters/NativeANNTurboModule.js.map +1 -1
  132. package/lib/module/api/navigation.js.map +1 -1
  133. package/lib/module/engine/commands.js +9 -7
  134. package/lib/module/engine/commands.js.map +1 -1
  135. package/lib/module/engine/layout.js +13 -9
  136. package/lib/module/engine/layout.js.map +1 -1
  137. package/lib/module/engine/options.js +44 -11
  138. package/lib/module/engine/options.js.map +1 -1
  139. package/lib/module/engine/places.js +44 -7
  140. package/lib/module/engine/places.js.map +1 -1
  141. package/lib/module/engine/props.js +30 -1
  142. package/lib/module/engine/props.js.map +1 -1
  143. package/lib/module/engine/rememberedOptions.js +1 -1
  144. package/lib/module/engine/rememberedOptions.js.map +1 -1
  145. package/lib/module/engine/screens.js +22 -12
  146. package/lib/module/engine/screens.js.map +1 -1
  147. package/lib/module/interfaces/contract/commands.json +101 -0
  148. package/lib/typescript/adapters/NativeANNTurboModule.d.ts +5 -2
  149. package/lib/typescript/adapters/NativeANNTurboModule.d.ts.map +1 -1
  150. package/lib/typescript/api/events/navigationEvents.d.ts +1 -1
  151. package/lib/typescript/api/events/navigationEvents.d.ts.map +1 -1
  152. package/lib/typescript/api/hooks/useNavigation.d.ts +1 -1
  153. package/lib/typescript/api/hooks/useNavigation.d.ts.map +1 -1
  154. package/lib/typescript/api/navigation.d.ts +4 -1
  155. package/lib/typescript/api/navigation.d.ts.map +1 -1
  156. package/lib/typescript/engine/commands.d.ts.map +1 -1
  157. package/lib/typescript/engine/options.d.ts +5 -2
  158. package/lib/typescript/engine/options.d.ts.map +1 -1
  159. package/lib/typescript/engine/places.d.ts +5 -2
  160. package/lib/typescript/engine/places.d.ts.map +1 -1
  161. package/lib/typescript/engine/props.d.ts +8 -1
  162. package/lib/typescript/engine/props.d.ts.map +1 -1
  163. package/lib/typescript/engine/rememberedOptions.d.ts.map +1 -1
  164. package/lib/typescript/engine/screens.d.ts.map +1 -1
  165. package/lib/typescript/interfaces/ComponentEvents.d.ts +2 -2
  166. package/lib/typescript/interfaces/ComponentEvents.d.ts.map +1 -1
  167. package/lib/typescript/interfaces/Events.d.ts +19 -10
  168. package/lib/typescript/interfaces/Events.d.ts.map +1 -1
  169. package/lib/typescript/interfaces/options/animations.d.ts +5 -2
  170. package/lib/typescript/interfaces/options/animations.d.ts.map +1 -1
  171. package/lib/typescript/interfaces/options/buttons.d.ts +13 -5
  172. package/lib/typescript/interfaces/options/buttons.d.ts.map +1 -1
  173. package/lib/typescript/interfaces/options/topBar.d.ts +22 -9
  174. package/lib/typescript/interfaces/options/topBar.d.ts.map +1 -1
  175. package/package.json +1 -1
  176. package/src/ARCHITECTURE.md +18 -18
  177. package/src/adapters/NativeANNTurboModule.ts +5 -2
  178. package/src/api/events/navigationEvents.ts +1 -1
  179. package/src/api/hooks/useNavigation.ts +1 -1
  180. package/src/api/navigation.ts +4 -1
  181. package/src/engine/commands.ts +19 -7
  182. package/src/engine/layout.ts +16 -9
  183. package/src/engine/options.ts +39 -11
  184. package/src/engine/places.ts +56 -7
  185. package/src/engine/props.ts +30 -1
  186. package/src/engine/rememberedOptions.ts +4 -1
  187. package/src/engine/screens.tsx +23 -13
  188. package/src/interfaces/ComponentEvents.ts +2 -2
  189. package/src/interfaces/Events.ts +19 -10
  190. package/src/interfaces/contract/commands.json +101 -0
  191. package/src/interfaces/options/animations.ts +5 -2
  192. package/src/interfaces/options/buttons.ts +13 -5
  193. package/src/interfaces/options/topBar.ts +22 -9
@@ -211,7 +211,7 @@ class NavigationTurboModule(reactContext: ReactApplicationContext) : NativeANNTu
211
211
  layout: ReadableMap,
212
212
  promise: Promise,
213
213
  ) {
214
- // Android has one column to show screens in: the secondary pane (`showInColumn`).
214
+ // Android's split view has the primary and the secondary pane; another column rejects (`showInColumn`).
215
215
  val what = "show the screen '${LayoutReader.idOf(layout)}' in the '$column' column"
216
216
  runner.run(
217
217
  name = "showInColumn",
@@ -219,7 +219,7 @@ class NavigationTurboModule(reactContext: ReactApplicationContext) : NativeANNTu
219
219
  what = what,
220
220
  promise = promise,
221
221
  check = { LayoutReader.node(layout, context = appContext()) },
222
- ) { node, answer -> navigator().stackCommands.showInColumn(componentId, node, answer, what) }
222
+ ) { node, answer -> navigator().stackCommands.showInColumn(componentId, column, node, answer, what) }
223
223
  }
224
224
 
225
225
  override fun setColumnShown(
@@ -295,9 +295,10 @@ class NavigationTurboModule(reactContext: ReactApplicationContext) : NativeANNTu
295
295
  })
296
296
  }
297
297
 
298
- // The navigation of the activity the command runs in; the runner checked there is one.
298
+ // The navigation of the activity the command runs in; the runner checked there is an activity. One without its
299
+ // navigation yet (or any more) has nowhere to show screens either: the command rejects with the runner's own text.
299
300
  private fun navigator(): Navigator =
300
- requireNotNull(Navigators.current()?.navigator) { "the activity has no navigation" }
301
+ Navigators.current()?.navigator ?: throw LayoutException(ErrorTexts.nowhereToShow())
301
302
 
302
303
  // The app's context, which the colors of the options resolve platform colors with.
303
304
  private fun appContext(): Context = reactApplicationContext.applicationContext
@@ -0,0 +1,48 @@
1
+ package dev.apexnative.navigation.react
2
+
3
+ import dev.apexnative.navigation.NavigationApplication
4
+ import dev.apexnative.navigation.utils.UiThread
5
+
6
+ /**
7
+ * The calls JavaScript made to the module that may not have reached the main thread yet: [after] runs a task on the
8
+ * main thread behind every one of them. A screen's first mount reaches the main thread from Fabric while a
9
+ * `mergeOptions` of the same React commit (its layout effects: `useScreenOptions`) may still be on its way: an
10
+ * asynchronous method goes from the JavaScript thread to the module's thread (`BridgelessNativeMethodCallInvoker`),
11
+ * which reads its options there and posts its body to the main thread ([CommandRunner]). The task goes the same way: a
12
+ * turn of the JavaScript thread, which comes after the task of the commit with its layout effects; then the module's
13
+ * thread, behind the calls that task made; then the main thread, behind their bodies. Each of the three runs its queue
14
+ * in order, so every call JavaScript made before the mount has applied when the task runs.
15
+ *
16
+ * Without JavaScript (none yet, a reload, a queue that ended) the task is posted to the main thread at once: nothing is
17
+ * on its way. A queue that ends while the task is on it drops it, and a wait with no limit of its own (a push with
18
+ * `waitForRender: true` whose content has mounted: the task is its only end) would not end; a queue ends only as the
19
+ * JavaScript goes (a reload, the host destroyed), and then the layouts go with it (`ReloadCoordinator`), the stack
20
+ * and its waiting push too. Main thread.
21
+ */
22
+ internal object PendingCalls {
23
+ /** A queue the task goes through: it runs what it takes in order, and says false when it ended and took nothing. */
24
+ fun interface Queue {
25
+ fun run(task: Runnable): Boolean
26
+ }
27
+
28
+ /** Runs [task] on the main thread, on a later turn, after every call JavaScript made so far has applied there. */
29
+ fun after(task: () -> Unit) {
30
+ val context = NavigationApplication.instanceOrNull?.reactGatewayOrNull?.currentReactContext
31
+ ?.takeIf { it.hasActiveReactInstance() }
32
+ val js = context?.jsMessageQueueThread?.let { Queue(it::runOnQueue) }
33
+ val module = context?.nativeModulesMessageQueueThread?.let { Queue(it::runOnQueue) }
34
+ after(js, module, UiThread::post, task)
35
+ }
36
+
37
+ /**
38
+ * The way of [after] over the queues given: [js], then [module], then [main]; straight to [main] without one of
39
+ * them, or from where a queue refused it. The unit tests run it over their own queues.
40
+ */
41
+ fun after(js: Queue?, module: Queue?, main: (Runnable) -> Unit, task: () -> Unit) {
42
+ val onMain = Runnable(task)
43
+ if (js == null || module == null) return main(onMain)
44
+ val toMain = Runnable { main(onMain) }
45
+ val toModule = Runnable { if (!module.run(toMain)) main(onMain) }
46
+ if (!js.run(toModule)) main(onMain)
47
+ }
48
+ }
@@ -391,12 +391,14 @@ internal object AppearanceDispatcher {
391
391
 
392
392
  /**
393
393
  * [modal] went with [count] modals (EVT-ModalDismissed): `ANN.ModalDismissed` with the id and the name of the
394
- * screen the modal shows, down its current children.
394
+ * screen the modal shows, down its current children ([modalScreen]); a modal that shows no screen gives its own id
395
+ * and no name.
395
396
  */
396
397
  fun modalDismissed(modal: ViewController<*>, count: Int) {
397
398
  if (!ReloadCoordinator.isCurrent(modal.jsGeneration)) return
398
- val screen = visibleNode(modal)
399
- sender.modalDismissed(screen.id, screen.currentComponentName.orEmpty(), count)
399
+ val named = modalScreen(modal)
400
+ val name = if (named is ParentController<*>) "" else named.currentComponentName.orEmpty()
401
+ sender.modalDismissed(named.id, name, count)
400
402
  }
401
403
 
402
404
  /**
@@ -449,7 +451,7 @@ internal object AppearanceDispatcher {
449
451
  * modal shows. Nothing for a JavaScript that went.
450
452
  */
451
453
  fun modalAttemptedToDismiss(modal: ViewController<*>) {
452
- val screen = visibleNode(modal)
454
+ val screen = modalScreen(modal)
453
455
  if (ReloadCoordinator.isCurrent(screen.jsGeneration)) sender.modalAttemptedToDismiss(screen.id)
454
456
  }
455
457
 
@@ -458,18 +460,26 @@ internal object AppearanceDispatcher {
458
460
  * Nothing for a JavaScript that went.
459
461
  */
460
462
  fun sheetDetentChanged(modal: ViewController<*>, detent: Any) {
461
- val screen = visibleNode(modal)
463
+ val screen = modalScreen(modal)
462
464
  if (ReloadCoordinator.isCurrent(screen.jsGeneration)) sender.sheetDetentChanged(screen.id, detent)
463
465
  }
464
466
 
465
467
  /**
466
468
  * The screen [node] shows: the node at the end of its current children (the one "visible screen" of the core): what
467
- * `ANN.ModalDismissed`, `ANN.ModalAttemptedToDismiss`, `ANN.SheetDetentChanged` and back's
468
- * `ANN.NavigationButtonPressed` of a modal name. A layout without a child answers itself.
469
+ * back's `ANN.NavigationButtonPressed` of a modal names, and, through [modalScreen], its `ANN.ModalDismissed`,
470
+ * `ANN.ModalAttemptedToDismiss` and `ANN.SheetDetentChanged`. A layout without a child answers itself.
469
471
  */
470
472
  fun visibleNode(node: ViewController<*>): ViewController<*> {
471
473
  var current = node
472
474
  while (current is ParentController<*>) current = current.currentChild ?: break
473
475
  return current
474
476
  }
477
+
478
+ /**
479
+ * The node the events of the modal [modal] (its root) name: its [visibleNode] when that is a screen; the root
480
+ * itself when the chain ends in a layout without a child, as on iOS. Asked again of what it gave, it gives the
481
+ * same.
482
+ */
483
+ fun modalScreen(modal: ViewController<*>): ViewController<*> =
484
+ visibleNode(modal).takeUnless { it is ParentController<*> } ?: modal
475
485
  }
@@ -9,14 +9,15 @@ interface SlotContainer {
9
9
  }
10
10
 
11
11
  /**
12
- * The native containers the slots of the app's React tree show their children in, by slot id. A destroyed container
13
- * drops out on its own.
12
+ * The native containers the slots of the app's React tree show their children in, by slot id, and the slot views by
13
+ * the same id: a change of a container goes straight to the views of its id. A destroyed container and a collected
14
+ * view drop out on their own.
14
15
  */
15
16
  object SlotContainers {
16
17
  private class Entry(val view: WeakReference<ViewGroup>, val passThrough: Boolean)
17
18
 
18
19
  private val containers = HashMap<String, Entry>()
19
- private val slots = LinkedHashSet<SlotView>()
20
+ private val slots = SlotViews<SlotView>()
20
21
 
21
22
  /** `passThrough`: touches outside the slot's content go to the screens below, as in an overlay for a toast. */
22
23
  fun put(slotId: String, container: ViewGroup, passThrough: Boolean = false) {
@@ -33,12 +34,14 @@ object SlotContainers {
33
34
  changed(slotId)
34
35
  }
35
36
 
36
- /** The container of a slot appeared or changed its size. */
37
+ /** The container of a slot appeared or changed its size: the slot views under its id, and only they, hear it. */
37
38
  fun changed(slotId: String) {
38
- slots.toList().forEach { it.containerChanged(slotId) }
39
+ slots.views(slotId).forEach { it.containerChanged(slotId) }
39
40
  }
40
41
 
41
- internal fun listen(slot: SlotView) = slots.add(slot)
42
+ /** The slot view goes under its slot id, and out from under the id it had. Main thread only. */
43
+ internal fun register(slotId: String, slot: SlotView) = slots.register(slotId, slot)
42
44
 
43
- internal fun stopListening(slot: SlotView) = slots.remove(slot)
45
+ /** The slot view unmounted or lost its slot id: no container change reaches it. Main thread only. */
46
+ internal fun unregister(slot: SlotView) = slots.unregister(slot)
44
47
  }
@@ -86,7 +86,6 @@ class SlotView(reactContext: ThemedReactContext) : ReactViewGroup(reactContext)
86
86
  init {
87
87
  content.visibility = INVISIBLE
88
88
  addView(content)
89
- SlotContainers.listen(this)
90
89
  }
91
90
 
92
91
  override fun setId(id: Int) {
@@ -100,7 +99,9 @@ class SlotView(reactContext: ThemedReactContext) : ReactViewGroup(reactContext)
100
99
  }
101
100
 
102
101
  fun attachContent() {
103
- val id = slotId ?: return
102
+ // The view hears the changes of the container under the id it has now, and no other.
103
+ val id = slotId ?: return SlotContainers.unregister(this)
104
+ SlotContainers.register(id, this)
104
105
  val container = SlotContainers.get(id)
105
106
  val target: ViewGroup = container ?: this
106
107
  if (content.parent !== target) {
@@ -135,7 +136,7 @@ class SlotView(reactContext: ThemedReactContext) : ReactViewGroup(reactContext)
135
136
  }
136
137
 
137
138
  fun drop() {
138
- SlotContainers.stopListening(this)
139
+ SlotContainers.unregister(this)
139
140
  (content.parent as? ViewGroup)?.removeView(content)
140
141
  }
141
142
  }
@@ -0,0 +1,40 @@
1
+ package dev.apexnative.navigation.slots
2
+
3
+ import java.lang.ref.WeakReference
4
+ import java.util.WeakHashMap
5
+
6
+ /**
7
+ * The slot views by their slot id, held weakly, so a change of a container reaches only the views of its id. A view is
8
+ * under one id at a time: registering it under a new one (its `nativeID` changed, or Fabric reused it) takes it out
9
+ * from under the old one. Two live views under one id both stay, in the order they came, and both hear the change, as
10
+ * every view with that id did when the change went to all of them. Main thread only.
11
+ */
12
+ internal class SlotViews<T : Any> {
13
+ private val byId = HashMap<String, MutableList<WeakReference<T>>>()
14
+ private val idOf = WeakHashMap<T, String>()
15
+
16
+ fun register(slotId: String, view: T) {
17
+ if (idOf[view] == slotId) return
18
+ unregister(view)
19
+ idOf[view] = slotId
20
+ byId.getOrPut(slotId) { ArrayList(1) }.add(WeakReference(view))
21
+ }
22
+
23
+ fun unregister(view: T) {
24
+ val slotId = idOf.remove(view) ?: return
25
+ prune(slotId) { it === view }
26
+ }
27
+
28
+ /** The live views under [slotId], a copy: a view may register or leave while it hears the change. */
29
+ fun views(slotId: String): List<T> {
30
+ prune(slotId) { false }
31
+ return byId[slotId]?.mapNotNull { it.get() }.orEmpty()
32
+ }
33
+
34
+ // Drops the collected views under the id and the ones `drop` picks; an id with no views left goes.
35
+ private fun prune(slotId: String, drop: (T) -> Boolean) {
36
+ val views = byId[slotId] ?: return
37
+ views.removeAll { reference -> reference.get()?.let(drop) ?: true }
38
+ if (views.isEmpty()) byId.remove(slotId)
39
+ }
40
+ }
@@ -0,0 +1,66 @@
1
+ package dev.apexnative.navigation.utils
2
+
3
+ import android.content.Context
4
+ import android.content.res.Configuration
5
+ import android.view.View
6
+ import java.io.File
7
+ import java.net.URI
8
+ import java.net.URISyntaxException
9
+
10
+ /**
11
+ * What an image of the options is cached by: its [uri] and what changes the decoded result for the same uri, the
12
+ * screen's density (a drawable of the app picks its density's file, a decoded file is scaled to it), light or dark
13
+ * (`drawable-night`) and the layout direction (`drawable-ldrtl`, auto-mirrored drawables). For a `file:` uri also the
14
+ * file's [modified] time and [size]: a file the app writes again at the same path is another image. The loader bakes
15
+ * no tint and no size into an image, so neither is part of it.
16
+ */
17
+ internal data class ImageKey(
18
+ val uri: String,
19
+ val densityDpi: Int,
20
+ val night: Boolean,
21
+ val rightToLeft: Boolean,
22
+ /** When the file of a `file:` uri last changed, in milliseconds; 0 for any other uri or a file that isn't there. */
23
+ val modified: Long = 0,
24
+ /** The bytes of the file of a `file:` uri; 0 for any other uri or a file that isn't there. */
25
+ val size: Long = 0,
26
+ ) {
27
+ companion object {
28
+ private const val FILE_SCHEME = "file:"
29
+ private const val FILE_PREFIX = "file://"
30
+
31
+ /** The key of [uri] in the current configuration of [context]. Any thread. */
32
+ fun of(context: Context, uri: String): ImageKey {
33
+ val configuration = context.resources.configuration
34
+ return of(uri, configuration.densityDpi, configuration.uiMode, configuration.layoutDirection)
35
+ }
36
+
37
+ /**
38
+ * The key of [uri] for a configuration's density, `uiMode` (only its night bits count) and layout direction;
39
+ * a `file:` uri reads its file's modification time and size. Any thread.
40
+ */
41
+ fun of(uri: String, densityDpi: Int, uiMode: Int, layoutDirection: Int): ImageKey {
42
+ val file = filePath(uri)?.let(::File)
43
+ return ImageKey(
44
+ uri = uri,
45
+ densityDpi = densityDpi,
46
+ night = uiMode and Configuration.UI_MODE_NIGHT_MASK == Configuration.UI_MODE_NIGHT_YES,
47
+ rightToLeft = layoutDirection == View.LAYOUT_DIRECTION_RTL,
48
+ // Both 0 when the file isn't there or can't be read.
49
+ modified = file?.lastModified() ?: 0,
50
+ size = file?.length() ?: 0,
51
+ )
52
+ }
53
+
54
+ // The path of a `file:` uri, decoded as the loader's `Uri.getPath` decodes it; none for any other uri. A uri
55
+ // that `URI` can't parse (an unescaped space) is taken as it is after its scheme.
56
+ private fun filePath(uri: String): String? {
57
+ if (!uri.startsWith(FILE_SCHEME)) return null
58
+ val path = try {
59
+ URI(uri).path
60
+ } catch (e: URISyntaxException) {
61
+ null
62
+ }
63
+ return path ?: uri.removePrefix(FILE_PREFIX).removePrefix(FILE_SCHEME).ifEmpty { null }
64
+ }
65
+ }
66
+ }
@@ -1,19 +1,8 @@
1
1
  package dev.apexnative.navigation.utils
2
2
 
3
3
  import android.content.Context
4
- import android.content.res.Resources
5
- import android.graphics.BitmapFactory
6
- import android.graphics.drawable.BitmapDrawable
7
4
  import android.graphics.drawable.Drawable
8
- import android.net.Uri
9
- import android.os.StrictMode
10
- import androidx.core.content.ContextCompat
11
- import com.facebook.react.views.imagehelper.ResourceDrawableIdHelper
12
- import java.io.FileInputStream
13
- import java.io.FileNotFoundException
14
5
  import java.io.IOException
15
- import java.io.InputStream
16
- import java.net.URL
17
6
 
18
7
  /** What an image loads into: [onComplete] with the image, or [onError] when it can't load. Main thread. */
19
8
  open class ImageLoadingListenerAdapter {
@@ -23,90 +12,19 @@ open class ImageLoadingListenerAdapter {
23
12
  }
24
13
 
25
14
  /**
26
- * Loads the images and icons of the options when they apply: synchronously on the main thread, without a cache, each
27
- * image its own mutable copy. A `file:` address is a file; anything else first a drawable of the app by name (as React
28
- * Native's images by resource name: release builds' assets, `{ android }` icons). Not found while the app is in
29
- * development: the address is read as a stream, Metro's asset over the network (network on the main thread allowed for
30
- * that read only) or through the content resolver. A remote address that is not Metro's asset is never loaded. A
31
- * failure never ends the app: the listener's [ImageLoadingListenerAdapter.onError], the log, and React Native's red
32
- * screen in development.
15
+ * Loads an image or an icon of the options at once, through the shared cache of [OptionImages]: each image its own
16
+ * mutable copy. For the places whose first frame needs the image (the top bar's buttons, the back button, menus, search
17
+ * suggestions); a place that can show it when ready asks through [ImageRequests]. A failure never ends the app: the
18
+ * listener's [ImageLoadingListenerAdapter.onError], and one warning per address.
33
19
  */
34
20
  class ImageLoader {
35
21
  /** Loads [uri] and gives it to [listener] before returning. Main thread. */
36
22
  fun loadIcon(context: Context, uri: String, listener: ImageLoadingListenerAdapter) {
37
- val drawable = try {
38
- load(context, uri)
39
- } catch (e: Exception) {
40
- val reason = if (e is LoadFailure) "" else ": ${e.message ?: e.javaClass.simpleName}"
41
- DevelopmentLog.error("Could not load image $uri$reason${(e as? LoadFailure)?.why.orEmpty()}", e)
42
- listener.onError(e)
23
+ val drawable = OptionImages.load(context, uri)
24
+ if (drawable == null) {
25
+ listener.onError(IOException("Could not load image $uri"))
43
26
  return
44
27
  }
45
28
  listener.onComplete(drawable)
46
29
  }
47
-
48
- private fun load(context: Context, uri: String): Drawable {
49
- if (uri.startsWith(FILE_SCHEME)) return decode(context) { FileInputStream(Uri.parse(uri).path) }
50
- resourceDrawable(context, uri)?.let { return it }
51
- if (!DevelopmentLog.isDevelopment) throw LoadFailure("")
52
- val address = Uri.parse(uri)
53
- return when (address.scheme) {
54
- "http", "https" -> {
55
- if (!isMetroAsset(address)) throw LoadFailure(": remote images are not supported in options")
56
- withNetworkOnMainThread { decode(context) { URL(uri).openStream() } }
57
- }
58
- else -> decode(context) {
59
- context.contentResolver.openInputStream(address) ?: throw FileNotFoundException(uri)
60
- }
61
- }
62
- }
63
-
64
- // A drawable of the app by name, its own copy; none when the app has none by that name.
65
- private fun resourceDrawable(context: Context, name: String): Drawable? {
66
- val id = ResourceDrawableIdHelper.getResourceDrawableId(context, name)
67
- if (id <= 0) return null
68
- return try {
69
- ContextCompat.getDrawable(context, id)?.mutate()
70
- } catch (e: Resources.NotFoundException) {
71
- null
72
- }
73
- }
74
-
75
- // The image of the stream [open] gives, closed after the read, also on an error; one it can't decode fails, and so
76
- // does one too large for the memory: that is a load error too, never the end of the app.
77
- private fun decode(context: Context, open: () -> InputStream): Drawable {
78
- val decoded = try {
79
- open().use { BitmapFactory.decodeStream(it) }
80
- } catch (e: OutOfMemoryError) {
81
- throw LoadFailure(": it is too large to decode")
82
- }
83
- val bitmap = decoded ?: throw LoadFailure(": it can't be decoded")
84
- return BitmapDrawable(context.resources, bitmap)
85
- }
86
-
87
- // A failure the loader names itself: `Could not load image <source>`, then [why].
88
- private class LoadFailure(val why: String) : IOException("Could not load image$why")
89
-
90
- // Metro's asset: an address of the server the JavaScript bundle came from, under `/assets/`.
91
- private fun isMetroAsset(address: Uri): Boolean {
92
- val bundle = DevelopmentLog.bundleUrl?.let(Uri::parse) ?: return false
93
- return address.host == bundle.host && address.port == bundle.port &&
94
- address.path.orEmpty().startsWith(ASSETS_PATH)
95
- }
96
-
97
- // Reading from the network on the main thread for [read] only; the thread's policy comes back, also on an error.
98
- private fun <T> withNetworkOnMainThread(read: () -> T): T {
99
- val policy = StrictMode.getThreadPolicy()
100
- StrictMode.setThreadPolicy(StrictMode.ThreadPolicy.Builder(policy).permitNetwork().build())
101
- try {
102
- return read()
103
- } finally {
104
- StrictMode.setThreadPolicy(policy)
105
- }
106
- }
107
-
108
- private companion object {
109
- const val FILE_SCHEME = "file:"
110
- const val ASSETS_PATH = "/assets/"
111
- }
112
30
  }
@@ -0,0 +1,76 @@
1
+ package dev.apexnative.navigation.utils
2
+
3
+ import android.graphics.drawable.Drawable
4
+
5
+ /** Where the images of the options come from, for one context: [OptionImages.source], or a fake in the tests. */
6
+ internal interface ImageSource {
7
+ /** The key of [uri] in the context's configuration now; for a `file:` uri it reads the file's version. */
8
+ fun key(uri: String): ImageKey
9
+
10
+ /** A copy of the image of [key] when the cache has it, none otherwise; never loads. */
11
+ fun cached(key: ImageKey): Drawable?
12
+
13
+ /** A copy of the image of [key], from the cache or loaded on the caller's thread; none on a failure. */
14
+ fun now(key: ImageKey): Drawable?
15
+
16
+ /** Whether [uri] can load off the main thread: a file or a drawable of the app. */
17
+ fun loadsLater(uri: String): Boolean
18
+
19
+ /** Loads [key] off the main thread and gives a copy of it, or none on a failure, to [done] on the main thread. */
20
+ fun later(key: ImageKey, done: (Drawable?) -> Unit)
21
+ }
22
+
23
+ /**
24
+ * The images that the places of a view asked for (an item's icon, a button's icon), by place [P]: what the cache has
25
+ * comes at once; what it doesn't, when the place can show it later, loads off the main thread and comes when ready;
26
+ * everything else loads at once. The last request of a place wins: a load that arrives after the place asked again,
27
+ * or after [forget] or [clear], is dropped. A load that fails gives the place no image, as an option without one:
28
+ * the image it showed before doesn't stay. A request builds the key of its image once (a file's version is read once).
29
+ * Main thread only.
30
+ */
31
+ internal class ImageRequests<P : Any>(private val source: ImageSource) {
32
+ // The open load of each place: its number, which a later request of the place replaces.
33
+ private val waiting = HashMap<P, Long>()
34
+ private var last = 0L
35
+
36
+ /** The key of [uri] now, for a caller that compares it before it asks ([request] with the key). */
37
+ fun key(uri: String): ImageKey = source.key(uri)
38
+
39
+ /** [request] for the image of [uri], its key built here. */
40
+ fun request(place: P, uri: String, mayWait: Boolean, show: (Drawable?) -> Unit) =
41
+ request(place, source.key(uri), mayWait, show)
42
+
43
+ /**
44
+ * Gives [show] the image of [key] for [place]: now when the cache has it, or when the place can't wait for it
45
+ * ([mayWait] false: the first frame needs it) or the uri can't load off the main thread (Metro's address, a content
46
+ * uri); otherwise later, unless [place] asks again first. Null when it doesn't load: the place shows none.
47
+ */
48
+ fun request(place: P, key: ImageKey, mayWait: Boolean, show: (Drawable?) -> Unit) {
49
+ waiting.remove(place)
50
+ source.cached(key)?.let {
51
+ show(it)
52
+ return
53
+ }
54
+ if (!mayWait || !source.loadsLater(key.uri)) {
55
+ show(source.now(key))
56
+ return
57
+ }
58
+ val number = ++last
59
+ waiting[place] = number
60
+ source.later(key) { image ->
61
+ if (waiting[place] != number) return@later
62
+ waiting.remove(place)
63
+ show(image)
64
+ }
65
+ }
66
+
67
+ /** The place shows no image of the options now: a load it asked for earlier is dropped. */
68
+ fun forget(place: P) {
69
+ waiting.remove(place)
70
+ }
71
+
72
+ /** The view goes: every open load is dropped. */
73
+ fun clear() {
74
+ waiting.clear()
75
+ }
76
+ }