2018-07-05 22:40:24 +02:00
|
|
|
import Vue from 'vue';
|
|
|
|
|
2018-07-10 14:32:48 +02:00
|
|
|
import { messages, getBrowserLocales } from '@/i18n';
|
|
|
|
import { storageAvailable } from '@/tools';
|
2018-07-27 16:13:16 +02:00
|
|
|
import { DEFAULT_TILE_SERVER, TILE_SERVERS, VERSION } from '@/constants';
|
2018-06-26 11:39:43 +02:00
|
|
|
import * as types from './mutations-types';
|
|
|
|
|
2018-07-26 09:05:22 +02:00
|
|
|
function loadDataFromStorage(name) {
|
2018-07-17 16:32:34 +02:00
|
|
|
try {
|
|
|
|
const value = localStorage.getItem(name);
|
|
|
|
if (value) {
|
|
|
|
return JSON.parse(value);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
} catch (e) {
|
2018-07-26 09:05:22 +02:00
|
|
|
console.error(`Unable to load data from storage using key ${name}: ${e}.`);
|
2018-07-17 16:32:34 +02:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 16:13:16 +02:00
|
|
|
function handleMigrations() {
|
|
|
|
if (!storageAvailable('localStorage')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const version = loadDataFromStorage('version');
|
|
|
|
|
|
|
|
// Migration from pre-0.1 to 0.1
|
|
|
|
if (version === null) {
|
|
|
|
const preventSuspend = loadDataFromStorage('preventSuspend');
|
|
|
|
if (preventSuspend !== null) {
|
|
|
|
localStorage.setItem('hasPreventSuspendPermission', JSON.stringify(preventSuspend));
|
|
|
|
}
|
|
|
|
localStorage.removeItem('preventSuspend');
|
|
|
|
localStorage.setItem('version', JSON.stringify(VERSION));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 09:05:22 +02:00
|
|
|
// Load unsent reports from storage
|
|
|
|
let unsentReports = null;
|
|
|
|
if (storageAvailable('localStorage')) {
|
|
|
|
unsentReports = loadDataFromStorage('unsentReports');
|
|
|
|
}
|
|
|
|
|
2018-07-10 14:32:48 +02:00
|
|
|
// Load settings from storage
|
|
|
|
let locale = null;
|
2018-07-27 16:13:16 +02:00
|
|
|
let hasGeolocationPermission = null;
|
|
|
|
let hasPlaySoundPermission = null;
|
|
|
|
let hasPreventSuspendPermission = null;
|
|
|
|
let hasVibratePermission = null;
|
2018-08-24 23:34:37 +02:00
|
|
|
let shouldAutorotateMap = null;
|
2018-07-16 17:26:10 +02:00
|
|
|
let skipOnboarding = null;
|
2018-07-17 16:32:34 +02:00
|
|
|
let tileServer = null;
|
2018-07-10 14:32:48 +02:00
|
|
|
if (storageAvailable('localStorage')) {
|
2018-07-27 16:13:16 +02:00
|
|
|
handleMigrations();
|
|
|
|
|
|
|
|
hasGeolocationPermission = loadDataFromStorage('hasGeolocationPermission');
|
|
|
|
hasPlaySoundPermission = loadDataFromStorage('hasPlaySoundPermission');
|
|
|
|
hasPreventSuspendPermission = loadDataFromStorage('hasPreventSuspendPermission');
|
|
|
|
hasVibratePermission = loadDataFromStorage('hasVibratePermission');
|
2018-08-24 23:34:37 +02:00
|
|
|
shouldAutorotateMap = loadDataFromStorage('shouldAutorotateMap');
|
2018-07-26 09:05:22 +02:00
|
|
|
skipOnboarding = loadDataFromStorage('skipOnboarding');
|
2018-07-17 16:32:34 +02:00
|
|
|
|
2018-07-26 09:05:22 +02:00
|
|
|
tileServer = loadDataFromStorage('tileServer');
|
2018-07-27 16:13:16 +02:00
|
|
|
if (tileServer && !TILE_SERVERS[tileServer] && !tileServer.startsWith('custom:')) {
|
2018-07-17 16:32:34 +02:00
|
|
|
tileServer = null;
|
2018-07-16 17:26:10 +02:00
|
|
|
}
|
2018-07-10 14:32:48 +02:00
|
|
|
|
2018-07-31 16:48:02 +02:00
|
|
|
locale = loadDataFromStorage('locale');
|
|
|
|
if (!(locale in messages)) {
|
2018-07-10 14:32:48 +02:00
|
|
|
locale = null;
|
|
|
|
}
|
|
|
|
if (!locale) {
|
|
|
|
// Get best matching locale from browser
|
|
|
|
const locales = getBrowserLocales();
|
|
|
|
for (let i = 0; i < locales.length; i += 1) {
|
|
|
|
if (messages[locales[i]]) {
|
|
|
|
locale = locales[i];
|
|
|
|
break; // Break at first matching locale
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-26 11:39:43 +02:00
|
|
|
export const initialState = {
|
2018-07-17 16:37:46 +02:00
|
|
|
hasGoneThroughIntro: false,
|
2018-08-13 20:18:14 +02:00
|
|
|
hasVibratedOnce: false,
|
2018-07-04 18:20:28 +02:00
|
|
|
isLoading: false,
|
2018-07-21 20:00:37 +02:00
|
|
|
location: {
|
|
|
|
error: null,
|
2018-08-02 16:58:08 +02:00
|
|
|
gpx: [],
|
2018-07-21 20:00:37 +02:00
|
|
|
watcherID: null,
|
|
|
|
},
|
2018-08-24 18:13:41 +02:00
|
|
|
lastReportFetchingLocation: [null, null],
|
2018-07-21 20:00:37 +02:00
|
|
|
map: {
|
|
|
|
center: [null, null],
|
|
|
|
zoom: null,
|
|
|
|
},
|
2018-07-12 17:48:26 +02:00
|
|
|
reportDetails: {
|
|
|
|
id: null,
|
2018-09-04 15:49:02 +02:00
|
|
|
previousId: null,
|
2018-07-12 17:48:26 +02:00
|
|
|
userAsked: null,
|
|
|
|
},
|
2018-06-26 11:39:43 +02:00
|
|
|
reports: [],
|
2018-07-26 09:05:22 +02:00
|
|
|
unsentReports: unsentReports || [],
|
2018-07-10 14:32:48 +02:00
|
|
|
settings: {
|
|
|
|
locale: locale || 'en',
|
2018-07-27 16:13:16 +02:00
|
|
|
hasGeolocationPermission: (
|
|
|
|
hasGeolocationPermission !== null ? hasGeolocationPermission : true
|
|
|
|
),
|
|
|
|
hasPlaySoundPermission: (
|
|
|
|
hasPlaySoundPermission !== null ? hasPlaySoundPermission : true
|
|
|
|
),
|
|
|
|
hasPreventSuspendPermission: (
|
|
|
|
hasPreventSuspendPermission !== null ? hasPreventSuspendPermission : true
|
|
|
|
),
|
|
|
|
hasVibratePermission: (
|
|
|
|
hasVibratePermission !== null ? hasVibratePermission : true
|
|
|
|
),
|
2018-08-24 23:34:37 +02:00
|
|
|
shouldAutorotateMap: shouldAutorotateMap !== null ? shouldAutorotateMap : false,
|
2018-07-16 17:26:10 +02:00
|
|
|
skipOnboarding: skipOnboarding || false,
|
2018-07-17 16:32:34 +02:00
|
|
|
tileServer: tileServer || DEFAULT_TILE_SERVER,
|
2018-07-10 14:32:48 +02:00
|
|
|
},
|
2018-06-26 11:39:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export const mutations = {
|
2018-08-13 20:18:14 +02:00
|
|
|
[types.HAS_VIBRATED_ONCE](state) {
|
|
|
|
state.hasVibratedOnce = true;
|
|
|
|
},
|
2018-07-17 16:37:46 +02:00
|
|
|
[types.INTRO_WAS_SEEN](state) {
|
|
|
|
state.hasGoneThroughIntro = true;
|
|
|
|
},
|
2018-08-01 11:35:49 +02:00
|
|
|
[types.INTRO_WAS_UNSEEN](state) {
|
|
|
|
state.hasGoneThroughIntro = false;
|
|
|
|
},
|
2018-07-21 20:00:37 +02:00
|
|
|
[types.IS_DONE_LOADING](state) {
|
|
|
|
state.isLoading = false;
|
|
|
|
},
|
2018-07-04 18:20:28 +02:00
|
|
|
[types.IS_LOADING](state) {
|
|
|
|
state.isLoading = true;
|
|
|
|
},
|
2018-07-21 20:00:37 +02:00
|
|
|
[types.PUSH_REPORT](state, { report }) {
|
|
|
|
const reportIndex = state.reports.findIndex(item => item.id === report.id);
|
|
|
|
if (reportIndex === -1) {
|
|
|
|
state.reports.push(report);
|
|
|
|
} else {
|
|
|
|
Vue.set(state.reports, reportIndex, report);
|
|
|
|
}
|
|
|
|
},
|
2018-07-26 09:05:22 +02:00
|
|
|
[types.PUSH_UNSENT_REPORT](state, { report }) {
|
|
|
|
state.unsentReports.push(report);
|
|
|
|
if (storageAvailable('localStorage')) {
|
|
|
|
localStorage.setItem('unsentReports', JSON.stringify(state.unsentReports));
|
|
|
|
}
|
|
|
|
},
|
2018-07-26 13:55:08 +02:00
|
|
|
[types.REMOVE_UNSENT_REPORT](state, { index }) {
|
|
|
|
state.unsentReports.splice(index, 1);
|
|
|
|
if (storageAvailable('localStorage')) {
|
|
|
|
localStorage.setItem('unsentReports', JSON.stringify(state.unsentReports));
|
|
|
|
}
|
|
|
|
},
|
2018-07-21 20:00:37 +02:00
|
|
|
[types.SET_CURRENT_MAP_CENTER](state, { center }) {
|
|
|
|
Vue.set(state.map, 'center', center);
|
|
|
|
},
|
|
|
|
[types.SET_CURRENT_MAP_ZOOM](state, { zoom }) {
|
|
|
|
Vue.set(state.map, 'zoom', zoom);
|
|
|
|
},
|
2018-08-02 16:58:08 +02:00
|
|
|
[types.SET_CURRENT_POSITION](state, { currentLocation }) {
|
|
|
|
state.location.gpx.push(currentLocation);
|
2018-08-24 18:13:41 +02:00
|
|
|
if (
|
|
|
|
!state.lastReportFetchingLocation
|
|
|
|
|| !state.lastReportFetchingLocation[0]
|
|
|
|
|| !state.lastReportFetchingLocation[1]
|
|
|
|
) {
|
|
|
|
state.lastReportFetchingLocation = [
|
|
|
|
currentLocation.latitude,
|
|
|
|
currentLocation.longitude,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
[types.SET_LAST_REPORT_FETCHING_LOCATION](state, { locationLatLng }) {
|
|
|
|
state.lastReportFetchingLocation = locationLatLng;
|
2018-07-21 20:00:37 +02:00
|
|
|
},
|
|
|
|
[types.SET_LOCATION_ERROR](state, { error }) {
|
|
|
|
Vue.set(state.location, 'error', error);
|
|
|
|
},
|
|
|
|
[types.SET_LOCATION_WATCHER_ID](state, { id }) {
|
|
|
|
Vue.set(state.location, 'watcherID', id);
|
2018-07-04 18:20:28 +02:00
|
|
|
},
|
2018-07-10 14:32:48 +02:00
|
|
|
[types.SET_SETTING](state, { setting, value }) {
|
|
|
|
if (storageAvailable('localStorage')) {
|
2018-07-17 16:32:34 +02:00
|
|
|
localStorage.setItem(setting, JSON.stringify(value));
|
2018-07-10 14:32:48 +02:00
|
|
|
}
|
|
|
|
state.settings[setting] = value;
|
|
|
|
},
|
2018-07-12 17:48:26 +02:00
|
|
|
[types.SHOW_REPORT_DETAILS](state, { id, userAsked }) {
|
2018-09-04 15:49:02 +02:00
|
|
|
if (id === null) {
|
|
|
|
// If closing the details, keep track of what the id was to prevent
|
|
|
|
// reopening the details immediately.
|
|
|
|
Vue.set(state.reportDetails, 'previousId', state.reportDetails.id);
|
|
|
|
}
|
2018-07-12 17:48:26 +02:00
|
|
|
Vue.set(state.reportDetails, 'id', id);
|
|
|
|
Vue.set(state.reportDetails, 'userAsked', userAsked);
|
2018-07-05 22:40:24 +02:00
|
|
|
},
|
2018-06-26 11:39:43 +02:00
|
|
|
[types.STORE_REPORTS](state, { reports }) {
|
|
|
|
state.reports = reports;
|
|
|
|
},
|
|
|
|
};
|