From 7257658c2ab3d8cd192c932c87186b4b65041823 Mon Sep 17 00:00:00 2001 From: ImJustChew Date: Tue, 16 Apr 2024 20:23:24 +0800 Subject: [PATCH] fix: changed download to share api --- package-lock.json | 9 +++++ package.json | 1 + .../Timetable/DownloadTimetableDialog.tsx | 36 ++++--------------- 3 files changed, 17 insertions(+), 29 deletions(-) diff --git a/package-lock.json b/package-lock.json index ef10594d..b4d9a8c8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -18,6 +18,7 @@ "@capacitor/haptics": "5.0.7", "@capacitor/ios": "5.6.0", "@capacitor/keyboard": "5.0.8", + "@capacitor/share": "^6.0.0", "@capacitor/status-bar": "5.0.7", "@dnd-kit/core": "^6.1.0", "@dnd-kit/modifiers": "^7.0.0", @@ -1775,6 +1776,14 @@ "@capacitor/core": "^5.0.0" } }, + "node_modules/@capacitor/share": { + "version": "6.0.0", + "resolved": "https://registry.npmjs.org/@capacitor/share/-/share-6.0.0.tgz", + "integrity": "sha512-Doqvtxe9tejkjq+P9hZzDslR6VetM02R6N61GBavVBWkQj4N+xWNDA8zISG3PzuGBffGtTfHC6L0W+XySFY5nw==", + "peerDependencies": { + "@capacitor/core": "^6.0.0" + } + }, "node_modules/@capacitor/status-bar": { "version": "5.0.7", "resolved": "https://registry.npmjs.org/@capacitor/status-bar/-/status-bar-5.0.7.tgz", diff --git a/package.json b/package.json index 454c6c96..24dc3f26 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "@capacitor/haptics": "5.0.7", "@capacitor/ios": "5.6.0", "@capacitor/keyboard": "5.0.8", + "@capacitor/share": "^6.0.0", "@capacitor/status-bar": "5.0.7", "@dnd-kit/core": "^6.1.0", "@dnd-kit/modifiers": "^7.0.0", diff --git a/src/components/Timetable/DownloadTimetableDialog.tsx b/src/components/Timetable/DownloadTimetableDialog.tsx index 78782a88..1377fa5a 100644 --- a/src/components/Timetable/DownloadTimetableDialog.tsx +++ b/src/components/Timetable/DownloadTimetableDialog.tsx @@ -5,14 +5,10 @@ import Timetable from './Timetable'; import useUserTimetable from '@/hooks/contexts/useUserTimetable'; import { toPng } from 'html-to-image'; import { useCallback, useRef, useState } from 'react'; -import {createTimetableFromCourses, colorMapFromCourses} from '@/helpers/timetable'; -import { useSettings } from '@/hooks/contexts/settings'; +import { createTimetableFromCourses } from '@/helpers/timetable'; import { MinimalCourse } from '@/types/courses'; -import { Device } from '@capacitor/device'; -import { Media } from '@capacitor-community/media'; -import { Filesystem, Directory, Encoding } from '@capacitor/filesystem'; -import { toast } from '../ui/use-toast'; import { Capacitor } from '@capacitor/core'; +import { Share } from '@capacitor/share'; const DownloadTimetableComponent = () => { const dict = useDictionary(); @@ -36,14 +32,9 @@ const DownloadTimetableComponent = () => { link.href = dataUrl; link.click(); } else { - const albums = await Media.getAlbums(); - if (albums.albums.every(album => album.name !== 'NTHUMods')) { - await Media.createAlbum({ name: 'NTHUMods' }); - } - await Media.savePhoto({ - path: dataUrl, - albumIdentifier: 'NTHUMods', - fileName: filename, + await Share.share({ + title: 'Share Timetable', + url: dataUrl, }); } }) @@ -84,22 +75,9 @@ const DownloadTimetableDialog = ({ onClose, icsfileLink }: { onClose: () => void link.href = icsfileLink; link.click(); } else { - const res = await Filesystem.checkPermissions() - if (!res.publicStorage) { - const result = await Filesystem.requestPermissions(); - if (result.publicStorage != 'granted') { - toast({ - title: 'Permission Denied', - description: 'Please allow storage permission to download the file.', - }); - return; - } - } - Filesystem.downloadFile({ - path: `ics/${filename}`, + await Share.share({ url: icsfileLink, - directory: Directory.Documents, - recursive: true, + dialogTitle: 'Open in Calendar', }); }