From 49f0ed890db2152ba7318ca5501dfc310293c287 Mon Sep 17 00:00:00 2001 From: Howard Wilson Date: Mon, 30 Sep 2024 22:18:09 +0200 Subject: [PATCH] Improve sync notifications and simplify code --- src/StravaSync.ts | 66 +++++++++++++++----------------- src/__tests__/StravaSync.test.ts | 2 +- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/src/StravaSync.ts b/src/StravaSync.ts index 3fd76ce..0165c2a 100644 --- a/src/StravaSync.ts +++ b/src/StravaSync.ts @@ -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); @@ -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); @@ -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); + } } diff --git a/src/__tests__/StravaSync.test.ts b/src/__tests__/StravaSync.test.ts index 1aff842..2a343e5 100644 --- a/src/__tests__/StravaSync.test.ts +++ b/src/__tests__/StravaSync.test.ts @@ -121,7 +121,7 @@ describe('StravaSync', () => { }); expect(Notice).toHaveBeenCalledWith( - '🏃 3 activities created, 0 already existing.', + '🏃 3 activities created.', expect.any(Number) ); });