Skip to content

Commit

Permalink
refactor: move status functions to footer view
Browse files Browse the repository at this point in the history
  • Loading branch information
arielsvg committed Sep 29, 2020
1 parent 9019dda commit 212a65f
Show file tree
Hide file tree
Showing 7 changed files with 155 additions and 199 deletions.
46 changes: 11 additions & 35 deletions app/assets/javascripts/services/statusManager.ts
Original file line number Diff line number Diff line change
@@ -1,54 +1,30 @@
import { removeFromArray } from 'snjs';
import { FooterStatus } from '@/types';

type StatusCallback = (string: string) => void
type StatusCallback = (string: string) => void;

export class StatusManager {
private _message = '';
private observers: StatusCallback[] = [];

private statuses: FooterStatus[] = []
private observers: StatusCallback[] = []

replaceStatusWithString(status: FooterStatus, string: string) {
this.removeStatus(status);
return this.addStatusFromString(string);
get message(): string {
return this._message;
}

addStatusFromString(string: string) {
const status = { string };
this.statuses.push(status);
setMessage(message: string) {
this._message = message;
this.notifyObservers();
return status;
}

removeStatus(status: FooterStatus) {
removeFromArray(this.statuses, status);
this.notifyObservers();
return undefined;
}

addStatusObserver(callback: StatusCallback) {
onStatusChange(callback: StatusCallback) {
this.observers.push(callback);
return () => {
removeFromArray(this.observers, callback);
}
};
}

private notifyObservers() {
for(const observer of this.observers) {
observer(this.getStatusString());
for (const observer of this.observers) {
observer(this._message);
}
}

private getStatusString() {
let result = '';
this.statuses.forEach((status, index) => {
if(index > 0) {
result += ' ';
}
result += status.string;
});

return result;
}

}
6 changes: 3 additions & 3 deletions app/assets/javascripts/ui_models/application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ type WebServices = {
autolockService: AutolockService
archiveService: ArchiveManager
nativeExtService: NativeExtManager
statusService: StatusManager
statusManager: StatusManager
themeService: ThemeManager
prefsService: PreferencesManager
keyboardService: KeyboardManager
Expand Down Expand Up @@ -135,8 +135,8 @@ export class WebApplication extends SNApplication {
return this.webServices.nativeExtService;
}

public getStatusService() {
return this.webServices.statusService;
getStatusManager() {
return this.webServices.statusManager;
}

public getThemeService() {
Expand Down
2 changes: 1 addition & 1 deletion app/assets/javascripts/ui_models/application_group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ export class ApplicationGroup extends SNApplicationGroup {
autolockService,
nativeExtService,
prefsService,
statusService,
statusManager: statusService,
themeService
});
return application;
Expand Down
111 changes: 8 additions & 103 deletions app/assets/javascripts/views/application/application_view.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ import {
STRING_DEFAULT_FILE_ERROR
} from '@/strings';
import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl';
import { alertDialog } from '@/services/alertService';

class ApplicationViewCtrl extends PureViewCtrl {
private $location?: ng.ILocationService
private $rootScope?: ng.IRootScopeService
public platformString: string
private completedInitialSync = false
private syncStatus: any
private notesCollapsed = false
private tagsCollapsed = false
private showingDownloadStatus = false
private uploadSyncStatus: any

/* @ngInject */
constructor(
Expand Down Expand Up @@ -89,35 +86,14 @@ class ApplicationViewCtrl extends PureViewCtrl {
/** @override */
async onAppEvent(eventName: ApplicationEvent) {
super.onAppEvent(eventName);
if (eventName === ApplicationEvent.LocalDataIncrementalLoad) {
this.updateLocalDataStatus();
} else if (
eventName === ApplicationEvent.SyncStatusChanged ||
eventName === ApplicationEvent.FailedSync
) {
this.updateSyncStatus();
} else if (eventName === ApplicationEvent.LocalDataLoaded) {
this.updateLocalDataStatus();
} else if (eventName === ApplicationEvent.WillSync) {
if (!this.completedInitialSync) {
this.syncStatus = this.application!.getStatusService().replaceStatusWithString(
this.syncStatus,
"Syncing…"
);
}
} else if (eventName === ApplicationEvent.CompletedFullSync) {
if (!this.completedInitialSync) {
this.syncStatus = this.application!.getStatusService().removeStatus(this.syncStatus);
this.completedInitialSync = true;
}
} else if (eventName === ApplicationEvent.LocalDatabaseReadError) {
this.application!.alertService!.alert(
'Unable to load local database. Please restart the app and try again.'
);
if (eventName === ApplicationEvent.LocalDatabaseReadError) {
alertDialog({
text: 'Unable to load local database. Please restart the app and try again.'
});
} else if (eventName === ApplicationEvent.LocalDatabaseWriteError) {
this.application!.alertService!.alert(
'Unable to write to local database. Please restart the app and try again.'
);
alertDialog({
text: 'Unable to write to local database. Please restart the app and try again.'
});
}
}

Expand All @@ -141,77 +117,6 @@ class ApplicationViewCtrl extends PureViewCtrl {
}
}

updateLocalDataStatus() {
const syncStatus = this.application!.getSyncStatus();
const stats = syncStatus.getStats();
const encryption = this.application!.isEncryptionAvailable();
if (stats.localDataDone) {
this.syncStatus = this.application!.getStatusService().removeStatus(this.syncStatus);
return;
}
const notesString = `${stats.localDataCurrent}/${stats.localDataTotal} items...`;
const loadingStatus = encryption
? `Decrypting ${notesString}`
: `Loading ${notesString}`;
this.syncStatus = this.application!.getStatusService().replaceStatusWithString(
this.syncStatus,
loadingStatus
);
}

updateSyncStatus() {
const syncStatus = this.application!.getSyncStatus();
const stats = syncStatus.getStats();
if (syncStatus.hasError()) {
this.syncStatus = this.application!.getStatusService().replaceStatusWithString(
this.syncStatus,
'Unable to Sync'
);
} else if (stats.downloadCount > 20) {
const text = `Downloading ${stats.downloadCount} items. Keep app open.`;
this.syncStatus = this.application!.getStatusService().replaceStatusWithString(
this.syncStatus,
text
);
this.showingDownloadStatus = true;
} else if (this.showingDownloadStatus) {
this.showingDownloadStatus = false;
const text = "Download Complete.";
this.syncStatus = this.application!.getStatusService().replaceStatusWithString(
this.syncStatus,
text
);
setTimeout(() => {
this.syncStatus = this.application!.getStatusService().removeStatus(this.syncStatus);
}, 2000);
} else if (stats.uploadTotalCount > 20) {
const completionPercentage = stats.uploadCompletionCount === 0
? 0
: stats.uploadCompletionCount / stats.uploadTotalCount;

const stringPercentage = completionPercentage.toLocaleString(
undefined,
{ style: 'percent' }
);

this.uploadSyncStatus = this.application!.getStatusService().replaceStatusWithString(
this.uploadSyncStatus,
`Syncing ${stats.uploadTotalCount} items (${stringPercentage} complete)`,
);
} else {
if (this.syncStatus) {
this.syncStatus = this.application!.getStatusService().removeStatus(
this.syncStatus
);
}
if (this.uploadSyncStatus) {
this.uploadSyncStatus = this.application!.getStatusService().removeStatus(
this.uploadSyncStatus
);
}
}
}

addDragDropHandlers() {
/**
* Disable dragging and dropping of files (but allow text) into main SN interface.
Expand Down
Loading

0 comments on commit 212a65f

Please sign in to comment.