@blotch/angular-lynx-native 22.2.23 → 22.2.24
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.
- package/README.md +1 -1
- package/package.json +1 -1
- package/templates/android/README.md +4 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellApplication.kt +24 -0
- package/templates/android/app/src/main/java/dev/angularlynx/shell/ShellModule.kt +20 -0
- package/templates/ios/App/AppDelegate.swift +13 -0
- package/templates/ios/App/LynxViewController.swift +3 -0
- package/templates/ios/App/ShellModule.swift +42 -0
package/README.md
CHANGED
|
@@ -21,7 +21,7 @@ Both are one full-screen `LynxView` running `main.lynx.bundle`; Angular Router d
|
|
|
21
21
|
|
|
22
22
|
Both send the same global props, which the runtime's services read: `platform`, `theme` (again whenever the phone switches between light and dark), `appLocale` (the phone's language as a BCP 47 tag), `appTimeZone` (its time zone's IANA name, which the `intl` build option's `Intl.DateTimeFormat` uses), `isNotchScreen`, `edgeToEdge`, the safe-area insets `safeAreaTop`, `safeAreaBottom`, `safeAreaLeft` and `safeAreaRight` (again whenever they change), `osVersion` (which Lynx leaves out of the main thread's `SystemInfo`), and the accessibility settings `reduceMotion`, `reduceTransparency` (iOS only) and `increaseContrast`, again whenever the user changes one. Android 12 and later also send `dynamicColors`, the wallpaper's Material 3 color roles in light and dark (`DynamicColors.kt`), again when the user changes their wallpaper or colors. Lynx Explorer sends `theme` and `isNotchScreen` too, but no `appLocale`, `appTimeZone`, `osVersion` or accessibility settings. See [`templates/android/README.md`](templates/android/README.md) for the Android shell's details.
|
|
23
23
|
|
|
24
|
-
Both register the native modules the runtime's services call, which Lynx Explorer doesn't have: `AngularLynxWebAuth` (`WebAuthModule.swift`, `WebAuthModule.kt`) opens `LynxWebAuthSession`'s sign-in page, in `ASWebAuthenticationSession` on iOS and a Custom Tab on Android, and returns the URL it redirects to. `AngularLynxLocalStorage` (`LocalStorageModule.swift`, `LocalStorageModule.kt`) keeps `LynxLocalStorage`'s values in a `UserDefaults` suite and a `SharedPreferences` file of their own. `AngularLynxSecureStorage` (`SecureStorageModule.swift`, `SecureStorageModule.kt`) keeps `LynxSecureStorage`'s in the Keychain on iOS and, on Android, in `SharedPreferences`, each value encrypted with AES-GCM by an Android Keystore key, a file Auto Backup leaves out (`res/xml/backup_rules.xml`, `data_extraction_rules.xml`). `AngularLynxLinking` (`LinkingModule.swift`, `LinkingModule.kt`) opens `LynxLinking`'s links: `openURL` with `UIApplication.open` and an `ACTION_VIEW` intent, `openInApp` in `SFSafariViewController` and a Custom Tab, and `canOpenURL` with `UIApplication.canOpenURL` and `resolveActivity`, which answer only for the schemes the generator writes from angular.json's `canOpenURLSchemes` (with `http`, `https`, `mailto`, `tel` and `sms`) into Info.plist's `LSApplicationQueriesSchemes` and the manifest's `<queries>`. `AngularLynxImagePicker` (`ImagePickerModule.swift`, `ImagePickerModule.kt`) lets the person pick a photo for `LynxImagePicker`, with `PHPickerViewController` on iOS and the Photo Picker on Android, neither of which needs a permission, and calls back with it as base64, re-encoded as an upright JPEG when the app asks for a smaller one or the photo is a format such as HEIC. `AngularLynxShare` (`ShareModule.swift`, `ShareModule.kt`) opens `LynxShare`'s share sheet, `UIActivityViewController` on iOS and an `ACTION_SEND` chooser on Android, with the files written to temporary files on iOS and shared through a `FileProvider` on Android. Save Image on iOS needs `NSPhotoLibraryAddUsageDescription`, which the generator always writes (angular.json's `permissions.savePhotos`, with a default).
|
|
24
|
+
Both register the native modules the runtime's services call, which Lynx Explorer doesn't have: `AngularLynxWebAuth` (`WebAuthModule.swift`, `WebAuthModule.kt`) opens `LynxWebAuthSession`'s sign-in page, in `ASWebAuthenticationSession` on iOS and a Custom Tab on Android, and returns the URL it redirects to. `AngularLynxLocalStorage` (`LocalStorageModule.swift`, `LocalStorageModule.kt`) keeps `LynxLocalStorage`'s values in a `UserDefaults` suite and a `SharedPreferences` file of their own. `AngularLynxSecureStorage` (`SecureStorageModule.swift`, `SecureStorageModule.kt`) keeps `LynxSecureStorage`'s in the Keychain on iOS and, on Android, in `SharedPreferences`, each value encrypted with AES-GCM by an Android Keystore key, a file Auto Backup leaves out (`res/xml/backup_rules.xml`, `data_extraction_rules.xml`). `AngularLynxLinking` (`LinkingModule.swift`, `LinkingModule.kt`) opens `LynxLinking`'s links: `openURL` with `UIApplication.open` and an `ACTION_VIEW` intent, `openInApp` in `SFSafariViewController` and a Custom Tab, and `canOpenURL` with `UIApplication.canOpenURL` and `resolveActivity`, which answer only for the schemes the generator writes from angular.json's `canOpenURLSchemes` (with `http`, `https`, `mailto`, `tel` and `sms`) into Info.plist's `LSApplicationQueriesSchemes` and the manifest's `<queries>`. `AngularLynxImagePicker` (`ImagePickerModule.swift`, `ImagePickerModule.kt`) lets the person pick a photo for `LynxImagePicker`, with `PHPickerViewController` on iOS and the Photo Picker on Android, neither of which needs a permission, and calls back with it as base64, re-encoded as an upright JPEG when the app asks for a smaller one or the photo is a format such as HEIC. `AngularLynxShare` (`ShareModule.swift`, `ShareModule.kt`) opens `LynxShare`'s share sheet, `UIActivityViewController` on iOS and an `ACTION_SEND` chooser on Android, with the files written to temporary files on iOS and shared through a `FileProvider` on Android. Save Image on iOS needs `NSPhotoLibraryAddUsageDescription`, which the generator always writes (angular.json's `permissions.savePhotos`, with a default). `AngularLynxShell` (`ShellModule.swift`, `ShellModule.kt`) has `collectGarbage`, which the runtime calls once the app has released about a screen's worth of elements and settled: it reports moderate memory pressure with `LynxEnv.trimMemory`, which makes Lynx run a full collection of the main thread's JavaScript heap, freeing what PrimJS's routine collections leave behind of the screens that went. On Android it also connects the back button. Both shells report the system's memory warnings to `trimMemory` too, and leaving the screen.
|
|
25
25
|
|
|
26
26
|
Only the development build (`ng serve --platform`) loads from a dev server: the code is in `App/DevServer.swift` and `App/DevServerModules.swift` under `#if DEBUG`, and in the debug source set on Android (`app/src/debug`, with a no-op `DevServer` in `app/src/release`), so a release build can't be pointed anywhere else. `DevServer` reads the URL from the launch argument or intent extra and keeps it (`UserDefaults`, `SharedPreferences`), so the app opened from the home screen loads from the same server. The loaders (`BundleResourceFetcher.swift`, `ResourceFetchers.kt`) and `AngularLynxAssets` then fetch every relative URL, and on Android every URL on the address the bundle was built with, from the dev server instead of the app, uncached. When the bundle itself doesn't load, `DevServer` covers the page with a message and loads it again every two seconds. rspeedy's live reload client talks to Lynx DevTool, which the shells don't have: `NativeModules.LynxWebSocketModule` (through `@lynx-js/websocket`) for the dev server's socket and `NativeModules.LynxDevToolSetModule.invokeCdp` for a full reload (a DevTool protocol `Page.reload`). The development build registers modules under those names (`DevWebSocketModule`, `DevReloadModule`): a WebSocket with `URLSessionWebSocketTask` and OkHttp that sends the same `websocketOpen`, `websocketMessage`, `websocketClosed` and `websocketFailed` global events as Lynx DevTool's, and a `Page.reload` that loads the bundle again, as DevTool's `LynxPageReloadHelper` does. URLSession sends an empty `Sec-WebSocket-Protocol` header for an empty protocol list, which the dev server refuses, so the iOS socket passes none then. On Android, the debug build's `network_security_config.xml` allows plain HTTP to any host, since the app's own `fetch()` of an imported file goes to the address the bundle was built with; the release build's allows it to `localhost` alone.
|
|
27
27
|
|
package/package.json
CHANGED
|
@@ -34,6 +34,10 @@ Build commands and outputs (no flavors, so the names are fixed):
|
|
|
34
34
|
|
|
35
35
|
Back is off by default, so it leaves the app like any root screen. The runtime turns it on while the Router has history by calling `NativeModules.AngularLynxShell.setBackHandlerEnabled(true)`, and off again on the first screen (`LynxBackButton` in `packages/runtime/src/lib/router/lynx-back-button.ts`, which `provideRouter()` starts; the call goes through the background thread, where `NativeModules` lives). While it's on, a back press sends the global event `angularLynxBack` instead, which the runtime turns into `Location.back()`. The app has to say beforehand because Android decides synchronously, at the start of the gesture, whether an app handles back (predictive back animates differently). Layer 6's `native/back-button.yaml` checks both ends.
|
|
36
36
|
|
|
37
|
+
## Memory
|
|
38
|
+
|
|
39
|
+
PrimJS, which runs the app's JavaScript on Lynx's main thread, collects garbage as the app runs, but those collections leave behind much of what a screen leaves when it goes, so native memory grows with every screen the app shows. Lynx runs a full collection, which frees it, when the host reports memory pressure with `LynxEnv.trimMemory`. The runtime asks for one through `NativeModules.AngularLynxShell.collectGarbage()` (`ShellModule.kt`, which reports moderate pressure) once the app has released about a screen's worth of elements and settled (`collect-garbage.ts` in `packages/runtime`), and `ShellApplication.kt` reports the system's `onTrimMemory` and `onLowMemory`, including the app leaving the screen.
|
|
40
|
+
|
|
37
41
|
## Web sign-in
|
|
38
42
|
|
|
39
43
|
`NativeModules.AngularLynxWebAuth.start(request, callback)` (`WebAuthModule.kt`, behind the runtime's `LynxWebAuthSession`) opens `request.url` in a Custom Tab (`androidx.browser`). The page's redirect to `request.callbackScheme` comes back as a deep link: the activity is `singleTask`, so the intent reaches the open one in `onNewIntent`, which hands the URL to the callback, and starting it closes the Custom Tab above it. That's why the scheme has to be in the app's `deepLinkSchemes`. Android says nothing when a Custom Tab is closed, so the activity resuming with a session still open, after having gone behind the tab, means the person canceled. iOS's `WebAuthModule.swift` reports the same results through `ASWebAuthenticationSession`.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
package dev.angularlynx.shell
|
|
2
2
|
|
|
3
3
|
import android.app.Application
|
|
4
|
+
import android.content.ComponentCallbacks2
|
|
4
5
|
import com.facebook.drawee.backends.pipeline.Fresco
|
|
5
6
|
import com.facebook.imagepipeline.core.ImagePipelineConfig
|
|
6
7
|
import com.facebook.imagepipeline.memory.PoolConfig
|
|
@@ -36,4 +37,27 @@ class ShellApplication : Application() {
|
|
|
36
37
|
// its loaders to the LynxView it builds.
|
|
37
38
|
LynxEnv.inst().init(this, null, null, null)
|
|
38
39
|
}
|
|
40
|
+
|
|
41
|
+
// Lynx leaves it to the host app to report memory pressure. Either level
|
|
42
|
+
// runs a full collection of each LynxView's main-thread JavaScript heap;
|
|
43
|
+
// critical also empties Lynx's text layout cache. Leaving the screen is
|
|
44
|
+
// reported too: out of sight, a collection's pause can't show, and the
|
|
45
|
+
// memory it frees makes the app less likely to be ended in the background.
|
|
46
|
+
override fun onTrimMemory(level: Int) {
|
|
47
|
+
super.onTrimMemory(level)
|
|
48
|
+
val pressure =
|
|
49
|
+
if (level == ComponentCallbacks2.TRIM_MEMORY_RUNNING_CRITICAL ||
|
|
50
|
+
level >= ComponentCallbacks2.TRIM_MEMORY_COMPLETE
|
|
51
|
+
) {
|
|
52
|
+
ShellModule.MEMORY_PRESSURE_CRITICAL
|
|
53
|
+
} else {
|
|
54
|
+
ShellModule.MEMORY_PRESSURE_MODERATE
|
|
55
|
+
}
|
|
56
|
+
LynxEnv.inst().trimMemory(pressure)
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
override fun onLowMemory() {
|
|
60
|
+
super.onLowMemory()
|
|
61
|
+
LynxEnv.inst().trimMemory(ShellModule.MEMORY_PRESSURE_CRITICAL)
|
|
62
|
+
}
|
|
39
63
|
}
|
|
@@ -5,6 +5,7 @@ import android.os.Handler
|
|
|
5
5
|
import android.os.Looper
|
|
6
6
|
import com.lynx.jsbridge.LynxMethod
|
|
7
7
|
import com.lynx.jsbridge.LynxModule
|
|
8
|
+
import com.lynx.tasm.LynxEnv
|
|
8
9
|
|
|
9
10
|
/**
|
|
10
11
|
* Native module the AngularLynx runtime calls as
|
|
@@ -19,6 +20,15 @@ import com.lynx.jsbridge.LynxModule
|
|
|
19
20
|
* `angularLynxBack` global event; while disabled (the default, and whenever the
|
|
20
21
|
* runtime doesn't know about this module) Android's default back behavior
|
|
21
22
|
* applies, which leaves the app.
|
|
23
|
+
*
|
|
24
|
+
* `collectGarbage` runs a full collection of the main thread's JavaScript
|
|
25
|
+
* heap. PrimJS, which runs it, collects as the app runs, but those collections
|
|
26
|
+
* leave behind much of what a screen leaves when it goes, and the app's native
|
|
27
|
+
* memory grows with every screen it has shown. Lynx runs a full one, which also
|
|
28
|
+
* hands the freed pages back to the system, when the host app reports memory
|
|
29
|
+
* pressure, so this reports a moderate one. The runtime asks once it has
|
|
30
|
+
* released about a screen's worth of elements and the app has settled
|
|
31
|
+
* (`collect-garbage.ts` in `packages/runtime`).
|
|
22
32
|
*/
|
|
23
33
|
class ShellModule(context: Context, param: Any?) : LynxModule(context, param) {
|
|
24
34
|
private val host = param as Host
|
|
@@ -33,8 +43,18 @@ class ShellModule(context: Context, param: Any?) : LynxModule(context, param) {
|
|
|
33
43
|
main.post { host.setBackHandlerEnabled(enabled) }
|
|
34
44
|
}
|
|
35
45
|
|
|
46
|
+
@LynxMethod
|
|
47
|
+
fun collectGarbage() {
|
|
48
|
+
main.post { LynxEnv.inst().trimMemory(MEMORY_PRESSURE_MODERATE) }
|
|
49
|
+
}
|
|
50
|
+
|
|
36
51
|
companion object {
|
|
37
52
|
const val NAME = "AngularLynxShell"
|
|
38
53
|
const val BACK_EVENT = "angularLynxBack"
|
|
54
|
+
|
|
55
|
+
// Lynx's MemoryPressureLevel values, from a package the app doesn't
|
|
56
|
+
// compile against.
|
|
57
|
+
const val MEMORY_PRESSURE_MODERATE = 1
|
|
58
|
+
const val MEMORY_PRESSURE_CRITICAL = 2
|
|
39
59
|
}
|
|
40
60
|
}
|
|
@@ -23,6 +23,13 @@ final class AppDelegate: UIResponder, UIApplicationDelegate {
|
|
|
23
23
|
return true
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
+
// Lynx leaves it to the host app to report memory pressure. Critical
|
|
27
|
+
// pressure runs a full collection of each LynxView's main-thread
|
|
28
|
+
// JavaScript heap and empties Lynx's text layout cache.
|
|
29
|
+
func applicationDidReceiveMemoryWarning(_ application: UIApplication) {
|
|
30
|
+
LynxEnv.sharedInstance().trimMemory(.critical)
|
|
31
|
+
}
|
|
32
|
+
|
|
26
33
|
// Apps built with the iOS 27 SDK must use the scene lifecycle.
|
|
27
34
|
func application(
|
|
28
35
|
_ application: UIApplication,
|
|
@@ -49,4 +56,10 @@ final class SceneDelegate: UIResponder, UIWindowSceneDelegate {
|
|
|
49
56
|
window.makeKeyAndVisible()
|
|
50
57
|
self.window = window
|
|
51
58
|
}
|
|
59
|
+
|
|
60
|
+
// Out of sight, a collection's pause can't show, and the memory it frees
|
|
61
|
+
// makes the app less likely to be ended while it's in the background.
|
|
62
|
+
func sceneDidEnterBackground(_ scene: UIScene) {
|
|
63
|
+
LynxEnv.sharedInstance().trimMemory(.moderate)
|
|
64
|
+
}
|
|
52
65
|
}
|
|
@@ -40,6 +40,9 @@ final class LynxViewController: UIViewController {
|
|
|
40
40
|
}
|
|
41
41
|
// Bundled files for the main thread's fetch and Image (AssetsModule).
|
|
42
42
|
config.register(AssetsModule.self)
|
|
43
|
+
// The full collections the runtime asks for after screens go
|
|
44
|
+
// (ShellModule).
|
|
45
|
+
config.register(ShellModule.self)
|
|
43
46
|
// `ng serve --platform ios`'s live reload, in the development build.
|
|
44
47
|
DevServer.shared.register(in: config)
|
|
45
48
|
let lynxView = LynxView { builder in
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import Lynx
|
|
2
|
+
import UIKit
|
|
3
|
+
|
|
4
|
+
/// The native module the AngularLynx runtime calls as
|
|
5
|
+
/// `NativeModules.AngularLynxShell` (from the background thread, where
|
|
6
|
+
/// NativeModules lives). Android's shell has one of the same name, which also
|
|
7
|
+
/// connects the back button.
|
|
8
|
+
///
|
|
9
|
+
/// `collectGarbage` runs a full collection of the main thread's JavaScript
|
|
10
|
+
/// heap. PrimJS, which runs it, collects as the app runs, but those collections
|
|
11
|
+
/// leave behind much of what a screen leaves when it goes, and the app's
|
|
12
|
+
/// footprint grows with every screen it has shown. Lynx runs a full one, which
|
|
13
|
+
/// also hands the freed pages back to the system, when the host app reports
|
|
14
|
+
/// memory pressure, so this reports a moderate one. The runtime asks once it
|
|
15
|
+
/// has released about a screen's worth of elements and the app has settled
|
|
16
|
+
/// (`collect-garbage.ts` in `packages/runtime`).
|
|
17
|
+
final class ShellModule: NSObject, LynxModule {
|
|
18
|
+
static var name: String { "AngularLynxShell" }
|
|
19
|
+
|
|
20
|
+
static var methodLookup: [String: String] {
|
|
21
|
+
["collectGarbage": NSStringFromSelector(#selector(collectGarbage))]
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
override init() {
|
|
25
|
+
super.init()
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// Swift can't import an optional initializer as optional, so Lynx's
|
|
29
|
+
// initWithParam: is required here. Registered without a param, the module
|
|
30
|
+
// ignores it.
|
|
31
|
+
init(param: Any) {
|
|
32
|
+
super.init()
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// Lynx calls modules off the main thread. trimMemory hands the collection
|
|
36
|
+
// to each LynxView's engine, which runs it on its own thread.
|
|
37
|
+
@objc func collectGarbage() {
|
|
38
|
+
DispatchQueue.main.async {
|
|
39
|
+
LynxEnv.sharedInstance().trimMemory(.moderate)
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
}
|