diff options
Diffstat (limited to 'lib/widgets/device_details.dart')
-rw-r--r-- | lib/widgets/device_details.dart | 94 |
1 files changed, 70 insertions, 24 deletions
diff --git a/lib/widgets/device_details.dart b/lib/widgets/device_details.dart index 79f76cf..0463522 100644 --- a/lib/widgets/device_details.dart +++ b/lib/widgets/device_details.dart @@ -13,10 +13,16 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see <https://www.gnu.org/licenses/>. +import 'dart:ui'; + +import 'package:uvok_epaper_badge/model/badge_motive_selection_factory.dart'; import 'package:uvok_epaper_badge/model/device/device.dart'; import 'package:flutter/material.dart'; import 'package:logger/logger.dart'; import 'package:uvok_epaper_badge/model/connection/device_connection.dart'; +import 'package:uvok_epaper_badge/model/motive_selection/badge_motive_selection.dart'; +import 'package:uvok_epaper_badge/view_model/badge_motive_view_model.dart'; +import 'package:uvok_epaper_badge/widgets/badge_motive_list.dart'; var logger = Logger(); @@ -24,11 +30,19 @@ class DeviceDetailsScreen extends StatefulWidget { final Device device; final DeviceConnection deviceConnection; - const DeviceDetailsScreen({ + late final BadgeMotiveSelection _motiveSelection; + late final BadgeMotiveViewModel _motiveVM; + + DeviceDetailsScreen({ super.key, required this.device, required this.deviceConnection, - }); + }) { + _motiveSelection = BadgeMotiveSelectionFactory.createBadgeMotiveSelection( + device, + ); + _motiveVM = BadgeMotiveViewModel(motivSelect: _motiveSelection); + } @override State<StatefulWidget> createState() { @@ -37,11 +51,21 @@ class DeviceDetailsScreen extends StatefulWidget { } class DeviceDetailsState extends State<DeviceDetailsScreen> { - String connectStatus = "<Status>"; + late final AppLifecycleListener appLL; /// Whether the back button should be active. bool backActive = false; + DeviceDetailsState() { + appLL = AppLifecycleListener( + onExitRequested: () async { + logger.i("Exit requested"); + dispose(); + return AppExitResponse.exit; + }, + ); + } + @override Widget build(BuildContext context) { return Scaffold( @@ -50,15 +74,41 @@ class DeviceDetailsState extends State<DeviceDetailsScreen> { title: Text("Device details"), ), body: Center( - child: Column( - spacing: 20, - children: [ - Text(connectStatus), - ElevatedButton( - onPressed: backActive ? backClick : null, - child: Text("Back"), - ), - ], + child: ValueListenableBuilder( + valueListenable: widget.deviceConnection.status, + builder: (connCtx, ConnectionStatus value, child) { + return Column( + spacing: 20, + children: [ + SizedBox(height: 20), + Text("Connection state: ${value.toString()}"), + ElevatedButton( + child: Text("Refresh"), + onPressed: () async { + await widget._motiveVM.updateMotives(); + await widget._motiveVM.getCurrentMotive(); + }, + ), + ListenableBuilder( + listenable: widget._motiveVM, + builder: (errorCtx, child) { + if (widget._motiveVM.errorMessage != null) { + var theme = Theme.of(errorCtx); + return Text( + widget._motiveVM.errorMessage!, + style: TextStyle( + color: theme.colorScheme.error, + fontWeight: FontWeight.bold, + ), + ); + } + return Container(); + }, + ), + BadgeMotiveList(motiveVM: widget._motiveVM), + ], + ); + }, ), ), ); @@ -70,33 +120,29 @@ class DeviceDetailsState extends State<DeviceDetailsScreen> { _doConnect(); } - void backClick() { - Navigator.pop(context); - } - @override void deactivate() { - widget.deviceConnection.disconnect().ignore(); - logger.i("Closing state"); + logger.i("(widget deactivate)"); // widget.device.disconnect().ignore(); super.deactivate(); } - void _doConnect() async { - final dev = widget.device; + @override + void dispose() { + logger.i("(widget dispose)"); + widget.deviceConnection.disconnect().ignore(); + super.dispose(); + } + void _doConnect() async { try { logger.i("Try to connect..."); await widget.deviceConnection.connect(); } catch (e) { logger.e(e); await widget.deviceConnection.disconnect(); - connectStatus = e.toString(); } finally { backActive = true; - if (mounted) { - setState(() {}); - } } } } |