top of page
bottom of page
/** * Mitko Health — native bridge helpers * * Paste this file's contents into a Wix "Custom Code" embed (loaded on all * pages, in the Body - end section) OR into the specific HTML embed on the * bariatric tracker / document upload pages. * * These functions check whether the page is running inside the Mitko Health * iOS app (Capacitor). If so, they use native camera / file / share sheet * APIs. If the same page is opened in a normal browser (desktop or mobile * Safari outside the app), they silently fall back to standard web behavior. * No page needs two versions — call these functions everywhere and they * do the right thing based on context. */ window.MitkoBridge = (function () { const isNativeApp = () => typeof window.Capacitor !== 'undefined' && window.Capacitor.isNativePlatform && window.Capacitor.isNativePlatform(); /** * Take or choose a photo. * Returns a Promise resolving to a base64 data URL string. * Use case: incision-site check photos, meal photos for bariatric tracker. * * source: 'camera' | 'photos' | 'prompt' (prompt shows both options) */ async function takePhoto(source = 'prompt') { if (isNativeApp()) { const { Camera, CameraResultType, CameraSource } = window.Capacitor.Plugins; const sourceMap = { camera: CameraSource.Camera, photos: CameraSource.Photos, prompt: CameraSource.Prompt }; const photo = await Camera.getPhoto({ quality: 80, resultType: CameraResultType.DataUrl, source: sourceMap[source] || CameraSource.Prompt, allowEditing: false }); return photo.dataUrl; } // Browser fallback: standard file input with capture attribute return new Promise((resolve, reject) => { const input = document.createElement('input'); input.type = 'file'; input.accept = 'image/*'; if (source === 'camera') input.capture = 'environment'; input.onchange = () => { const file = input.files && input.files[0]; if (!file) return reject(new Error('No file selected')); const reader = new FileReader(); reader.onload = () => resolve(reader.result); reader.onerror = reject; reader.readAsDataURL(file); }; input.click(); }); } /** * Write a file (e.g. a filled PFML PDF) to a temporary cache location just * long enough to hand it to the share sheet, and return its path. This is * NOT permanent storage — it uses the Cache directory (not visible in the * Files app, and iOS may clear it automatically). Nothing is retained * after the user shares or dismisses the share sheet. * data: base64 string (no data: prefix) * fileName: e.g. 'PFML-Application.pdf' */ async function saveFile(data, fileName) { if (isNativeApp()) { const { Filesystem, Directory } = window.Capacitor.Plugins; const result = await Filesystem.writeFile({ path: fileName, data: data, directory: Directory.Cache }); return result.uri; } // Browser fallback: trigger a normal download const link = document.createElement('a'); link.href = `data:application/pdf;base64,${data}`; link.download = fileName; document.body.appendChild(link); link.click(); document.body.removeChild(link); return null; } /** * Open the native share sheet (Messages, Mail, AirDrop, Drive, etc.) * for a file already saved via saveFile(), or for a plain text/link share. */ async function shareFile({ title, text, url, filePath }) { if (isNativeApp()) { const { Share } = window.Capacitor.Plugins; await Share.share({ title: title || 'Mitko Health', text: text || '', url: filePath || url || '', dialogTitle: title || 'Share' }); return; } // Browser fallback: use Web Share API if available, else no-op if (navigator.share) { await navigator.share({ title, text, url }); } else { console.warn('Sharing is not supported in this browser. Use the download link instead.'); } } /** * Delete a file previously written by saveFile(). Call this right after * shareFile() completes so nothing lingers in the app's cache. */ async function deleteTempFile(fileName) { if (!isNativeApp()) return; const { Filesystem, Directory } = window.Capacitor.Plugins; try { await Filesystem.deleteFile({ path: fileName, directory: Directory.Cache }); } catch (err) { // Already gone or never existed — fine to ignore. } } return { isNativeApp, takePhoto, saveFile, shareFile, deleteTempFile }; })();