Skip to content

Commit

Permalink
Merge pull request #64 from GeoffreyChen777/electron-dev
Browse files Browse the repository at this point in the history
+ custom atlas and pause sync
  • Loading branch information
GeoffreyChen777 authored Apr 12, 2022
2 parents 38b72ce + 835c870 commit 17b3f4d
Show file tree
Hide file tree
Showing 11 changed files with 103 additions and 6 deletions.
2 changes: 1 addition & 1 deletion quasar.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ module.exports = configure(function (ctx) {
builder: {
// https://www.electron.build/configuration/configuration
appId: 'dev.paperlib.app',
productName: 'paperlib',
productName: 'Paperlib',
publish: [
{
provider: 'generic',
Expand Down
2 changes: 1 addition & 1 deletion quasar.conf.mac.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ module.exports = configure(function (ctx) {
builder: {
// https://www.electron.build/configuration/configuration
appId: 'dev.paperlib.app',
productName: 'paperlib',
productName: 'Paperlib',
publish: [
{
provider: 'generic',
Expand Down
2 changes: 1 addition & 1 deletion quasar.conf.win.js
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ module.exports = configure(function (ctx) {
builder: {
// https://www.electron.build/configuration/configuration
appId: 'dev.paperlib.app',
productName: 'paperlib',
productName: 'Paperlib',
publish: [
{
provider: 'generic',
Expand Down
8 changes: 8 additions & 0 deletions src-electron/electron-main.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ function createWindow() {
mainWindow = null;
});

mainWindow.on('blur', () => {
mainWindow.webContents.send('window-lost-focus');
});

mainWindow.on('focus', () => {
mainWindow.webContents.send('window-gained-focus');
});

setMainMenu();
}

Expand Down
2 changes: 1 addition & 1 deletion src/css/preference-view.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
.preference-view {
width: 800px;
height: 420px;
min-height: 450px;
background-color: var(--q-bg-primary) !important;
color: var(--q-text) !important;
}
Expand Down
21 changes: 21 additions & 0 deletions src/interactors/entity-interactor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,27 @@ export class EntityInteractor {
this.sharedState.set('viewState.realmReinited', new Date().getTime());
}

pauseSync() {
this.scheduler.removeById('pauseSync');
const task = new Task('pauseSync', () => {
void this.dbRepository.pauseSync();
this.scheduler.removeById('pauseSync');
});

const job = new SimpleIntervalJob(
{ seconds: 36, runImmediately: false },
task,
'pauseSync'
);

this.scheduler.addSimpleIntervalJob(job);
}

resumeSync() {
this.scheduler.removeById('pauseSync');
void this.dbRepository.resumeSync();
}

// ============================================================
initFileRepository() {
if (this.preference.get('syncFileStorage') === 'webdav') {
Expand Down
8 changes: 8 additions & 0 deletions src/pages/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ export default defineComponent({
void window.entityInteractor.addFromPlugin([message as string]);
});
window.systemInteractor.registerSignal('window-lost-focus', (_event, _message) => {
void window.entityInteractor.pauseSync();
});
window.systemInteractor.registerSignal('window-gained-focus', (_event, _message) => {
void window.entityInteractor.resumeSync();
});
window.systemInteractor.registerState('viewState.realmReinited', (_event, _message) => {
void (async () => {
await reloadEntities();
Expand Down
33 changes: 32 additions & 1 deletion src/pages/preference_view/components/CloudTab.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,32 @@
<template>
<q-tab-panel name="cloud" class="preference-tab">
<div class="row setting-title" style="text-align: left !important">
<span style="font-weight: 500">
Cloud Backend
</span>
</div>
<div class="row setting-content q-mb-md">
<div class="col-2">
<q-radio v-model="syncCloudBackend" val="official" label="Official" size="xs" @update:model-value="(value) => onUpdate('syncCloudBackend', value)" />
</div>
<div class="col-4">
<q-radio class='q-ml-sm' v-model="syncCloudBackend" val="custom-atlas" label="Custom Mongodb Atlas" size="xs" @update:model-value="(value) => onUpdate('syncCloudBackend', value)" />
</div>
<div class="col-4 q-mt-xs" v-if="syncCloudBackend === 'custom-atlas'">
<div class="radius-border setting-content preference-input" >
<q-input
borderless
v-model="syncAPPID"
placeholder="Mongodb Atlas APP ID"
dense
style="max-height: 22px"
@update:model-value="(value) => onUpdate('syncAPPID', value)"
/>
</div>
</div>
</div>


<div class="row setting-title q-mb-xs" style="text-align: left !important">
<span style="font-weight: 500">
Cloud Account
Expand Down Expand Up @@ -53,7 +80,7 @@
v-if="preference.useSync"
/>
</div>
<div class="col-1">
<div class="col-1" v-if="syncCloudBackend === 'official'">
<q-btn
unelevated
no-caps
Expand Down Expand Up @@ -190,6 +217,8 @@ export default defineComponent({
const syncEmail = ref(props.preference.syncEmail);
const syncPassword = ref('');
const syncFileStorage = ref(props.preference.syncFileStorage);
const syncCloudBackend = ref(props.preference.syncCloudBackend);
const syncAPPID = ref(props.preference.syncAPPID);
const syncFileStorageAvaliable = ref(window.systemInteractor.getState('viewState.syncFileStorageAvaliable') as unknown as boolean);
const webdavURL = ref(props.preference.webdavURL);
const webdavUsername = ref(props.preference.webdavUsername);
Expand Down Expand Up @@ -245,6 +274,8 @@ export default defineComponent({
syncEmail,
syncPassword,
syncFileStorage,
syncCloudBackend,
syncAPPID,
syncFileStorageAvaliable,
webdavURL,
webdavUsername,
Expand Down
23 changes: 22 additions & 1 deletion src/repositories/db-repository/db-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export async function initRealm(this: DBRepository, reinit = false) {
this._realm = null;
this.app = null;
this.cloudConfig = null;
this.syncSession = null;
this.localConfig = null;
this.entitiesListenerInited = false;
this.categorizersListenerInited = {
Expand All @@ -34,6 +35,7 @@ export async function initRealm(this: DBRepository, reinit = false) {
if (this.cloudConfig) {
try {
this._realm = new Realm(this.cloudConfig);
this.syncSession = this._realm.syncSession;
} catch (err) {
this.sharedState.set(
'viewState.alertInformation',
Expand Down Expand Up @@ -113,8 +115,15 @@ export async function loginCloud(
): Promise<Realm.User | null> {
if (!this.app) {
process.chdir(this.sharedState.dbState.defaultPath.value as string);

let id;
if (this.preference.get('syncCloudBackend') === 'official') {
id = 'paperlib-iadbj';
} else {
id = this.preference.get('syncAPPID') as string;
}
this.app = new Realm.App({
id: 'paperlib-iadbj',
id: id,
});
}

Expand Down Expand Up @@ -160,3 +169,15 @@ export async function logoutCloud(this: DBRepository) {

await this.initRealm(true);
}

export function pauseSync(this: DBRepository) {
if (this.syncSession) {
this.syncSession.pause();
}
}

export function resumeSync(this: DBRepository) {
if (this.syncSession) {
this.syncSession.resume();
}
}
6 changes: 6 additions & 0 deletions src/repositories/db-repository/db-repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ import {
initRealm,
loginCloud,
logoutCloud,
pauseSync,
resumeSync,
} from './db-init';
import {
categorizers,
Expand All @@ -36,6 +38,8 @@ export class DBRepository {
cloudConfig: Realm.Configuration | null;
localConfig: Realm.Configuration | null;

syncSession: Realm.App.Sync.Session | null = null;

entitiesListenerInited: boolean;
categorizersListenerInited: Record<string, boolean>;

Expand Down Expand Up @@ -72,6 +76,8 @@ export class DBRepository {
loginCloud = loginCloud;
logoutCloud = logoutCloud;
migrateLocaltoCloud = migrateLocaltoCloud;
pauseSync = pauseSync;
resumeSync = resumeSync;

// CRUD Func
entitiesByIds = entitiesByIds;
Expand Down
2 changes: 2 additions & 0 deletions src/utils/preference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ export const defaultPreferences: PreferenceType = {
exportReplacement: [],

useSync: false,
syncCloudBackend: 'official',
syncAPPID: '',
syncAPIKey: '',
syncEmail: '',

Expand Down

0 comments on commit 17b3f4d

Please sign in to comment.