From 87452d6045bfc1b534e16f4824018e9ad796d18b Mon Sep 17 00:00:00 2001 From: David <4661784+retyui@users.noreply.github.com> Date: Sun, 13 Aug 2023 14:29:21 +0200 Subject: [PATCH] 1.0.3 Prevent app crash on old Android versions --- .../widgetpicker/WidgetPickerModuleImpl.kt | 25 ++++++++++++++++--- package.json | 2 +- 2 files changed, 22 insertions(+), 5 deletions(-) diff --git a/android/src/main/java/com/retyui/widgetpicker/WidgetPickerModuleImpl.kt b/android/src/main/java/com/retyui/widgetpicker/WidgetPickerModuleImpl.kt index a37110b..7206549 100644 --- a/android/src/main/java/com/retyui/widgetpicker/WidgetPickerModuleImpl.kt +++ b/android/src/main/java/com/retyui/widgetpicker/WidgetPickerModuleImpl.kt @@ -25,14 +25,31 @@ class WidgetPickerModuleImpl(private val reactContext: ReactApplicationContext) @RequiresApi(Build.VERSION_CODES.O) fun requestPinAppWidget(widgetClassKey: String, promise: Promise) { try { + if (isRequestPinAppWidgetSupported().not()) { + promise.resolve(Arguments.createMap().apply { + putString("message", "not supported") + }) + return + } + val appWidgetManager: AppWidgetManager = reactContext.getSystemService(AppWidgetManager::class.java) - val myProvider = ComponentName(reactContext, widgets[widgetClassKey]!!) - val result = appWidgetManager.requestPinAppWidget(myProvider, null, null) - if(result){ + + val widgetClass = widgets[widgetClassKey]; + + if (widgetClass == null) { + promise.resolve(Arguments.createMap().apply { + putString("message", "widget not found") + }) + return + } + + val provider = ComponentName(reactContext, widgetClass) + val result = appWidgetManager.requestPinAppWidget(provider, null, null) + if (result) { promise.resolve(Arguments.createMap().apply { putString("message", "success") }) - }else{ + } else { promise.resolve(Arguments.createMap().apply { // launcher doesn't support this feature // or widget not found diff --git a/package.json b/package.json index 27d50b3..61ff108 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "react-native-widget-picker", - "version": "1.0.2", + "version": "1.0.3", "license": "MIT", "main": "dist/cjs", "module": "dist/mjs",