Skip to content
This repository has been archived by the owner on Jul 1, 2023. It is now read-only.

Commit

Permalink
fix: render correct screen when activity is destroyed
Browse files Browse the repository at this point in the history
  • Loading branch information
jimmyloi committed Jan 18, 2020
1 parent af47241 commit 26c2d7a
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 9 deletions.
27 changes: 19 additions & 8 deletions packages/ulangi-mobile/src/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ import { setDefaultNavigationOptions } from './setup/setDefaultNavigationOptions

export class App {
private initialized: boolean;
private preloaded: boolean;

public constructor() {
this.initialized = false;
this.preloaded = false;
}

public start(): void {
Expand Down Expand Up @@ -113,20 +115,29 @@ export class App {

const rootScreenDelegate = new RootScreenDelegate(themeStore);

if (userStore.currentUser !== null) {
if (setStore.currentSetId !== null) {
rootScreenDelegate.setRootToTabBasedScreen();
if (!this.isPreloaded()) {
this.preloaded = true;
rootScreenDelegate.setRootToSingleScreen(ScreenName.PRELOAD_SCREEN);
} else {
if (userStore.currentUser !== null) {
if (setStore.currentSetId !== null) {
rootScreenDelegate.setRootToTabBasedScreen();
} else {
rootScreenDelegate.setRootToSingleScreen(
ScreenName.CREATE_FIRST_SET_SCREEN,
);
}
} else {
rootScreenDelegate.setRootToSingleScreen(
ScreenName.CREATE_FIRST_SET_SCREEN,
);
rootScreenDelegate.setRootToSingleScreen(ScreenName.WELCOME_SCREEN);
}
} else {
rootScreenDelegate.setRootToSingleScreen(ScreenName.PRELOAD_SCREEN);
}
}

private isInitialized(): boolean {
return this.initialized;
}

private isPreloaded(): boolean {
return this.preloaded;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
} from '@ulangi/ulangi-observable';
import { boundClass } from 'autobind-decorator';
import * as _ from 'lodash';
import { BackHandler } from 'react-native';

import { CreateFirstSetScreenIds } from '../../constants/ids/CreateFirstSetScreenIds';
import { FullRoundedButtonStyle } from '../../styles/FullRoundedButtonStyle';
Expand Down Expand Up @@ -125,6 +126,19 @@ export class CreateFirstSetScreenDelegate extends AddEditSetScreenDelegate {
});
}

public addBackButtonHandler(handler: () => boolean): void {
BackHandler.addEventListener('hardwareBackPress', handler);
}

public removeBackButtonHandler(handler: () => boolean): void {
BackHandler.removeEventListener('hardwareBackPress', handler);
}

public handleHardwareBackButton(): boolean {
this.showConfirmLogout();
return true;
}

private navigateToTabBasedScreen(): void {
this.rootScreenDelegate.setRootToTabBasedScreen();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export class WelcomeScreenDelegate {
}

public navigateToSignInScreen(): void {
this.navigatorDelegate.resetTo(ScreenName.SIGN_IN_SCREEN, {});
this.navigatorDelegate.push(ScreenName.SIGN_IN_SCREEN, {});
}

private showSigningInAsGuestDialog(): void {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@ export class CreateFirstSetScreenContainer extends Container {
);
}

public componentDidMount(): void {
this.screenDelegate.addBackButtonHandler(
this.screenDelegate.handleHardwareBackButton,
);
}

public componentWillUnmount(): void {
this.screenDelegate.removeBackButtonHandler(
this.screenDelegate.handleHardwareBackButton,
);
}

public render(): React.ReactElement<any> {
return (
<CreateFirstSetScreen
Expand Down

0 comments on commit 26c2d7a

Please sign in to comment.