forked from trent_larson/crowd-funder-for-time-pwa
refactor(platform): replace platform checks with capability-based system
- Add PlatformCapabilities interface to define available features - Remove isWeb(), isCapacitor(), isElectron(), isPyWebView() methods - Update platform services to implement getCapabilities() - Refactor DataExportSection to use capability checks instead of platform checks - Improve platform abstraction and separation of concerns - Make platform-specific logic more maintainable and extensible This change decouples components from specific platform implementations, making the codebase more maintainable and easier to extend with new platforms.
This commit is contained in:
@@ -9,13 +9,38 @@ export interface ImageResult {
|
||||
fileName: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Platform capabilities interface defining what features are available
|
||||
* on the current platform implementation
|
||||
*/
|
||||
export interface PlatformCapabilities {
|
||||
/** Whether the platform supports native file system access */
|
||||
hasFileSystem: boolean;
|
||||
/** Whether the platform supports native camera access */
|
||||
hasCamera: boolean;
|
||||
/** Whether the platform is a mobile device */
|
||||
isMobile: boolean;
|
||||
/** Whether the platform is iOS specifically */
|
||||
isIOS: boolean;
|
||||
/** Whether the platform supports native file download */
|
||||
hasFileDownload: boolean;
|
||||
/** Whether the platform requires special file handling instructions */
|
||||
needsFileHandlingInstructions: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Platform-agnostic interface for handling platform-specific operations.
|
||||
* Provides a common API for file system operations, camera interactions,
|
||||
* platform detection, and deep linking across different platforms
|
||||
* (web, mobile, desktop).
|
||||
* and platform detection across different platforms (web, mobile, desktop).
|
||||
*/
|
||||
export interface PlatformService {
|
||||
// Platform capabilities
|
||||
/**
|
||||
* Gets the current platform's capabilities
|
||||
* @returns Object describing what features are available on this platform
|
||||
*/
|
||||
getCapabilities(): PlatformCapabilities;
|
||||
|
||||
// File system operations
|
||||
/**
|
||||
* Reads the contents of a file at the specified path.
|
||||
@@ -59,32 +84,6 @@ export interface PlatformService {
|
||||
*/
|
||||
pickImage(): Promise<ImageResult>;
|
||||
|
||||
// Platform specific features
|
||||
/**
|
||||
* Checks if the current platform is Capacitor (mobile).
|
||||
* @returns true if running on Capacitor
|
||||
*/
|
||||
isCapacitor(): boolean;
|
||||
|
||||
/**
|
||||
* Checks if the current platform is Electron (desktop).
|
||||
* @returns true if running on Electron
|
||||
*/
|
||||
isElectron(): boolean;
|
||||
|
||||
/**
|
||||
* Checks if the current platform is PyWebView.
|
||||
* @returns true if running on PyWebView
|
||||
*/
|
||||
isPyWebView(): boolean;
|
||||
|
||||
/**
|
||||
* Checks if the current platform is web browser.
|
||||
* @returns true if running in a web browser
|
||||
*/
|
||||
isWeb(): boolean;
|
||||
|
||||
// Deep linking
|
||||
/**
|
||||
* Handles deep link URLs for the application.
|
||||
* @param url - The deep link URL to handle
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ImageResult, PlatformService } from "../PlatformService";
|
||||
import { ImageResult, PlatformService, PlatformCapabilities } from "../PlatformService";
|
||||
import { Filesystem, Directory } from "@capacitor/filesystem";
|
||||
import { Camera, CameraResultType, CameraSource } from "@capacitor/camera";
|
||||
import { logger } from "../../utils/logger";
|
||||
@@ -11,6 +11,21 @@ import { logger } from "../../utils/logger";
|
||||
* - Platform-specific features
|
||||
*/
|
||||
export class CapacitorPlatformService implements PlatformService {
|
||||
/**
|
||||
* Gets the capabilities of the Capacitor platform
|
||||
* @returns Platform capabilities object
|
||||
*/
|
||||
getCapabilities(): PlatformCapabilities {
|
||||
return {
|
||||
hasFileSystem: true,
|
||||
hasCamera: true,
|
||||
isMobile: true,
|
||||
isIOS: /iPad|iPhone|iPod/.test(navigator.userAgent),
|
||||
hasFileDownload: false,
|
||||
needsFileHandlingInstructions: true
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a file from the app's data directory.
|
||||
* @param path - Relative path to the file in the app's data directory
|
||||
@@ -146,38 +161,6 @@ export class CapacitorPlatformService implements PlatformService {
|
||||
return new Blob(byteArrays, { type: "image/jpeg" });
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on Capacitor platform.
|
||||
* @returns true, as this is the Capacitor implementation
|
||||
*/
|
||||
isCapacitor(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on Electron platform.
|
||||
* @returns false, as this is not Electron
|
||||
*/
|
||||
isElectron(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on PyWebView platform.
|
||||
* @returns false, as this is not PyWebView
|
||||
*/
|
||||
isPyWebView(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on web platform.
|
||||
* @returns false, as this is not web
|
||||
*/
|
||||
isWeb(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles deep link URLs for the application.
|
||||
* Note: Capacitor handles deep links automatically.
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ImageResult, PlatformService } from "../PlatformService";
|
||||
import { ImageResult, PlatformService, PlatformCapabilities } from "../PlatformService";
|
||||
import { logger } from "../../utils/logger";
|
||||
|
||||
/**
|
||||
@@ -14,6 +14,21 @@ import { logger } from "../../utils/logger";
|
||||
* - System-level features
|
||||
*/
|
||||
export class ElectronPlatformService implements PlatformService {
|
||||
/**
|
||||
* Gets the capabilities of the Electron platform
|
||||
* @returns Platform capabilities object
|
||||
*/
|
||||
getCapabilities(): PlatformCapabilities {
|
||||
return {
|
||||
hasFileSystem: false, // Not implemented yet
|
||||
hasCamera: false, // Not implemented yet
|
||||
isMobile: false,
|
||||
isIOS: false,
|
||||
hasFileDownload: false, // Not implemented yet
|
||||
needsFileHandlingInstructions: false
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a file from the filesystem.
|
||||
* @param _path - Path to the file to read
|
||||
@@ -79,38 +94,6 @@ export class ElectronPlatformService implements PlatformService {
|
||||
throw new Error("Not implemented");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on Capacitor platform.
|
||||
* @returns false, as this is not Capacitor
|
||||
*/
|
||||
isCapacitor(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on Electron platform.
|
||||
* @returns true, as this is the Electron implementation
|
||||
*/
|
||||
isElectron(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on PyWebView platform.
|
||||
* @returns false, as this is not PyWebView
|
||||
*/
|
||||
isPyWebView(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on web platform.
|
||||
* @returns false, as this is not web
|
||||
*/
|
||||
isWeb(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should handle deep link URLs for the desktop application.
|
||||
* @param _url - The deep link URL to handle
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ImageResult, PlatformService } from "../PlatformService";
|
||||
import { ImageResult, PlatformService, PlatformCapabilities } from "../PlatformService";
|
||||
import { logger } from "../../utils/logger";
|
||||
|
||||
/**
|
||||
@@ -15,6 +15,21 @@ import { logger } from "../../utils/logger";
|
||||
* - Python-JavaScript bridge functionality
|
||||
*/
|
||||
export class PyWebViewPlatformService implements PlatformService {
|
||||
/**
|
||||
* Gets the capabilities of the PyWebView platform
|
||||
* @returns Platform capabilities object
|
||||
*/
|
||||
getCapabilities(): PlatformCapabilities {
|
||||
return {
|
||||
hasFileSystem: false, // Not implemented yet
|
||||
hasCamera: false, // Not implemented yet
|
||||
isMobile: false,
|
||||
isIOS: false,
|
||||
hasFileDownload: false, // Not implemented yet
|
||||
needsFileHandlingInstructions: false
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads a file using the Python backend.
|
||||
* @param _path - Path to the file to read
|
||||
@@ -80,38 +95,6 @@ export class PyWebViewPlatformService implements PlatformService {
|
||||
throw new Error("Not implemented");
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on Capacitor platform.
|
||||
* @returns false, as this is not Capacitor
|
||||
*/
|
||||
isCapacitor(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on Electron platform.
|
||||
* @returns false, as this is not Electron
|
||||
*/
|
||||
isElectron(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on PyWebView platform.
|
||||
* @returns true, as this is the PyWebView implementation
|
||||
*/
|
||||
isPyWebView(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if running on web platform.
|
||||
* @returns false, as this is not web
|
||||
*/
|
||||
isWeb(): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Should handle deep link URLs through the Python backend.
|
||||
* @param _url - The deep link URL to handle
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
import { ImageResult, PlatformService } from "../PlatformService";
|
||||
import { ImageResult, PlatformService, PlatformCapabilities } from "../PlatformService";
|
||||
import { logger } from "../../utils/logger";
|
||||
|
||||
/**
|
||||
@@ -15,6 +15,21 @@ import { logger } from "../../utils/logger";
|
||||
* due to browser security restrictions. These methods throw appropriate errors.
|
||||
*/
|
||||
export class WebPlatformService implements PlatformService {
|
||||
/**
|
||||
* Gets the capabilities of the web platform
|
||||
* @returns Platform capabilities object
|
||||
*/
|
||||
getCapabilities(): PlatformCapabilities {
|
||||
return {
|
||||
hasFileSystem: false,
|
||||
hasCamera: true, // Through file input with capture
|
||||
isMobile: /iPhone|iPad|iPod|Android/i.test(navigator.userAgent),
|
||||
isIOS: /iPad|iPhone|iPod/.test(navigator.userAgent),
|
||||
hasFileDownload: true,
|
||||
needsFileHandlingInstructions: false
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Not supported in web platform.
|
||||
* @param _path - Unused path parameter
|
||||
|
||||
Reference in New Issue
Block a user