MyProfileComponent.js 7.05 KB
import React, {useState, useContext, useEffect, useCallback} from 'react';
import {View, Text, Button, Image, TouchableOpacity, StyleSheet, Alert} from 'react-native';
import {useDispatch, useSelector} from "react-redux";
import {LOG_IN_REQUEST, LOG_OUT_REQUEST} from "../reducers/user";
import {MaterialCommunityIcons} from "@expo/vector-icons";
import {useNavigation} from '@react-navigation/native';
import {SET_PERVELOCITY_SUCCESS} from "../reducers/location";
import Login from "../screens/Login";
import Papa from 'papaparse';


const MyProfileComponent = () => {
    const navigation = useNavigation();
    const [loading, setLoading] = useState(true);
    const [numRecord, setNumRecord] = useState(0);

    const {me} = useSelector(state => state.user);
    const {isLoggingIn} = useSelector(state => state.user);

    const downloadFile = async () => {
        const uri = "https://www.mapmyfitness.com/workout/export/csv";
        let fileUri = FileSystem.documentDirectory + "userVelocity.txt";
        FileSystem.downloadAsync(uri, fileUri)
            .then(({uri}) => {
                saveFile(uri);
            })
            .catch(error => {
                console.error(error);
            })
    }

    const saveFile = async (fileUri) => {
        const {status} = await Permissions.askAsync(Permissions.CAMERA_ROLL);
        if (status === "granted") {
            const asset = await MediaLibrary.createAssetAsync(fileUri)
            await MediaLibrary.createAlbumAsync("Download", asset, false)
        }
    }

    const dispatch = useDispatch();
    const loadPersonalVelocity = async () => {
        try {

            const userVelocity = require('../assets/userFile/userVelocity');
            var allAvgSpeed = 0;
            setNumRecord(userVelocity.length);
            userVelocity.map((i, index) => {
                allAvgSpeed = allAvgSpeed + i.avgSpeed;
            });

            var personalVelocity = parseInt(allAvgSpeed / userVelocity.length);
            await dispatch({
                type: SET_PERVELOCITY_SUCCESS,
                data: {
                    personalVelocity: personalVelocity
                }
            });

            Alert.alert(
                'MAP 사용자 평균 속도 설정',
                ` 사용자 평균 속도를 설정하였습니다. `,
                [
                    {
                        text: 'Cancel',
                        onPress: () => console.log('Cancel Pressed'),
                        style: 'cancel',
                    },
                    {text: 'OK', onPress: () => console.log('OK Pressed')},
                ],
                {cancelable: false}
            );

        } catch (e) {
            console.log(e);
        }

    };

    useEffect(() => {
        setNumRecord(0);
    }, []);

    useEffect(() => {
        console.log(numRecord)
    }, [numRecord]);

    const onLogout = async () => {
        await dispatch({
            type: LOG_OUT_REQUEST
        });
        console.log('onLogout');
    };
    return (
        <View>
            {me ?
                <View>
                    <View style={{flexDirection: 'row', paddingTop: 15}}>
                        <View style={{flex: 1, alignItems: 'center'}}>
                            <Image source={{url: 'https://steemitimages.com/u/anpigon/avatar'}}
                                   style={{width: 75, height: 75, borderRadius: 37.5}}/>
                        </View>
                        <View style={{flex: 3}}>
                            <View style={{flexDirection: 'row', justifyContent: 'space-around', marginTop: 12}}>
                                <View style={{alignItems: 'center'}}>
                                    <Text style={{fontSize: 15, fontWeight: 'bold'}}>{me.email}</Text>
                                    <Text style={{fontSize: 10, color: 'gray'}}>email</Text>
                                </View>
                                <View style={{alignItems: 'center'}}>
                                    <Text style={{fontSize: 15, fontWeight: 'bold'}}>{me.nickName}</Text>
                                    <Text style={{fontSize: 10, color: 'gray'}}>nickName</Text>
                                </View>
                                <View style={{alignItems: 'center'}}>
                                    <Text style={{fontSize: 15, fontWeight: 'bold'}}>{numRecord}</Text>
                                    <Text style={{fontSize: 10, color: 'gray'}}>numRecord</Text>
                                </View>
                            </View>
                            <View style={{flexDirection: 'row'}}>
                                <TouchableOpacity
                                    onPress={loadPersonalVelocity}
                                    style={{
                                        flex: 4,
                                        marginLeft: 10,
                                        justifyContent: 'center',
                                        alignItems: 'center',
                                        borderWidth: 1,
                                        borderColor: 'black',
                                        height: 30,
                                        marginTop: 17
                                    }}>
                                    <Text style={{fontSize: 13}}>load personal velocity</Text>
                                </TouchableOpacity>
                                <TouchableOpacity
                                    onPress={onLogout}
                                    style={{
                                        borderColor: 'black',
                                        borderWidth: 1,
                                        flex: 1,
                                        marginRight: 10,
                                        marginLeft: 5,
                                        justifyContent: 'center',
                                        alignItems: 'center',
                                        height: 30,
                                        marginTop: 17
                                    }}>
                                    <MaterialCommunityIcons color={'black'} name={'logout'} size={20}/>
                                </TouchableOpacity>
                            </View>
                        </View>
                    </View>
                    < View style={{paddingHorizontal: 20, paddingVertical: 10}}>
                    </View>
                </View>
                :
                <View style={{alignItems: 'center', justifyContent: 'center', marginTop: 200}}>
                    <Text style={{fontSize: 30, textAlign: 'center', fontWeight: 'bold'}}>유저 정보가 없습니다.</Text>
                </View>
            }
        </View>
    )
};

const styles = StyleSheet.create({
    containerStyle: {
        marginTop: 10,
        alignItems: 'center',
        justifyContent: 'center',
    },
    TextStyle: {
        width: 200,
        height: 44,
        padding: 10,
        borderWidth: 1,
        borderColor: '#778899',
        marginBottom: 10,
    }
});

export default MyProfileComponent;