Skip to content

Commit

Permalink
Improve sync notifications and simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
watsonbox committed Sep 30, 2024
1 parent 7f7e62b commit 49f0ed8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 37 deletions.
66 changes: 30 additions & 36 deletions src/StravaSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,24 +77,9 @@ export default class StravaSync extends Plugin {
async importActivitiesFromCSV() {
try {
const fileContents = await this.fileSelector.selectContents();
const activities = await new ActivitiesCSVImporter(fileContents).import();
this.activities = await new ActivitiesCSVImporter(fileContents).import();

this.activities = activities;

let createdCount = 0;
let updatedCount = 0;

await Promise.all(
this.activities.map(async (activity) => {
if (await this.activitySerializer.serialize(activity)) {
createdCount++;
} else {
updatedCount++;
}
})
);

new Notice(`πŸƒ ${createdCount} activities created, ${updatedCount} already existing.`, SUCCESS_NOTICE_DURATION);
await this.serializeActivities(false);
} catch (error) {
if (error instanceof CSVImportError) {
new Notice(`πŸ›‘ CSV Import Error:\n\n${error.message}`, ERROR_NOTICE_DURATION);
Expand All @@ -114,32 +99,18 @@ export default class StravaSync extends Plugin {

new Notice(`πŸ”„ Importing new activities from Strava...`, SUCCESS_NOTICE_DURATION);

const activities = await new ActivityImporter(
this.activities = await new ActivityImporter(
this.stravaApi,
this.settings.sync.lastActivityTimestamp
).importLatestActivities();

let createdCount = 0;
let updatedCount = 0;

await Promise.all(
activities.map(async (activity) => {
if (await this.activitySerializer.serialize(activity)) {
createdCount++;
} else {
updatedCount++;
}
})
);

if (activities.length > 0) {
this.settings.sync.lastActivityTimestamp = Math.max(...activities.map(activity => activity.start_date.getTime() / 1000));
await this.serializeActivities(true);

if (this.activities.length > 0) {
this.settings.sync.lastActivityTimestamp = Math.max(...this.activities.map(activity => activity.start_date.getTime() / 1000));
}

await this.saveSettings();

// FIXME: Improve messages
new Notice(`πŸƒ ${createdCount} new activities created, ${updatedCount} already existing.`, SUCCESS_NOTICE_DURATION);
} catch (error) {
console.error("Unexpected error during Strava import:", error);
new Notice(`πŸ›‘ An unexpected error occurred during import. Check the console for details.`, ERROR_NOTICE_DURATION);
Expand All @@ -153,4 +124,27 @@ export default class StravaSync extends Plugin {
async saveSettings() {
await this.saveData(this.settings);
}

private async serializeActivities(newLabel: boolean) {
let createdCount = 0;
let updatedCount = 0;

await Promise.all(
this.activities.map(async (activity) => {
if (await this.activitySerializer.serialize(activity)) {
createdCount++;
} else {
updatedCount++;
}
})
);

let message = `πŸƒ ${createdCount} ${newLabel ? 'new ' : ''}activities created`;

if (updatedCount > 0) {
message += `, ${updatedCount} already existing`;
}

new Notice(`${message}.`, SUCCESS_NOTICE_DURATION);
}
}
2 changes: 1 addition & 1 deletion src/__tests__/StravaSync.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('StravaSync', () => {
});

expect(Notice).toHaveBeenCalledWith(
'πŸƒ 3 activities created, 0 already existing.',
'πŸƒ 3 activities created.',
expect.any(Number)
);
});
Expand Down

0 comments on commit 49f0ed8

Please sign in to comment.