code-craft-studio
Version:
A comprehensive QR code and barcode scanning/generation library for React. Works with or without Capacitor. Supports 22+ QR data types and 14+ barcode formats (EAN, UPC, Code 128, etc.), with customizable designs, analytics, and React components. Provider
197 lines • 7.71 kB
JavaScript
import { CapacitorStorageAdapter } from '../storage/capacitor-storage';
import { WebPlatformAdapter } from '../web/web-adapter';
import { logger } from '../../utils/logger';
export class CapacitorPlatformAdapter {
constructor(storage) {
this.name = 'capacitor';
this.storage = storage || new CapacitorStorageAdapter();
this.webAdapter = new WebPlatformAdapter(this.storage);
// Start with web capabilities, will be updated after plugin loads
this.capabilities = Object.assign(Object.assign({}, this.webAdapter.capabilities), { hasNativeScanning: false, hasNativeGeneration: false });
this.loadCapacitorPlugin();
}
async loadCapacitorPlugin() {
try {
// Dynamically import Capacitor
const { registerPlugin, Capacitor } = await import('@capacitor/core');
this.capacitorCore = { registerPlugin, Capacitor };
// Set flag for detector
if (typeof window !== 'undefined') {
window.__capacitor_loaded__ = true;
}
// Check if we're on a native platform
const platform = Capacitor.getPlatform();
const isNative = platform === 'ios' || platform === 'android';
if (isNative) {
// Update capabilities for native platform
this.capabilities.hasNativeScanning = true;
this.capabilities.hasNativeGeneration = true;
// Register the plugin
this.plugin = registerPlugin('QRCodeStudio', {
web: () => this.webAdapter
});
}
}
catch (error) {
logger.warn('CapacitorPlatformAdapter: Failed to load Capacitor plugin, using web fallback', error);
}
}
async scanQRCode(options) {
if (this.plugin && this.capabilities.hasNativeScanning) {
try {
return await this.plugin.scanQRCode(options);
}
catch (error) {
logger.warn('Native QR scanning failed, falling back to web', error);
}
}
return this.webAdapter.scanQRCode(options);
}
async generateQRCode(data, options) {
if (this.plugin && this.capabilities.hasNativeGeneration) {
try {
return await this.plugin.generateQRCode({ data, options });
}
catch (error) {
logger.warn('Native QR generation failed, falling back to web', error);
}
}
return this.webAdapter.generateQRCode(data, options);
}
async validateQRData(data) {
if (this.plugin && this.plugin.validateQRData) {
try {
return await this.plugin.validateQRData({ data });
}
catch (error) {
logger.warn('Native validation failed, falling back to web', error);
}
}
return this.webAdapter.validateQRData(data);
}
async scanBarcode(options) {
if (this.plugin && this.capabilities.hasNativeScanning) {
try {
return await this.plugin.scanBarcode(options);
}
catch (error) {
logger.warn('Native barcode scanning failed, falling back to web', error);
}
}
return this.webAdapter.scanBarcode(options);
}
async generateBarcode(data, format, options) {
if (this.plugin && this.capabilities.hasNativeGeneration) {
try {
return await this.plugin.generateBarcode({ data, format, options });
}
catch (error) {
logger.warn('Native barcode generation failed, falling back to web', error);
}
}
return this.webAdapter.generateBarcode(data, format, options);
}
async validateBarcode(data, format) {
if (this.plugin && this.plugin.validateBarcode) {
try {
return await this.plugin.validateBarcode({ data, format });
}
catch (error) {
logger.warn('Native barcode validation failed, falling back to web', error);
}
}
return this.webAdapter.validateBarcode(data, format);
}
async saveToHistory(item) {
if (this.plugin && this.plugin.saveToHistory) {
try {
return await this.plugin.saveToHistory({ item });
}
catch (error) {
logger.warn('Native history save failed, falling back to web', error);
}
}
return this.webAdapter.saveToHistory(item);
}
async getHistory(options) {
if (this.plugin && this.plugin.getHistory) {
try {
const result = await this.plugin.getHistory(options);
return result.items || [];
}
catch (error) {
logger.warn('Native history retrieval failed, falling back to web', error);
}
}
return this.webAdapter.getHistory(options);
}
async clearHistory() {
if (this.plugin && this.plugin.clearHistory) {
try {
return await this.plugin.clearHistory();
}
catch (error) {
logger.warn('Native history clear failed, falling back to web', error);
}
}
return this.webAdapter.clearHistory();
}
async getAnalytics() {
if (this.plugin && this.plugin.getAnalytics) {
try {
return await this.plugin.getAnalytics();
}
catch (error) {
logger.warn('Native analytics failed, falling back to web', error);
}
}
return this.webAdapter.getAnalytics();
}
async exportCode(dataUrl, options) {
if (this.plugin && this.plugin.exportCode && this.capacitorCore) {
try {
const { Filesystem } = await import('@capacitor/filesystem');
// Convert data URL to base64
const base64Data = dataUrl.split(',')[1];
// Generate filename
const timestamp = new Date().getTime();
const extension = options.format.toLowerCase();
const fileName = `${options.fileName || 'code'}_${timestamp}.${extension}`;
// Save to device
const result = await Filesystem.writeFile({
path: fileName,
data: base64Data,
directory: Filesystem.Directory.Documents
});
return { filePath: result.uri };
}
catch (error) {
logger.warn('Native export failed, falling back to web', error);
}
}
return this.webAdapter.exportCode(dataUrl, options);
}
async checkPermissions() {
if (this.plugin && this.plugin.checkPermissions) {
try {
return await this.plugin.checkPermissions();
}
catch (error) {
logger.warn('Native permission check failed, falling back to web', error);
}
}
return this.webAdapter.checkPermissions();
}
async requestPermissions() {
if (this.plugin && this.plugin.requestPermissions) {
try {
return await this.plugin.requestPermissions();
}
catch (error) {
logger.warn('Native permission request failed, falling back to web', error);
}
}
return this.webAdapter.requestPermissions();
}
}
//# sourceMappingURL=capacitor-adapter.js.map