forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdev_api_key_provider.dart
More file actions
75 lines (63 loc) · 2.19 KB
/
Copy pathdev_api_key_provider.dart
File metadata and controls
75 lines (63 loc) · 2.19 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
import 'package:flutter/material.dart';
import 'package:omi/backend/http/api/dev_api.dart';
import 'package:omi/backend/schema/dev_api_key.dart';
class DevApiKeyProvider with ChangeNotifier {
List<DevApiKey> _keys = [];
List<DevApiKey> get keys => _keys;
bool _isLoading = false;
bool get isLoading => _isLoading;
String? _error;
String? get error => _error;
Future<void> fetchKeys({bool force = false}) async {
// Don't refetch if we already have keys and force is false
if (!force && _keys.isNotEmpty && !_isLoading) {
return;
}
_isLoading = true;
_error = null;
notifyListeners();
try {
_keys = await DevApi.getDevApiKeys();
} catch (e) {
_error = e.toString();
} finally {
_isLoading = false;
notifyListeners();
}
}
Future<DevApiKeyCreated?> createKey(String name, {List<String>? scopes}) async {
// The dialog handles its own loading state. We don't set _isLoading here
// to avoid a loading indicator on the main list view while creating.
_error = null;
DevApiKeyCreated? newKey;
try {
newKey = await DevApi.createDevApiKey(name, scopes: scopes);
// Add the new key to the top of the list, as the API returns keys sorted by creation date.
// DevApiKeyCreated and DevApiKey are now sibling wire types (no inheritance),
// so convert explicitly. The round-trip drops the one-off plaintext `key`.
_keys.insert(0, DevApiKey.fromJson(newKey.toJson()));
} catch (e) {
_error = e.toString();
} finally {
// Notify listeners to rebuild the UI with the new key or to reflect an error state.
notifyListeners();
}
return newKey;
}
Future<void> deleteKey(String keyId) async {
// Optimistically remove the key from the UI
final keyIndex = _keys.indexWhere((key) => key.id == keyId);
if (keyIndex == -1) return;
final keyToRemove = _keys[keyIndex];
_keys.removeAt(keyIndex);
notifyListeners();
try {
await DevApi.deleteDevApiKey(keyId);
} catch (e) {
// If deletion fails, add the key back and show an error
_keys.insert(keyIndex, keyToRemove);
_error = e.toString();
notifyListeners();
}
}
}