expo-targets 0.1.0
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/.eslintrc.js +2 -0
- package/README.md +244 -0
- package/android/build.gradle +22 -0
- package/android/src/main/java/expo/modules/targets/ExpoTargetsLogger.kt +98 -0
- package/android/src/main/java/expo/modules/targets/ExpoTargetsReceiver.kt +136 -0
- package/android/src/main/java/expo/modules/targets/ExpoTargetsRemoteViewsProvider.kt +72 -0
- package/android/src/main/java/expo/modules/targets/ExpoTargetsWidgetProvider.kt +47 -0
- package/android/src/main/java/expo/modules/targets/ExpoTargetsWidgetSync.kt +78 -0
- package/android/src/main/java/expo/modules/targets/ExpoTargetsWidgetUpdateReceiver.kt +30 -0
- package/android/src/main/java/expo/modules/targets/extension/ExpoTargetsExtensionModule.kt +27 -0
- package/android/src/main/java/expo/modules/targets/messages/ExpoTargetsMessagesModule.kt +40 -0
- package/android/src/main/java/expo/modules/targets/storage/ExpoTargetsStorageModule.kt +119 -0
- package/app.plugin.js +1 -0
- package/build/metro/src/index.d.ts +2 -0
- package/build/metro/src/index.d.ts.map +1 -0
- package/build/metro/src/index.js +6 -0
- package/build/metro/src/index.js.map +1 -0
- package/build/metro/src/withTargetsMetro.d.ts +6 -0
- package/build/metro/src/withTargetsMetro.d.ts.map +1 -0
- package/build/metro/src/withTargetsMetro.js +171 -0
- package/build/metro/src/withTargetsMetro.js.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidColors.d.ts +7 -0
- package/build/plugin/src/android/config-plugins/withAndroidColors.d.ts.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidColors.js +56 -0
- package/build/plugin/src/android/config-plugins/withAndroidColors.js.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidManifest.d.ts +7 -0
- package/build/plugin/src/android/config-plugins/withAndroidManifest.d.ts.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidManifest.js +70 -0
- package/build/plugin/src/android/config-plugins/withAndroidManifest.js.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidModule.d.ts +7 -0
- package/build/plugin/src/android/config-plugins/withAndroidModule.d.ts.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidModule.js +41 -0
- package/build/plugin/src/android/config-plugins/withAndroidModule.js.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidResources.d.ts +7 -0
- package/build/plugin/src/android/config-plugins/withAndroidResources.d.ts.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidResources.js +75 -0
- package/build/plugin/src/android/config-plugins/withAndroidResources.js.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidTarget.d.ts +28 -0
- package/build/plugin/src/android/config-plugins/withAndroidTarget.d.ts.map +1 -0
- package/build/plugin/src/android/config-plugins/withAndroidTarget.js +28 -0
- package/build/plugin/src/android/config-plugins/withAndroidTarget.js.map +1 -0
- package/build/plugin/src/android/config-plugins/withGradleModule.d.ts +7 -0
- package/build/plugin/src/android/config-plugins/withGradleModule.d.ts.map +1 -0
- package/build/plugin/src/android/config-plugins/withGradleModule.js +86 -0
- package/build/plugin/src/android/config-plugins/withGradleModule.js.map +1 -0
- package/build/plugin/src/android/config-plugins/withGradleSettings.d.ts +7 -0
- package/build/plugin/src/android/config-plugins/withGradleSettings.d.ts.map +1 -0
- package/build/plugin/src/android/config-plugins/withGradleSettings.js +23 -0
- package/build/plugin/src/android/config-plugins/withGradleSettings.js.map +1 -0
- package/build/plugin/src/android/withAndroidAppBuildGradle.d.ts +7 -0
- package/build/plugin/src/android/withAndroidAppBuildGradle.d.ts.map +1 -0
- package/build/plugin/src/android/withAndroidAppBuildGradle.js +31 -0
- package/build/plugin/src/android/withAndroidAppBuildGradle.js.map +1 -0
- package/build/plugin/src/android/withAndroidKotlinGradleProperty.d.ts +3 -0
- package/build/plugin/src/android/withAndroidKotlinGradleProperty.d.ts.map +1 -0
- package/build/plugin/src/android/withAndroidKotlinGradleProperty.js +24 -0
- package/build/plugin/src/android/withAndroidKotlinGradleProperty.js.map +1 -0
- package/build/plugin/src/android/withAndroidProjectKotlinVersion.d.ts +3 -0
- package/build/plugin/src/android/withAndroidProjectKotlinVersion.d.ts.map +1 -0
- package/build/plugin/src/android/withAndroidProjectKotlinVersion.js +27 -0
- package/build/plugin/src/android/withAndroidProjectKotlinVersion.js.map +1 -0
- package/build/plugin/src/android/withAndroidTarget.d.ts +10 -0
- package/build/plugin/src/android/withAndroidTarget.d.ts.map +1 -0
- package/build/plugin/src/android/withAndroidTarget.js +22 -0
- package/build/plugin/src/android/withAndroidTarget.js.map +1 -0
- package/build/plugin/src/android/withAndroidWidget.d.ts +13 -0
- package/build/plugin/src/android/withAndroidWidget.d.ts.map +1 -0
- package/build/plugin/src/android/withAndroidWidget.js +564 -0
- package/build/plugin/src/android/withAndroidWidget.js.map +1 -0
- package/build/plugin/src/config.d.ts +141 -0
- package/build/plugin/src/config.d.ts.map +1 -0
- package/build/plugin/src/config.js +54 -0
- package/build/plugin/src/config.js.map +1 -0
- package/build/plugin/src/index.d.ts +9 -0
- package/build/plugin/src/index.d.ts.map +1 -0
- package/build/plugin/src/index.js +11 -0
- package/build/plugin/src/index.js.map +1 -0
- package/build/plugin/src/ios/config-plugins/withEntitlements.d.ts +11 -0
- package/build/plugin/src/ios/config-plugins/withEntitlements.d.ts.map +1 -0
- package/build/plugin/src/ios/config-plugins/withEntitlements.js +49 -0
- package/build/plugin/src/ios/config-plugins/withEntitlements.js.map +1 -0
- package/build/plugin/src/ios/config-plugins/withIOSTarget.d.ts +17 -0
- package/build/plugin/src/ios/config-plugins/withIOSTarget.d.ts.map +1 -0
- package/build/plugin/src/ios/config-plugins/withIOSTarget.js +152 -0
- package/build/plugin/src/ios/config-plugins/withIOSTarget.js.map +1 -0
- package/build/plugin/src/ios/config-plugins/withIosColorset.d.ts +8 -0
- package/build/plugin/src/ios/config-plugins/withIosColorset.d.ts.map +1 -0
- package/build/plugin/src/ios/config-plugins/withIosColorset.js +25 -0
- package/build/plugin/src/ios/config-plugins/withIosColorset.js.map +1 -0
- package/build/plugin/src/ios/config-plugins/withIosStickerPack.d.ts +8 -0
- package/build/plugin/src/ios/config-plugins/withIosStickerPack.d.ts.map +1 -0
- package/build/plugin/src/ios/config-plugins/withIosStickerPack.js +52 -0
- package/build/plugin/src/ios/config-plugins/withIosStickerPack.js.map +1 -0
- package/build/plugin/src/ios/config-plugins/withPodfile.d.ts +12 -0
- package/build/plugin/src/ios/config-plugins/withPodfile.d.ts.map +1 -0
- package/build/plugin/src/ios/config-plugins/withPodfile.js +162 -0
- package/build/plugin/src/ios/config-plugins/withPodfile.js.map +1 -0
- package/build/plugin/src/ios/config-plugins/withXcodeChanges.d.ts +15 -0
- package/build/plugin/src/ios/config-plugins/withXcodeChanges.d.ts.map +1 -0
- package/build/plugin/src/ios/config-plugins/withXcodeChanges.js +646 -0
- package/build/plugin/src/ios/config-plugins/withXcodeChanges.js.map +1 -0
- package/build/plugin/src/ios/target.d.ts +41 -0
- package/build/plugin/src/ios/target.d.ts.map +1 -0
- package/build/plugin/src/ios/target.js +574 -0
- package/build/plugin/src/ios/target.js.map +1 -0
- package/build/plugin/src/ios/utils/asset.d.ts +60 -0
- package/build/plugin/src/ios/utils/asset.d.ts.map +1 -0
- package/build/plugin/src/ios/utils/asset.js +375 -0
- package/build/plugin/src/ios/utils/asset.js.map +1 -0
- package/build/plugin/src/ios/utils/file.d.ts +33 -0
- package/build/plugin/src/ios/utils/file.d.ts.map +1 -0
- package/build/plugin/src/ios/utils/file.js +88 -0
- package/build/plugin/src/ios/utils/file.js.map +1 -0
- package/build/plugin/src/ios/utils/index.d.ts +12 -0
- package/build/plugin/src/ios/utils/index.d.ts.map +1 -0
- package/build/plugin/src/ios/utils/index.js +38 -0
- package/build/plugin/src/ios/utils/index.js.map +1 -0
- package/build/plugin/src/ios/utils/paths.d.ts +128 -0
- package/build/plugin/src/ios/utils/paths.d.ts.map +1 -0
- package/build/plugin/src/ios/utils/paths.js +141 -0
- package/build/plugin/src/ios/utils/paths.js.map +1 -0
- package/build/plugin/src/ios/utils/plist.d.ts +29 -0
- package/build/plugin/src/ios/utils/plist.d.ts.map +1 -0
- package/build/plugin/src/ios/utils/plist.js +114 -0
- package/build/plugin/src/ios/utils/plist.js.map +1 -0
- package/build/plugin/src/ios/utils/podfile.d.ts +82 -0
- package/build/plugin/src/ios/utils/podfile.d.ts.map +1 -0
- package/build/plugin/src/ios/utils/podfile.js +408 -0
- package/build/plugin/src/ios/utils/podfile.js.map +1 -0
- package/build/plugin/src/ios/utils/reactNativeSwift.d.ts +11 -0
- package/build/plugin/src/ios/utils/reactNativeSwift.d.ts.map +1 -0
- package/build/plugin/src/ios/utils/reactNativeSwift.js +263 -0
- package/build/plugin/src/ios/utils/reactNativeSwift.js.map +1 -0
- package/build/plugin/src/ios/utils/xcode.d.ts +185 -0
- package/build/plugin/src/ios/utils/xcode.d.ts.map +1 -0
- package/build/plugin/src/ios/utils/xcode.js +706 -0
- package/build/plugin/src/ios/utils/xcode.js.map +1 -0
- package/build/plugin/src/logger.d.ts +10 -0
- package/build/plugin/src/logger.d.ts.map +1 -0
- package/build/plugin/src/logger.js +62 -0
- package/build/plugin/src/logger.js.map +1 -0
- package/build/plugin/src/parseTargetConfig.d.ts +3 -0
- package/build/plugin/src/parseTargetConfig.d.ts.map +1 -0
- package/build/plugin/src/parseTargetConfig.js +113 -0
- package/build/plugin/src/parseTargetConfig.js.map +1 -0
- package/build/plugin/src/withTargetsDir.d.ts +6 -0
- package/build/plugin/src/withTargetsDir.d.ts.map +1 -0
- package/build/plugin/src/withTargetsDir.js +90 -0
- package/build/plugin/src/withTargetsDir.js.map +1 -0
- package/build/src/Target.d.ts +43 -0
- package/build/src/Target.d.ts.map +1 -0
- package/build/src/Target.js +128 -0
- package/build/src/Target.js.map +1 -0
- package/build/src/index.d.ts +9 -0
- package/build/src/index.d.ts.map +1 -0
- package/build/src/index.js +27 -0
- package/build/src/index.js.map +1 -0
- package/build/src/modules/extension/index.d.ts +18 -0
- package/build/src/modules/extension/index.d.ts.map +1 -0
- package/build/src/modules/extension/index.js +44 -0
- package/build/src/modules/extension/index.js.map +1 -0
- package/build/src/modules/messages/index.d.ts +40 -0
- package/build/src/modules/messages/index.d.ts.map +1 -0
- package/build/src/modules/messages/index.js +94 -0
- package/build/src/modules/messages/index.js.map +1 -0
- package/build/src/modules/storage/index.d.ts +16 -0
- package/build/src/modules/storage/index.d.ts.map +1 -0
- package/build/src/modules/storage/index.js +122 -0
- package/build/src/modules/storage/index.js.map +1 -0
- package/expo-module.config.json +17 -0
- package/ios/ExpoTargetsExtension.podspec +23 -0
- package/ios/ExpoTargetsExtensionModule.swift +181 -0
- package/ios/ExpoTargetsMessages.podspec +23 -0
- package/ios/ExpoTargetsMessagesModule.swift +267 -0
- package/ios/ExpoTargetsStorage.podspec +23 -0
- package/ios/ExpoTargetsStorageModule.swift +91 -0
- package/ios/Tests/.swiftlint.yml +52 -0
- package/ios/Tests/Helpers/MockExtensionContext.swift +58 -0
- package/ios/Tests/Helpers/MockUserDefaults.swift +70 -0
- package/ios/Tests/Helpers/MockWidgetCenter.swift +71 -0
- package/ios/Tests/Helpers/TestHelpers.swift +90 -0
- package/ios/Tests/Integration/TargetIntegrationTests.swift +478 -0
- package/ios/Tests/Main/AllTests.swift +80 -0
- package/ios/Tests/Package.swift +66 -0
- package/ios/Tests/Performance/StoragePerformanceTests.swift +461 -0
- package/ios/Tests/Unit/Extension/ExpoTargetsExtensionModuleTests.swift +501 -0
- package/ios/Tests/Unit/Storage/ExpoTargetsStorageModuleTests.swift +578 -0
- package/ios/Tests/Unit/Template/ShareExtensionTemplateTests.swift +421 -0
- package/ios/Tests/XCTestPlan.xctestplan +87 -0
- package/metro/src/index.ts +1 -0
- package/metro/src/withTargetsMetro.ts +247 -0
- package/metro/tsconfig.build.json +9 -0
- package/package.json +74 -0
- package/plugin/src/android/withAndroidAppBuildGradle.ts +34 -0
- package/plugin/src/android/withAndroidTarget.ts +26 -0
- package/plugin/src/android/withAndroidWidget.ts +778 -0
- package/plugin/src/config.ts +255 -0
- package/plugin/src/index.ts +19 -0
- package/plugin/src/ios/config-plugins/withEntitlements.ts +69 -0
- package/plugin/src/ios/config-plugins/withIOSTarget.ts +179 -0
- package/plugin/src/ios/config-plugins/withIosColorset.ts +29 -0
- package/plugin/src/ios/config-plugins/withIosStickerPack.ts +65 -0
- package/plugin/src/ios/config-plugins/withPodfile.ts +238 -0
- package/plugin/src/ios/config-plugins/withXcodeChanges.ts +891 -0
- package/plugin/src/ios/target.ts +668 -0
- package/plugin/src/ios/templates/MessagesViewController.swift +120 -0
- package/plugin/src/ios/templates/ReactNativeViewController.swift +240 -0
- package/plugin/src/ios/templates/action-extension-data.swift +106 -0
- package/plugin/src/ios/templates/clip-extension-data.swift +28 -0
- package/plugin/src/ios/templates/messages-extension-data.swift +28 -0
- package/plugin/src/ios/templates/share-extension-data.swift +113 -0
- package/plugin/src/ios/utils/asset.ts +490 -0
- package/plugin/src/ios/utils/file.ts +80 -0
- package/plugin/src/ios/utils/index.ts +12 -0
- package/plugin/src/ios/utils/paths.ts +253 -0
- package/plugin/src/ios/utils/plist.ts +101 -0
- package/plugin/src/ios/utils/podfile.ts +534 -0
- package/plugin/src/ios/utils/reactNativeSwift.ts +283 -0
- package/plugin/src/ios/utils/xcode.ts +989 -0
- package/plugin/src/logger.ts +63 -0
- package/plugin/src/parseTargetConfig.ts +115 -0
- package/plugin/src/types/normalize-colors.d.ts +3 -0
- package/plugin/src/withTargetsDir.ts +120 -0
- package/plugin/tsconfig.build.json +9 -0
- package/plugin/tsconfig.json +9 -0
- package/src/Target.ts +239 -0
- package/src/index.ts +52 -0
- package/src/modules/extension/index.ts +54 -0
- package/src/modules/messages/index.ts +124 -0
- package/src/modules/storage/index.ts +126 -0
- package/tsconfig.json +8 -0
package/.eslintrc.js
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
# expo-targets
|
|
2
|
+
|
|
3
|
+
Add iOS widgets, App Clips, iMessage stickers, and other native extensions to your Expo app with a simple, type-safe API.
|
|
4
|
+
|
|
5
|
+
> **Part of the expo-targets monorepo**. See the [main README](../../README.md) for complete documentation.
|
|
6
|
+
|
|
7
|
+
## Quick Start
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
bun add expo-targets
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
// targets/hello-widget/index.ts
|
|
15
|
+
import { defineTarget } from 'expo-targets';
|
|
16
|
+
|
|
17
|
+
export const HelloWidget = defineTarget({
|
|
18
|
+
name: 'hello-widget',
|
|
19
|
+
appGroup: 'group.com.yourapp',
|
|
20
|
+
type: 'widget',
|
|
21
|
+
platforms: {
|
|
22
|
+
ios: {
|
|
23
|
+
deploymentTarget: '14.0',
|
|
24
|
+
colors: {
|
|
25
|
+
$accent: '#007AFF',
|
|
26
|
+
},
|
|
27
|
+
},
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
|
|
31
|
+
export type HelloWidgetData = {
|
|
32
|
+
message: string;
|
|
33
|
+
};
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
```typescript
|
|
37
|
+
// App.tsx
|
|
38
|
+
import { HelloWidget } from './targets/hello-widget';
|
|
39
|
+
|
|
40
|
+
HelloWidget.set('message', 'Hello Widget!');
|
|
41
|
+
HelloWidget.refresh();
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## Package Structure
|
|
45
|
+
|
|
46
|
+
This package contains four components:
|
|
47
|
+
|
|
48
|
+
### 1. TypeScript API (`src/`)
|
|
49
|
+
|
|
50
|
+
Runtime API for data sharing and widget control.
|
|
51
|
+
|
|
52
|
+
```typescript
|
|
53
|
+
import {
|
|
54
|
+
defineTarget,
|
|
55
|
+
refreshAllTargets,
|
|
56
|
+
close,
|
|
57
|
+
openHostApp,
|
|
58
|
+
} from 'expo-targets';
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
**Exports:**
|
|
62
|
+
|
|
63
|
+
- `defineTarget(options)`: Create type-safe target instance
|
|
64
|
+
- `TargetStorage`: Legacy storage class
|
|
65
|
+
- `AppGroupStorage`: Low-level storage class
|
|
66
|
+
- `refreshAllTargets()`: Refresh all widgets/controls
|
|
67
|
+
- `close()`: Close extension (share/action)
|
|
68
|
+
- `openHostApp(path)`: Open main app from extension
|
|
69
|
+
- `clearSharedData()`: Clear shared data
|
|
70
|
+
|
|
71
|
+
### 2. Config Plugin (`plugin/`)
|
|
72
|
+
|
|
73
|
+
Expo config plugin for automatic Xcode project setup.
|
|
74
|
+
|
|
75
|
+
```json
|
|
76
|
+
{
|
|
77
|
+
"expo": {
|
|
78
|
+
"plugins": ["expo-targets"]
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
**Features:**
|
|
84
|
+
|
|
85
|
+
- Scans `targets/*/index.ts` for `defineTarget()` calls
|
|
86
|
+
- Parses configuration using Babel AST
|
|
87
|
+
- Creates native Xcode targets
|
|
88
|
+
- Links Swift files from `targets/*/ios/`
|
|
89
|
+
- Generates color and image assets
|
|
90
|
+
- Syncs entitlements from main app
|
|
91
|
+
- Configures frameworks and build settings
|
|
92
|
+
|
|
93
|
+
### 3. Metro Wrapper (`metro/`)
|
|
94
|
+
|
|
95
|
+
Metro bundler wrapper for React Native extensions.
|
|
96
|
+
|
|
97
|
+
```typescript
|
|
98
|
+
import { withTargetsMetro } from 'expo-targets/metro';
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
**Usage:**
|
|
102
|
+
|
|
103
|
+
```javascript
|
|
104
|
+
// metro.config.js
|
|
105
|
+
const { getDefaultConfig } = require('expo/metro-config');
|
|
106
|
+
const { withTargetsMetro } = require('expo-targets/metro');
|
|
107
|
+
|
|
108
|
+
module.exports = withTargetsMetro(getDefaultConfig(__dirname));
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
**Required for:**
|
|
112
|
+
|
|
113
|
+
- Share extensions with React Native
|
|
114
|
+
- Action extensions with React Native
|
|
115
|
+
- App Clips with React Native
|
|
116
|
+
|
|
117
|
+
### 4. Native Module (`ios/`)
|
|
118
|
+
|
|
119
|
+
Swift module for data storage and widget lifecycle.
|
|
120
|
+
|
|
121
|
+
**Capabilities:**
|
|
122
|
+
|
|
123
|
+
- App Group storage via `UserDefaults`
|
|
124
|
+
- Widget refresh (`WidgetCenter` API)
|
|
125
|
+
- Control Center refresh (iOS 18+)
|
|
126
|
+
- Extension lifecycle management
|
|
127
|
+
- Deep linking to main app
|
|
128
|
+
|
|
129
|
+
## Exports
|
|
130
|
+
|
|
131
|
+
### Main Package
|
|
132
|
+
|
|
133
|
+
```typescript
|
|
134
|
+
// Core API
|
|
135
|
+
import {
|
|
136
|
+
defineTarget,
|
|
137
|
+
TargetStorage,
|
|
138
|
+
AppGroupStorage,
|
|
139
|
+
refreshAllTargets,
|
|
140
|
+
close,
|
|
141
|
+
openHostApp,
|
|
142
|
+
clearSharedData,
|
|
143
|
+
} from 'expo-targets';
|
|
144
|
+
|
|
145
|
+
// Types
|
|
146
|
+
import type {
|
|
147
|
+
Target,
|
|
148
|
+
DefineTargetOptions,
|
|
149
|
+
TargetConfig,
|
|
150
|
+
IOSTargetConfig,
|
|
151
|
+
AndroidTargetConfig,
|
|
152
|
+
ExtensionType,
|
|
153
|
+
Color,
|
|
154
|
+
} from 'expo-targets';
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
### Metro Subpath
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
import { withTargetsMetro } from 'expo-targets/metro';
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Development
|
|
164
|
+
|
|
165
|
+
### Build
|
|
166
|
+
|
|
167
|
+
```bash
|
|
168
|
+
bun run build # Build all components
|
|
169
|
+
bun run build:main # Build TypeScript API
|
|
170
|
+
bun run build:plugin # Build config plugin
|
|
171
|
+
bun run build:metro # Build Metro wrapper
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Clean
|
|
175
|
+
|
|
176
|
+
```bash
|
|
177
|
+
bun run clean # Remove all build artifacts
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Lint
|
|
181
|
+
|
|
182
|
+
```bash
|
|
183
|
+
bun run lint # Lint source code
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## Documentation
|
|
187
|
+
|
|
188
|
+
- **[Getting Started](../../docs/getting-started.md)**: Step-by-step tutorial
|
|
189
|
+
- **[API Reference](../../docs/api-reference.md)**: Complete API documentation
|
|
190
|
+
- **[Config Reference](../../docs/config-reference.md)**: Configuration options
|
|
191
|
+
- **[TypeScript Guide](../../docs/typescript-config-guide.md)**: Advanced patterns
|
|
192
|
+
- **[Main README](../../README.md)**: Project overview
|
|
193
|
+
|
|
194
|
+
## Examples
|
|
195
|
+
|
|
196
|
+
- **[widget-basic](../../apps/widget-basic/)**: Complete working widget with data sharing
|
|
197
|
+
|
|
198
|
+
## Features
|
|
199
|
+
|
|
200
|
+
- 🎯 **Type-Safe API**: Full TypeScript support with IDE autocomplete
|
|
201
|
+
- 📦 **Simple Configuration**: Single `index.ts` file per target
|
|
202
|
+
- 🔄 **Data Sharing**: Built-in App Group storage
|
|
203
|
+
- ⚛️ **React Native**: Optional RN rendering in compatible extensions
|
|
204
|
+
- 🎨 **Asset Generation**: Automatic colors and images
|
|
205
|
+
- 🔧 **Xcode Integration**: Full project manipulation
|
|
206
|
+
- 📱 **iOS Production Ready**: Widgets, clips, iMessage tested
|
|
207
|
+
|
|
208
|
+
## Platform Support
|
|
209
|
+
|
|
210
|
+
- **iOS**: ✅ Production ready (iOS 14+)
|
|
211
|
+
- **Android**: 📋 Coming soon (architecture prepared)
|
|
212
|
+
|
|
213
|
+
## Requirements
|
|
214
|
+
|
|
215
|
+
- Expo SDK 50+
|
|
216
|
+
- iOS 14+ (for widgets)
|
|
217
|
+
- macOS with Xcode 14+
|
|
218
|
+
- TypeScript recommended
|
|
219
|
+
|
|
220
|
+
## Version
|
|
221
|
+
|
|
222
|
+
Current version: **0.1.0**
|
|
223
|
+
|
|
224
|
+
See [CHANGELOG.md](../../CHANGELOG.md) for version history.
|
|
225
|
+
|
|
226
|
+
## License
|
|
227
|
+
|
|
228
|
+
[MIT](../../LICENSE)
|
|
229
|
+
|
|
230
|
+
## Contributing
|
|
231
|
+
|
|
232
|
+
See [Contributing Guide](../../CONTRIBUTING.md) (coming soon)
|
|
233
|
+
|
|
234
|
+
## Support
|
|
235
|
+
|
|
236
|
+
- **Issues**: [GitHub Issues](https://github.com/your-org/expo-targets/issues)
|
|
237
|
+
- **Documentation**: [docs/](../../docs/)
|
|
238
|
+
- **Examples**: [apps/](../../apps/)
|
|
239
|
+
|
|
240
|
+
## Credits
|
|
241
|
+
|
|
242
|
+
Part of expo-targets by [Your Organization]
|
|
243
|
+
|
|
244
|
+
Inspired by [@bacons/apple-targets](https://github.com/EvanBacon/expo-apple-targets), [expo-widgets](https://github.com/bittingz/expo-widgets), and others.
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
plugins {
|
|
2
|
+
id 'com.android.library'
|
|
3
|
+
id 'expo-module-gradle-plugin'
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
group = 'expo.modules.targets'
|
|
7
|
+
version = '0.1.0'
|
|
8
|
+
|
|
9
|
+
android {
|
|
10
|
+
namespace "expo.modules.targets"
|
|
11
|
+
defaultConfig {
|
|
12
|
+
versionCode 1
|
|
13
|
+
versionName "0.1.0"
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
dependencies {
|
|
18
|
+
// Glance dependencies for widget sync utilities
|
|
19
|
+
implementation "androidx.glance:glance-appwidget:1.1.1"
|
|
20
|
+
implementation "androidx.datastore:datastore-preferences:1.1.1"
|
|
21
|
+
}
|
|
22
|
+
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
package expo.modules.targets
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import android.content.pm.ApplicationInfo
|
|
5
|
+
import android.util.Log
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Debug logger for expo-targets
|
|
9
|
+
*
|
|
10
|
+
* Logging is enabled when:
|
|
11
|
+
* 1. The app is built in debug mode (ApplicationInfo.FLAG_DEBUGGABLE)
|
|
12
|
+
* 2. Or explicitly enabled via setDebugEnabled(true)
|
|
13
|
+
*
|
|
14
|
+
* Usage in logcat: `adb logcat -s ExpoTargets:*`
|
|
15
|
+
*/
|
|
16
|
+
object ExpoTargetsLogger {
|
|
17
|
+
private const val TAG = "ExpoTargets"
|
|
18
|
+
|
|
19
|
+
@Volatile
|
|
20
|
+
private var debugOverride: Boolean? = null
|
|
21
|
+
|
|
22
|
+
@Volatile
|
|
23
|
+
private var isDebuggable: Boolean? = null
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Explicitly enable or disable debug logging
|
|
27
|
+
*/
|
|
28
|
+
fun setDebugEnabled(enabled: Boolean) {
|
|
29
|
+
debugOverride = enabled
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Initialize logger with context (checks if app is debuggable)
|
|
34
|
+
*/
|
|
35
|
+
fun init(context: Context) {
|
|
36
|
+
if (isDebuggable == null) {
|
|
37
|
+
isDebuggable = (context.applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Check if debug logging is enabled
|
|
43
|
+
*/
|
|
44
|
+
fun isEnabled(): Boolean {
|
|
45
|
+
return debugOverride ?: isDebuggable ?: true
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
fun d(message: String) {
|
|
49
|
+
if (isEnabled()) {
|
|
50
|
+
Log.d(TAG, message)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
fun d(component: String, message: String) {
|
|
55
|
+
if (isEnabled()) {
|
|
56
|
+
Log.d(TAG, "[$component] $message")
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
fun i(message: String) {
|
|
61
|
+
if (isEnabled()) {
|
|
62
|
+
Log.i(TAG, message)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
fun i(component: String, message: String) {
|
|
67
|
+
if (isEnabled()) {
|
|
68
|
+
Log.i(TAG, "[$component] $message")
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
fun w(message: String) {
|
|
73
|
+
// Warnings always logged
|
|
74
|
+
Log.w(TAG, message)
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
fun w(component: String, message: String) {
|
|
78
|
+
Log.w(TAG, "[$component] $message")
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
fun e(message: String, throwable: Throwable? = null) {
|
|
82
|
+
// Errors always logged
|
|
83
|
+
if (throwable != null) {
|
|
84
|
+
Log.e(TAG, message, throwable)
|
|
85
|
+
} else {
|
|
86
|
+
Log.e(TAG, message)
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
fun e(component: String, message: String, throwable: Throwable? = null) {
|
|
91
|
+
if (throwable != null) {
|
|
92
|
+
Log.e(TAG, "[$component] $message", throwable)
|
|
93
|
+
} else {
|
|
94
|
+
Log.e(TAG, "[$component] $message")
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
package expo.modules.targets
|
|
2
|
+
|
|
3
|
+
import android.content.BroadcastReceiver
|
|
4
|
+
import android.content.ComponentName
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.content.Intent
|
|
7
|
+
import kotlin.concurrent.thread
|
|
8
|
+
|
|
9
|
+
open class ExpoTargetsReceiver : BroadcastReceiver() {
|
|
10
|
+
companion object {
|
|
11
|
+
private const val TAG = "Receiver"
|
|
12
|
+
const val WIDGET_EVENT_ACTION = "expo.modules.targets.WIDGET_EVENT"
|
|
13
|
+
|
|
14
|
+
fun refreshWidget(context: Context, widgetName: String) {
|
|
15
|
+
ExpoTargetsLogger.init(context)
|
|
16
|
+
ExpoTargetsLogger.d(TAG, "refreshWidget called: widgetName=$widgetName")
|
|
17
|
+
|
|
18
|
+
val intent = Intent(WIDGET_EVENT_ACTION).apply {
|
|
19
|
+
setPackage(context.packageName)
|
|
20
|
+
putExtra("EVENT_TYPE", "REFRESH")
|
|
21
|
+
putExtra("WIDGET_NAME", widgetName)
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
context.sendBroadcast(intent)
|
|
25
|
+
ExpoTargetsLogger.d(TAG, "Broadcast sent for widget: $widgetName")
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
override fun onReceive(context: Context, intent: Intent?) {
|
|
30
|
+
ExpoTargetsLogger.init(context)
|
|
31
|
+
ExpoTargetsLogger.d(TAG, "onReceive: action=${intent?.action}, extras=${intent?.extras?.keySet()?.toList()}")
|
|
32
|
+
|
|
33
|
+
val pendingIntent = goAsync()
|
|
34
|
+
thread {
|
|
35
|
+
try {
|
|
36
|
+
intent?.run { handleIntent(context, intent) }
|
|
37
|
+
} finally {
|
|
38
|
+
pendingIntent.finish()
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
private fun handleIntent(context: Context, intent: Intent) {
|
|
44
|
+
val eventType = intent.getStringExtra("EVENT_TYPE")
|
|
45
|
+
ExpoTargetsLogger.d(TAG, "handleIntent: eventType=$eventType")
|
|
46
|
+
|
|
47
|
+
when (eventType) {
|
|
48
|
+
"REFRESH" -> {
|
|
49
|
+
val widgetName = intent.getStringExtra("WIDGET_NAME")
|
|
50
|
+
ExpoTargetsLogger.d(TAG, "Handling REFRESH event for widget: $widgetName")
|
|
51
|
+
refreshWidgetViews(context, widgetName)
|
|
52
|
+
}
|
|
53
|
+
else -> {
|
|
54
|
+
ExpoTargetsLogger.d(TAG, "Unknown event type: $eventType")
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
private fun refreshWidgetViews(context: Context, widgetName: String?) {
|
|
60
|
+
ExpoTargetsLogger.d(TAG, "refreshWidgetViews: widgetName=$widgetName")
|
|
61
|
+
|
|
62
|
+
if (widgetName == null) {
|
|
63
|
+
ExpoTargetsLogger.w(TAG, "Widget name is null, cannot refresh")
|
|
64
|
+
return
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
try {
|
|
68
|
+
// Sanitize widget name same way as plugin does (replace non-alphanumeric with underscore)
|
|
69
|
+
val widgetNameSanitized = widgetName.lowercase().replace(Regex("[^a-z0-9_]"), "_")
|
|
70
|
+
// Convert to PascalCase for class name
|
|
71
|
+
val widgetNamePascal = widgetName.split(Regex("[-_]"))
|
|
72
|
+
.joinToString("") { it.replaceFirstChar { c -> c.uppercaseChar() } }
|
|
73
|
+
|
|
74
|
+
ExpoTargetsLogger.d(TAG, "Widget name transformations: original=$widgetName, sanitized=$widgetNameSanitized, pascal=$widgetNamePascal")
|
|
75
|
+
|
|
76
|
+
// Try Glance UpdateReceiver first (pattern: {packageName}.widget.{widgetnamelower}.{WidgetName}UpdateReceiver)
|
|
77
|
+
val updateReceiverClass = "${context.packageName}.widget.${widgetNameSanitized}.${widgetNamePascal}UpdateReceiver"
|
|
78
|
+
val updateComponentName = ComponentName(context.packageName, updateReceiverClass)
|
|
79
|
+
|
|
80
|
+
// Check if Glance UpdateReceiver exists
|
|
81
|
+
val packageManager = context.packageManager
|
|
82
|
+
val updateReceiverInfo = try {
|
|
83
|
+
packageManager.getReceiverInfo(updateComponentName, 0)
|
|
84
|
+
true
|
|
85
|
+
} catch (e: Exception) {
|
|
86
|
+
ExpoTargetsLogger.d(TAG, "Glance UpdateReceiver not found: $updateReceiverClass (${e.message})")
|
|
87
|
+
false
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
if (updateReceiverInfo) {
|
|
91
|
+
// Glance widget - send direct update
|
|
92
|
+
val updateIntent = Intent().apply {
|
|
93
|
+
component = updateComponentName
|
|
94
|
+
action = "expo.modules.targets.UPDATE_WIDGET"
|
|
95
|
+
}
|
|
96
|
+
ExpoTargetsLogger.d(TAG, "Sending direct update to Glance widget: $updateReceiverClass")
|
|
97
|
+
context.sendBroadcast(updateIntent)
|
|
98
|
+
} else {
|
|
99
|
+
// Try RemoteViews Provider (pattern: {packageName}.widget.{widgetnamelower}.{WidgetName}Provider)
|
|
100
|
+
val providerClass = "${context.packageName}.widget.${widgetNameSanitized}.${widgetNamePascal}Provider"
|
|
101
|
+
val providerComponentName = ComponentName(context.packageName, providerClass)
|
|
102
|
+
|
|
103
|
+
ExpoTargetsLogger.d(TAG, "Looking for RemoteViews Provider: $providerClass")
|
|
104
|
+
|
|
105
|
+
val providerInfo = try {
|
|
106
|
+
packageManager.getReceiverInfo(providerComponentName, 0)
|
|
107
|
+
true
|
|
108
|
+
} catch (e: Exception) {
|
|
109
|
+
ExpoTargetsLogger.d(TAG, "RemoteViews Provider not found: $providerClass (${e.message})")
|
|
110
|
+
false
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
if (providerInfo) {
|
|
114
|
+
ExpoTargetsLogger.d(TAG, "Found RemoteViews Provider: $providerClass")
|
|
115
|
+
|
|
116
|
+
// RemoteViews widget - send direct intent to provider to trigger onReceive -> onUpdate
|
|
117
|
+
val refreshIntent = Intent(WIDGET_EVENT_ACTION).apply {
|
|
118
|
+
component = providerComponentName
|
|
119
|
+
putExtra("EVENT_TYPE", "REFRESH")
|
|
120
|
+
putExtra("WIDGET_NAME", widgetName)
|
|
121
|
+
}
|
|
122
|
+
ExpoTargetsLogger.d(TAG, "Sending direct refresh intent to RemoteViews provider: $providerClass")
|
|
123
|
+
context.sendBroadcast(refreshIntent)
|
|
124
|
+
} else {
|
|
125
|
+
ExpoTargetsLogger.w(TAG, "Neither Glance UpdateReceiver nor RemoteViews Provider found for widget: $widgetName")
|
|
126
|
+
ExpoTargetsLogger.w(TAG, "Tried: Glance=$updateReceiverClass, RemoteViews=$providerClass")
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
ExpoTargetsLogger.i(TAG, "Successfully triggered refresh for $widgetName")
|
|
131
|
+
} catch (e: Exception) {
|
|
132
|
+
ExpoTargetsLogger.e(TAG, "Failed to refresh widget $widgetName", e)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
package expo.modules.targets
|
|
2
|
+
|
|
3
|
+
import android.appwidget.AppWidgetManager
|
|
4
|
+
import android.appwidget.AppWidgetProvider
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.content.Intent
|
|
7
|
+
import android.widget.RemoteViews
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* Base provider for RemoteViews widgets.
|
|
11
|
+
* Automatically reads from SharedPreferences and calls updateWidget().
|
|
12
|
+
*/
|
|
13
|
+
abstract class ExpoTargetsRemoteViewsProvider(
|
|
14
|
+
private val prefsName: String
|
|
15
|
+
) : AppWidgetProvider() {
|
|
16
|
+
|
|
17
|
+
companion object {
|
|
18
|
+
private const val TAG = "RemoteViewsProvider"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** Layout resource ID for the widget */
|
|
22
|
+
abstract val layoutResId: Int
|
|
23
|
+
|
|
24
|
+
override fun onUpdate(context: Context, manager: AppWidgetManager, appWidgetIds: IntArray) {
|
|
25
|
+
ExpoTargetsLogger.init(context)
|
|
26
|
+
ExpoTargetsLogger.d(TAG, "onUpdate called: prefsName=$prefsName, widgetIds=${appWidgetIds.toList()}")
|
|
27
|
+
|
|
28
|
+
val prefs = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE)
|
|
29
|
+
val data = prefs.all
|
|
30
|
+
ExpoTargetsLogger.d(TAG, "SharedPreferences data: $data")
|
|
31
|
+
|
|
32
|
+
for (widgetId in appWidgetIds) {
|
|
33
|
+
val views = RemoteViews(context.packageName, layoutResId)
|
|
34
|
+
updateWidget(context, views, data)
|
|
35
|
+
manager.updateAppWidget(widgetId, views)
|
|
36
|
+
ExpoTargetsLogger.d(TAG, "Updated widget id=$widgetId")
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
ExpoTargetsLogger.d(TAG, "Updated ${appWidgetIds.size} widget(s)")
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
override fun onReceive(context: Context, intent: Intent) {
|
|
43
|
+
ExpoTargetsLogger.init(context)
|
|
44
|
+
ExpoTargetsLogger.d(TAG, "onReceive: action=${intent.action}, component=${intent.component}, extras=${intent.extras?.keySet()?.toList()}")
|
|
45
|
+
|
|
46
|
+
super.onReceive(context, intent)
|
|
47
|
+
|
|
48
|
+
// Handle expo-targets refresh broadcasts
|
|
49
|
+
if (intent.action == ExpoTargetsReceiver.WIDGET_EVENT_ACTION ||
|
|
50
|
+
intent.action == "expo.modules.targets.UPDATE_WIDGET") {
|
|
51
|
+
ExpoTargetsLogger.d(TAG, "Handling expo-targets refresh broadcast")
|
|
52
|
+
val manager = AppWidgetManager.getInstance(context)
|
|
53
|
+
val componentName = android.content.ComponentName(context, this::class.java)
|
|
54
|
+
val ids = manager.getAppWidgetIds(componentName)
|
|
55
|
+
ExpoTargetsLogger.d(TAG, "Widget IDs for ${this::class.java.name}: ${ids.toList()}")
|
|
56
|
+
if (ids.isNotEmpty()) {
|
|
57
|
+
onUpdate(context, manager, ids)
|
|
58
|
+
} else {
|
|
59
|
+
ExpoTargetsLogger.w(TAG, "No widget instances found for component: ${componentName.className}")
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Override to populate the RemoteViews with data.
|
|
66
|
+
* @param context Application context
|
|
67
|
+
* @param views RemoteViews to populate
|
|
68
|
+
* @param data All key-value pairs from SharedPreferences
|
|
69
|
+
*/
|
|
70
|
+
abstract fun updateWidget(context: Context, views: RemoteViews, data: Map<String, *>)
|
|
71
|
+
}
|
|
72
|
+
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
package expo.modules.targets
|
|
2
|
+
|
|
3
|
+
import android.appwidget.AppWidgetManager
|
|
4
|
+
import android.appwidget.AppWidgetProvider
|
|
5
|
+
import android.content.Context
|
|
6
|
+
import android.content.SharedPreferences
|
|
7
|
+
|
|
8
|
+
abstract class ExpoTargetsWidgetProvider : AppWidgetProvider() {
|
|
9
|
+
|
|
10
|
+
abstract fun getWidgetName(): String
|
|
11
|
+
|
|
12
|
+
override fun onUpdate(
|
|
13
|
+
context: Context,
|
|
14
|
+
appWidgetManager: AppWidgetManager,
|
|
15
|
+
appWidgetIds: IntArray
|
|
16
|
+
) {
|
|
17
|
+
super.onUpdate(context, appWidgetManager, appWidgetIds)
|
|
18
|
+
|
|
19
|
+
for (appWidgetId in appWidgetIds) {
|
|
20
|
+
renderWidget(context, appWidgetManager, appWidgetId)
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
override fun onDeleted(context: Context, appWidgetIds: IntArray) {
|
|
25
|
+
super.onDeleted(context, appWidgetIds)
|
|
26
|
+
|
|
27
|
+
for (appWidgetId in appWidgetIds) {
|
|
28
|
+
getWidgetPreferences(context).edit().clear().apply()
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
protected fun getWidgetPreferences(context: Context): SharedPreferences {
|
|
33
|
+
return context.getSharedPreferences(
|
|
34
|
+
"expo_targets_${getWidgetName()}",
|
|
35
|
+
Context.MODE_PRIVATE
|
|
36
|
+
)
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
protected open fun renderWidget(
|
|
40
|
+
context: Context,
|
|
41
|
+
appWidgetManager: AppWidgetManager,
|
|
42
|
+
appWidgetId: Int
|
|
43
|
+
) {
|
|
44
|
+
// Override in user's widget implementation
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
package expo.modules.targets
|
|
2
|
+
|
|
3
|
+
import android.content.Context
|
|
4
|
+
import androidx.datastore.preferences.core.MutablePreferences
|
|
5
|
+
import androidx.datastore.preferences.core.Preferences
|
|
6
|
+
import androidx.datastore.preferences.core.booleanPreferencesKey
|
|
7
|
+
import androidx.datastore.preferences.core.floatPreferencesKey
|
|
8
|
+
import androidx.datastore.preferences.core.intPreferencesKey
|
|
9
|
+
import androidx.datastore.preferences.core.longPreferencesKey
|
|
10
|
+
import androidx.datastore.preferences.core.stringPreferencesKey
|
|
11
|
+
import androidx.datastore.preferences.core.stringSetPreferencesKey
|
|
12
|
+
import androidx.glance.GlanceId
|
|
13
|
+
import androidx.glance.appwidget.GlanceAppWidget
|
|
14
|
+
import androidx.glance.appwidget.GlanceAppWidgetManager
|
|
15
|
+
import androidx.glance.appwidget.state.updateAppWidgetState
|
|
16
|
+
import androidx.glance.state.GlanceStateDefinition
|
|
17
|
+
import kotlin.reflect.KClass
|
|
18
|
+
|
|
19
|
+
/** Syncs SharedPreferences to Glance state, mirroring iOS WidgetKit behavior. */
|
|
20
|
+
object ExpoTargetsWidgetSync {
|
|
21
|
+
private const val TAG = "WidgetSync"
|
|
22
|
+
|
|
23
|
+
suspend fun <T : GlanceAppWidget> syncAndUpdate(
|
|
24
|
+
context: Context,
|
|
25
|
+
widgetClass: KClass<T>,
|
|
26
|
+
prefsName: String
|
|
27
|
+
) {
|
|
28
|
+
ExpoTargetsLogger.init(context)
|
|
29
|
+
|
|
30
|
+
val sharedPrefs = context.getSharedPreferences(prefsName, Context.MODE_PRIVATE)
|
|
31
|
+
val allData = sharedPrefs.all
|
|
32
|
+
|
|
33
|
+
val manager = GlanceAppWidgetManager(context)
|
|
34
|
+
val glanceIds = manager.getGlanceIds(widgetClass.java)
|
|
35
|
+
|
|
36
|
+
if (glanceIds.isEmpty()) return
|
|
37
|
+
|
|
38
|
+
val widget = widgetClass.java.getDeclaredConstructor().newInstance()
|
|
39
|
+
|
|
40
|
+
for (glanceId in glanceIds) {
|
|
41
|
+
@Suppress("UNCHECKED_CAST")
|
|
42
|
+
val stateDefinition = widget.stateDefinition as GlanceStateDefinition<Preferences>
|
|
43
|
+
syncToGlanceState(context, stateDefinition, glanceId, allData)
|
|
44
|
+
widget.update(context, glanceId)
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
ExpoTargetsLogger.d(TAG, "Synced ${allData.size} keys → ${glanceIds.size} widget(s)")
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
private suspend fun syncToGlanceState(
|
|
51
|
+
context: Context,
|
|
52
|
+
stateDefinition: GlanceStateDefinition<Preferences>,
|
|
53
|
+
glanceId: GlanceId,
|
|
54
|
+
data: Map<String, *>
|
|
55
|
+
) {
|
|
56
|
+
updateAppWidgetState(context, stateDefinition, glanceId) { prefs: Preferences ->
|
|
57
|
+
prefs.toMutablePreferences().also { mutablePrefs: MutablePreferences ->
|
|
58
|
+
mutablePrefs.clear()
|
|
59
|
+
|
|
60
|
+
data.forEach { (key, value) ->
|
|
61
|
+
when (value) {
|
|
62
|
+
is Int -> mutablePrefs[intPreferencesKey(key)] = value
|
|
63
|
+
is Long -> mutablePrefs[longPreferencesKey(key)] = value
|
|
64
|
+
is Float -> mutablePrefs[floatPreferencesKey(key)] = value
|
|
65
|
+
is Boolean -> mutablePrefs[booleanPreferencesKey(key)] = value
|
|
66
|
+
is String -> mutablePrefs[stringPreferencesKey(key)] = value
|
|
67
|
+
is Set<*> -> {
|
|
68
|
+
@Suppress("UNCHECKED_CAST")
|
|
69
|
+
mutablePrefs[stringSetPreferencesKey(key)] = value as Set<String>
|
|
70
|
+
}
|
|
71
|
+
else -> mutablePrefs[stringPreferencesKey(key)] = value.toString()
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|