Skip to content

Commit 8cedf20

Browse files
Merge pull request #19 from calebsmithdev/issue-16
Notifications are now triggered after all files are processed
2 parents 6f98abe + fe4e6f3 commit 8cedf20

3 files changed

Lines changed: 34 additions & 23 deletions

File tree

src-tauri/src/thing_api.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
use std::collections::HashMap;
2-
3-
use tauri_plugin_notification::NotificationExt;
42
use tauri_plugin_store::StoreExt;
53

64
/// Sends a POST request to the WoWthing API with the provided form data and returns the response.
@@ -33,21 +31,9 @@ pub async fn submit_addon_data(app: tauri::AppHandle, contents: &str) -> Result<
3331
match response.status() {
3432
reqwest::StatusCode::OK => match response.text().await {
3533
Ok(text) => {
36-
app.notification()
37-
.builder()
38-
.title("Wowthing Sync")
39-
.body("Upload was completed succesfully")
40-
.show()
41-
.unwrap();
4234
Ok(format!("Sync completed: {:?}", text))
4335
}
4436
Err(_) => {
45-
app.notification()
46-
.builder()
47-
.title("Wowthing Sync")
48-
.body("An error occurred while uploading. Please try again later.")
49-
.show()
50-
.unwrap();
5137
Err(format!("There was an issue reading the response."))
5238
}
5339
},

src/composables/useFileUpload.ts

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const useFileUpload = () => {
1616
const lastUpdatedFromNow = ref('');
1717
const lastUpdated = useState(LAST_UPDATED, () => dayjs());
1818
const watchingFiles = useState('watching-files', () => ([]));
19+
const notifications = useNotifications();
1920
// const { addLog } = useLogs();
2021

2122
const startFileWatchingProcess = async () => {
@@ -103,48 +104,52 @@ export const useFileUpload = () => {
103104

104105
const handleUpload = async (force = false) => {
105106
const lastStartedDate = await getStorageItem<string>(LAST_STARTED_DATE);
106-
if(!force && lastStartedDate && dayjs().diff(dayjs(lastStartedDate), 'seconds') < 15) {
107+
if (!force && lastStartedDate && dayjs().diff(dayjs(lastStartedDate), 'seconds') < 15) {
107108
return;
108109
}
109110
isProcessing.value = true;
110-
await saveStorageItem<string>(LAST_STARTED_DATE, dayjs().format())
111+
await saveStorageItem<string>(LAST_STARTED_DATE, dayjs().format());
111112
const files = await getAllFiles();
112113
const dedupedFiles = removeDuplicateFiles(files);
113-
const apiKey = await getStorageItem<string>(API_KEY)
114+
114115
for (const file of dedupedFiles) {
115116
// addLog({
116117
// date: new Date(),
117118
// title: 'Attempting to upload...',
118119
// note: `File path: ${file.path}`
119-
// })
120+
// });
120121
const contents = await readBigFile(file, { baseDir: BaseDirectory.Home });
121122

122123
try {
123-
const message = await invoke('submit_addon_data', {contents})
124+
const message = await invoke('submit_addon_data', { contents });
124125

125126
// addLog({
126127
// date: new Date(),
127128
// title: 'Uploaded file!',
128129
// note: `File path: ${file.path}; Return: ${message}`
129-
// })
130+
// });
130131
console.log(`File: ${file}; Return: ${message}`);
131132
} catch (error) {
132133
// addLog({
133134
// date: new Date(),
134135
// title: 'File failed to upload!',
135136
// note: `File path: ${file.path}; Return: ${error}`
136-
// })
137+
// });
137138
console.log(`File: ${file}; Return: ${error}`);
139+
await notifications.send({ title: 'Wowthing Sync', body: 'An error occurred while uploading. Please try again later.' });
140+
isProcessing.value = false;
141+
return; // Stop the rest of the loop from working
138142
}
139143
}
140144

141145
await handleLastUpdated();
146+
await notifications.send({ title: 'Wowthing Sync', body: 'Upload was completed successfully' });
142147
isProcessing.value = false;
143-
}
148+
};
144149

145150
const removeDuplicateFiles = (arr) => {
146151
var unique = arr.reduce(function (files, file) {
147-
if(!files.find(m => m.path == file.path)) {
152+
if(!files.find(m => m == file)) {
148153
files.push(file);
149154
}
150155
return files;
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { isPermissionGranted, requestPermission, sendNotification } from "@tauri-apps/plugin-notification";
2+
3+
export const useNotifications = () => {
4+
const send = async (options) => {
5+
let permissionGranted = await isPermissionGranted();
6+
7+
if (!permissionGranted) {
8+
const permission = await requestPermission();
9+
permissionGranted = permission === 'granted';
10+
}
11+
12+
if (permissionGranted) {
13+
sendNotification(options);
14+
}
15+
};
16+
17+
return {
18+
send
19+
}
20+
}

0 commit comments

Comments
 (0)