expo-widgets 58.0.16 → 58.0.18

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 (59) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/android/build.gradle +2 -2
  3. package/android/src/main/AndroidManifest.xml +6 -1
  4. package/android/src/main/java/expo/modules/widgets/ExpoWidgetEmittableTree.kt +7 -7
  5. package/android/src/main/java/expo/modules/widgets/ExpoWidgetsPeekWidget.kt +1 -1
  6. package/android/src/main/java/expo/modules/widgets/WidgetInteractionAction.kt +34 -4
  7. package/android/src/main/java/expo/modules/widgets/WidgetsEvents.kt +8 -4
  8. package/android/src/main/java/expo/modules/widgets/WidgetsModule.kt +45 -0
  9. package/android/src/main/java/expo/modules/widgets/notifications/CustomNotification.kt +28 -0
  10. package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationBuilder.kt +116 -0
  11. package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationExceptions.kt +50 -0
  12. package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationFactory.kt +36 -0
  13. package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationOptions.kt +86 -0
  14. package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationRenderer.kt +44 -0
  15. package/android/src/main/java/expo/modules/widgets/notifications/CustomNotificationState.kt +65 -0
  16. package/android/src/main/java/expo/modules/widgets/notifications/CustomNotifications.kt +107 -0
  17. package/android/src/main/java/expo/modules/widgets/notifications/ExpoWidgetsNotificationReceiver.kt +67 -0
  18. package/android/src/main/res/values/expo_widgets_notifications.xml +4 -0
  19. package/build/CustomNotifications.d.ts +128 -0
  20. package/build/CustomNotifications.js +162 -0
  21. package/build/CustomNotifications.js.map +1 -0
  22. package/build/CustomNotifications.types.d.ts +124 -0
  23. package/build/CustomNotifications.types.js +3 -0
  24. package/build/CustomNotifications.types.js.map +1 -0
  25. package/build/ExpoWidgets.d.ts +3 -0
  26. package/build/ExpoWidgets.js +26 -0
  27. package/build/ExpoWidgets.js.map +1 -1
  28. package/build/ExpoWidgets.native.d.ts +5 -0
  29. package/build/ExpoWidgets.native.js.map +1 -1
  30. package/build/Widgets.d.ts +14 -0
  31. package/build/Widgets.js +14 -0
  32. package/build/Widgets.js.map +1 -1
  33. package/build/Widgets.types.d.ts +24 -0
  34. package/build/index.d.ts +2 -0
  35. package/build/index.js +1 -0
  36. package/build/index.js.map +1 -1
  37. package/bundle/decorator.ts +13 -0
  38. package/bundle/index.ts +19 -2
  39. package/bundle/layout-registry-stub.ts +19 -0
  40. package/ios/ExpoWidgets.podspec +5 -7
  41. package/package.json +5 -5
  42. package/plugin/build/android/withAndroidNotificationPermission.d.ts +6 -0
  43. package/plugin/build/android/withAndroidNotificationPermission.js +31 -0
  44. package/plugin/build/android/withAndroidNotificationPermission.js.map +1 -0
  45. package/plugin/build/android/withAndroidWidgets.d.ts +4 -2
  46. package/plugin/build/android/withAndroidWidgets.js +6 -1
  47. package/plugin/build/android/withAndroidWidgets.js.map +1 -1
  48. package/plugin/build/withWidgets.js +1 -0
  49. package/plugin/build/withWidgets.js.map +1 -1
  50. package/plugin/src/android/withAndroidNotificationPermission.ts +22 -0
  51. package/plugin/src/android/withAndroidWidgets.ts +11 -1
  52. package/plugin/src/withWidgets.ts +2 -2
  53. package/src/CustomNotifications.ts +213 -0
  54. package/src/CustomNotifications.types.ts +138 -0
  55. package/src/ExpoWidgets.native.ts +8 -0
  56. package/src/ExpoWidgets.ts +41 -0
  57. package/src/Widgets.ts +14 -0
  58. package/src/Widgets.types.ts +24 -0
  59. package/src/index.ts +8 -0
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # Changelog
2
2
 
3
+ ## 58.0.18
4
+
5
+ ### Patch Changes
6
+
7
+ - Updated dependencies. ([#51459](https://github.com/expo/expo/pull/51459), [#51563](https://github.com/expo/expo/pull/51563), [#51606](https://github.com/expo/expo/pull/51606))
8
+ - @expo/ui@58.0.18
9
+
10
+ ## 58.0.17
11
+
12
+ ### Patch Changes
13
+
14
+ - [Android] Add custom notifications. ([#51461](https://github.com/expo/expo/pull/51461) by [@jakex7](https://github.com/jakex7))
15
+ - [iOS] Rebuild the widgets JS bundle on every Xcode build so JS changes are picked up without a clean build. ([#50200](https://github.com/expo/expo/pull/50200) by [@jakex7](https://github.com/jakex7))
16
+ - Updated dependencies. ([#51480](https://github.com/expo/expo/pull/51480), [#51426](https://github.com/expo/expo/pull/51426))
17
+ - @expo/ui@58.0.17
18
+
3
19
  ## 58.0.16
4
20
 
5
21
  ### Patch Changes
@@ -6,7 +6,7 @@ plugins {
6
6
  }
7
7
 
8
8
  group = 'expo.modules.widgets'
9
- version = '58.0.16'
9
+ version = '58.0.18'
10
10
 
11
11
  def expoWidgetsPackageDirFile = project.projectDir.parentFile
12
12
  def expoWidgetsPackageDir = expoWidgetsPackageDirFile.absolutePath
@@ -39,7 +39,7 @@ android {
39
39
  namespace "expo.modules.widgets"
40
40
  defaultConfig {
41
41
  versionCode 1
42
- versionName '58.0.16'
42
+ versionName '58.0.18'
43
43
  consumerProguardFiles("proguard-rules.pro")
44
44
 
45
45
  externalNativeBuild {
@@ -1,2 +1,7 @@
1
- <manifest>
1
+ <manifest xmlns:android="http://schemas.android.com/apk/res/android">
2
+ <application>
3
+ <receiver
4
+ android:name="expo.modules.widgets.notifications.ExpoWidgetsNotificationReceiver"
5
+ android:exported="false" />
6
+ </application>
2
7
  </manifest>
@@ -120,7 +120,7 @@ private class WidgetConverterContext(
120
120
  override val runtime: Runtime? = null
121
121
  }
122
122
 
123
- internal fun ReadableMap.toPeekRoot(context: Context, source: String, size: DpSize): Emittable {
123
+ internal fun ReadableMap.toPeekRoot(context: Context, source: InteractionSource, size: DpSize): Emittable {
124
124
  val converterContext = WidgetConverterContext(context, size)
125
125
  return EmittableColumn().also { root ->
126
126
  root.children += toPeekNodes(converterContext, source)
@@ -136,7 +136,7 @@ private fun createErrorText(message: String): EmittableText {
136
136
  }
137
137
  }
138
138
 
139
- private fun ReadableMap.toPeekNodes(converterContext: WidgetConverterContext, source: String): List<Emittable> {
139
+ private fun ReadableMap.toPeekNodes(converterContext: WidgetConverterContext, source: InteractionSource): List<Emittable> {
140
140
  return when (typeName()) {
141
141
  "BoxView" -> listOf(toPeekBox(converterContext, source))
142
142
  "CheckboxView" -> listOf(toPeekCheckBox(converterContext))
@@ -160,7 +160,7 @@ private fun ReadableMap.toPeekNodes(converterContext: WidgetConverterContext, so
160
160
  }
161
161
  }
162
162
 
163
- private fun ReadableMap.toPeekBox(converterContext: WidgetConverterContext, source: String): EmittableBox {
163
+ private fun ReadableMap.toPeekBox(converterContext: WidgetConverterContext, source: InteractionSource): EmittableBox {
164
164
  val props = props<LayoutProps>(converterContext)
165
165
  return EmittableBox().also {
166
166
  it.modifier = props.modifiers.toPeekModifier(converterContext)
@@ -169,7 +169,7 @@ private fun ReadableMap.toPeekBox(converterContext: WidgetConverterContext, sour
169
169
  }
170
170
  }
171
171
 
172
- private fun ReadableMap.toPeekRow(converterContext: WidgetConverterContext, source: String): EmittableRow {
172
+ private fun ReadableMap.toPeekRow(converterContext: WidgetConverterContext, source: InteractionSource): EmittableRow {
173
173
  val props = props<LayoutProps>(converterContext)
174
174
  return EmittableRow().also {
175
175
  it.modifier = props.modifiers.toPeekModifier(converterContext)
@@ -179,7 +179,7 @@ private fun ReadableMap.toPeekRow(converterContext: WidgetConverterContext, sour
179
179
  }
180
180
  }
181
181
 
182
- private fun ReadableMap.toPeekColumn(converterContext: WidgetConverterContext, source: String): EmittableColumn {
182
+ private fun ReadableMap.toPeekColumn(converterContext: WidgetConverterContext, source: InteractionSource): EmittableColumn {
183
183
  val props = props<LayoutProps>(converterContext)
184
184
  return EmittableColumn().also {
185
185
  it.modifier = props.modifiers.toPeekModifier(converterContext)
@@ -189,7 +189,7 @@ private fun ReadableMap.toPeekColumn(converterContext: WidgetConverterContext, s
189
189
  }
190
190
  }
191
191
 
192
- private fun ReadableMap.toPeekLazyColumn(converterContext: WidgetConverterContext, source: String): EmittableLazyColumn {
192
+ private fun ReadableMap.toPeekLazyColumn(converterContext: WidgetConverterContext, source: InteractionSource): EmittableLazyColumn {
193
193
  val props = props<LayoutProps>(converterContext)
194
194
  val alignment = props.toPeekHorizontalAlignment()
195
195
  return EmittableLazyColumn().also { lazyColumn ->
@@ -292,7 +292,7 @@ private fun ImageContentScale.toGlanceContentScale(): ContentScale {
292
292
  }
293
293
  }
294
294
 
295
- private fun ReadableMap.toPeekButton(converterContext: WidgetConverterContext, source: String): Emittable {
295
+ private fun ReadableMap.toPeekButton(converterContext: WidgetConverterContext, source: InteractionSource): Emittable {
296
296
  val props = props<WidgetButtonProps>(converterContext)
297
297
  val children = children()
298
298
  val action =
@@ -28,6 +28,6 @@ internal class ExpoWidgetsPeekWidget(
28
28
  val environment = getWidgetEnvironment(appContext)
29
29
  val node = evaluateLayout(appContext, layout, props, environment)
30
30
 
31
- return node.toPeekRoot(appContext, widgetName, size)
31
+ return node.toPeekRoot(appContext, InteractionSource.Widget(widgetName), size)
32
32
  }
33
33
  }
@@ -2,22 +2,52 @@ package expo.modules.widgets
2
2
 
3
3
  import android.content.Context
4
4
  import android.content.Intent
5
+ import android.net.Uri
5
6
  import androidx.glance.action.Action
6
7
  import androidx.glance.appwidget.action.actionSendBroadcast
8
+ import expo.modules.widgets.notifications.ExpoWidgetsNotificationReceiver
7
9
 
8
10
  internal const val WIDGET_INTERACTION_ACTION = "expo.modules.widgets.ACTION_WIDGET_INTERACTION"
11
+ internal const val NOTIFICATION_INTERACTION_ACTION = "expo.modules.widgets.ACTION_NOTIFICATION_INTERACTION"
9
12
  internal const val WIDGET_INTERACTION_SOURCE_EXTRA = "expo.modules.widgets.extra.SOURCE"
10
13
  internal const val WIDGET_INTERACTION_TARGET_EXTRA = "expo.modules.widgets.extra.TARGET"
14
+ internal const val WIDGET_INTERACTION_NOTIFICATION_ID_EXTRA = "expo.modules.widgets.extra.NOTIFICATION_ID"
15
+
16
+ internal sealed interface InteractionSource {
17
+ val name: String
18
+
19
+ data class Widget(override val name: String) : InteractionSource
20
+
21
+ data class Notification(override val name: String, val notificationId: String) : InteractionSource
22
+ }
11
23
 
12
24
  internal data class WidgetInteraction(
13
- val source: String,
25
+ val source: InteractionSource,
14
26
  val target: String
15
27
  )
16
28
 
17
29
  internal fun WidgetInteraction.toGlanceAction(context: Context): Action {
18
- val intent = Intent(WIDGET_INTERACTION_ACTION)
19
- .setComponent(widgetProviderComponentName(context, source))
20
- .putExtra(WIDGET_INTERACTION_SOURCE_EXTRA, source)
30
+ val intent = when (source) {
31
+ is InteractionSource.Widget -> Intent(WIDGET_INTERACTION_ACTION)
32
+ .setComponent(widgetProviderComponentName(context, source.name))
33
+
34
+ is InteractionSource.Notification -> Intent(NOTIFICATION_INTERACTION_ACTION)
35
+ .setClass(context, ExpoWidgetsNotificationReceiver::class.java)
36
+ // Extras do not participate in PendingIntent identity. Glance's default URI uses the
37
+ // same appWidgetId for every notification, so provide our own stable button identity.
38
+ .setData(
39
+ Uri.Builder()
40
+ .scheme("expo-widgets")
41
+ .authority("notification")
42
+ .appendPath(source.name)
43
+ .appendPath(source.notificationId)
44
+ .appendPath(target)
45
+ .build()
46
+ )
47
+ .putExtra(WIDGET_INTERACTION_NOTIFICATION_ID_EXTRA, source.notificationId)
48
+ }
49
+ intent
50
+ .putExtra(WIDGET_INTERACTION_SOURCE_EXTRA, source.name)
21
51
  .putExtra(WIDGET_INTERACTION_TARGET_EXTRA, target)
22
52
  return actionSendBroadcast(intent)
23
53
  }
@@ -18,16 +18,20 @@ internal object WidgetsEvents {
18
18
  }
19
19
 
20
20
  @Synchronized
21
- fun sendUserInteraction(source: String, target: String) {
22
- userInteractionObserver?.invoke(userInteractionEvent(source, target))
21
+ fun sendUserInteraction(source: String, target: String, notificationId: String? = null) {
22
+ userInteractionObserver?.invoke(userInteractionEvent(source, target, notificationId))
23
23
  }
24
24
 
25
- private fun userInteractionEvent(source: String, target: String): Map<String, Any?> {
26
- return mapOf(
25
+ private fun userInteractionEvent(source: String, target: String, notificationId: String?): Map<String, Any?> {
26
+ val event = mutableMapOf<String, Any?>(
27
27
  "source" to source,
28
28
  "target" to target,
29
29
  "timestamp" to System.currentTimeMillis(),
30
30
  "type" to EVENT_TYPE_USER_INTERACTION
31
31
  )
32
+ if (notificationId != null) {
33
+ event["notificationId"] = notificationId
34
+ }
35
+ return event
32
36
  }
33
37
  }
@@ -2,8 +2,13 @@ package expo.modules.widgets
2
2
 
3
3
  import android.content.Context
4
4
  import expo.modules.kotlin.exception.Exceptions
5
+ import expo.modules.kotlin.functions.Coroutine
5
6
  import expo.modules.kotlin.modules.Module
6
7
  import expo.modules.kotlin.modules.ModuleDefinition
8
+ import expo.modules.widgets.notifications.CustomNotification
9
+ import expo.modules.widgets.notifications.CustomNotificationFactory
10
+ import expo.modules.widgets.notifications.CustomNotificationOptions
11
+ import expo.modules.widgets.notifications.CustomNotificationUpdateOptions
7
12
  import java.io.File
8
13
 
9
14
  class WidgetsModule : Module() {
@@ -55,5 +60,45 @@ class WidgetsModule : Module() {
55
60
  widget.updateSnapshot(props)
56
61
  }
57
62
  }
63
+
64
+ Class("CustomNotificationFactory", CustomNotificationFactory::class) {
65
+ Constructor { name: String, layout: String ->
66
+ CustomNotificationFactory(context.applicationContext, name, layout)
67
+ }
68
+
69
+ AsyncFunction("post") Coroutine { factory: CustomNotificationFactory, id: String, props: Map<String, Any?>?, options: CustomNotificationOptions ->
70
+ factory.post(id, props, options)
71
+ }
72
+
73
+ AsyncFunction("get") { factory: CustomNotificationFactory, id: String ->
74
+ factory.get(id)
75
+ }
76
+
77
+ AsyncFunction("getActive") { factory: CustomNotificationFactory ->
78
+ factory.getActive()
79
+ }
80
+
81
+ AsyncFunction("cancelAll") { factory: CustomNotificationFactory ->
82
+ factory.cancelAll()
83
+ }
84
+ }
85
+
86
+ Class("CustomNotification", CustomNotification::class) {
87
+ Function("getId") { notification: CustomNotification ->
88
+ notification.id
89
+ }
90
+
91
+ AsyncFunction("getProps") { notification: CustomNotification ->
92
+ notification.getProps()
93
+ }
94
+
95
+ AsyncFunction("update") Coroutine { notification: CustomNotification, props: Map<String, Any?>?, options: CustomNotificationUpdateOptions ->
96
+ notification.update(props, options)
97
+ }
98
+
99
+ AsyncFunction("cancel") { notification: CustomNotification ->
100
+ notification.cancel()
101
+ }
102
+ }
58
103
  }
59
104
  }
@@ -0,0 +1,28 @@
1
+ package expo.modules.widgets.notifications
2
+
3
+ import android.content.Context
4
+ import expo.modules.kotlin.sharedobjects.SharedObject
5
+
6
+ internal class CustomNotification(
7
+ private val context: Context,
8
+ val name: String,
9
+ val id: String
10
+ ) : SharedObject() {
11
+ fun getProps(): Map<String, Any?> {
12
+ val active = CustomNotificationState.find(context, name, id) ?: throw CustomNotificationNotFoundException(name, id)
13
+ return active.props.orEmpty()
14
+ }
15
+
16
+ suspend fun update(props: Map<String, Any?>?, overrides: CustomNotificationUpdateOptions) {
17
+ val updated = CustomNotifications.shared.update(context, name, id) { active ->
18
+ active.copy(props = props, options = active.options.merge(overrides))
19
+ }
20
+ if (!updated) {
21
+ throw CustomNotificationNotFoundException(name, id)
22
+ }
23
+ }
24
+
25
+ fun cancel() {
26
+ CustomNotifications.shared.cancel(context, name, id)
27
+ }
28
+ }
@@ -0,0 +1,116 @@
1
+ package expo.modules.widgets.notifications
2
+
3
+ import android.app.Notification
4
+ import android.app.NotificationChannel
5
+ import android.app.NotificationManager
6
+ import android.app.PendingIntent
7
+ import android.content.Context
8
+ import android.content.Intent
9
+ import android.content.pm.PackageManager
10
+ import android.graphics.Color
11
+ import android.net.Uri
12
+ import android.os.Build
13
+ import android.os.Bundle
14
+ import androidx.core.app.NotificationCompat
15
+ import androidx.core.app.NotificationManagerCompat
16
+ import expo.modules.widgets.R
17
+ import io.github.jakex7.peek.notification.PeekNotificationViews
18
+ import io.github.jakex7.peek.notification.setPeekContent
19
+
20
+ internal object CustomNotificationBuilder {
21
+ private const val DEFAULT_CHANNEL_ID = "expo_widgets_notifications"
22
+
23
+ // Meta-data written by the expo-notifications config plugin.
24
+ private const val NOTIFICATIONS_DEFAULT_ICON_META_DATA = "expo.modules.notifications.default_notification_icon"
25
+ private const val NOTIFICATIONS_DEFAULT_COLOR_META_DATA = "expo.modules.notifications.default_notification_color"
26
+
27
+ fun build(
28
+ context: Context,
29
+ state: CustomNotificationState,
30
+ views: PeekNotificationViews
31
+ ): Notification {
32
+ val options = state.options
33
+ val channelId = resolveChannelId(context, options.channelId)
34
+ val builder = NotificationCompat.Builder(context, channelId)
35
+ .setSmallIcon(resolveSmallIcon(context, options.smallIcon))
36
+ .setPeekContent(views, decorated = true, ongoing = options.ongoing)
37
+ .setAutoCancel(options.autoCancel)
38
+ .setOnlyAlertOnce(options.alertOnlyOnce)
39
+
40
+ options.title?.let(builder::setContentTitle)
41
+ options.text?.let(builder::setContentText)
42
+ options.timeoutAfter?.let(builder::setTimeoutAfter)
43
+ resolveColor(context, options.color)?.let { builder.color = it }
44
+ contentIntent(context, state.tag, options.url)?.let(builder::setContentIntent)
45
+ builder.addExtras(state.toExtras())
46
+
47
+ return builder.build()
48
+ }
49
+
50
+ private fun resolveChannelId(context: Context, requestedChannelId: String?): String {
51
+ if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
52
+ return requestedChannelId ?: DEFAULT_CHANNEL_ID
53
+ }
54
+ val manager = NotificationManagerCompat.from(context)
55
+ if (requestedChannelId != null) {
56
+ // Android silently drops notifications posted to an unknown channel.
57
+ if (manager.getNotificationChannel(requestedChannelId) == null) {
58
+ throw MissingNotificationChannelException(requestedChannelId)
59
+ }
60
+ return requestedChannelId
61
+ }
62
+ if (manager.getNotificationChannel(DEFAULT_CHANNEL_ID) == null) {
63
+ manager.createNotificationChannel(
64
+ NotificationChannel(
65
+ DEFAULT_CHANNEL_ID,
66
+ context.getString(R.string.expo_widgets_notification_channel_name),
67
+ NotificationManager.IMPORTANCE_LOW
68
+ )
69
+ )
70
+ }
71
+ return DEFAULT_CHANNEL_ID
72
+ }
73
+
74
+ private fun resolveSmallIcon(context: Context, name: String?): Int {
75
+ if (name != null) {
76
+ return context.resources.getIdentifier(name, "drawable", context.packageName).takeIf { it != 0 }
77
+ ?: context.resources.getIdentifier(name, "mipmap", context.packageName).takeIf { it != 0 }
78
+ ?: throw SmallIconNotFoundException(name)
79
+ }
80
+ applicationMetaData(context)?.getInt(NOTIFICATIONS_DEFAULT_ICON_META_DATA, 0)?.takeIf { it != 0 }?.let { return it }
81
+ return context.applicationInfo.icon.takeIf { it != 0 } ?: android.R.drawable.sym_def_app_icon
82
+ }
83
+
84
+ private fun resolveColor(context: Context, color: String?): Int? {
85
+ if (color != null) {
86
+ return runCatching { Color.parseColor(color) }.getOrElse { throw InvalidNotificationColorException(color) }
87
+ }
88
+ val resourceId = applicationMetaData(context)?.getInt(NOTIFICATIONS_DEFAULT_COLOR_META_DATA, 0) ?: 0
89
+ if (resourceId == 0) {
90
+ return null
91
+ }
92
+ return runCatching { context.resources.getColor(resourceId, null) }.getOrNull()
93
+ }
94
+
95
+ private fun contentIntent(context: Context, tag: String, url: String?): PendingIntent? {
96
+ val intent = if (url != null) {
97
+ Intent(Intent.ACTION_VIEW, Uri.parse(url)).setPackage(context.packageName)
98
+ } else {
99
+ runCatching { context.packageManager.getLaunchIntentForPackage(context.packageName) }.getOrNull()
100
+ } ?: return null
101
+ intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
102
+ return PendingIntent.getActivity(
103
+ context,
104
+ tag.hashCode(),
105
+ intent,
106
+ PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
107
+ )
108
+ }
109
+
110
+ @Suppress("DEPRECATION")
111
+ private fun applicationMetaData(context: Context): Bundle? {
112
+ return runCatching {
113
+ context.packageManager.getApplicationInfo(context.packageName, PackageManager.GET_META_DATA).metaData
114
+ }.getOrNull()
115
+ }
116
+ }
@@ -0,0 +1,50 @@
1
+ package expo.modules.widgets.notifications
2
+
3
+ import expo.modules.kotlin.exception.CodedException
4
+
5
+ internal class NotificationsDisabledException :
6
+ CodedException(
7
+ "The custom notification could not be posted because notifications are disabled for this app. " +
8
+ "Set `enablePushNotifications: true` in the expo-widgets config plugin to add the POST_NOTIFICATIONS permission, " +
9
+ "request it at runtime (for example with expo-notifications' requestPermissionsAsync()), " +
10
+ "and check the app's notification settings."
11
+ )
12
+
13
+ internal class MissingNotificationChannelException(channelId: String) :
14
+ CodedException(
15
+ "The custom notification could not be posted because the notification channel '$channelId' does not exist. " +
16
+ "Create the channel first (for example with expo-notifications' setNotificationChannelAsync()) " +
17
+ "or omit `channelId` to use the default channel."
18
+ )
19
+
20
+ internal class CustomNotificationLayoutNotFoundException(name: String) :
21
+ CodedException(
22
+ "No layout is registered for the custom notification '$name'. " +
23
+ "Call createCustomNotification('$name', layout) in the app before posting or updating it."
24
+ )
25
+
26
+ internal class MissingCollapsedSectionException(name: String) :
27
+ CodedException(
28
+ "The layout of the custom notification '$name' returned no `collapsed` section. " +
29
+ "Return `{ collapsed: <element> }` from the layout function; `expanded` and `headsUp` are optional."
30
+ )
31
+
32
+ internal class CustomNotificationNotFoundException(name: String, id: String) :
33
+ CodedException(
34
+ "The custom notification '$id' of '$name' is not active. " +
35
+ "The user may have dismissed it, or it was cancelled or timed out. Post it again with post()."
36
+ )
37
+
38
+ internal class CustomNotificationCancelledException :
39
+ CodedException("The custom notification operation was cancelled before it could be posted.")
40
+
41
+ internal class SmallIconNotFoundException(name: String) :
42
+ CodedException(
43
+ "The drawable '$name' passed as `smallIcon` was not found in the app's resources. " +
44
+ "Add it to the app's res/drawable directory or omit `smallIcon` to use the default icon."
45
+ )
46
+
47
+ internal class InvalidNotificationColorException(color: String) :
48
+ CodedException(
49
+ "The value '$color' passed as `color` is not a valid color. Use the #RRGGBB or #AARRGGBB format."
50
+ )
@@ -0,0 +1,36 @@
1
+ package expo.modules.widgets.notifications
2
+
3
+ import android.content.Context
4
+ import expo.modules.kotlin.sharedobjects.SharedObject
5
+ import expo.modules.widgets.WidgetsStorage
6
+
7
+ internal class CustomNotificationFactory(
8
+ private val context: Context,
9
+ val name: String,
10
+ layout: String
11
+ ) : SharedObject() {
12
+ init {
13
+ WidgetsStorage.set(context, "__expo_widgets_notification_${name}_layout", layout)
14
+ }
15
+
16
+ suspend fun post(id: String, props: Map<String, Any?>?, options: CustomNotificationOptions): CustomNotification {
17
+ CustomNotifications.shared.post(context, CustomNotificationState(name, id, props, options))
18
+ return CustomNotification(context, name, id)
19
+ }
20
+
21
+ fun get(id: String): CustomNotification? {
22
+ return CustomNotificationState.find(context, name, id)?.let {
23
+ CustomNotification(context, name, id)
24
+ }
25
+ }
26
+
27
+ fun getActive(): List<CustomNotification> {
28
+ return CustomNotificationState.all(context)
29
+ .filter { it.name == name }
30
+ .map { CustomNotification(context, name, it.id) }
31
+ }
32
+
33
+ fun cancelAll() {
34
+ CustomNotifications.shared.cancelAll(context, name)
35
+ }
36
+ }
@@ -0,0 +1,86 @@
1
+ package expo.modules.widgets.notifications
2
+
3
+ import android.os.Bundle
4
+ import expo.modules.kotlin.records.Field
5
+ import expo.modules.kotlin.records.Record
6
+ import expo.modules.kotlin.types.OptimizedRecord
7
+ import expo.modules.kotlin.types.ValueOrUndefined
8
+
9
+ @OptimizedRecord
10
+ internal data class CustomNotificationOptions(
11
+ @Field val channelId: String? = null,
12
+ @Field val title: String? = null,
13
+ @Field val text: String? = null,
14
+ @Field val ongoing: Boolean = false,
15
+ @Field val autoCancel: Boolean = true,
16
+ @Field val alertOnlyOnce: Boolean = true,
17
+ @Field val timeoutAfter: Long? = null,
18
+ @Field val url: String? = null,
19
+ @Field val smallIcon: String? = null,
20
+ @Field val color: String? = null
21
+ ) : Record {
22
+ fun merge(overrides: CustomNotificationUpdateOptions): CustomNotificationOptions {
23
+ return copy(
24
+ channelId = overrides.channelId.orElse(channelId),
25
+ title = overrides.title.orElse(title),
26
+ text = overrides.text.orElse(text),
27
+ ongoing = overrides.ongoing.orElse(ongoing),
28
+ autoCancel = overrides.autoCancel.orElse(autoCancel),
29
+ alertOnlyOnce = overrides.alertOnlyOnce.orElse(alertOnlyOnce),
30
+ timeoutAfter = overrides.timeoutAfter.orElse(timeoutAfter),
31
+ url = overrides.url.orElse(url),
32
+ smallIcon = overrides.smallIcon.orElse(smallIcon),
33
+ color = overrides.color.orElse(color)
34
+ )
35
+ }
36
+
37
+ fun toBundle(): Bundle = Bundle().apply {
38
+ putString("channelId", channelId)
39
+ putString("title", title)
40
+ putString("text", text)
41
+ putBoolean("ongoing", ongoing)
42
+ putBoolean("autoCancel", autoCancel)
43
+ putBoolean("alertOnlyOnce", alertOnlyOnce)
44
+ timeoutAfter?.let { putLong("timeoutAfter", it) }
45
+ putString("url", url)
46
+ putString("smallIcon", smallIcon)
47
+ putString("color", color)
48
+ }
49
+
50
+ companion object {
51
+ fun fromBundle(bundle: Bundle): CustomNotificationOptions = with(bundle) {
52
+ CustomNotificationOptions(
53
+ channelId = getString("channelId"),
54
+ title = getString("title"),
55
+ text = getString("text"),
56
+ ongoing = getBoolean("ongoing", false),
57
+ autoCancel = getBoolean("autoCancel", true),
58
+ alertOnlyOnce = getBoolean("alertOnlyOnce", true),
59
+ timeoutAfter = if (containsKey("timeoutAfter")) getLong("timeoutAfter") else null,
60
+ url = getString("url"),
61
+ smallIcon = getString("smallIcon"),
62
+ color = getString("color")
63
+ )
64
+ }
65
+ }
66
+ }
67
+
68
+ // Undefined fields keep their current value; explicit null clears a nullable option.
69
+ @OptimizedRecord
70
+ internal data class CustomNotificationUpdateOptions(
71
+ @Field val channelId: ValueOrUndefined<String?> = ValueOrUndefined.Undefined(),
72
+ @Field val title: ValueOrUndefined<String?> = ValueOrUndefined.Undefined(),
73
+ @Field val text: ValueOrUndefined<String?> = ValueOrUndefined.Undefined(),
74
+ @Field val ongoing: ValueOrUndefined<Boolean> = ValueOrUndefined.Undefined(),
75
+ @Field val autoCancel: ValueOrUndefined<Boolean> = ValueOrUndefined.Undefined(),
76
+ @Field val alertOnlyOnce: ValueOrUndefined<Boolean> = ValueOrUndefined.Undefined(),
77
+ @Field val timeoutAfter: ValueOrUndefined<Long?> = ValueOrUndefined.Undefined(),
78
+ @Field val url: ValueOrUndefined<String?> = ValueOrUndefined.Undefined(),
79
+ @Field val smallIcon: ValueOrUndefined<String?> = ValueOrUndefined.Undefined(),
80
+ @Field val color: ValueOrUndefined<String?> = ValueOrUndefined.Undefined()
81
+ ) : Record
82
+
83
+ private fun <T> ValueOrUndefined<T>.orElse(fallback: T): T = when (this) {
84
+ is ValueOrUndefined.Value -> value
85
+ is ValueOrUndefined.Undefined -> fallback
86
+ }
@@ -0,0 +1,44 @@
1
+ package expo.modules.widgets.notifications
2
+
3
+ import android.content.Context
4
+ import com.facebook.react.bridge.ReadableMap
5
+ import com.facebook.react.bridge.ReadableType
6
+ import expo.modules.widgets.InteractionSource
7
+ import expo.modules.widgets.WidgetsStorage
8
+ import expo.modules.widgets.evaluateLayout
9
+ import expo.modules.widgets.getWidgetEnvironment
10
+ import expo.modules.widgets.toPeekRoot
11
+ import io.github.jakex7.peek.emittables.peekEmittableNotificationViews
12
+ import io.github.jakex7.peek.notification.PeekNotificationSizes
13
+ import io.github.jakex7.peek.notification.PeekNotificationViews
14
+
15
+ internal object CustomNotificationRenderer {
16
+ suspend fun render(context: Context, state: CustomNotificationState): PeekNotificationViews {
17
+ val (name, id, props) = state
18
+ val layout = WidgetsStorage.getString(context, "__expo_widgets_notification_${name}_layout")
19
+ ?.takeIf { it.isNotEmpty() }
20
+ ?: throw CustomNotificationLayoutNotFoundException(name)
21
+ val sections = evaluateLayout(context, layout, props, getWidgetEnvironment(context))
22
+ val sizes = PeekNotificationSizes()
23
+ val source = InteractionSource.Notification(name, id)
24
+
25
+ val collapsedRoot = sections.section("collapsed")?.toPeekRoot(context, source, sizes.collapsed)
26
+ // A bare element instead of a section map, for example the error node from a failed evaluation.
27
+ ?: sections.takeIf { it.hasKey("type") }?.toPeekRoot(context, source, sizes.collapsed)
28
+ ?: throw MissingCollapsedSectionException(name)
29
+ val expandedRoot = sections.section("expanded")?.toPeekRoot(context, source, sizes.expanded)
30
+ val headsUpRoot = sections.section("headsUp")?.toPeekRoot(context, source, sizes.headsUp)
31
+
32
+ return peekEmittableNotificationViews(
33
+ context = context,
34
+ collapsed = collapsedRoot,
35
+ expanded = expandedRoot,
36
+ headsUp = headsUpRoot,
37
+ sizes = sizes
38
+ )
39
+ }
40
+
41
+ private fun ReadableMap.section(key: String): ReadableMap? {
42
+ return if (hasKey(key) && !isNull(key) && getType(key) == ReadableType.Map) getMap(key) else null
43
+ }
44
+ }