forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsync_state.dart
More file actions
138 lines (124 loc) · 4.04 KB
/
Copy pathsync_state.dart
File metadata and controls
138 lines (124 loc) · 4.04 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
import 'package:omi/backend/schema/conversation.dart';
import 'package:omi/services/wals.dart';
enum SyncStatus { idle, syncing, fetchingConversations, completed, error }
enum SyncPhase { idle, downloadingFromDevice, waitingForInternet, uploadingToCloud, processingOnServer }
extension SyncMethodExtension on SyncMethod {
String get displayName {
switch (this) {
case SyncMethod.ble:
return 'Bluetooth';
}
}
String get shortName {
switch (this) {
case SyncMethod.ble:
return 'BLE';
}
}
String get description {
switch (this) {
case SyncMethod.ble:
return 'Syncing via Bluetooth';
}
}
}
class SyncState {
final SyncStatus status;
final SyncPhase phase;
final double progress;
final String? errorMessage;
final Wal? failedWal;
final List<SyncedConversationPointer> syncedConversations;
final double? speedKBps; // Download speed in KB/s
final SyncMethod? syncMethod;
final int? currentFile; // 1-based index of file being transferred
final int? totalFiles; // Total files to transfer
final int? uploadedBytes; // Bytes uploaded to cloud so far
final int? totalBytesToUpload; // Total bytes to upload to cloud
const SyncState({
this.status = SyncStatus.idle,
this.phase = SyncPhase.idle,
this.progress = 0.0,
this.errorMessage,
this.failedWal,
this.syncedConversations = const [],
this.speedKBps,
this.syncMethod,
this.currentFile,
this.totalFiles,
this.uploadedBytes,
this.totalBytesToUpload,
});
SyncState copyWith({
SyncStatus? status,
SyncPhase? phase,
double? progress,
String? errorMessage,
Wal? failedWal,
List<SyncedConversationPointer>? syncedConversations,
double? speedKBps,
SyncMethod? syncMethod,
bool clearSyncMethod = false,
int? currentFile,
int? totalFiles,
int? uploadedBytes,
int? totalBytesToUpload,
}) {
return SyncState(
status: status ?? this.status,
phase: phase ?? this.phase,
progress: progress ?? this.progress,
errorMessage: errorMessage,
failedWal: failedWal,
syncedConversations: syncedConversations ?? this.syncedConversations,
speedKBps: speedKBps,
syncMethod: clearSyncMethod ? null : (syncMethod ?? this.syncMethod),
currentFile: currentFile ?? this.currentFile,
totalFiles: totalFiles ?? this.totalFiles,
uploadedBytes: uploadedBytes,
totalBytesToUpload: totalBytesToUpload,
);
}
bool get isIdle => status == SyncStatus.idle;
bool get isSyncing => status == SyncStatus.syncing;
bool get isFetchingConversations => status == SyncStatus.fetchingConversations;
bool get isCompleted => status == SyncStatus.completed;
bool get hasError => status == SyncStatus.error;
bool get isProcessing => isSyncing || isFetchingConversations;
SyncState toIdle() => copyWith(
status: SyncStatus.idle,
phase: SyncPhase.idle,
progress: 0.0,
errorMessage: null,
failedWal: null,
syncedConversations: [],
clearSyncMethod: true,
);
SyncState toSyncing({
double progress = 0.0,
double? speedKBps,
SyncMethod? syncMethod,
SyncPhase? phase,
int? currentFile,
int? totalFiles,
int? uploadedBytes,
int? totalBytesToUpload,
}) =>
copyWith(
status: SyncStatus.syncing,
phase: phase,
progress: progress,
errorMessage: null,
speedKBps: speedKBps,
syncMethod: syncMethod,
currentFile: currentFile,
totalFiles: totalFiles,
uploadedBytes: uploadedBytes,
totalBytesToUpload: totalBytesToUpload,
);
SyncState toFetchingConversations() => copyWith(status: SyncStatus.fetchingConversations);
SyncState toCompleted({required List<SyncedConversationPointer> conversations}) =>
copyWith(status: SyncStatus.completed, syncedConversations: conversations);
SyncState toError({required String message, Wal? failedWal}) =>
copyWith(status: SyncStatus.error, errorMessage: message, failedWal: failedWal, progress: 0.0);
}