@independo/capacitor-emoji-picker 1.0.0-dev.1
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/IndependoCapacitorEmojiPicker.podspec +17 -0
- package/LICENSE +21 -0
- package/Package.swift +28 -0
- package/README.md +223 -0
- package/android/build.gradle +109 -0
- package/android/src/main/AndroidManifest.xml +2 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/EmojiPicker.java +82 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/EmojiPickerWebBridgeInterface.java +28 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/core/EmojiCloseButtonOptions.java +18 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/core/EmojiPickerCallback.java +11 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/core/EmojiPickerResult.java +16 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/core/ErrorCodes.java +14 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/presenter/DefaultEmojiPickerDialogFactory.java +35 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/presenter/DispatchingEmojiPickerPresenter.java +26 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/presenter/EmojiPickerDialogFactory.java +26 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/presenter/EmojiPickerPresenter.java +16 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/presenter/NativeEmojiPickerPresenter.java +220 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/presenter/WebFallbackEmojiPickerPresenter.java +228 -0
- package/android/src/main/java/app/independo/capacitoremojipicker/service/EmojiPickerService.java +46 -0
- package/android/src/main/res/.gitkeep +0 -0
- package/dist/docs.json +169 -0
- package/dist/esm/core/error-codes.d.ts +9 -0
- package/dist/esm/core/error-codes.js +10 -0
- package/dist/esm/core/error-codes.js.map +1 -0
- package/dist/esm/definitions.d.ts +81 -0
- package/dist/esm/definitions.js +2 -0
- package/dist/esm/definitions.js.map +1 -0
- package/dist/esm/index.d.ts +4 -0
- package/dist/esm/index.js +9 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/platform/web/EmojiPickerWebAdapter.d.ts +10 -0
- package/dist/esm/platform/web/EmojiPickerWebAdapter.js +12 -0
- package/dist/esm/platform/web/EmojiPickerWebAdapter.js.map +1 -0
- package/dist/esm/platform/web/WebEmojiPickerPresenter.d.ts +22 -0
- package/dist/esm/platform/web/WebEmojiPickerPresenter.js +292 -0
- package/dist/esm/platform/web/WebEmojiPickerPresenter.js.map +1 -0
- package/dist/esm/platform/web/emoji-data-source.d.ts +5 -0
- package/dist/esm/platform/web/emoji-data-source.js +16 -0
- package/dist/esm/platform/web/emoji-data-source.js.map +1 -0
- package/dist/esm/platform/web/nativeWebBridge.d.ts +29 -0
- package/dist/esm/platform/web/nativeWebBridge.js +58 -0
- package/dist/esm/platform/web/nativeWebBridge.js.map +1 -0
- package/dist/esm/service/EmojiPickerService.d.ts +16 -0
- package/dist/esm/service/EmojiPickerService.js +23 -0
- package/dist/esm/service/EmojiPickerService.js.map +1 -0
- package/dist/esm/web.d.ts +9 -0
- package/dist/esm/web.js +16 -0
- package/dist/esm/web.js.map +1 -0
- package/dist/plugin.cjs.js +3274 -0
- package/dist/plugin.cjs.js.map +1 -0
- package/dist/plugin.js +3277 -0
- package/dist/plugin.js.map +1 -0
- package/ios/Sources/EmojiPicker/Bridge/EmojiPicker.swift +79 -0
- package/ios/Sources/EmojiPicker/Bridge/EmojiPickerScriptMessageHandler.swift +19 -0
- package/ios/Sources/EmojiPicker/Core/EmojiPickerResult.swift +12 -0
- package/ios/Sources/EmojiPicker/Core/ErrorCodes.swift +12 -0
- package/ios/Sources/EmojiPicker/Presenter/DefaultEmojiKeyboardPresentationFactory.swift +370 -0
- package/ios/Sources/EmojiPicker/Presenter/DispatchingEmojiPickerPresenter.swift +19 -0
- package/ios/Sources/EmojiPicker/Presenter/EmojiKeyboardPresentationFactory.swift +36 -0
- package/ios/Sources/EmojiPicker/Presenter/EmojiPickerPresenter.swift +31 -0
- package/ios/Sources/EmojiPicker/Presenter/NativeEmojiPickerPresenter.swift +186 -0
- package/ios/Sources/EmojiPicker/Presenter/WebFallbackEmojiPickerPresenter.swift +122 -0
- package/ios/Sources/EmojiPicker/Service/EmojiPickerService.swift +25 -0
- package/package.json +120 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
package app.independo.capacitoremojipicker.presenter;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Builds the native picker UI.
|
|
7
|
+
*
|
|
8
|
+
* Isolates dialog construction from {@link NativeEmojiPickerPresenter} so the presenter's
|
|
9
|
+
* settle-once/reentry logic can be unit-tested with a fake, without a real {@code BottomSheetDialog}.
|
|
10
|
+
*/
|
|
11
|
+
public interface EmojiPickerDialogFactory {
|
|
12
|
+
|
|
13
|
+
/** Creates and shows the dialog for the given activity, reporting outcomes via {@code listener}. */
|
|
14
|
+
EmojiPickerDialogHandle create(Activity activity, boolean dismissOnBackdropTap, Listener listener);
|
|
15
|
+
|
|
16
|
+
/** Callback used by the factory's dialog implementation to report selection/dismissal. */
|
|
17
|
+
interface Listener {
|
|
18
|
+
void onEmojiSelected(String emoji);
|
|
19
|
+
void onDismissed();
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/** Handle allowing the presenter to dismiss a previously-shown dialog defensively. */
|
|
23
|
+
interface EmojiPickerDialogHandle {
|
|
24
|
+
void dismiss();
|
|
25
|
+
}
|
|
26
|
+
}
|
package/android/src/main/java/app/independo/capacitoremojipicker/presenter/EmojiPickerPresenter.java
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
package app.independo.capacitoremojipicker.presenter;
|
|
2
|
+
|
|
3
|
+
import app.independo.capacitoremojipicker.core.EmojiCloseButtonOptions;
|
|
4
|
+
import app.independo.capacitoremojipicker.core.EmojiPickerCallback;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Performs the platform-specific presentation.
|
|
8
|
+
*
|
|
9
|
+
* Kept separate from the Capacitor bridge and service so the native picker UI (issue #4) can be
|
|
10
|
+
* implemented without touching plugin wiring or the concurrency-guard logic.
|
|
11
|
+
*/
|
|
12
|
+
public interface EmojiPickerPresenter {
|
|
13
|
+
|
|
14
|
+
/** Presents the picker for the given presentation mode and reports the outcome. */
|
|
15
|
+
void present(String presentation, boolean dismissOnBackdropTap, EmojiCloseButtonOptions closeButton, EmojiPickerCallback callback);
|
|
16
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
package app.independo.capacitoremojipicker.presenter;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import androidx.lifecycle.DefaultLifecycleObserver;
|
|
5
|
+
import androidx.lifecycle.LifecycleOwner;
|
|
6
|
+
import app.independo.capacitoremojipicker.core.EmojiCloseButtonOptions;
|
|
7
|
+
import app.independo.capacitoremojipicker.core.EmojiPickerCallback;
|
|
8
|
+
import app.independo.capacitoremojipicker.core.EmojiPickerResult;
|
|
9
|
+
import app.independo.capacitoremojipicker.core.ErrorCodes;
|
|
10
|
+
|
|
11
|
+
/** Presents the native AndroidX emoji picker via an injected {@link EmojiPickerDialogFactory}. */
|
|
12
|
+
public class NativeEmojiPickerPresenter implements EmojiPickerPresenter {
|
|
13
|
+
|
|
14
|
+
/** Supplies the current hosting Activity; returns null if none is available. */
|
|
15
|
+
public interface ActivityProvider {
|
|
16
|
+
Activity get();
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Decides whether an Activity can currently host a dialog.
|
|
21
|
+
*
|
|
22
|
+
* Isolated from {@link #present} so unit tests can inject a fake that never calls real
|
|
23
|
+
* {@code Activity#isFinishing()}/{@code #isDestroyed()} methods, which throw under the
|
|
24
|
+
* Android Gradle Plugin's mockable android.jar outside of Robolectric or instrumented tests.
|
|
25
|
+
*/
|
|
26
|
+
interface ActivityAvailabilityChecker {
|
|
27
|
+
boolean isAvailable(Activity activity);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** Default checker used in production: mirrors the standard Activity-availability contract. */
|
|
31
|
+
private static final ActivityAvailabilityChecker DEFAULT_AVAILABILITY_CHECKER = (activity) ->
|
|
32
|
+
activity != null && !activity.isFinishing() && !activity.isDestroyed();
|
|
33
|
+
|
|
34
|
+
private final ActivityProvider activityProvider;
|
|
35
|
+
private final EmojiPickerDialogFactory dialogFactory;
|
|
36
|
+
private final ActivityAvailabilityChecker availabilityChecker;
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The presentation currently owning the dialog/lifecycle-observer, if any. Only the {@link
|
|
40
|
+
* Presentation} instance that is still {@code current} may settle and clear this field; a
|
|
41
|
+
* stale presentation whose async dismissal callback fires after a newer one has taken over
|
|
42
|
+
* must not be able to touch it. See {@link Presentation#settle}.
|
|
43
|
+
*/
|
|
44
|
+
private Presentation current;
|
|
45
|
+
|
|
46
|
+
public NativeEmojiPickerPresenter(ActivityProvider activityProvider, EmojiPickerDialogFactory dialogFactory) {
|
|
47
|
+
this(activityProvider, dialogFactory, DEFAULT_AVAILABILITY_CHECKER);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** Package-private: lets tests inject a fake {@link ActivityAvailabilityChecker}. */
|
|
51
|
+
NativeEmojiPickerPresenter(
|
|
52
|
+
ActivityProvider activityProvider,
|
|
53
|
+
EmojiPickerDialogFactory dialogFactory,
|
|
54
|
+
ActivityAvailabilityChecker availabilityChecker
|
|
55
|
+
) {
|
|
56
|
+
this.activityProvider = activityProvider;
|
|
57
|
+
this.dialogFactory = dialogFactory;
|
|
58
|
+
this.availabilityChecker = availabilityChecker;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
@Override
|
|
62
|
+
public void present(String presentation, boolean dismissOnBackdropTap, EmojiCloseButtonOptions closeButton, EmojiPickerCallback callback) {
|
|
63
|
+
if (!"auto".equals(presentation)) {
|
|
64
|
+
// 'web' (and any future non-native presentation) isn't implemented on Android yet;
|
|
65
|
+
// honestly reject rather than silently showing the native picker instead.
|
|
66
|
+
callback.onError(ErrorCodes.NOT_IMPLEMENTED);
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
Activity activity = activityProvider.get();
|
|
71
|
+
Runnable work = () -> presentOnUiThread(activity, dismissOnBackdropTap, callback);
|
|
72
|
+
// Capacitor invokes @PluginMethod handlers on a background "CapacitorPlugins" thread, but
|
|
73
|
+
// BottomSheetDialog#show() and Lifecycle#addObserver() are UI-thread-only APIs. Hop onto the
|
|
74
|
+
// UI thread whenever we have a real Activity to hop through; a null Activity means there's no
|
|
75
|
+
// UI to touch, so run synchronously and let the availability check below report the error.
|
|
76
|
+
if (activity != null) {
|
|
77
|
+
activity.runOnUiThread(work);
|
|
78
|
+
} else {
|
|
79
|
+
work.run();
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
private void presentOnUiThread(Activity activity, boolean dismissOnBackdropTap, EmojiPickerCallback callback) {
|
|
84
|
+
if (!availabilityChecker.isAvailable(activity)) {
|
|
85
|
+
callback.onError(ErrorCodes.NATIVE_UNAVAILABLE);
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Dismiss the prior presentation's dialog, but do NOT clear `current` here: a real
|
|
90
|
+
// BottomSheetDialog#dismiss() delivers its OnDismissListener asynchronously (posted to the
|
|
91
|
+
// message queue), even from the UI thread. The old Presentation's own settle() - triggered
|
|
92
|
+
// whenever that callback eventually fires - will clear `current` itself, and only if it's
|
|
93
|
+
// still the active one, so it can never clobber the new Presentation created below.
|
|
94
|
+
if (current != null) {
|
|
95
|
+
current.dismissDialog();
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
Presentation presentation = new Presentation(callback);
|
|
99
|
+
LifecycleOwner lifecycleOwner = (activity instanceof LifecycleOwner) ? (LifecycleOwner) activity : null;
|
|
100
|
+
current = presentation;
|
|
101
|
+
presentation.start(activity, dismissOnBackdropTap, lifecycleOwner);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Owns everything about ONE presentation attempt: its dialog handle, its lifecycle observer,
|
|
106
|
+
* and whether it has already settled. Identity - not a shared field - determines whether a
|
|
107
|
+
* (possibly late-arriving, async) callback is allowed to mutate {@code current}.
|
|
108
|
+
*/
|
|
109
|
+
private final class Presentation {
|
|
110
|
+
|
|
111
|
+
private final EmojiPickerCallback callback;
|
|
112
|
+
private EmojiPickerDialogFactory.EmojiPickerDialogHandle handle;
|
|
113
|
+
private DefaultLifecycleObserver observer;
|
|
114
|
+
private LifecycleOwner lifecycleOwner;
|
|
115
|
+
private boolean settled;
|
|
116
|
+
|
|
117
|
+
Presentation(EmojiPickerCallback callback) {
|
|
118
|
+
this.callback = callback;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
void start(Activity activity, boolean dismissOnBackdropTap, LifecycleOwner lifecycleOwner) {
|
|
122
|
+
this.lifecycleOwner = lifecycleOwner;
|
|
123
|
+
|
|
124
|
+
EmojiPickerDialogFactory.EmojiPickerDialogHandle createdHandle;
|
|
125
|
+
try {
|
|
126
|
+
createdHandle = dialogFactory.create(
|
|
127
|
+
activity,
|
|
128
|
+
dismissOnBackdropTap,
|
|
129
|
+
new EmojiPickerDialogFactory.Listener() {
|
|
130
|
+
@Override
|
|
131
|
+
public void onEmojiSelected(String emoji) {
|
|
132
|
+
settle(new EmojiPickerResult(emoji));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
@Override
|
|
136
|
+
public void onDismissed() {
|
|
137
|
+
settle(new EmojiPickerResult(null));
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
);
|
|
141
|
+
} catch (RuntimeException e) {
|
|
142
|
+
// e.g. BadTokenException if the Activity finished in the window between the
|
|
143
|
+
// availability check and dialog creation/show. Settle with NATIVE_UNAVAILABLE
|
|
144
|
+
// instead of letting the exception propagate uncaught out of the UI-thread
|
|
145
|
+
// Runnable, which would crash the app and leave isPresenting stuck forever.
|
|
146
|
+
settleWithError(ErrorCodes.NATIVE_UNAVAILABLE);
|
|
147
|
+
return;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
if (settled) {
|
|
151
|
+
// Defensive: if create() somehow synchronously settled us (e.g. immediate
|
|
152
|
+
// dismissal), don't leak the handle - dismiss it right away.
|
|
153
|
+
try {
|
|
154
|
+
createdHandle.dismiss();
|
|
155
|
+
} catch (RuntimeException ignored) {
|
|
156
|
+
// Window may already be gone.
|
|
157
|
+
}
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
this.handle = createdHandle;
|
|
162
|
+
|
|
163
|
+
if (lifecycleOwner != null) {
|
|
164
|
+
DefaultLifecycleObserver lifecycleObserver = new DefaultLifecycleObserver() {
|
|
165
|
+
@Override
|
|
166
|
+
public void onDestroy(LifecycleOwner owner) {
|
|
167
|
+
// Dismiss before settling: dismissing a dialog whose window has already
|
|
168
|
+
// been torn down alongside the Activity can throw (e.g.
|
|
169
|
+
// IllegalArgumentException: "View not attached to window manager"), which
|
|
170
|
+
// dismissDialog() guards against.
|
|
171
|
+
dismissDialog();
|
|
172
|
+
settle(new EmojiPickerResult(null));
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
this.observer = lifecycleObserver;
|
|
176
|
+
lifecycleOwner.getLifecycle().addObserver(lifecycleObserver);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Dismisses this presentation's dialog handle, if any. Does not settle synchronously. */
|
|
181
|
+
void dismissDialog() {
|
|
182
|
+
if (handle != null) {
|
|
183
|
+
try {
|
|
184
|
+
handle.dismiss();
|
|
185
|
+
} catch (RuntimeException e) {
|
|
186
|
+
// Window may already be gone.
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
private void settleWithError(String errorCode) {
|
|
192
|
+
if (settled) {
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
settled = true;
|
|
196
|
+
closeOut();
|
|
197
|
+
callback.onError(errorCode);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
private void settle(EmojiPickerResult result) {
|
|
201
|
+
if (settled) {
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
settled = true;
|
|
205
|
+
closeOut();
|
|
206
|
+
callback.onResult(result);
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Removes the lifecycle observer and, if still current, clears `current`. Never touches
|
|
210
|
+
* another Presentation instance's state. */
|
|
211
|
+
private void closeOut() {
|
|
212
|
+
if (observer != null && lifecycleOwner != null) {
|
|
213
|
+
lifecycleOwner.getLifecycle().removeObserver(observer);
|
|
214
|
+
}
|
|
215
|
+
if (current == this) {
|
|
216
|
+
current = null;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
package app.independo.capacitoremojipicker.presenter;
|
|
2
|
+
|
|
3
|
+
import android.app.Activity;
|
|
4
|
+
import android.os.Handler;
|
|
5
|
+
import android.os.Looper;
|
|
6
|
+
import androidx.lifecycle.DefaultLifecycleObserver;
|
|
7
|
+
import androidx.lifecycle.LifecycleOwner;
|
|
8
|
+
import app.independo.capacitoremojipicker.core.EmojiCloseButtonOptions;
|
|
9
|
+
import app.independo.capacitoremojipicker.core.EmojiPickerCallback;
|
|
10
|
+
import app.independo.capacitoremojipicker.core.EmojiPickerResult;
|
|
11
|
+
import app.independo.capacitoremojipicker.core.ErrorCodes;
|
|
12
|
+
import java.util.HashMap;
|
|
13
|
+
import java.util.Map;
|
|
14
|
+
import java.util.UUID;
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Presents the web bottom sheet inside the app's own Capacitor webview by evaluating JS there
|
|
18
|
+
* (registered by {@code registerNativeWebBridge()} in the JS layer), and resolves once that JS
|
|
19
|
+
* reports back via {@link #onWebResult}. Kept independent of any real {@code Bridge}/{@code
|
|
20
|
+
* WebView} type so it stays unit-testable without the Android Gradle Plugin's mockable
|
|
21
|
+
* {@code android.jar}: production wiring in {@code EmojiPicker#load()} supplies a {@link
|
|
22
|
+
* JsEvaluator} backed by {@code Bridge#eval} and an {@link ActivityProvider}; tests supply fakes.
|
|
23
|
+
*/
|
|
24
|
+
public class WebFallbackEmojiPickerPresenter implements EmojiPickerPresenter {
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Evaluates JS in the app's webview. Abstracts away the real Capacitor {@code Bridge}.
|
|
28
|
+
* {@code onEvaluated} fires once the script has at least started executing (i.e. the
|
|
29
|
+
* bridge/webview is alive) - it is a liveness signal, not a signal that the picker has
|
|
30
|
+
* settled.
|
|
31
|
+
*/
|
|
32
|
+
public interface JsEvaluator {
|
|
33
|
+
void eval(String js, Runnable onEvaluated);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** Supplies the current hosting Activity; returns null if none is available. */
|
|
37
|
+
public interface ActivityProvider {
|
|
38
|
+
Activity get();
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** Posts/cancels delayed work. Abstracts away {@code Handler} for tests. */
|
|
42
|
+
interface Scheduler {
|
|
43
|
+
void postDelayed(Runnable runnable, long delayMillis);
|
|
44
|
+
void cancel(Runnable runnable);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Hops onto the UI thread to run {@code work}. Abstracts away {@code Activity#runOnUiThread}
|
|
49
|
+
* (a {@code final} method, so it can't be overridden by a test fake {@code Activity} subclass)
|
|
50
|
+
* for tests.
|
|
51
|
+
*/
|
|
52
|
+
interface UiThreadDispatcher {
|
|
53
|
+
void runOnUiThread(Activity activity, Runnable work);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** How long to wait for the JS side to report back before giving up. */
|
|
57
|
+
static final long TIMEOUT_MILLIS = 3000;
|
|
58
|
+
|
|
59
|
+
private final JsEvaluator jsEvaluator;
|
|
60
|
+
private final ActivityProvider activityProvider;
|
|
61
|
+
private final Scheduler scheduler;
|
|
62
|
+
private final UiThreadDispatcher uiThreadDispatcher;
|
|
63
|
+
private final Map<String, PendingRequest> pending = new HashMap<>();
|
|
64
|
+
|
|
65
|
+
public WebFallbackEmojiPickerPresenter(JsEvaluator jsEvaluator, ActivityProvider activityProvider) {
|
|
66
|
+
this(jsEvaluator, activityProvider, defaultScheduler(), defaultUiThreadDispatcher());
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Package-private: lets tests inject a fake {@link Scheduler} and {@link UiThreadDispatcher}. */
|
|
70
|
+
WebFallbackEmojiPickerPresenter(
|
|
71
|
+
JsEvaluator jsEvaluator,
|
|
72
|
+
ActivityProvider activityProvider,
|
|
73
|
+
Scheduler scheduler,
|
|
74
|
+
UiThreadDispatcher uiThreadDispatcher
|
|
75
|
+
) {
|
|
76
|
+
this.jsEvaluator = jsEvaluator;
|
|
77
|
+
this.activityProvider = activityProvider;
|
|
78
|
+
this.scheduler = scheduler;
|
|
79
|
+
this.uiThreadDispatcher = uiThreadDispatcher;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
private static Scheduler defaultScheduler() {
|
|
83
|
+
Handler handler = new Handler(Looper.getMainLooper());
|
|
84
|
+
return new Scheduler() {
|
|
85
|
+
@Override
|
|
86
|
+
public void postDelayed(Runnable runnable, long delayMillis) {
|
|
87
|
+
handler.postDelayed(runnable, delayMillis);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
@Override
|
|
91
|
+
public void cancel(Runnable runnable) {
|
|
92
|
+
handler.removeCallbacks(runnable);
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
// Capacitor invokes @PluginMethod handlers on a background "CapacitorPlugins" thread, but
|
|
98
|
+
// Lifecycle#addObserver() is a UI-thread-only API (mirrors the same hop
|
|
99
|
+
// NativeEmojiPickerPresenter already does for its own UI-thread-only calls). Hop onto the UI
|
|
100
|
+
// thread whenever we have a real Activity to hop through; a null Activity means there's no UI
|
|
101
|
+
// to touch, so run synchronously (no Lifecycle to register against either way).
|
|
102
|
+
private static UiThreadDispatcher defaultUiThreadDispatcher() {
|
|
103
|
+
return (activity, work) -> {
|
|
104
|
+
if (activity != null) {
|
|
105
|
+
activity.runOnUiThread(work);
|
|
106
|
+
} else {
|
|
107
|
+
work.run();
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
@Override
|
|
113
|
+
public void present(String presentation, boolean dismissOnBackdropTap, EmojiCloseButtonOptions closeButton, EmojiPickerCallback callback) {
|
|
114
|
+
Activity activity = activityProvider.get();
|
|
115
|
+
uiThreadDispatcher.runOnUiThread(activity, () -> presentOnUiThread(activity, dismissOnBackdropTap, closeButton, callback));
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
private void presentOnUiThread(
|
|
119
|
+
Activity activity,
|
|
120
|
+
boolean dismissOnBackdropTap,
|
|
121
|
+
EmojiCloseButtonOptions closeButton,
|
|
122
|
+
EmojiPickerCallback callback
|
|
123
|
+
) {
|
|
124
|
+
String requestId = UUID.randomUUID().toString();
|
|
125
|
+
|
|
126
|
+
// Genuine timeout - i.e. the eval-completion callback below never fired at all (extremely
|
|
127
|
+
// rare: webview torn down mid-call, etc). This means the JS side never even started
|
|
128
|
+
// processing the request, so it's a bridge-liveness failure, not a user dismissal:
|
|
129
|
+
// defensively tell the JS side to close (in case the bridge partially works), but settle
|
|
130
|
+
// as an error rather than reusing dismiss()'s success/null semantics.
|
|
131
|
+
Runnable timeoutRunnable = () -> {
|
|
132
|
+
jsEvaluator.eval("window.__CapacitorEmojiPickerDismissWeb && window.__CapacitorEmojiPickerDismissWeb('" + requestId + "')", () -> {});
|
|
133
|
+
settle(requestId, null, ErrorCodes.NOT_IMPLEMENTED);
|
|
134
|
+
};
|
|
135
|
+
|
|
136
|
+
DefaultLifecycleObserver lifecycleObserver = null;
|
|
137
|
+
LifecycleOwner lifecycleOwner = null;
|
|
138
|
+
if (activity instanceof LifecycleOwner) {
|
|
139
|
+
lifecycleOwner = (LifecycleOwner) activity;
|
|
140
|
+
lifecycleObserver = new DefaultLifecycleObserver() {
|
|
141
|
+
@Override
|
|
142
|
+
public void onPause(LifecycleOwner owner) {
|
|
143
|
+
dismiss(requestId);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
@Override
|
|
147
|
+
public void onDestroy(LifecycleOwner owner) {
|
|
148
|
+
dismiss(requestId);
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
lifecycleOwner.getLifecycle().addObserver(lifecycleObserver);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
pending.put(requestId, new PendingRequest(callback, timeoutRunnable, lifecycleObserver, lifecycleOwner));
|
|
155
|
+
scheduler.postDelayed(timeoutRunnable, TIMEOUT_MILLIS);
|
|
156
|
+
|
|
157
|
+
// The eval "completion" here only means the script at least started executing (i.e. the
|
|
158
|
+
// bridge/webview is alive) - it cancels the timeout WITHOUT settling the request, which
|
|
159
|
+
// stays pending for the real result reported later via onWebResult.
|
|
160
|
+
jsEvaluator.eval(
|
|
161
|
+
"window.__CapacitorEmojiPickerPresentWeb('" + requestId + "', '" + encodeOptionsBase64Free(dismissOnBackdropTap, closeButton) + "')",
|
|
162
|
+
() -> scheduler.cancel(timeoutRunnable)
|
|
163
|
+
);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/** Called by the JS-interface bridge object once the JS sheet settles. */
|
|
167
|
+
public void onWebResult(String requestId, String emoji, String errorCode) {
|
|
168
|
+
settle(requestId, emoji, errorCode);
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Force-dismisses a still-pending presentation (e.g. on Activity destruction/backgrounding). */
|
|
172
|
+
public void dismiss(String requestId) {
|
|
173
|
+
jsEvaluator.eval("window.__CapacitorEmojiPickerDismissWeb && window.__CapacitorEmojiPickerDismissWeb('" + requestId + "')", () -> {});
|
|
174
|
+
settle(requestId, null, null);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
private void settle(String requestId, String emoji, String errorCode) {
|
|
178
|
+
PendingRequest request = pending.remove(requestId);
|
|
179
|
+
if (request == null) {
|
|
180
|
+
return;
|
|
181
|
+
}
|
|
182
|
+
scheduler.cancel(request.timeoutRunnable);
|
|
183
|
+
if (request.lifecycleObserver != null && request.lifecycleOwner != null) {
|
|
184
|
+
request.lifecycleOwner.getLifecycle().removeObserver(request.lifecycleObserver);
|
|
185
|
+
}
|
|
186
|
+
if (errorCode != null) {
|
|
187
|
+
request.callback.onError(errorCode);
|
|
188
|
+
} else {
|
|
189
|
+
request.callback.onResult(new EmojiPickerResult(emoji));
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* Hand-rolled instead of a JSON library: `size`/`position` are always one of a small fixed
|
|
195
|
+
* set of ASCII enum values validated/defaulted in {@code EmojiPicker#present}, never
|
|
196
|
+
* arbitrary user text, so plain string interpolation is safe here. This also sidesteps
|
|
197
|
+
* `org.json`/`android.util.Base64` throwing under the mockable `android.jar` in plain JUnit
|
|
198
|
+
* unit tests (see the plan's Global Constraints).
|
|
199
|
+
*/
|
|
200
|
+
private static String encodeOptionsBase64Free(boolean dismissOnBackdropTap, EmojiCloseButtonOptions closeButton) {
|
|
201
|
+
return "{"
|
|
202
|
+
+ "\"dismissOnBackdropTap\":" + dismissOnBackdropTap + ","
|
|
203
|
+
+ "\"closeButton\":{"
|
|
204
|
+
+ "\"size\":\"" + closeButton.size + "\","
|
|
205
|
+
+ "\"position\":\"" + closeButton.position + "\","
|
|
206
|
+
+ "\"hidden\":" + closeButton.hidden
|
|
207
|
+
+ "}}";
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
private static final class PendingRequest {
|
|
211
|
+
final EmojiPickerCallback callback;
|
|
212
|
+
final Runnable timeoutRunnable;
|
|
213
|
+
final DefaultLifecycleObserver lifecycleObserver;
|
|
214
|
+
final LifecycleOwner lifecycleOwner;
|
|
215
|
+
|
|
216
|
+
PendingRequest(
|
|
217
|
+
EmojiPickerCallback callback,
|
|
218
|
+
Runnable timeoutRunnable,
|
|
219
|
+
DefaultLifecycleObserver lifecycleObserver,
|
|
220
|
+
LifecycleOwner lifecycleOwner
|
|
221
|
+
) {
|
|
222
|
+
this.callback = callback;
|
|
223
|
+
this.timeoutRunnable = timeoutRunnable;
|
|
224
|
+
this.lifecycleObserver = lifecycleObserver;
|
|
225
|
+
this.lifecycleOwner = lifecycleOwner;
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
}
|
package/android/src/main/java/app/independo/capacitoremojipicker/service/EmojiPickerService.java
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
package app.independo.capacitoremojipicker.service;
|
|
2
|
+
|
|
3
|
+
import app.independo.capacitoremojipicker.core.EmojiCloseButtonOptions;
|
|
4
|
+
import app.independo.capacitoremojipicker.core.EmojiPickerCallback;
|
|
5
|
+
import app.independo.capacitoremojipicker.core.EmojiPickerResult;
|
|
6
|
+
import app.independo.capacitoremojipicker.core.ErrorCodes;
|
|
7
|
+
import app.independo.capacitoremojipicker.presenter.EmojiPickerPresenter;
|
|
8
|
+
|
|
9
|
+
/** Orchestrates presenter calls and guards against overlapping presentations. */
|
|
10
|
+
public class EmojiPickerService {
|
|
11
|
+
|
|
12
|
+
private final EmojiPickerPresenter presenter;
|
|
13
|
+
private volatile boolean isPresenting = false;
|
|
14
|
+
|
|
15
|
+
public EmojiPickerService(EmojiPickerPresenter presenter) {
|
|
16
|
+
this.presenter = presenter;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Presents the picker, rejecting a second concurrent call instead of overlapping pickers. */
|
|
20
|
+
public void present(String presentation, boolean dismissOnBackdropTap, EmojiCloseButtonOptions closeButton, EmojiPickerCallback callback) {
|
|
21
|
+
if (isPresenting) {
|
|
22
|
+
callback.onError(ErrorCodes.ALREADY_PRESENTING);
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
isPresenting = true;
|
|
27
|
+
presenter.present(
|
|
28
|
+
presentation,
|
|
29
|
+
dismissOnBackdropTap,
|
|
30
|
+
closeButton,
|
|
31
|
+
new EmojiPickerCallback() {
|
|
32
|
+
@Override
|
|
33
|
+
public void onResult(EmojiPickerResult result) {
|
|
34
|
+
isPresenting = false;
|
|
35
|
+
callback.onResult(result);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
@Override
|
|
39
|
+
public void onError(String code) {
|
|
40
|
+
isPresenting = false;
|
|
41
|
+
callback.onError(code);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
File without changes
|