Skip to content

Commit

Permalink
fix: Lint issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Crash-- committed Dec 6, 2019
1 parent f4f4382 commit d160404
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 228 deletions.
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ node_modules/**
src/authentication/node_modules/**
src/authentication/build/**
src/drive/targets/mobile/**
!src/drive/targets/mobile/InitAppMobile.*
259 changes: 128 additions & 131 deletions src/drive/targets/mobile/InitAppMobile.jsx
Original file line number Diff line number Diff line change
@@ -1,174 +1,171 @@
/* global __DEVELOPMENT__ */
/* global __DEVELOPMENT__, cordova */

import "whatwg-fetch";
import React from "react";
import { render } from "react-dom";
import { hashHistory } from "react-router";
import localforage from "localforage";
import 'whatwg-fetch'
import React from 'react'
import { render } from 'react-dom'
import { hashHistory } from 'react-router'
import localforage from 'localforage'

import { CozyProvider } from "cozy-client";
import { I18n, initTranslation } from "cozy-ui/transpiled/react/I18n";
import { isIOSApp } from "cozy-device-helper";
import { Document } from "cozy-doctypes";
import Alerter from "cozy-ui/transpiled/react/Alerter";
import { CozyProvider } from 'cozy-client'
import { I18n, initTranslation } from 'cozy-ui/transpiled/react/I18n'
import { isIOSApp } from 'cozy-device-helper'
import { Document } from 'cozy-doctypes'
import Alerter from 'cozy-ui/transpiled/react/Alerter'

import configureStore from "drive/store/configureStore";
import { loadState } from "drive/store/persistedState";
import { startBackgroundService } from "drive/mobile/lib/background";
import { configureReporter } from "drive/lib/reporter";
import configureStore from 'drive/store/configureStore'
import { loadState } from 'drive/store/persistedState'
import { startBackgroundService } from 'drive/mobile/lib/background'
import { configureReporter } from 'drive/lib/reporter'

import {
startTracker,
useHistoryForTracker,
startHeartBeat,
stopHeartBeat
} from "drive/mobile/lib/tracker";
import { getTranslateFunction } from "drive/mobile/lib/i18n";
import { scheduleNotification } from "drive/mobile/lib/notification";
import { getLang, initClient } from "drive/mobile/lib/cozy-helper";
import DriveMobileRouter from "drive/mobile/modules/authorization/DriveMobileRouter";
import { backupImages } from "drive/mobile/modules/mediaBackup/duck";
} from 'drive/mobile/lib/tracker'
import { getTranslateFunction } from 'drive/mobile/lib/i18n'
import { scheduleNotification } from 'drive/mobile/lib/notification'
import { getLang, initClient } from 'drive/mobile/lib/cozy-helper'
import DriveMobileRouter from 'drive/mobile/modules/authorization/DriveMobileRouter'
import { backupImages } from 'drive/mobile/modules/mediaBackup/duck'
import {
getClientSettings,
getToken
} from "drive/mobile/modules/authorization/duck";
import {
getServerUrl,
isAnalyticsOn
} from "drive/mobile/modules/settings/duck";
import { ONBOARDED_ITEM } from "drive/mobile/modules/onboarding/OnBoarding";
} from 'drive/mobile/modules/authorization/duck'
import { getServerUrl, isAnalyticsOn } from 'drive/mobile/modules/settings/duck'
import { ONBOARDED_ITEM } from 'drive/mobile/modules/onboarding/OnBoarding'

// Allows to know if the launch of the application has been done by the service background
// @see: https://git.io/vSQBC
const isBackgroundServiceParameter = () => {
const queryDict = location.search
.substr(1)
.split("&")
.split('&')
.reduce((acc, item) => {
const [prop, val] = item.split("=");
return { ...acc, [prop]: val };
}, {});
const [prop, val] = item.split('=')
return { ...acc, [prop]: val }
}, {})

return queryDict.backgroundservice;
};
return queryDict.backgroundservice
}

class InitAppMobile {
initialize = () => {
this.appReady = new Promise((resolve, reject) => {
this.resolvePromise = resolve;
this.rejectPromise = reject;
});
this.bindEvents();
this.stardedApp = false;
this.isStarting = false;
if (__DEVELOPMENT__ && typeof cordova === "undefined") this.onDeviceReady();
return this.appReady;
};
this.resolvePromise = resolve
this.rejectPromise = reject
})
this.bindEvents()
this.stardedApp = false
this.isStarting = false
if (__DEVELOPMENT__ && typeof cordova === 'undefined') this.onDeviceReady()
return this.appReady
}

bindEvents = () => {
document.addEventListener(
"deviceready",
'deviceready',
this.onDeviceReady.bind(this),
false
);
document.addEventListener("resume", this.onResume.bind(this), false);
document.addEventListener("pause", this.onPause.bind(this), false);
)
document.addEventListener('resume', this.onResume.bind(this), false)
document.addEventListener('pause', this.onPause.bind(this), false)
/*We add fastclick only for iOS since Chrome removed this behavior (iOS also, but
we still use UIWebview and not WKWebview... )*/
if (isIOSApp()) {
var FastClick = require("fastclick");
var FastClick = require('fastclick')
document.addEventListener(
"DOMContentLoaded",
'DOMContentLoaded',
function() {
FastClick.attach(document.body);
FastClick.attach(document.body)
},
false
);
)
}
};
}

getCozyURL = async () => {
if (this.cozyURL) return this.cozyURL;
const persistedState = (await this.getPersistedState()) || {};
if (this.cozyURL) return this.cozyURL
const persistedState = (await this.getPersistedState()) || {}
// TODO: not ideal to access the server URL in the persisted state like this...
this.cozyURL = persistedState.mobile
? persistedState.mobile.settings.serverUrl
: "";
return this.cozyURL;
};
: ''
return this.cozyURL
}

getPersistedState = async () => {
if (this.persistedState) return this.persistedState;
this.persistedState = await loadState();
return this.persistedState;
};
if (this.persistedState) return this.persistedState
this.persistedState = await loadState()
return this.persistedState
}

getClient = async () => {
if (this.client) return this.client;
const cozyURL = await this.getCozyURL();
this.client = initClient(cozyURL);
if (this.client) return this.client
const cozyURL = await this.getCozyURL()
this.client = initClient(cozyURL)

if (!Document.cozyClient) {
Document.registerClient(this.client);
Document.registerClient(this.client)
}
return this.client;
};
return this.client
}

getPolyglot = () => {
if (!this.polyglot) {
this.polyglot = initTranslation(getLang(), lang =>
require(`drive/locales/${lang}`)
);
)
}
return this.polyglot;
};
return this.polyglot
}

getStore = async () => {
if (this.store) return this.store;
const client = await this.getClient();
const polyglot = this.getPolyglot();
const persistedState = await this.getPersistedState();
if (this.store) return this.store
const client = await this.getClient()
const polyglot = this.getPolyglot()
const persistedState = await this.getPersistedState()
this.store = configureStore(
client,
polyglot.t.bind(polyglot),
persistedState
);
return this.store;
};
)
return this.store
}

onDeviceReady = async () => {
if (this.isStarting === true) {
return;
return
}
this.isStarting = true;
const store = await this.getStore();
this.startApplication();
await this.appReady;
this.openWith();
this.isStarting = true
const store = await this.getStore()
this.startApplication()
await this.appReady
this.openWith()
if (isBackgroundServiceParameter()) {
startBackgroundService();
startBackgroundService()
}
store.dispatch(backupImages());
if (navigator && navigator.splashscreen) navigator.splashscreen.hide();
};
store.dispatch(backupImages())
if (navigator && navigator.splashscreen) navigator.splashscreen.hide()
}

onResume = async () => {
const store = await this.getStore();
store.dispatch(backupImages());
if (isAnalyticsOn(store.getState())) startHeartBeat();
};
const store = await this.getStore()
store.dispatch(backupImages())
if (isAnalyticsOn(store.getState())) startHeartBeat()
}

onPause = async () => {
const store = await this.getStore();
if (isAnalyticsOn(store.getState())) stopHeartBeat();
const store = await this.getStore()
if (isAnalyticsOn(store.getState())) stopHeartBeat()
// TODO: selector
if (store.getState().mobile.mediaBackup.currentUpload && isIOSApp()) {
const t = getTranslateFunction();
const t = getTranslateFunction()
scheduleNotification({
text: t("mobile.notifications.backup_paused")
});
text: t('mobile.notifications.backup_paused')
})
}
};
}

/**
* openWith is called when the app is launched by the OS
Expand All @@ -186,64 +183,64 @@ class InitAppMobile {
console.log("init failed: " + err);
}

cordova.openwith.init(initSuccess, initError);
cordova.openwith.addHandler(this.openWithHandler);
};
cordova.openwith.init(initSuccess, initError)
cordova.openwith.addHandler(this.openWithHandler)
}
//We write the items in localStorage and then push
//a to a specific route
openWithHandler = async intent => {
try {
await localforage.removeItem("importedFiles");
await localforage.removeItem('importedFiles')
} catch (e) {
console.log("error during removeItem", e);
return Alerter.error("ImportToDrive.error");
console.log('error during removeItem', e)
return Alerter.error('ImportToDrive.error')
}
if (intent.items && intent.items.length > 0) {
try {
await localforage.setItem("importedFiles", intent.items);
hashHistory.push("/uploadfrommobile");
await localforage.setItem('importedFiles', intent.items)
hashHistory.push('/uploadfrommobile')
} catch (error) {
Alerter.error("ImportToDrive.error");
Alerter.error('ImportToDrive.error')
}
}
};
}
migrateToCozyAuth = async () => {
const store = await this.getStore();
const oauthOptions = getClientSettings(store.getState());
const token = getToken(store.getState());
const uri = await this.getCozyURL();
const alreadyMigrated = await localforage.getItem("credentials");
const store = await this.getStore()
const oauthOptions = getClientSettings(store.getState())
const token = getToken(store.getState())
const uri = await this.getCozyURL()
const alreadyMigrated = await localforage.getItem('credentials')

if (uri && oauthOptions && token && !alreadyMigrated) {
await localforage.setItem(ONBOARDED_ITEM, true);
return await localforage.setItem("credentials", {
await localforage.setItem(ONBOARDED_ITEM, true)
return await localforage.setItem('credentials', {
uri,
oauthOptions,
token
});
})
}

return;
};
return
}
startApplication = async () => {
if (this.stardedApp) return;
if (this.stardedApp) return

const store = await this.getStore();
const client = await this.getClient();
const polyglot = await this.getPolyglot();
const store = await this.getStore()
const client = await this.getClient()
const polyglot = await this.getPolyglot()

//needed to migrate from cozy-drive auth to cozy-authenticate.
//@TODO should be remove one day. It has been added for the migration
//from 1.18.17 to 1.18.18
await this.migrateToCozyAuth();
await this.migrateToCozyAuth()

configureReporter();
useHistoryForTracker(hashHistory);
configureReporter()
useHistoryForTracker(hashHistory)
if (isAnalyticsOn(store.getState())) {
startTracker(getServerUrl(store.getState()));
startTracker(getServerUrl(store.getState()))
}

const root = document.querySelector("[role=application]");
const root = document.querySelector('[role=application]')
render(
<I18n lang={getLang()} polyglot={polyglot}>
<CozyProvider client={client}>
Expand All @@ -252,12 +249,12 @@ class InitAppMobile {
</I18n>,
root,
() => {
this.stardedApp = true;
this.isStarting = false;
this.resolvePromise(true);
this.stardedApp = true
this.isStarting = false
this.resolvePromise(true)
}
);
};
)
}
}

export default InitAppMobile;
export default InitAppMobile
Loading

0 comments on commit d160404

Please sign in to comment.