App.js 2.75 KB
Newer Older
Giang Tran committed
1 2 3
import React, {useEffect, useRef, useState} from 'react';
import {Animated, Dimensions, StyleSheet, Text, View} from 'react-native';
import BootSplash from 'react-native-bootsplash';
Giang Tran committed
4 5 6 7 8 9 10
import {Provider} from 'react-redux';
import {createStore, applyMiddleware} from 'redux';
import rootReducer from './src/Reducers/index';
import RootView from './src/RootView';
import createSagaMiddleware from 'redux-saga';
import rootSaga from './src/Saga/rootSaga';

Giang Tran committed
11 12
let bootSplashLogo = require('./src/assets/images/iconSplash.png');

Giang Tran committed
13 14 15
let fakeApiCallWithoutBadNetwork = (ms) =>
  new Promise((resolve) => setTimeout(resolve, ms));

Giang Tran committed
16 17 18 19
const sagaMiddleware = createSagaMiddleware();

let store = createStore(rootReducer, applyMiddleware(sagaMiddleware));
sagaMiddleware.run(rootSaga);
Giang Tran committed
20 21

let App = () => {
Giang Tran committed
22 23 24 25 26
  let [bootSplashIsVisible, setBootSplashIsVisible] = useState(true);
  let [bootSplashLogoIsLoaded, setBootSplashLogoIsLoaded] = useState(false);
  let opacity = useRef(new Animated.Value(1));
  let translateY = useRef(new Animated.Value(0));

Giang Tran committed
27
  let init = async () => {
Giang Tran committed
28
    // You can uncomment this line to add a delay on app startup
Giang Tran committed
29
    await fakeApiCallWithoutBadNetwork(1000);
Giang Tran committed
30

Giang Tran committed
31
    await BootSplash.hide();
Giang Tran committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51

    Animated.stagger(250, [
      Animated.spring(translateY.current, {
        useNativeDriver: true,
        toValue: -50,
      }),
      Animated.spring(translateY.current, {
        useNativeDriver: true,
        toValue: Dimensions.get('window').height,
      }),
    ]).start();

    Animated.timing(opacity.current, {
      useNativeDriver: true,
      toValue: 0,
      duration: 150,
      delay: 350,
    }).start(() => {
      setBootSplashIsVisible(false);
    });
Giang Tran committed
52 53 54
  };

  useEffect(() => {
Giang Tran committed
55 56
    init();
  }, []);
Giang Tran committed
57

Giang Tran committed
58
  return (
Giang Tran committed
59 60 61 62
    <View style={styles.container}>
      <Provider store={store}>
        <RootView />
      </Provider>
Giang Tran committed
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81

      {bootSplashIsVisible && (
        <Animated.View
          style={[
            StyleSheet.absoluteFill,
            styles.bootsplash,
            {opacity: opacity.current},
          ]}>
          <Animated.Image
            source={bootSplashLogo}
            fadeDuration={0}
            onLoadEnd={() => setBootSplashLogoIsLoaded(true)}
            style={[
              styles.logo,
              {transform: [{translateY: translateY.current}]},
            ]}
          />
        </Animated.View>
      )}
Giang Tran committed
82
    </View>
Giang Tran committed
83 84 85
  );
};

Giang Tran committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
const styles = StyleSheet.create({
  container: {
    flex: 1,
  },
  text: {
    fontSize: 24,
    fontWeight: '700',
    margin: 20,
    lineHeight: 30,
    color: '#333',
    textAlign: 'center',
  },
  bootsplash: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
    backgroundColor: '#1C6AF6',
  },
  logo: {
    width: 250,
    height: 80,
    resizeMode: 'contain',
  },
});

Giang Tran committed
111
export default App;