Skip to content

Commit

Permalink
finally working. dart v3 version and glitches fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
herzhenr committed Aug 20, 2023
1 parent ed82a84 commit 6fd9753
Show file tree
Hide file tree
Showing 8 changed files with 174 additions and 130 deletions.
41 changes: 22 additions & 19 deletions lib/screens/home/bottom_sheet_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ import '../../widgets/universal_ui_components.dart';
abstract class ModularBottomFormPage extends StatefulWidget {
final String title;
final Device device;
final Function(List<StorageDevice>) onSubmitDeviceCallback;
final List<StorageDevice> devices;
final Function(List<StorageDevice>, String?) onSubmitDeviceCallback;
final bool deleteButton;

ModularBottomFormPage(
{Key? key,
required this.device,
required this.devices,
required this.title,
required this.onSubmitDeviceCallback,
this.deleteButton = false})
Expand Down Expand Up @@ -57,6 +59,7 @@ abstract class ModularBottomFormPage extends StatefulWidget {
macAddress: controllerMac.text,
modified: DateTime.now(),
wolPort: wolPort,
isOnline: storageDevice.isOnline,
deviceType: deviceType);
} else {
return NetworkDevice(
Expand All @@ -74,14 +77,13 @@ abstract class ModularBottomFormPage extends StatefulWidget {

/// dataOperationOnSave() is an abstract method that is implemented in the child classes and is called when the submitButton is pressed
/// it saves the device to the json file and returns the updated [StorageDevice] list
Future<List<StorageDevice>> dataOperationOnSave();
Future<(List<StorageDevice>, StorageDevice)> dataOperationOnSave();

/// dataOperationOnDelete() is triggered when the delete button is pressed and delete a device from the json file and returns the updated [StorageDevice] list
Future<List<StorageDevice>> dataOperationOnDelete() async {
StorageDevice device = getDevice as StorageDevice;
List<StorageDevice> devices = await deviceStorage.deleteDevice(
device.id,
);
List<StorageDevice> devices =
await deviceStorage.deleteDevice(device.id, this.devices);
return devices;
}

Expand Down Expand Up @@ -213,10 +215,11 @@ class _ModularBottomFormPageState extends State<ModularBottomFormPage> {
onPressed: () => {
validateFormFields(onSubmitDeviceCallback: () async {
Navigator.popUntil(context, (route) => route.isFirst);
List<StorageDevice> device =
(List<StorageDevice>, StorageDevice) updatedDevices =
await widget.dataOperationOnSave();
// sent device to callback function in order to update the UI
widget.onSubmitDeviceCallback(device);
widget.onSubmitDeviceCallback(
updatedDevices.$1, updatedDevices.$2.id);
})
},
text: AppLocalizations.of(context)!.formApplyButtonText,
Expand Down Expand Up @@ -529,9 +532,9 @@ class _ModularBottomFormPageState extends State<ModularBottomFormPage> {
rightColor: Theme.of(context).colorScheme.error,
rightOnPressed: () async {
Navigator.popUntil(context, (route) => route.isFirst);
List<StorageDevice> device = await widget.dataOperationOnDelete();
List<StorageDevice> devices = await widget.dataOperationOnDelete();
// sent device to callback function in order to update the UI
widget.onSubmitDeviceCallback(device);
widget.onSubmitDeviceCallback(devices, null);
},
);
},
Expand All @@ -558,15 +561,15 @@ class NetworkDeviceFormPage extends ModularBottomFormPage {
NetworkDeviceFormPage(
{super.key,
required super.device,
required super.devices,
required super.title,
required super.onSubmitDeviceCallback});

@override
Future<List<StorageDevice>> dataOperationOnSave() async {
List<StorageDevice> devices = await deviceStorage.addDevice(
getDevice as NetworkDevice,
);
return devices;
Future<(List<StorageDevice>, StorageDevice)> dataOperationOnSave() async {
(List<StorageDevice>, StorageDevice) updatedDevices =
await deviceStorage.addDevice(getDevice as NetworkDevice, devices);
return updatedDevices;
}
}

Expand All @@ -576,15 +579,15 @@ class EditDeviceFormPage extends ModularBottomFormPage {
{super.key,
required super.device,
required super.title,
required super.devices,
required super.onSubmitDeviceCallback})
: super(deleteButton: true);

@override
Future<List<StorageDevice>> dataOperationOnSave() async {
List<StorageDevice> devices = await deviceStorage.updateDevice(
getDevice as StorageDevice,
);
return devices;
Future<(List<StorageDevice>, StorageDevice)> dataOperationOnSave() async {
(List<StorageDevice>, StorageDevice) updatedDevices =
await deviceStorage.updateDevice(getDevice as StorageDevice, devices);
return updatedDevices;
}
}

Expand Down
9 changes: 7 additions & 2 deletions lib/screens/home/discover.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import '../../widgets/layout_elements.dart';
import '../../widgets/universal_ui_components.dart';

class DiscoverPage extends StatefulWidget {
final Function(List<StorageDevice>) updateDevicesList;
final Function(List<StorageDevice>, String?) updateDevicesList;
final List<StorageDevice> devices;

const DiscoverPage({Key? key, required this.updateDevicesList})
const DiscoverPage(
{Key? key, required this.updateDevicesList, required this.devices})
: super(key: key);

@override
Expand Down Expand Up @@ -105,6 +107,7 @@ class _DiscoverPageState extends State<DiscoverPage> {
title:
AppLocalizations.of(context)!.discoverAddDeviceAlertTitle,
device: NetworkDevice(),
devices: widget.devices,
onSubmitDeviceCallback: widget.updateDevicesList)),
text: AppLocalizations.of(context)!.discoverAddCustomDeviceButton,
icon: const Icon(Icons.add)),
Expand Down Expand Up @@ -167,6 +170,7 @@ class _DiscoverPageState extends State<DiscoverPage> {
.discoverAddDeviceAlertTitle,
device: _devices[index]
.copyWith(wolPort: 9),
devices: widget.devices,
onSubmitDeviceCallback:
widget.updateDevicesList)),
);
Expand Down Expand Up @@ -221,6 +225,7 @@ class _DiscoverPageState extends State<DiscoverPage> {
builder: (context) => NetworkDeviceFormPage(
title: title,
device: device.copyWith(wolPort: port),
devices: widget.devices,
onSubmitDeviceCallback: widget.updateDevicesList),
);
}
Expand Down
Loading

0 comments on commit 6fd9753

Please sign in to comment.