diff --git a/lib/models/device_group.dart b/lib/models/device_group.dart index 9e5e5d5..abd874d 100644 --- a/lib/models/device_group.dart +++ b/lib/models/device_group.dart @@ -172,11 +172,21 @@ class DeviceGroup { return result; } + Future 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) { diff --git a/lib/models/device_group.g.dart b/lib/models/device_group.g.dart index e1540ad..aa4d948 100644 --- a/lib/models/device_group.g.dart +++ b/lib/models/device_group.g.dart @@ -204,6 +204,7 @@ DeviceGroup _deviceGroupDeserialize( Attribute(), ), ); + object.favorite = reader.readBool(offsets[3]); object.isarId = id; return object; } diff --git a/lib/models/device_instance.dart b/lib/models/device_instance.dart index 6dd2ffb..2e516cf 100644 --- a/lib/models/device_instance.dart +++ b/lib/models/device_instance.dart @@ -134,10 +134,6 @@ class DeviceInstance { } } - toggleFavorite() { - setFavorite(!favorite); - } - @JsonKey(includeFromJson: false, includeToJson: false) @ignore DeviceConnectionStatus get connectionStatus { diff --git a/lib/services/device_groups.dart b/lib/services/device_groups.dart index b3975e6..fd0f080 100644 --- a/lib/services/device_groups.dart +++ b/lib/services/device_groups.dart @@ -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); diff --git a/lib/widgets/shared/favorize_button.dart b/lib/widgets/shared/favorize_button.dart index 0d2eed5..13c4e04 100644 --- a/lib/widgets/shared/favorize_button.dart +++ b/lib/widgets/shared/favorize_button.dart @@ -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) || @@ -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(); }