File size: 5,507 Bytes
4a5ef51
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { useEffect, useRef, useState } from 'react';
import { Keyboard, Platform, Pressable, Text, View, StyleSheet, StatusBar } from 'react-native';
import { SafeAreaProvider, SafeAreaView } from 'react-native-safe-area-context';
import { KeyboardVisibleContext } from './components/keyboardLayout';
import { TabBar, TabKey } from './components/TabBar';
import { CustomMentraLiveOtaFlow } from './ota/CustomMentraLiveOtaFlow';
import { DeviceScreen } from './screens/DeviceScreen';
import { CameraScreen } from './screens/CameraScreen';
import { StreamScreen } from './screens/StreamScreen';
import { SystemScreen } from './screens/SystemScreen';
import { ConsoleScreen } from './screens/ConsoleScreen';
import { otaFlowAdmission } from './otaFlowAdmission';
import { connectedWifiStatus } from './sdkFormat';
import { colors } from './components/theme';
import { isMentraLiveRuntime, useBluetoothSdkExample } from './useBluetoothSdkExample';

export default function App() {
  const [tab, setTab] = useState<TabKey>('device');
  const [keyboardVisible, setKeyboardVisible] = useState(false);
  const [otaVisible, setOtaVisible] = useState(false);
  const [waitingForWifi, setWaitingForWifi] = useState(false);
  const otaCompletedGenerationRef = useRef<number | null>(null);
  const sdk = useBluetoothSdkExample({activeTab: tab});
  const currentWifi = connectedWifiStatus(sdk.glasses);
  // A failed attempt to join another network must not block the current one.
  const otaWifiSetupComplete = currentWifi !== null && sdk.wifiConnectingSsid === null;
  const mentraLiveConnected = isMentraLiveRuntime(sdk.glasses);
  const connectionGeneration = mentraLiveConnected && sdk.glasses.connected
    ? sdk.glassesConnectionGeneration
    : null;

  useEffect(() => {
    const admission = otaFlowAdmission({
      completedConnectionGeneration: otaCompletedGenerationRef.current,
      connectionGeneration,
      waitingForWifi,
    });

    if (admission === 'idle') {
      if (!otaVisible) {
        otaCompletedGenerationRef.current = null;
        setWaitingForWifi(false);
      }
      return;
    }

    if (admission === 'wait_for_wifi' || admission === 'done') {
      return;
    }

    setWaitingForWifi(false);
    setOtaVisible(true);
  }, [connectionGeneration, mentraLiveConnected, otaVisible, sdk.glasses, waitingForWifi]);

  useEffect(() => {
    const showEvent = Platform.OS === 'ios' ? 'keyboardWillShow' : 'keyboardDidShow';
    const hideEvent = Platform.OS === 'ios' ? 'keyboardWillHide' : 'keyboardDidHide';
    const showSub = Keyboard.addListener(showEvent, () => setKeyboardVisible(true));
    const hideSub = Keyboard.addListener(hideEvent, () => setKeyboardVisible(false));
    return () => {
      showSub.remove();
      hideSub.remove();
    };
  }, []);

  const finishOta = () => {
    otaCompletedGenerationRef.current = connectionGeneration;
    setOtaVisible(false);
  };

  const openWifiSetup = () => {
    setWaitingForWifi(true);
    setOtaVisible(false);
    setTab('system');
  };

  const openOta = () => {
    otaCompletedGenerationRef.current = null;
    setWaitingForWifi(false);
    setOtaVisible(true);
  };

  return (
    <SafeAreaProvider>

      <StatusBar barStyle="dark-content" />

      <KeyboardVisibleContext.Provider value={keyboardVisible}>

        {otaVisible ? (

          <CustomMentraLiveOtaFlow

            deviceName="Mentra Live"

            onFinished={finishOta}

            onOpenWifiSetup={openWifiSetup}

          />

        ) : (

          <SafeAreaView style={styles.root} edges={['top']}>

            {waitingForWifi && tab === 'system' && (

              <View style={styles.otaResume}>

                <Text style={styles.otaResumeText}>

                  {otaWifiSetupComplete

                    ? `Connected to ${currentWifi?.ssid}`

                    : 'Connect to a Wi-Fi network to continue the update.'}

                </Text>

                {otaWifiSetupComplete && (

                  <Pressable

                    accessibilityRole="button"

                    onPress={openOta}

                    style={styles.otaResumeButton}>

                    <Text style={styles.otaResumeLabel}>Continue update</Text>

                  </Pressable>

                )}

              </View>

            )}

            <View style={styles.screen}>

              {tab === 'device' && <DeviceScreen sdk={sdk} onOpenOta={openOta} />}

              {tab === 'camera' && <CameraScreen sdk={sdk} />}

              {tab === 'stream' && <StreamScreen sdk={sdk} />}

              {tab === 'system' && <SystemScreen sdk={sdk} />}

              {tab === 'console' && <ConsoleScreen sdk={sdk} />}

            </View>

            {!keyboardVisible && <TabBar active={tab} onChange={setTab} />}

          </SafeAreaView>

        )}

      </KeyboardVisibleContext.Provider>

    </SafeAreaProvider>
  );
}

const styles = StyleSheet.create({
  root: { flex: 1, backgroundColor: '#fff' },
  screen: { flex: 1 },
  otaResume: { padding: 16, gap: 12, borderBottomWidth: StyleSheet.hairlineWidth, borderBottomColor: colors.hairline },
  otaResumeText: { color: colors.ink, fontSize: 14 },
  otaResumeButton: { backgroundColor: colors.greenInk, borderRadius: 14, minHeight: 48, alignItems: 'center', justifyContent: 'center' },
  otaResumeLabel: { color: colors.bg, fontSize: 14, fontWeight: '700' },
});