Skip to content

Commit

Permalink
SNRGY-3272 save groups locally
Browse files Browse the repository at this point in the history
  • Loading branch information
zsco committed May 6, 2024
1 parent 8c02721 commit 1f963d2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 8 deletions.
12 changes: 11 additions & 1 deletion lib/models/device_group.dart
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,21 @@ class DeviceGroup {
return result;
}

Future<bool> isFavorite() async {
final group =
await isar!.deviceGroups.where().idEqualTo(id).findFirst();
return group?.favorite ?? false;
}

@JsonKey(includeFromJson: false, includeToJson: false)
@Index()
bool favorite = false;

/*
bool get favorite {
final i = attributes?.indexWhere((element) => element.key == attributeFavorite && element.origin == appOrigin);
return i != null && i != -1;
}
}*/

setFavorite(bool val) {
if (val) {
Expand Down
1 change: 1 addition & 0 deletions lib/models/device_group.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions lib/models/device_instance.dart
Original file line number Diff line number Diff line change
Expand Up @@ -134,10 +134,6 @@ class DeviceInstance {
}
}

toggleFavorite() {
setFavorite(!favorite);
}

@JsonKey(includeFromJson: false, includeToJson: false)
@ignore
DeviceConnectionStatus get connectionStatus {
Expand Down
1 change: 1 addition & 0 deletions lib/services/device_groups.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ class DeviceGroupsService {
}));
}
await Future.wait(futures);
groupsRepo.forEach((element) async {element.favorite = await element.isFavorite();});
if (isar != null && collection != null) {
await isar!.writeTxn(() async {
await collection.putAll(groupsRepo);
Expand Down
8 changes: 5 additions & 3 deletions lib/widgets/shared/favorize_button.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import 'package:mobile_app/shared/isar.dart';

class FavorizeButton extends StatelessWidget {
final DeviceInstance? _device;
DeviceGroup? _group;
final DeviceGroup? _group;

FavorizeButton(this._device, this._group, {super.key}) {
if ((_device == null && _group == null) ||
Expand All @@ -50,8 +50,10 @@ class FavorizeButton extends StatelessWidget {
await isar!.deviceInstances.put(_device!);
});
} else {
_group!.toggleFavorite();
_group = await DeviceGroupsService.saveDeviceGroup(_group!);
_group!.favorite = !_group!.favorite ;
await isar?.writeTxn(() async {
await isar!.deviceGroups.put(_group!);
});
}
AppState().notifyListeners();
}
Expand Down

0 comments on commit 1f963d2

Please sign in to comment.