Skip to content

Commit

Permalink
feat: Add parameter to each widget constructor to handle when managed…
Browse files Browse the repository at this point in the history
… value is changed
  • Loading branch information
ShapeLayer committed Oct 21, 2024
1 parent f93491d commit 912d044
Show file tree
Hide file tree
Showing 10 changed files with 189 additions and 72 deletions.
27 changes: 26 additions & 1 deletion configurator/lib/components/device_selector.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import 'dart:async';

import 'package:configurator/models/error_serial_device.dart';
import 'package:configurator/models/notifying_events.dart';
import 'package:flutter/material.dart';
import 'package:flutter_libserialport/flutter_libserialport.dart';
Expand Down Expand Up @@ -66,10 +69,32 @@ class _DeviceSelectorState extends State<DeviceSelector> {
setState(() {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(EventNotifier.eventNotifyingMessage(
context, NotifyingEvents.serialDeviceDoesNotResponse))));
context,
NotifyingEvents
.serialDeviceDoesNotResponseMayInvalidDevice))));
});
return;
}

bool errorThrownDuringLoad = false;
try {
Globals.instance.loadCurrentSerialDeviceConfig();
} on SerialPortNotInstantiatedWell {
errorThrownDuringLoad = true;
} on SerialPortCannotOpen {
errorThrownDuringLoad = true;
} on StreamControllerNotInstantiatedWell {
errorThrownDuringLoad = true;
} on TimeoutException {
errorThrownDuringLoad = true;
}
if (errorThrownDuringLoad) {
setState(() {
ScaffoldMessenger.of(context).showSnackBar(SnackBar(
content: Text(EventNotifier.eventNotifyingMessage(
context, NotifyingEvents.serialDeviceDoesNotResponse))));
});
}
},
);
}
Expand Down
40 changes: 39 additions & 1 deletion configurator/lib/components/key_config_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ class KeyConfigListItemContainer {
}

class KeyConfigList extends StatefulWidget {
const KeyConfigList({super.key});
const KeyConfigList({super.key, this.onKeyConfigUpdated});
final Null Function()? onKeyConfigUpdated;

@override
State<KeyConfigList> createState() => _KeyConfigListState();
}

class _KeyConfigListState extends State<KeyConfigList> {
void _onChangeHandler(Keycode keycode) {
/**
* Todo:
* onKeyConfigUpdated is intended to alert to parent value need to be saved.
* Parent should display the fact that change detected and save button.
*/
widget.onKeyConfigUpdated?.call();
}

List<KeyConfigListItemContainer> getKeyConfigEssentialContainers(
BuildContext context) {
return <KeyConfigListItemContainer>[
Expand All @@ -34,69 +44,79 @@ class _KeyConfigListState extends State<KeyConfigList> {
enabled: Globals.instance.updatedKeyConfig.tuneLeftSide.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneLeftSide.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTuneS,
key: Globals.instance.updatedKeyConfig.tuneS.keycode,
enabled: Globals.instance.updatedKeyConfig.tuneS.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneS.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTuneD,
key: Globals.instance.updatedKeyConfig.tuneD.keycode,
enabled: Globals.instance.updatedKeyConfig.tuneD.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneD.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTuneF,
key: Globals.instance.updatedKeyConfig.tuneF.keycode,
enabled: Globals.instance.updatedKeyConfig.tuneF.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneF.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTuneF,
key: Globals.instance.updatedKeyConfig.tuneC.keycode,
enabled: Globals.instance.updatedKeyConfig.tuneC.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneC.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTuneF,
key: Globals.instance.updatedKeyConfig.tuneM.keycode,
enabled: Globals.instance.updatedKeyConfig.tuneM.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneM.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTuneJ,
key: Globals.instance.updatedKeyConfig.tuneJ.keycode,
enabled: Globals.instance.updatedKeyConfig.tuneJ.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneJ.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTuneK,
key: Globals.instance.updatedKeyConfig.tuneK.keycode,
enabled: Globals.instance.updatedKeyConfig.tuneK.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneK.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTuneL,
key: Globals.instance.updatedKeyConfig.tuneL.keycode,
enabled: Globals.instance.updatedKeyConfig.tuneL.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneL.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTuneRightSide,
key: Globals.instance.updatedKeyConfig.tuneRightSide.keycode,
enabled: Globals.instance.updatedKeyConfig.tuneRightSide.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tuneRightSide.keycode = keycode;
_onChangeHandler(keycode);
}),
];
}
Expand All @@ -110,90 +130,103 @@ class _KeyConfigListState extends State<KeyConfigList> {
enabled: Globals.instance.updatedKeyConfig.esc.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.esc.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameEnter,
key: Globals.instance.updatedKeyConfig.enter.keycode,
enabled: Globals.instance.updatedKeyConfig.enter.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.enter.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameTab,
key: Globals.instance.updatedKeyConfig.tab.keycode,
enabled: Globals.instance.updatedKeyConfig.tab.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.tab.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameSpace,
key: Globals.instance.updatedKeyConfig.space.keycode,
enabled: Globals.instance.updatedKeyConfig.space.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.space.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameSpeedUp,
key: Globals.instance.updatedKeyConfig.speedUp.keycode,
enabled: Globals.instance.updatedKeyConfig.speedUp.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.speedUp.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameSpeedDown,
key: Globals.instance.updatedKeyConfig.speedDown.keycode,
enabled: Globals.instance.updatedKeyConfig.speedDown.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.speedDown.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameRewind,
key: Globals.instance.updatedKeyConfig.rewind.keycode,
enabled: Globals.instance.updatedKeyConfig.rewind.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.rewind.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameLeftShift,
key: Globals.instance.updatedKeyConfig.leftShift.keycode,
enabled: Globals.instance.updatedKeyConfig.leftShift.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.leftShift.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameRightShift,
key: Globals.instance.updatedKeyConfig.rightShift.keycode,
enabled: Globals.instance.updatedKeyConfig.rightShift.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.rightShift.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameArrowUp,
key: Globals.instance.updatedKeyConfig.arrowUp.keycode,
enabled: Globals.instance.updatedKeyConfig.arrowUp.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.arrowUp.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameArrowDown,
key: Globals.instance.updatedKeyConfig.arrowDown.keycode,
enabled: Globals.instance.updatedKeyConfig.arrowDown.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.arrowDown.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameArrowLeft,
key: Globals.instance.updatedKeyConfig.arrowLeft.keycode,
enabled: Globals.instance.updatedKeyConfig.arrowLeft.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.arrowLeft.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameArrowRight,
key: Globals.instance.updatedKeyConfig.arrowRight.keycode,
enabled: Globals.instance.updatedKeyConfig.arrowRight.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.arrowRight.keycode = keycode;
_onChangeHandler(keycode);
}),
];
}
Expand All @@ -207,34 +240,39 @@ class _KeyConfigListState extends State<KeyConfigList> {
enabled: Globals.instance.updatedKeyConfig.emoticon1.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.emoticon1.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameEmoticon2,
key: Globals.instance.updatedKeyConfig.emoticon2.keycode,
enabled: Globals.instance.updatedKeyConfig.emoticon2.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.emoticon2.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameEmoticon3,
key: Globals.instance.updatedKeyConfig.emoticon3.keycode,
enabled: Globals.instance.updatedKeyConfig.emoticon3.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.emoticon3.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameEmoticon4,
key: Globals.instance.updatedKeyConfig.emoticon4.keycode,
enabled: Globals.instance.updatedKeyConfig.emoticon4.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.emoticon4.keycode = keycode;
_onChangeHandler(keycode);
}),
KeyConfigListItemContainer(
name: AppLocalizations.of(context)!.keyNameEmoticon5,
key: Globals.instance.updatedKeyConfig.emoticon5.keycode,
enabled: Globals.instance.updatedKeyConfig.emoticon5.enabled,
handler: (Keycode keycode) {
Globals.instance.updatedKeyConfig.emoticon5.keycode = keycode;
_onChangeHandler(keycode);
}),
];
}
Expand Down
23 changes: 23 additions & 0 deletions configurator/lib/globals.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:configurator/build_config.dart';
import 'package:configurator/models/each_key_config.dart';
import 'package:configurator/models/error_serial_device.dart';
import 'package:configurator/models/keycode.dart';
import 'package:configurator/models/key_config.dart';
import 'package:configurator/models/serial_device.dart';
Expand All @@ -12,6 +13,10 @@ class Globals {
late String currentSerialDevicePort;
late Map<String, SerialDevice> _serialDevices;

bool get keyConfigUpdated {
return !(keyConfig == updatedKeyConfig);
}

static Globals instance = Globals._privateConstructor();

factory Globals() {
Expand Down Expand Up @@ -153,4 +158,22 @@ class Globals {
SerialDevice current = currentSerialDevice;
return await current.checkDeviceIsValid();
}

Future<KeyConfig> _getCurrentSerialDeviceConfig() async {
SerialDevice current = currentSerialDevice;
return await current.requestLoadKeyConfiguration();
}

void loadCurrentSerialDeviceConfig() async {
/// Load device's keyconfig data
///
/// Below errors must be handled:
/// - SerialPortNotInstantiatedWell
/// - SerialPortCannotOpen
/// - StreamControllerNotInstantiatedWell
/// - TimeoutException
/// - SerialPortCommunicationDoneIncompleted
keyConfig = await _getCurrentSerialDeviceConfig();
updatedKeyConfig = KeyConfig.clone(keyConfig);
}
}
3 changes: 2 additions & 1 deletion configurator/lib/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,6 @@
"keyNameEmoticon3": "Emoticon #3",
"keyNameEmoticon4": "Emoticon #4",
"keyNameEmoticon5": "Emoticon #5",
"notifyingEventsSerialDeviceDoesNotResponse": "Selected device doesn't response. It seems to be not correct device or software installed."
"notifyingEventsSerialDeviceDoesNotResponse": "Selected device doesn't response.",
"notifyingEventsSerialDeviceDoesNotResponse_mayInvalidDevice": "Selected device doesn't response. It seems to be not correct device or software installed."
}
2 changes: 2 additions & 0 deletions configurator/lib/models/each_key_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ class EachKeyConfig {
Keycode keycode;
bool enabled;
EachKeyConfig({required this.keycode, required this.enabled});
EachKeyConfig.clone(EachKeyConfig other)
: this(keycode: other.keycode, enabled: other.enabled);
}
10 changes: 10 additions & 0 deletions configurator/lib/models/error_serial_device.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
class SerialPortNotInstantiatedWell implements Exception {
String cause;
SerialPortNotInstantiatedWell(this.cause);
}

class SerialPortIsNotOpened implements Exception {
String cause;
SerialPortIsNotOpened(this.cause);
Expand Down Expand Up @@ -27,3 +32,8 @@ class StreamControllerNotInstantiatedWell implements Exception {
String cause;
StreamControllerNotInstantiatedWell(this.cause);
}

class SerialPortCommunicationDoneIncompleted implements Exception {
String cause;
SerialPortCommunicationDoneIncompleted(this.cause);
}
Loading

0 comments on commit 912d044

Please sign in to comment.