Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,15 @@ export default class ObsidianGit extends Plugin {
}
}

handleOnlineStatusChange() {
if (!this.state.offlineMode) return;

if (navigator.onLine) {
this.setPluginState({ offlineMode: false });
this.promiseQueue.addTask(() => this.pullChangesFromRemote());
}
}

/** This method only registers events, views, commands and more.
*
* This only needs to be called once since the registered events are
Expand All @@ -209,6 +218,7 @@ export default class ObsidianGit extends Plugin {
this.refresh().catch((e) => this.displayError(e));
})
);

this.registerEvent(
this.app.workspace.on("obsidian-git:head-change", () => {
this.refreshUpdatedHead();
Expand All @@ -226,6 +236,12 @@ export default class ObsidianGit extends Plugin {
this.onActiveLeafChange(leaf);
})
);

this.registerDomEvent(window, "online", () => {
this.handleOnlineStatusChange();
}
);

this.registerEvent(
this.app.vault.on("modify", () => {
this.debRefresh();
Expand Down Expand Up @@ -466,6 +482,12 @@ export default class ObsidianGit extends Plugin {
return Platform.isDesktopApp;
}

hasConnectivity() {
if (navigator.onLine) return true;
this.setPluginState({ offlineMode: true });
return (new Notice('No Connectivity'), false)
}

async init({ fromReload = false }): Promise<void> {
if (this.settings.showStatusBar) {
const statusBarEl = this.addStatusBarItem();
Expand Down Expand Up @@ -663,6 +685,7 @@ export default class ObsidianGit extends Plugin {
///Used for command
async pullChangesFromRemote(): Promise<void> {
if (!(await this.isAllInitialized())) return;
if (!(this.hasConnectivity())) return;

const filesUpdated = await this.pull();
await this.automaticsManager.setUpAutoCommitAndSync();
Expand Down Expand Up @@ -695,11 +718,13 @@ export default class ObsidianGit extends Plugin {
commitMessage?: string
): Promise<void> {
if (!(await this.isAllInitialized())) return;
const isConnected = this.hasConnectivity();

if (
this.settings.syncMethod == "reset" &&
this.settings.pullBeforePush
) {
if (!isConnected) return;
await this.pull();
}

Expand All @@ -716,11 +741,13 @@ export default class ObsidianGit extends Plugin {
this.settings.syncMethod != "reset" &&
this.settings.pullBeforePush
) {
if (!isConnected) return;
await this.pull();
}

if (!this.settings.disablePush) {
// Prevent trying to push every time. Only if unpushed commits are present
if (!isConnected) return;
if (
(await this.remotesAreSet()) &&
(await this.gitManager.canPush())
Expand Down Expand Up @@ -891,9 +918,9 @@ export default class ObsidianGit extends Plugin {
*/
async push(): Promise<boolean> {
if (!(await this.isAllInitialized())) return false;
if (!(await this.remotesAreSet())) {
return false;
}
if (!(await this.remotesAreSet())) return false;
if (!(this.hasConnectivity())) return false;

const hadConflict = this.localStorage.getConflict();
try {
if (this.gitManager instanceof SimpleGit)
Expand Down