Skip to content
This repository has been archived by the owner on Nov 16, 2020. It is now read-only.

Commit

Permalink
Merge pull request #182 from ito-org/cancel-upload
Browse files Browse the repository at this point in the history
Cancel upload using header back button, fix physical back button behavior
  • Loading branch information
sascha10000 authored May 4, 2020
2 parents daccb3a + 6a6cf1c commit 35b413a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 22 deletions.
17 changes: 15 additions & 2 deletions App/screens/DataUpload.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React from 'react';
import {StyleSheet, View, Text} from 'react-native';
import React, {useCallback} from 'react';
import {StyleSheet, View, Text, BackHandler} from 'react-native';
import {useFocusEffect} from '@react-navigation/native';
import {StackNavigationProp} from '@react-navigation/stack';
import {RootStackParamList} from 'App/App';
import Header from '../components/Header';
Expand Down Expand Up @@ -28,6 +29,18 @@ export const DataUpload: React.FC<{
}> = ({navigation}) => {
const {t} = useTranslation();

useFocusEffect(
useCallback(() => {
const onBackPress = (): boolean => {
navigation.navigate('Home');
return true;
};
BackHandler.addEventListener('hardwareBackPress', onBackPress);
return (): void =>
BackHandler.removeEventListener('hardwareBackPress', onBackPress);
}, [navigation]),
);

return (
<View style={global.container}>
<Header
Expand Down
8 changes: 2 additions & 6 deletions App/screens/PositiveResult.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,10 @@ export const PositiveResult: React.FC<{
}
if (uploadSuccess) {
console.log('TCN Upload succeeded');
setTimeout((): void => {
navigation.navigate('DataUpload');
}, 600);
navigation.navigate('DataUpload');
} else {
console.warn('TCN Upload failed');
setTimeout((): void => {
navigation.navigate('DataUpload');
}, 600);
navigation.navigate('DataUpload');
}
}, [navigation, uploadSuccess]);
const doUpload = (): void => {
Expand Down
26 changes: 12 additions & 14 deletions App/screens/Upload.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,23 @@ export const Upload: React.FC<{
}).start(() => startAnimation());
};
startAnimation();
const cancelUpload = (): void => {
if (!deferUploadTimeout.current) {
console.warn('failed to clear deferUploadTimeout', deferUploadTimeout);
return;
}
console.log('clearing deferUploadTimeout', deferUploadTimeout);
clearTimeout(deferUploadTimeout.current);
deferUploadTimeout.current = null;
navigation.navigate('PositiveResult');
};

return (
<View style={[global.container]}>
<Header
navigationButton={{
title: t('global.cancel'),
fn: (): void => navigation.goBack(),
fn: cancelUpload,
}}
showHelp={true}
showAlpha={true}
Expand All @@ -84,19 +94,7 @@ export const Upload: React.FC<{
title={t('uploadData.buttonTitleCancel')}
variant="outlined"
buttonStyle={styles.cancelButton}
onPress={(): void => {
if (!deferUploadTimeout.current) {
console.warn(
'failed to clear deferUploadTimeout',
deferUploadTimeout,
);
return;
}
console.log('clearing deferUploadTimeout', deferUploadTimeout);
clearTimeout(deferUploadTimeout.current);
deferUploadTimeout.current = null;
navigation.navigate('PositiveResult');
}}
onPress={cancelUpload}
/>
</View>
);
Expand Down

0 comments on commit 35b413a

Please sign in to comment.