Source

frontend/controllers/cloud_sync/cloud_sync_status_service.js

/* This file is part of Ezra Bible App.

   Copyright (C) 2019 - 2026 Ezra Bible App Development Team <contact@ezrabibleapp.net>

   Ezra Bible App is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation, either version 2 of the License, or
   (at your option) any later version.

   Ezra Bible App is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with Ezra Bible App. See the file LICENSE.
   If not, see <http://www.gnu.org/licenses/>. */

/**
 * Reads the latest cloud-sync result and displays a provider-labeled success or
 * failure notification, respecting the Wi-Fi-only setting.
 */
class CloudSyncStatusService {
  constructor(getIpcSettings, platformHelper) {
    this.getIpcSettings = getIpcSettings;
    this.platformHelper = platformHelper;
  }

  get ipcSettings() {
    return this.getIpcSettings();
  }

  async show(providerAdapter) {
    const onlyWifi = await this.ipcSettings.get('cloudSyncOnlyWifi', false);
    if (onlyWifi && navigator.connection.type != 'wifi') {
      return;
    }

    let syncTime = '--';
    const rawSyncTime = await this.ipcSettings.get('lastCloudSyncTime', null);
    if (rawSyncTime !== null) {
      const date = new Date(rawSyncTime);
      syncTime = date.toLocaleDateString() + ' / ' + date.toLocaleTimeString();
    }

    const syncResult = await this.ipcSettings.get('lastCloudSyncResult', '');
    console.log(`Last ${providerAdapter.getDisplayName()} sync result: ${syncResult}`);

    if (syncResult == null || syncResult == '' || syncResult == 'NONE') {
      return;
    }

    const msgPosition = this.platformHelper.getIziPosition();
    const displayName = providerAdapter.getDisplayName();

    if (syncResult.indexOf('FAILED') != -1) {
      // eslint-disable-next-line no-undef
      iziToast.error({
        title: i18n.t('cloud-sync.sync-msg-title', { provider: displayName }),
        message: i18n.t('cloud-sync.sync-failed-msg', {
          provider: displayName,
          date: syncTime,
          syncResult
        }),
        position: msgPosition,
        timeout: 10000
      });
    } else {
      // eslint-disable-next-line no-undef
      iziToast.success({
        title: i18n.t('cloud-sync.sync-msg-title', { provider: displayName }),
        message: i18n.t('cloud-sync.sync-success-msg', { provider: displayName, date: syncTime }),
        position: msgPosition,
        timeout: 5000
      });
    }
  }
}

module.exports = CloudSyncStatusService;