• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

akvo / nmis-mobile / 5809705549

09 Aug 2023 01:29PM UTC coverage: 81.112% (+0.008%) from 81.104%
5809705549

Pull #129

github

web-flow
Merge be089b390 into c07a250b6
Pull Request #129: Feature/112 fix gps accuracy behavior

674 of 893 branches covered (75.48%)

Branch coverage included in aggregate %.

26 of 26 new or added lines in 3 files covered. (100.0%)

1426 of 1696 relevant lines covered (84.08%)

18.07 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

91.3
/app/src/form/fields/TypeGeo.js
1
import React, { useState, useEffect, useMemo, useCallback } from 'react';
2
import { View } from 'react-native';
3
import { Text, Button } from '@rneui/themed';
4
import { useNavigation, useRoute } from '@react-navigation/native';
5

6
import { UIState, FormState, BuildParamsState } from '../../store';
7
import { FieldLabel } from '../support';
8
import { styles } from '../styles';
9
import { loc, i18n } from '../../lib';
10

11
const TypeGeo = ({ onChange, values, keyform, id, name, tooltip, required, requiredSign }) => {
4✔
12
  const [errorMsg, setErrorMsg] = useState(null);
22✔
13
  const [gpsAccuracy, setGpsAccuracy] = useState(null);
22✔
14
  const [loading, setLoading] = useState({ current: false, map: false });
22✔
15
  const currentValues = FormState.useState((s) => s.currentValues);
22✔
16
  const [latitude, longitude] = currentValues?.[id] || [];
22✔
17

18
  const gpsThreshold = BuildParamsState.useState((s) => s.gpsThreshold);
22✔
19
  const isOnline = UIState.useState((s) => s.online);
22✔
20
  const activeLang = FormState.useState((s) => s.lang);
22✔
21

22
  const trans = i18n.text(activeLang);
22✔
23

24
  const navigation = useNavigation();
22✔
25
  const route = useRoute();
22✔
26
  const requiredValue = required ? requiredSign : null;
22✔
27

28
  const handleOpenMap = () => {
22✔
29
    if (latitude && longitude) {
1!
30
      const params = { latitude, longitude, id };
×
31
      navigation.navigate('MapView', { ...route?.params, ...params });
×
32
    } else {
33
      handleGetCurrLocation(true);
1✔
34
    }
35
  };
36

37
  const handleGetCurrLocation = useCallback(
22✔
38
    async (openMap = false) => {
×
39
      const loadingKey = openMap ? 'map' : 'current';
4✔
40
      setLoading({
4✔
41
        ...loading,
42
        [loadingKey]: true,
43
      });
44
      await loc.getCurrentLocation(
4✔
45
        ({ coords }) => {
46
          const { latitude: lat, longitude: lng, accuracy } = coords;
3✔
47
          /**
48
           * accuracy number in meters, doc: https://docs.expo.dev/versions/latest/sdk/location/#locationgeocodedlocation
49
           */
50
          setGpsAccuracy(Math.floor(accuracy));
3✔
51
          console.info('GPS accuracy:', accuracy, 'GPS Threshold:', gpsThreshold);
3✔
52
          if ((accuracy <= gpsThreshold && !openMap) || openMap) {
3!
53
            onChange(id, [lat, lng]);
3✔
54
            setLoading({
3✔
55
              ...loading,
56
              [loadingKey]: false,
57
            });
58
          }
59
          if (openMap) {
3✔
60
            const params = { latitude: lat, longitude: lng, id };
1✔
61
            navigation.navigate('MapView', { ...route?.params, ...params });
1✔
62
          }
63
        },
64
        ({ message }) => {
65
          setLoading({
1✔
66
            ...loading,
67
            [loadingKey]: false,
68
          });
69
          setErrorMsg(message);
1✔
70
        },
71
      );
72
    },
73
    [gpsThreshold],
74
  );
75

76
  useEffect(() => {
22✔
77
    if (gpsAccuracy && gpsAccuracy >= gpsThreshold && loading.current) {
15✔
78
      handleGetCurrLocation(false);
1✔
79
    }
80
  }, [handleGetCurrLocation, gpsAccuracy, gpsThreshold, loading.current]);
81

82
  return (
22✔
83
    <View>
84
      <FieldLabel keyform={keyform} name={name} tooltip={tooltip} requiredSign={requiredValue} />
85
      <View style={styles.inputGeoContainer}>
86
        <View>
87
          <Text testID="text-lat">
88
            {trans.latitude}: {latitude}
89
          </Text>
90
          <Text testID="text-lng">
91
            {trans.longitude}: {longitude}
92
          </Text>
93
        </View>
94
        {errorMsg && (
23✔
95
          <Text testID="text-error" style={styles.errorText}>
96
            {errorMsg}
97
          </Text>
98
        )}
99
        <View style={styles.geoButtonGroup}>
100
          <Button onPress={() => handleGetCurrLocation(false)} testID="button-curr-location">
2✔
101
            {loading.current ? trans.fetchingLocation : trans.buttonCurrLocation}
22✔
102
          </Button>
103
          {isOnline && (
43✔
104
            <Button type="outline" onPress={handleOpenMap} testID="button-open-map">
105
              {loading.map ? trans.loadingText : trans.buttonOpenMap}
21✔
106
            </Button>
107
          )}
108
        </View>
109
      </View>
110
    </View>
111
  );
112
};
113

114
export default TypeGeo;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc