forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevice_onboarding_provider.dart
More file actions
194 lines (162 loc) · 4.88 KB
/
Copy pathdevice_onboarding_provider.dart
File metadata and controls
194 lines (162 loc) · 4.88 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import 'dart:async';
import 'package:flutter/foundation.dart';
import 'package:omi/backend/preferences.dart';
import 'package:omi/backend/schema/transcript_segment.dart';
enum PowerCycleSubState { waitingForOff, deviceOff, waitingForReconnect, reconnected }
class DeviceOnboardingProvider extends ChangeNotifier {
static const int totalSteps = 4;
static const int _wordThreshold = 5;
int currentStep = 0;
bool isOnboardingActive = false;
// Step 0: Transcription demo
List<TranscriptSegment> demoSegments = [];
int wordCount = 0;
bool transcriptionComplete = false;
// Step 1: Single press - ask a question
bool voiceSessionActive = false;
bool questionSent = false;
String? aiResponse;
// Step 2: Power cycle
PowerCycleSubState powerCycleState = PowerCycleSubState.waitingForOff;
// Step 3: Double press config
int selectedDoubleTapAction = -1; // -1 = none selected
int doublePressCount = 0;
bool showSingleTapHint = false;
Timer? _hintTimer;
bool _disposed = false;
void startOnboarding() {
currentStep = 0;
isOnboardingActive = true;
_resetStepState();
notifyListeners();
}
void _resetStepState() {
demoSegments = [];
wordCount = 0;
transcriptionComplete = false;
voiceSessionActive = false;
questionSent = false;
aiResponse = null;
powerCycleState = PowerCycleSubState.waitingForOff;
selectedDoubleTapAction = -1;
doublePressCount = 0;
showSingleTapHint = false;
_hintTimer?.cancel();
_hintTimer = null;
}
void advanceStep() {
if (currentStep < totalSteps - 1) {
currentStep++;
_resetStepState();
notifyListeners();
}
}
void completeOnboarding() {
isOnboardingActive = false;
_hintTimer?.cancel();
_hintTimer = null;
notifyListeners();
}
// --- Step 0: Transcription ---
void onTranscriptSegments(List<TranscriptSegment> segments) {
if (currentStep != 0 || transcriptionComplete) return;
demoSegments = segments;
int count = 0;
for (final seg in segments) {
count += seg.text.trim().split(RegExp(r'\s+')).where((w) => w.isNotEmpty).length;
}
wordCount = count;
if (wordCount >= _wordThreshold && !transcriptionComplete) {
transcriptionComplete = true;
}
notifyListeners();
}
// --- Step 1: Single press (ask question) ---
void onButtonEvent(int buttonState) {
switch (currentStep) {
case 1:
_handleStep1Button(buttonState);
break;
case 2:
// Button events not used for power cycle — we detect disconnect/reconnect instead
break;
case 3:
_handleStep3Button(buttonState);
break;
}
}
void _handleStep1Button(int buttonState) {
if (buttonState != 1) return;
if (!voiceSessionActive) {
voiceSessionActive = true;
notifyListeners();
} else {
// Second press — question is being sent
voiceSessionActive = false;
questionSent = true;
notifyListeners();
}
}
void onVoiceResponseReceived(String response) {
if (currentStep != 1) return;
aiResponse = response;
notifyListeners();
}
// --- Step 2: Power cycle ---
void onDeviceDisconnected() {
if (currentStep != 2) return;
if (powerCycleState == PowerCycleSubState.waitingForOff) {
powerCycleState = PowerCycleSubState.deviceOff;
notifyListeners();
// After short delay, transition to waiting for reconnect
Future.delayed(const Duration(seconds: 1), () {
if (_disposed) return;
if (powerCycleState == PowerCycleSubState.deviceOff) {
powerCycleState = PowerCycleSubState.waitingForReconnect;
notifyListeners();
}
});
}
}
void onDeviceReconnected() {
if (currentStep != 2) return;
if (powerCycleState == PowerCycleSubState.waitingForReconnect || powerCycleState == PowerCycleSubState.deviceOff) {
powerCycleState = PowerCycleSubState.reconnected;
notifyListeners();
}
}
void startPowerCycleHintTimer(VoidCallback onHint) {
_hintTimer?.cancel();
_hintTimer = Timer(const Duration(seconds: 30), () {
if (powerCycleState == PowerCycleSubState.waitingForOff) {
onHint();
}
});
}
// --- Step 3: Double press config ---
void selectDoubleTapAction(int action) {
selectedDoubleTapAction = action;
doublePressCount = 0;
notifyListeners();
}
void _handleStep3Button(int buttonState) {
if (buttonState == 1) {
showSingleTapHint = true;
notifyListeners();
return;
}
if (buttonState != 2) return;
if (selectedDoubleTapAction == -1) return; // no option selected yet
showSingleTapHint = false;
doublePressCount++;
// Save the selected action
SharedPreferencesUtil().doubleTapAction = selectedDoubleTapAction;
notifyListeners();
}
@override
void dispose() {
_disposed = true;
_hintTimer?.cancel();
super.dispose();
}
}