refactor: remove unused ShareImage plugin code, simplify to temp file approach

Remove unused Capacitor plugin implementation and simplify shared image
handling to use only the temp file approach, which is already working
reliably for both iOS and Android.

Changes:
- Delete ios/App/App/ShareImagePlugin.swift (unused, never registered)
- Delete ios/App/App/ShareImageBridge.swift (only used by plugin)
- Remove ~80 lines of plugin fallback code from src/main.capacitor.ts
- Simplify error handling to single code path
- Add documentation comment explaining temp file approach and future
  plugin implementation possibility

Benefits:
- ~154 lines of code removed
- Single, consistent code path for both platforms
- Easier to maintain and debug
- No functional changes - temp file approach already working

The temp file approach uses Capacitor's Filesystem plugin to read shared
image data written by native code (AppDelegate on iOS, MainActivity on
Android). This is simpler and more reliable than the plugin approach,
which required registration and bridge setup.

Future improvement: Consider implementing Capacitor plugins for direct
native-to-JS communication if lower latency becomes a priority, though
the current approach performs well in production.
This commit is contained in:
Jose Olarte III
2025-11-27 21:23:34 +08:00
parent e1eb91f26d
commit 0627cd32b7
4 changed files with 17 additions and 178 deletions

View File

@@ -192,18 +192,17 @@ async function checkAndStoreNativeSharedImage(): Promise<{
`[Main] Checking for ${platform} shared image from native layer`,
);
// Use Capacitor's native bridge to call the ShareImagePlugin
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const capacitor = (window as any).Capacitor;
if (!capacitor || !capacitor.Plugins) {
logger.debug("[Main] Capacitor plugins not available");
isProcessingSharedImage = false;
return { success: false };
}
// WORKAROUND: Since the plugin isn't being auto-discovered, use a temp file bridge
// Native layer (AppDelegate on iOS, MainActivity on Android) writes the shared image data to a temp file, and we read it here
// TEMP FILE APPROACH:
// Native layer (AppDelegate on iOS, MainActivity on Android) writes the shared image data
// to a temp file, and we read it here using Capacitor's Filesystem plugin.
//
// This approach is simple, reliable, and works consistently across both platforms.
// The temp file is deleted immediately after reading to prevent re-processing.
//
// FUTURE IMPROVEMENT: Consider implementing Capacitor plugins for iOS and Android
// to provide a more direct native-to-JS bridge. This would eliminate the need for
// file I/O and polling, providing lower latency and a more "native" integration.
// However, the current temp file approach is production-ready and performs well.
const tempFilePath = "timesafari_shared_photo.json";
// Use platform-specific directory:
@@ -250,96 +249,20 @@ async function checkAndStoreNativeSharedImage(): Promise<{
}
}
} catch (fileError: unknown) {
// File exists but can't be read - log and continue to plugin method
logger.debug(
"[Main] Temp file exists but couldn't be read, trying plugin method",
logger.error(
"[Main] Temp file exists but couldn't be read:",
fileError,
);
}
} else {
logger.debug(
"[Main] Temp file not found after polling (this is normal if plugin works)",
);
}
// NOTE: Plugin registration issue - ShareImage plugin is not being auto-discovered
// This is a known issue that needs to be resolved. For now, we use the temp file workaround above.
// Try multiple methods to call the plugin (fallback if temp file method fails)
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const plugins = (capacitor as any).Plugins;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const shareImagePlugin = (plugins as any)?.ShareImage;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let result: any = null;
if (
shareImagePlugin &&
typeof shareImagePlugin.getSharedImageData === "function"
) {
logger.debug("[Main] Using direct plugin method");
try {
result = await shareImagePlugin.getSharedImageData();
} catch (pluginError) {
logger.error("[Main] Plugin call failed:", pluginError);
isProcessingSharedImage = false;
return { success: false };
}
} else {
// Method 2: Use Capacitor's execute method
logger.debug(
"[Main] Plugin not found directly, trying Capacitor.execute",
);
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const bridge = (capacitor as any).getBridge?.();
if (bridge) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result = await bridge.execute({
pluginId: "ShareImage",
methodName: "getSharedImageData",
options: {},
});
} else {
// Method 3: Try execute on Plugins object
// eslint-disable-next-line @typescript-eslint/no-explicit-any
if ((plugins as any)?.execute) {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
result = await (plugins as any).execute(
"ShareImage",
"getSharedImageData",
{},
);
}
}
} catch (executeError) {
logger.error("[Main] Execute method failed:", executeError);
isProcessingSharedImage = false;
return { success: false };
}
logger.debug("[Main] Temp file not found after polling");
}
if (!result || !result.success || !result.data) {
logger.debug("[Main] No shared image data found in result");
isProcessingSharedImage = false;
return { success: false };
}
const { base64, fileName } = result.data;
if (!base64) {
logger.debug("[Main] Shared image data missing base64");
isProcessingSharedImage = false;
return { success: false };
}
logger.info("[Main] Native shared image found, storing in temp DB");
// Store in temp database using extracted method
await storeSharedImageInTempDB(base64, fileName);
// No shared image found
isProcessingSharedImage = false;
return { success: true, fileName: fileName || "shared-image.jpg" };
return { success: false };
} catch (error) {
logger.error("[Main] Error checking for native shared image:", error);
isProcessingSharedImage = false;