import 'dart:convert'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:mycore_api/api.dart'; import 'package:mycore_api/api.dart' as API; import 'package:myhomie_app/Components/Alarms/getCurrentAlarmModeIcon.dart'; import 'package:myhomie_app/Components/Custom_Navigation_Bar/CustomAppBar.dart'; import 'package:myhomie_app/Components/check_input_container.dart'; import 'package:myhomie_app/Components/loading_common.dart'; import 'package:myhomie_app/Components/string_input_container.dart'; import 'package:myhomie_app/Models/homieContext.dart'; import 'package:myhomie_app/app_context.dart'; import 'package:myhomie_app/constants.dart'; import 'package:provider/provider.dart'; class AutomationDetailPage extends StatefulWidget { const AutomationDetailPage({Key? key, required this.automationDTO}) : super(key: key); final API.AutomationDTO automationDTO; @override State createState() => _AutomationDetailPageState(); } class _AutomationDetailPageState extends State { final GlobalKey _scaffoldKey = GlobalKey(); AutomationDetailDTO? automationDetailDTO; @override void initState() { super.initState(); } @override void dispose() { super.dispose(); } @override Widget build(BuildContext context) { final appContext = Provider.of(context); Size size = MediaQuery.of(context).size; //final notchInset = MediaQuery.of(context).padding; HomieAppContext homieAppContext = appContext.getContext(); debugPrint(widget.automationDTO.id!); return Scaffold( key: _scaffoldKey, appBar: CustomAppBar( title: widget.automationDTO.name!, //titleIcon: getAlarmModeIcon(widget.alarmMode), isTextSizeButton: true, ), body: FutureBuilder( future: homieAppContext.clientAPI.automationApi!.automationGetDetail(widget.automationDTO.id!), builder: (context, AsyncSnapshot snapshot) { if(snapshot.data != null ) { automationDetailDTO = snapshot.data; return SingleChildScrollView( child: Column( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.center, crossAxisAlignment: CrossAxisAlignment.center, children: [ SizedBox( width: size.width *1.1, child: Padding( padding: const EdgeInsets.all(8.0), child: StringInputContainer( label: "Nom:", color: kMainColor, textColor: Colors.white, initialValue: automationDetailDTO!.name, onChanged: (String value) { setState(() { automationDetailDTO!.name = value; // TODO SAVE or not }); }, ), ), ), /*Padding( padding: const EdgeInsets.all(8.0), child: CheckInputContainer( icon: Icons.notifications_active, label: "Notification :", fontSize: 20, isChecked: alarmModeDetailDTO!.notification!, onChanged: (bool value) { alarmModeDetailDTO!.notification = value; }, ), ),*/ ExpansionTile( title: Text( "Triggers", style: TextStyle( fontSize: 18.0, fontWeight: FontWeight.bold ), ), children: [ for(var trigger in automationDetailDTO!.triggers!) if(automationDetailDTO!.devices!.where((d) => d.id == trigger.deviceId).isNotEmpty) ExpansionTile( title: Text(automationDetailDTO!.devices!.firstWhere((d) => d.id == trigger.deviceId).name!), children: [ ListTile( title: Text("name: "+trigger.stateName! + " - " + "value: "+ trigger.stateValue!), ) ], ), ListTile( title: Text("Ajouter +", style: TextStyle(), textAlign: TextAlign.center), tileColor: Colors.lightGreen, onTap: () { // Show popup ajout debugPrint("Add device to trigger popup"); }, ) ], ), ExpansionTile( title: Text( "Actions", style: TextStyle( fontSize: 18.0, fontWeight: FontWeight.bold ), ), children: [ for(var action in automationDetailDTO!.actions!) actionTile(action), ListTile( title: Text("Ajouter +", style: TextStyle(), textAlign: TextAlign.center), tileColor: Colors.lightGreen, onTap: () { // Show popup ajout debugPrint("Add device to trigger popup"); }, ) ], ), Text("Actions"), for(var device in automationDetailDTO!.devices!.where((d) => automationDetailDTO!.actions!.map((t)=> t.deviceId).contains(d.id))) Text(device.name!), Text(automationDetailDTO!.actions!.toString()), Text("Devices"), Text(automationDetailDTO!.devices!.toString()), ], ), ); } else { return const LoadingCommon(); } } ) ); } actionTile(API.Action action) { switch(action.type) { case ActionType.DELAY: return ListTile( title: Text(action.type.toString()), ); case ActionType.DEVICE: return ExpansionTile( title: Text(action.type.toString()), children: [ if(automationDetailDTO!.devices!.where((d) => d.id == action.deviceId).isNotEmpty) ExpansionTile( title: Text(automationDetailDTO!.devices!.firstWhere((d) => d.id == action.deviceId).name!), children: [ ListTile( title: Text(action.states.toString()) ), ], ) ], ); case ActionType.GROUP: return ListTile( title: Text(action.type.toString()), ); case ActionType.HTTP: return ListTile( title: Text(action.type.toString()), ); case ActionType.MQTT: return ExpansionTile( title: Text(action.type.toString()), children: [ ListTile( title: Text(action.rawRequest!), ) ], ); case ActionType.zIGBEE2MQTT: return ListTile( title: Text(action.type.toString()), ); } } }