nn-widgets 0.1.4 → 0.1.5

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.
@@ -0,0 +1,126 @@
1
+ package expo.modules.nnwidgets
2
+
3
+ import android.appwidget.AppWidgetManager
4
+ import android.content.ComponentName
5
+ import android.content.Context
6
+ import android.content.Intent
7
+ import android.content.SharedPreferences
8
+ import androidx.core.os.bundleOf
9
+ import expo.modules.kotlin.modules.Module
10
+ import expo.modules.kotlin.modules.ModuleDefinition
11
+ import expo.modules.kotlin.Promise
12
+ import org.json.JSONObject
13
+
14
+ class NNWidgetsModule : Module() {
15
+
16
+ companion object {
17
+ const val PREFS_NAME = "nn_widgets_data"
18
+ const val WIDGET_DATA_KEYS = "widget_data_keys"
19
+ }
20
+
21
+ private val context get() = requireNotNull(appContext.reactContext)
22
+
23
+ private fun getPreferences(): SharedPreferences {
24
+ return context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE)
25
+ }
26
+
27
+ override fun definition() = ModuleDefinition {
28
+ Name("NNWidgets")
29
+
30
+ // Check if widgets are supported (Android 4.0+, API 14+)
31
+ Function("isSupported") {
32
+ true // Widgets are supported on all modern Android versions
33
+ }
34
+
35
+ // Set widget data to SharedPreferences
36
+ AsyncFunction("setWidgetData") { data: Map<String, Any?>, promise: Promise ->
37
+ try {
38
+ val prefs = getPreferences()
39
+ val editor = prefs.edit()
40
+ val keys = mutableListOf<String>()
41
+
42
+ for ((key, value) in data) {
43
+ val prefKey = "widget_$key"
44
+ keys.add(prefKey)
45
+
46
+ when (value) {
47
+ is String -> editor.putString(prefKey, value)
48
+ is Int -> editor.putInt(prefKey, value)
49
+ is Long -> editor.putLong(prefKey, value)
50
+ is Float -> editor.putFloat(prefKey, value)
51
+ is Double -> editor.putFloat(prefKey, value.toFloat())
52
+ is Boolean -> editor.putBoolean(prefKey, value)
53
+ null -> editor.remove(prefKey)
54
+ else -> editor.putString(prefKey, value.toString())
55
+ }
56
+ }
57
+
58
+ // Store keys list
59
+ editor.putStringSet(WIDGET_DATA_KEYS, keys.toSet())
60
+ editor.apply()
61
+
62
+ println("[NNWidgets] Widget data saved: ${keys.joinToString(", ")}")
63
+ promise.resolve(true)
64
+ } catch (e: Exception) {
65
+ println("[NNWidgets] Failed to save widget data: ${e.message}")
66
+ promise.resolve(false)
67
+ }
68
+ }
69
+
70
+ // Get widget data from SharedPreferences
71
+ AsyncFunction("getWidgetData") { promise: Promise ->
72
+ try {
73
+ val prefs = getPreferences()
74
+ val keys = prefs.getStringSet(WIDGET_DATA_KEYS, emptySet()) ?: emptySet()
75
+ val result = mutableMapOf<String, Any?>()
76
+
77
+ for (key in keys) {
78
+ val cleanKey = key.removePrefix("widget_")
79
+ val value = prefs.all[key]
80
+ if (value != null) {
81
+ result[cleanKey] = value
82
+ }
83
+ }
84
+
85
+ promise.resolve(result)
86
+ } catch (e: Exception) {
87
+ println("[NNWidgets] Failed to get widget data: ${e.message}")
88
+ promise.resolve(emptyMap<String, Any?>())
89
+ }
90
+ }
91
+
92
+ // Request widget update
93
+ AsyncFunction("reloadWidget") { widgetClassName: String?, promise: Promise ->
94
+ try {
95
+ val appWidgetManager = AppWidgetManager.getInstance(context)
96
+
97
+ if (widgetClassName != null) {
98
+ // Update specific widget
99
+ val componentName = ComponentName(context.packageName, widgetClassName)
100
+ val widgetIds = appWidgetManager.getAppWidgetIds(componentName)
101
+
102
+ if (widgetIds.isNotEmpty()) {
103
+ val intent = Intent(AppWidgetManager.ACTION_APPWIDGET_UPDATE).apply {
104
+ putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, widgetIds)
105
+ component = componentName
106
+ }
107
+ context.sendBroadcast(intent)
108
+ println("[NNWidgets] Reloaded widget: $widgetClassName")
109
+ }
110
+ } else {
111
+ // Broadcast update to all widgets in the app
112
+ // This requires knowing the widget class names
113
+ // For now, we'll use a custom broadcast that widgets can listen to
114
+ val intent = Intent("${context.packageName}.WIDGET_UPDATE")
115
+ context.sendBroadcast(intent)
116
+ println("[NNWidgets] Sent update broadcast to all widgets")
117
+ }
118
+
119
+ promise.resolve(true)
120
+ } catch (e: Exception) {
121
+ println("[NNWidgets] Failed to reload widget: ${e.message}")
122
+ promise.resolve(false)
123
+ }
124
+ }
125
+ }
126
+ }
@@ -4,6 +4,7 @@
4
4
  "modules": ["NNWidgetsModule"]
5
5
  },
6
6
  "android": {
7
- "modules": ["expo.modules.nnwidgets.NNWidgetsModule"]
7
+ "modules": ["expo.modules.nnwidgets.NNWidgetsModule"],
8
+ "packageImportPath": "expo.modules.nnwidgets"
8
9
  }
9
10
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nn-widgets",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "Expo config plugin for adding native widgets (iOS WidgetKit & Android App Widgets)",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",