[RN] Legacy support of calendar-sync's knownDomains

Knowledge is power, man!

We moved "knownDomains" from calendar-sync to base/known-domains.
However, we do have an official release in the app stores and I'd like
us to not throw away the knowledge it has acquired.
This commit is contained in:
Lyubo Marinov 2018-05-14 14:05:41 -05:00
parent d4dea22576
commit 631f51d627
2 changed files with 70 additions and 21 deletions

View File

@ -3,7 +3,7 @@
import RNCalendarEvents from 'react-native-calendar-events';
import { APP_WILL_MOUNT } from '../app';
import { ADD_KNOWN_DOMAINS } from '../base/known-domains';
import { ADD_KNOWN_DOMAINS, addKnownDomains } from '../base/known-domains';
import { MiddlewareRegistry } from '../base/redux';
import { APP_LINK_SCHEME, parseURIString } from '../base/util';
import { APP_STATE_CHANGED } from '../mobile/background';
@ -31,24 +31,58 @@ const MAX_LIST_LENGTH = 10;
CALENDAR_ENABLED
&& MiddlewareRegistry.register(store => next => action => {
const result = next(action);
switch (action.type) {
case ADD_KNOWN_DOMAINS:
case APP_WILL_MOUNT:
_fetchCalendarEntries(store, false, false);
break;
case ADD_KNOWN_DOMAINS: {
// XXX Fetch new calendar entries only when an actual domain has
// become known.
const { getState } = store;
const oldValue = getState()['features/base/known-domains'];
const result = next(action);
const newValue = getState()['features/base/known-domains'];
case APP_STATE_CHANGED:
_maybeClearAccessStatus(store, action);
break;
case REFRESH_CALENDAR:
_fetchCalendarEntries(store, true, action.forcePermission);
break;
}
oldValue === newValue || _fetchCalendarEntries(store, false, false);
return result;
}
case APP_STATE_CHANGED: {
const result = next(action);
_maybeClearAccessStatus(store, action);
return result;
}
case APP_WILL_MOUNT: {
// For legacy purposes, we've allowed the deserialization of
// knownDomains and now we're to translate it to base/known-domains.
const state = store.getState()['features/calendar-sync'];
if (state) {
const { knownDomains } = state;
Array.isArray(knownDomains)
&& knownDomains.length
&& store.dispatch(addKnownDomains(knownDomains));
}
const result = next(action);
_fetchCalendarEntries(store, false, false);
return result;
}
case REFRESH_CALENDAR: {
const result = next(action);
_fetchCalendarEntries(store, true, action.forcePermission);
return result;
}
}
return next(action);
});
/**
@ -122,9 +156,7 @@ function _fetchCalendarEntries(
logger.warn('Calendar access not granted.');
}
})
.catch(reason => {
logger.error('Error accessing calendar.', reason);
});
.catch(reason => logger.error('Error accessing calendar.', reason));
}
/**

View File

@ -1,6 +1,8 @@
// @flow
import { ReducerRegistry } from '../base/redux';
import { APP_WILL_MOUNT } from '../app';
import { ReducerRegistry, set } from '../base/redux';
import { PersistenceRegistry } from '../base/storage';
import {
SET_CALENDAR_AUTHORIZATION,
@ -20,9 +22,25 @@ const DEFAULT_STATE = {
const STORE_NAME = 'features/calendar-sync';
// XXX For legacy purposes, read any {@code knownDomains} persisted by the
// feature calendar-sync.
CALENDAR_ENABLED
&& PersistenceRegistry.register(STORE_NAME, {
knownDomains: true
});
CALENDAR_ENABLED
&& ReducerRegistry.register(STORE_NAME, (state = DEFAULT_STATE, action) => {
switch (action.type) {
case APP_WILL_MOUNT:
// For legacy purposes, we've allowed the deserialization of
// knownDomains. At this point, it should have already been
// translated into the new state format (namely, base/known-domains)
// and the app no longer needs it.
if (typeof state.knownDomains !== 'undefined') {
return set(state, 'knownDomains', undefined);
}
break;
case SET_CALENDAR_AUTHORIZATION:
return {
@ -35,8 +53,7 @@ CALENDAR_ENABLED
...state,
events: action.events
};
default:
return state;
}
return state;
});