Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
Merge pull request #579 from cds-snc/hotfix/android-background-task
Browse files Browse the repository at this point in the history
Fix wrong hook usage of StorageService in Android background job
  • Loading branch information
henrytao-me authored Jul 9, 2020
2 parents b435851 + b65c88d commit daea775
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 33 deletions.
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ import {getBackgroundI18n} from 'locale';

import {name as appName} from '../app.json';

import {useStorageService} from './services/StorageService';
import {createStorageService} from './services/StorageService';
import App from './App';

AppRegistry.registerComponent(appName, () => App);

BackgroundScheduler.registerAndroidHeadlessPeriodicTask(async () => {
const storageService = useStorageService();
const storageService = await createStorageService();
const backendService = new BackendService(RETRIEVE_URL, SUBMIT_URL, HMAC_KEY, storageService?.region);
const i18n = await getBackgroundI18n();
const exposureNotificationService = new ExposureNotificationService(
Expand Down
31 changes: 12 additions & 19 deletions src/locale/i18n.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
import {I18n, I18nManager} from '@shopify/react-i18n';
import {StorageService} from 'services/StorageService';
import {createStorageService} from 'services/StorageService';

import LOCALES from './translations';

export const getBackgroundI18n = async (forceLocale?: string) => {
const storageService = new StorageService();
return new Promise<I18n>(resolve => {
storageService.ready.observe(ready => {
if (!ready) {
return;
}
const locale = forceLocale || storageService.locale.get();
const i18nManager = new I18nManager({
locale,
onError(error) {
console.log('>>> i18N', error);
},
});
const translations = (locale: string) => LOCALES[locale];
i18nManager.register({id: 'global', translations, fallback: LOCALES[locale]});
const i18n = new I18n(LOCALES[locale], {...i18nManager.details});
resolve(i18n);
});
const storageService = await createStorageService();
const locale = forceLocale || storageService.locale.get();
const i18nManager = new I18nManager({
locale,
onError(error) {
console.log('>>> i18N', error);
},
});
const translations = (locale: string) => LOCALES[locale];
i18nManager.register({id: 'global', translations, fallback: LOCALES[locale]});
const i18n = new I18n(LOCALES[locale], {...i18nManager.details});
return i18n;
};
14 changes: 7 additions & 7 deletions src/services/StorageService/StorageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,13 @@ export class StorageService {
forceScreen: Observable<string | undefined>;
skipAllSet: Observable<boolean>;

ready: Observable<boolean>;

constructor() {
this.isOnboarding = new Observable<boolean>(true);
this.locale = new Observable<string>(getSystemLocale());
this.ready = new Observable<boolean>(false);
this.region = new Observable<Region | undefined>(undefined);
this.onboardedDatetime = new Observable<Date | undefined>(undefined);
this.forceScreen = new Observable<string | undefined>(undefined);
this.skipAllSet = new Observable<boolean>(false);
this.init();
}

setOnboarded = async (value: boolean) => {
Expand Down Expand Up @@ -63,7 +59,7 @@ export class StorageService {
this.skipAllSet.set(value);
};

private init = async () => {
init = async () => {
const isOnboarded = (await AsyncStorage.getItem(Key.IsOnboarded)) === '1';
this.isOnboarding.set(!isOnboarded);

Expand All @@ -83,7 +79,11 @@ export class StorageService {

const skipAllSet = (await AsyncStorage.getItem(Key.SkipAllSet)) === '1';
this.skipAllSet.set(skipAllSet);

this.ready.set(true);
};
}

export const createStorageService = async () => {
const storageService = new StorageService();
await storageService.init();
return storageService;
};
16 changes: 11 additions & 5 deletions src/services/StorageService/StorageServiceProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import AsyncStorage from '@react-native-community/async-storage';
import {I18nContext} from '@shopify/react-i18n';
import {getSystemLocale} from 'locale';
import {DevSettings} from 'react-native';
import {createCancellableCallbackPromise} from 'shared/cancellablePromise';

import {StorageService} from './StorageService';
import {StorageService, createStorageService} from './StorageService';

const StorageServiceContext = createContext<StorageService | undefined>(undefined);

Expand All @@ -13,12 +14,17 @@ export interface StorageServiceProviderProps {
}

export const StorageServiceProvider = ({children}: StorageServiceProviderProps) => {
const storageService = useMemo(() => new StorageService(), []);
const [ready, setReady] = useState(false);
const [storageService, setStorageService] = useState<StorageService>();

useEffect(() => storageService.ready.observe(setReady), [storageService]);
useEffect(() => {
const {callable, cancelable} = createCancellableCallbackPromise(() => createStorageService(), setStorageService);
callable();
return cancelable;
}, []);

return <StorageServiceContext.Provider value={storageService}>{ready && children}</StorageServiceContext.Provider>;
return (
<StorageServiceContext.Provider value={storageService}>{storageService && children}</StorageServiceContext.Provider>
);
};

export const useStorageService = () => {
Expand Down

0 comments on commit daea775

Please sign in to comment.