Skip to content

Commit 2b203a1

Browse files
committed
convert hive boxes to lazy boxes after first load to save ram
1 parent df4fbf8 commit 2b203a1

9 files changed

Lines changed: 132 additions & 196 deletions

File tree

lib/Backend/Bluetooth/known_devices.dart

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import 'dart:io';
44
import 'package:built_collection/built_collection.dart';
55
import 'package:flutter/foundation.dart';
66
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
7+
import 'package:hive_ce/hive.dart';
78
import 'package:logging/logging.dart' as log;
89
import 'package:wakelock_plus/wakelock_plus.dart';
910

@@ -23,8 +24,13 @@ class KnownDevices with ChangeNotifier {
2324
static final KnownDevices instance = KnownDevices._internal();
2425

2526
KnownDevices._internal() {
26-
BuiltList<BaseStoredDevice> storedDevices =
27-
HiveProxy.getAll<BaseStoredDevice>(devicesBox).toBuiltList();
27+
BuiltList<BaseStoredDevice> storedDevices = Hive.box<BaseStoredDevice>(
28+
devicesBox,
29+
).values.toBuiltList();
30+
31+
// after all device entries are loaded, close the box. The box will be
32+
// re-opened as a lazy box to save ram
33+
Hive.box<BaseStoredDevice>(devicesBox).close();
2834
Map<String, BaseStatefulDevice> results = {};
2935
try {
3036
if (storedDevices.isNotEmpty) {
@@ -66,11 +72,10 @@ class KnownDevices with ChangeNotifier {
6672
}
6773

6874
Future<void> store() async {
69-
await HiveProxy.clear<BaseStoredDevice>(devicesBox);
70-
await HiveProxy.addAll<BaseStoredDevice>(
71-
devicesBox,
72-
state.values.map((e) => e.baseStoredDevice),
73-
);
75+
LazyBox<BaseStoredDevice> lazyBox =
76+
await Hive.openLazyBox<BaseStoredDevice>(devicesBox);
77+
await lazyBox.clear();
78+
await lazyBox.addAll(state.values.map((e) => e.baseStoredDevice));
7479
_notify();
7580
}
7681

lib/Backend/audio.dart

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:io';
33
import 'package:audioplayers/audioplayers.dart';
44
import 'package:built_collection/built_collection.dart';
55
import 'package:flutter/material.dart';
6+
import 'package:hive_ce/hive.dart';
67
import 'package:logging/logging.dart';
78

89
import '../constants.dart';
@@ -25,18 +26,20 @@ Future<void> playSound(String file) async {
2526

2627
class UserAudioActions with ChangeNotifier {
2728
BuiltList<AudioAction> _state = BuiltList();
29+
2830
BuiltList<AudioAction> get state => _state;
2931

3032
static final UserAudioActions instance = UserAudioActions._internal();
3133

3234
UserAudioActions._internal() {
33-
List<AudioAction> results = [];
35+
Iterable<AudioAction> results = [];
3436
try {
35-
results = HiveProxy.getAll<AudioAction>(audioActionsBox).toList(growable: true);
37+
results = Hive.box<AudioAction>(audioActionsBox).values;
3638
} catch (e, s) {
3739
_audioLogger.severe("Unable to load audio: $e", e, s);
3840
}
39-
_state = results.build();
41+
Hive.box<AudioAction>(audioActionsBox).close();
42+
_state = results.toList().build();
4043
}
4144

4245
Future<void> add(AudioAction action) async {
@@ -49,7 +52,9 @@ class UserAudioActions with ChangeNotifier {
4952
}
5053

5154
Future<void> remove(AudioAction action) async {
52-
_state = _state.rebuild((p0) => p0.removeWhere((element) => element.uuid == action.uuid));
55+
_state = _state.rebuild(
56+
(p0) => p0.removeWhere((element) => element.uuid == action.uuid),
57+
);
5358
store();
5459
}
5560

@@ -59,8 +64,11 @@ class UserAudioActions with ChangeNotifier {
5964

6065
Future<void> store() async {
6166
_audioLogger.info("Storing Custom Audio");
62-
await HiveProxy.clear<AudioAction>(audioActionsBox);
63-
await HiveProxy.addAll<AudioAction>(audioActionsBox, _state);
67+
LazyBox<AudioAction> lazyBox = await Hive.openLazyBox<AudioAction>(
68+
audioActionsBox,
69+
);
70+
await lazyBox.clear();
71+
await lazyBox.addAll(_state);
6472
notifyListeners();
6573
}
6674
}

lib/Backend/favorite_actions.dart

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,16 @@ final _favoriteActionsLogger = Logger('Favorites');
1717

1818
@HiveType(typeId: 13)
1919
@freezed
20-
abstract class FavoriteAction with _$FavoriteAction implements Comparable<FavoriteAction> {
20+
abstract class FavoriteAction
21+
with _$FavoriteAction
22+
implements Comparable<FavoriteAction> {
2123
const FavoriteAction._();
2224

2325
@Implements<Comparable<FavoriteAction>>()
24-
const factory FavoriteAction({@HiveField(1) required String actionUUID, @HiveField(2) required int id}) = _FavoriteAction;
26+
const factory FavoriteAction({
27+
@HiveField(1) required String actionUUID,
28+
@HiveField(2) required int id,
29+
}) = _FavoriteAction;
2530

2631
@override
2732
int compareTo(other) {
@@ -32,17 +37,21 @@ abstract class FavoriteAction with _$FavoriteAction implements Comparable<Favori
3237

3338
class FavoriteActions with ChangeNotifier {
3439
BuiltList<FavoriteAction> _state = BuiltList();
40+
3541
BuiltList<FavoriteAction> get state => _state;
3642

3743
static final FavoriteActions instance = FavoriteActions._internal();
3844

3945
FavoriteActions._internal() {
4046
List<FavoriteAction> results = [];
4147
try {
42-
results = HiveProxy.getAll<FavoriteAction>(favoriteActionsBox).toList(growable: true);
48+
results = Hive.box<FavoriteAction>(
49+
favoriteActionsBox,
50+
).values.toList(growable: true);
4351
} catch (e, s) {
4452
_favoriteActionsLogger.severe("Unable to load favorites: $e", e, s);
4553
}
54+
Hive.box<FavoriteAction>(favoriteActionsBox).close();
4655
_state = results.toBuiltList();
4756
}
4857

@@ -56,7 +65,9 @@ class FavoriteActions with ChangeNotifier {
5665
}
5766

5867
Future<void> remove(BaseAction action) async {
59-
_state = _state.rebuild((p0) => p0.removeWhere((element) => element.actionUUID == action.uuid));
68+
_state = _state.rebuild(
69+
(p0) => p0.removeWhere((element) => element.actionUUID == action.uuid),
70+
);
6071
await store();
6172
}
6273

@@ -66,8 +77,11 @@ class FavoriteActions with ChangeNotifier {
6677

6778
Future<void> store() async {
6879
_favoriteActionsLogger.info("Storing favorites");
69-
await HiveProxy.clear<FavoriteAction>(favoriteActionsBox);
70-
await HiveProxy.addAll<FavoriteAction>(favoriteActionsBox, _state);
80+
LazyBox<FavoriteAction> lazyBox = await Hive.openLazyBox<FavoriteAction>(
81+
favoriteActionsBox,
82+
);
83+
await lazyBox.clear();
84+
await lazyBox.addAll(_state);
7185
updateShortcuts(_state);
7286
// ignore: unused_result
7387
notifyListeners();

lib/Backend/logging_wrappers.dart

Lines changed: 5 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,21 @@
11
import 'package:hive_ce/hive.dart';
22
import 'package:logarte/logarte.dart';
33

4-
import '../constants.dart';
5-
64
// ignore: library_private_types_in_public_api
75
_HiveProxyImpl HiveProxy = _HiveProxyImpl();
8-
List<String> genericBoxes = [settings];
96

107
class _HiveProxyImpl {
118
Future<void> put<E>(String box, dynamic key, E value) {
12-
logarte.database(
13-
target: '$key',
14-
value: '$value',
15-
source: box,
16-
);
17-
if (genericBoxes.contains(box)) {
18-
return Hive.box(box).put(key, value);
19-
} else {
20-
return Hive.box<E>(box).put(key, value);
21-
}
9+
logarte.database(target: '$key', value: '$value', source: box);
10+
return Hive.box(box).put(key, value);
2211
}
2312

24-
Future<void> deleteKey<E>(String box, dynamic key) {
25-
if (genericBoxes.contains(box)) {
26-
return Hive.box(box).delete(key);
27-
} else {
28-
return Hive.box<E>(box).delete(key);
29-
}
13+
Future<void> deleteKey(String box, dynamic key) {
14+
return Hive.box(box).delete(key);
3015
}
3116

3217
E getOrDefault<E>(String box, dynamic key, {required E? defaultValue}) {
33-
if (genericBoxes.contains(box)) {
34-
return Hive.box(box).get(key, defaultValue: defaultValue)!;
35-
} else {
36-
return Hive.box<E>(box).get(key, defaultValue: defaultValue)!;
37-
}
38-
}
39-
40-
Future<int> clear<E>(String box) {
41-
if (genericBoxes.contains(box)) {
42-
return Hive.box(box).clear();
43-
} else {
44-
return Hive.box<E>(box).clear();
45-
}
46-
}
47-
48-
Future<Iterable<int>> addAll<E>(String name, Iterable<E> values) {
49-
return Hive.box<E>(name).addAll(values);
50-
}
51-
52-
Iterable<E> getAll<E>(String name) {
53-
return Hive.box<E>(name).values;
18+
return Hive.box(box).get(key, defaultValue: defaultValue)!;
5419
}
5520
}
5621

lib/Backend/move_lists_backend.dart

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,38 @@ extension EasingTypeExtension on EasingType {
2727
case EasingType.linear:
2828
return SizedBox(
2929
width: 65,
30-
child: Sparkline(data: const [0, 1], lineColor: Theme.of(context).colorScheme.outline, lineWidth: 5),
30+
child: Sparkline(
31+
data: const [0, 1],
32+
lineColor: Theme.of(context).colorScheme.outline,
33+
lineWidth: 5,
34+
),
3135
);
3236
case EasingType.cubic:
3337
return SizedBox(
3438
width: 65,
3539
child: Sparkline(
36-
data: const [0.271, 0.488, 0.657, 0.784, 0.875, 0.936, 0.973, 0.992, 0.999, 1, 1.001, 1.008, 1.027, 1.064, 1.125, 1.216, 1.343, 1.512, 1.729, 2],
40+
data: const [
41+
0.271,
42+
0.488,
43+
0.657,
44+
0.784,
45+
0.875,
46+
0.936,
47+
0.973,
48+
0.992,
49+
0.999,
50+
1,
51+
1.001,
52+
1.008,
53+
1.027,
54+
1.064,
55+
1.125,
56+
1.216,
57+
1.343,
58+
1.512,
59+
1.729,
60+
2,
61+
],
3762
lineColor: Theme.of(context).colorScheme.outline,
3863
lineWidth: 5,
3964
),
@@ -98,7 +123,13 @@ class Move {
98123

99124
Move();
100125

101-
Move.move({this.leftServo = 0, this.rightServo = 0, this.speed = 50, this.easingType = EasingType.linear, this.moveType = MoveType.move});
126+
Move.move({
127+
this.leftServo = 0,
128+
this.rightServo = 0,
129+
this.speed = 50,
130+
this.easingType = EasingType.linear,
131+
this.moveType = MoveType.move,
132+
});
102133

103134
Move.delay(this.time) {
104135
moveType = MoveType.delay;
@@ -133,11 +164,18 @@ class Move {
133164
moveType == other.moveType;
134165

135166
@override
136-
int get hashCode => leftServo.hashCode ^ rightServo.hashCode ^ speed.hashCode ^ time.hashCode ^ easingType.hashCode ^ moveType.hashCode;
167+
int get hashCode =>
168+
leftServo.hashCode ^
169+
rightServo.hashCode ^
170+
speed.hashCode ^
171+
time.hashCode ^
172+
easingType.hashCode ^
173+
moveType.hashCode;
137174
}
138175

139176
class MoveLists with ChangeNotifier {
140177
BuiltList<MoveList> _state = BuiltList();
178+
141179
BuiltList<MoveList> get state => _state;
142180

143181
static final MoveLists instance = MoveLists._internal();
@@ -150,10 +188,11 @@ class MoveLists with ChangeNotifier {
150188
void reload() {
151189
List<MoveList> results = [];
152190
try {
153-
results = HiveProxy.getAll<MoveList>(sequencesBox).toList(growable: true);
191+
results = Hive.box<MoveList>(sequencesBox).values.toList();
154192
} catch (e, s) {
155193
sequencesLogger.severe("Unable to load sequences: $e", e, s);
156194
}
195+
Hive.box<MoveList>(sequencesBox).close();
157196
_state = results.toBuiltList();
158197
}
159198

@@ -179,8 +218,9 @@ class MoveLists with ChangeNotifier {
179218

180219
Future<void> store() async {
181220
sequencesLogger.info("Storing sequences");
182-
await HiveProxy.clear<MoveList>(sequencesBox);
183-
await HiveProxy.addAll<MoveList>(sequencesBox, _state);
221+
LazyBox<MoveList> lazyBox = await Hive.openLazyBox<MoveList>(sequencesBox);
222+
await lazyBox.clear();
223+
await lazyBox.addAll(_state);
184224
notifyListeners();
185225
}
186226
}

lib/Backend/sensors.dart

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1426,7 +1426,7 @@ class TriggerList with ChangeNotifier {
14261426
TriggerList._internal() {
14271427
List<Trigger> results = [];
14281428
try {
1429-
results = HiveProxy.getAll<Trigger>(triggerBox)
1429+
results = Hive.box<Trigger>(triggerBox).values
14301430
.map((trigger) {
14311431
Trigger trigger2 = Trigger.trigDef(
14321432
TriggerDefinitionList.allTriggerDefinitions.firstWhere(
@@ -1441,6 +1441,7 @@ class TriggerList with ChangeNotifier {
14411441
} catch (e, s) {
14421442
sensorsLogger.severe("Unable to load stored triggers: $e", e, s);
14431443
}
1444+
Hive.box<Trigger>(triggerBox).close();
14441445
if (results.isEmpty) {
14451446
TriggerDefinition triggerDefinition = TriggerDefinitionList
14461447
.allTriggerDefinitions
@@ -1475,8 +1476,9 @@ class TriggerList with ChangeNotifier {
14751476

14761477
Future<void> store() async {
14771478
sensorsLogger.info("Storing triggers");
1478-
await HiveProxy.clear<Trigger>(triggerBox);
1479-
await HiveProxy.addAll<Trigger>(triggerBox, _state);
1479+
LazyBox<Trigger> lazyBox = await Hive.openLazyBox<Trigger>(triggerBox);
1480+
await lazyBox.clear();
1481+
await lazyBox.addAll(_state);
14801482
notifyListeners();
14811483
updateWearData(reason: "Trigger Added/Removed");
14821484
}

lib/Frontend/pages/settings.dart

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -225,21 +225,6 @@ class _SettingsState extends State<Settings> {
225225
},
226226
),
227227
),
228-
/* ListTile(
229-
title: Text(convertToUwU(scanDemoGear())),
230-
leading: const Icon(Icons.explore),
231-
subtitle: Text(convertToUwU(scanDemoGearTip())),
232-
trailing: Switch(
233-
value: HiveProxy.getOrDefault(settings, showDemoGear, defaultValue: showDemoGearDefault),
234-
onChanged: (bool value) async {
235-
setState(
236-
() {
237-
HiveProxy.put(settings, showDemoGear, value);
238-
},
239-
);
240-
},
241-
),
242-
), */
243228
ListTile(
244229
title: Text(convertToUwU(settingsUwUToggleTitle())),
245230
leading: const Icon(Icons.explore),

lib/main.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,8 @@ Future<void> initHive() async {
249249
Hive.registerAdapter(VersionAdapter());
250250
}
251251
await Hive.openBox(settings); // Do not set type here
252+
253+
// closed after first read, reloads as lazybox
252254
await Hive.openBox<Trigger>(triggerBox);
253255
await Hive.openBox<FavoriteAction>(favoriteActionsBox);
254256
await Hive.openBox<AudioAction>(audioActionsBox);

0 commit comments

Comments
 (0)