[Preferences] support listening to changes for a key
Harel M
It would be great if I could use the Preferences plugin to register for notifications in iOS/Android native code for changes in the value of keys I store in persistant storage.
The main candidate for this is CarPlay and Android Auto where you need the app running without the webview, so you need access to these stored values. The idea of having them notify when they change can really help this architecture. I've wrote something similar as part of implementing Android Auto and Carplay, but I think it makes sense to have is as part of the preferences plugin.
Harel M
Here's my implementation in case this is interesting:
Android:
https://github.com/IsraelHikingMap/Site/blob/5ff9361964c3e66c9a23a22afbe61a203c23e489/IsraelHiking.Web/android/app/src/main/java/com/mapeak/car/CapacitorStore.kt
https://github.com/IsraelHikingMap/Site/blob/5ff9361964c3e66c9a23a22afbe61a203c23e489/IsraelHiking.Web/android/app/src/main/java/com/mapeak/car/ReactivePreferencesPlugin.kt
iOS:
https://github.com/IsraelHikingMap/Site/blob/5ff9361964c3e66c9a23a22afbe61a203c23e489/IsraelHiking.Web/ios/App/App/Car/CapacitorStore.swift
https://github.com/IsraelHikingMap/Site/blob/5ff9361964c3e66c9a23a22afbe61a203c23e489/IsraelHiking.Web/ios/App/App/Car/CapacitorStore.swift
I'm not sure if this is relevant for the web or not, It probably can use indexdb or something, but I didn't have a need for it right now, so I didn't add that implementation. I've tested this to work on both iOS and Android.
Martin Donadieu (Capgo)
Harel M thanks to your feedback we added the feature in this plugin https://github.com/Cap-go/capacitor-auto :)