diff --git a/lib/Helpers/MQTTHelper.dart b/lib/Helpers/MQTTHelper.dart index 01d988b..13375c9 100644 --- a/lib/Helpers/MQTTHelper.dart +++ b/lib/Helpers/MQTTHelper.dart @@ -4,15 +4,15 @@ import 'package:fluttertoast/fluttertoast.dart'; import 'package:mqtt_client/mqtt_client.dart'; import 'package:mqtt_client/mqtt_server_client.dart'; import 'package:myhomie_app/Models/homieContext.dart'; +import 'package:myhomie_app/app_context.dart'; class MQTTHelper { MQTTHelper._privateConstructor(); bool isInstantiated = false; static final MQTTHelper instance = MQTTHelper._privateConstructor(); - String lastMessage; - void onConnected(dynamic appContext) { + void onConnected(AppContext appContext) { print('Connected !!!!!!!!!!!! ----------------------------------'); HomieAppContext homieAppContext = appContext.getContext(); @@ -26,7 +26,7 @@ class MQTTHelper { final payload = MqttPublishPayload.bytesToStringAsString(message.payload.message); print('Received message:$payload from topic: ${c[0].topic}'); - lastMessage = 'Received message:$payload from topic: ${c[0].topic}'; + appContext.setLastMessage('Received message:$payload from topic: ${c[0].topic}'); var topic = c[0].topic.split('/')[0]; @@ -67,12 +67,6 @@ class MQTTHelper { print('Unsubscribed topic: $topic'); } - String getLastMessage() { - print("LAST MESSAAAGE"); - print(lastMessage); - return lastMessage; - } - Future connect(dynamic appContext) async { HomieAppContext homieAppContext = appContext.getContext(); diff --git a/lib/Screens/Debug/DebugPage.dart b/lib/Screens/Debug/DebugPage.dart index 75ab7cf..ca9a901 100644 --- a/lib/Screens/Debug/DebugPage.dart +++ b/lib/Screens/Debug/DebugPage.dart @@ -11,37 +11,57 @@ class DebugPage extends StatefulWidget { } class _DebugPageState extends State { + List lastMessages = []; @override Widget build(BuildContext context) { - final appContext = Provider.of(context); + final AppContext appContext = Provider.of(context); String lastMessage = "test"; //lastMessage = MQTTHelper.instance.getLastMessage() == null ? "rien" : MQTTHelper.instance.getLastMessage(); return Scaffold( appBar: AppBar( - title: Text("Debug TODO"), + title: Text("Debug"), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ - Text( - "Last Message", - ), - Text( - MQTTHelper.instance.getLastMessage() == null ? "rien" : MQTTHelper.instance.getLastMessage(), - style: Theme.of(context).textTheme.headline4, + new Expanded( + child: new ListView.separated + ( + reverse: true, + separatorBuilder: (context, index) { + return Divider(); + }, + itemCount: appContext.getLastMQTTMessages()?.length, + itemBuilder: (BuildContext ctxt, int Index) { + if (appContext.getLastMQTTMessages().length > 0) { + if (appContext.getLastMQTTMessages()[Index].toString().length > 350) { + var textToShow = appContext.getLastMQTTMessages()[Index].toString().substring(0,350); + return new Text("$textToShow ..."); + } else { + return new Text(appContext.getLastMQTTMessages()[Index].toString()); + } + }else { + return new Text("rien"); + } + } + ) ), ], ), ), - floatingActionButton: FloatingActionButton( + /*floatingActionButton: FloatingActionButton( onPressed: () => {}, tooltip: 'Increment', child: Icon(Icons.add), - ), + ),*/ ); } + + Widget buildBody(BuildContext context, int index) { + return new Text(lastMessages[index]); + } } \ No newline at end of file diff --git a/lib/Screens/Home/HomePage.dart b/lib/Screens/Home/HomePage.dart index f560220..7182ea1 100644 --- a/lib/Screens/Home/HomePage.dart +++ b/lib/Screens/Home/HomePage.dart @@ -136,7 +136,36 @@ class _HomePageState extends State { ], ), ), - floatingActionButton: FloatingActionButton( + drawer: Drawer( + child: ListView( + // Important: Remove any padding from the ListView. + padding: EdgeInsets.zero, + children: [ + const DrawerHeader( + decoration: BoxDecoration( + color: Colors.blue, + ), + child: Text('Drawer Header'), + ), + ListTile( + title: const Text('Debug screen'), + onTap: () { + Navigator.pop(context); + Navigator.push( + context, + MaterialPageRoute( + builder: (context) { + return DebugPage(); + }, + ) + ); + + }, + ), + ], + ), + ), + /*floatingActionButton: FloatingActionButton( onPressed: () => { Navigator.push( context, @@ -149,7 +178,7 @@ class _HomePageState extends State { }, tooltip: 'Increment', child: Icon(Icons.add), - ), + ),*/ ); } } \ No newline at end of file diff --git a/lib/app_context.dart b/lib/app_context.dart index cdbb662..891396c 100644 --- a/lib/app_context.dart +++ b/lib/app_context.dart @@ -6,6 +6,7 @@ import 'Models/homieContext.dart'; class AppContext with ChangeNotifier { HomieAppContext _homieContext; Client clientAPI; + List _lastMQTTMessages = []; AppContext(this._homieContext); @@ -15,4 +16,15 @@ class AppContext with ChangeNotifier { notifyListeners(); } + + getLastMQTTMessages() => _lastMQTTMessages; + setLastMessage(String lastMessage) async { + _lastMQTTMessages.add(lastMessage); + + if (_lastMQTTMessages.length > 10) { + _lastMQTTMessages.removeAt(0); + } + + notifyListeners(); + } } diff --git a/pubspec.lock b/pubspec.lock index ea6fbca..bea18dc 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -63,7 +63,7 @@ packages: name: cupertino_icons url: "https://pub.dartlang.org" source: hosted - version: "1.0.2" + version: "1.0.4" enum_to_string: dependency: "direct main" description: @@ -142,6 +142,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.12.11" + material_color_utilities: + dependency: transitive + description: + name: material_color_utilities + url: "https://pub.dartlang.org" + source: hosted + version: "0.1.3" meta: dependency: transitive description: @@ -183,7 +190,7 @@ packages: name: pedantic url: "https://pub.dartlang.org" source: hosted - version: "1.11.0" + version: "1.11.1" provider: dependency: "direct main" description: @@ -216,14 +223,14 @@ packages: name: sqflite url: "https://pub.dartlang.org" source: hosted - version: "2.0.0+4" + version: "2.0.2" sqflite_common: dependency: transitive description: name: sqflite_common url: "https://pub.dartlang.org" source: hosted - version: "2.0.1+1" + version: "2.2.0" stack_trace: dependency: transitive description: @@ -265,7 +272,7 @@ packages: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.4.3" + version: "0.4.8" typed_data: dependency: transitive description: @@ -281,5 +288,5 @@ packages: source: hosted version: "2.1.1" sdks: - dart: ">=2.12.0 <3.0.0" + dart: ">=2.15.0 <3.0.0" flutter: ">=1.16.0"