Debug page from drawer and semi working

This commit is contained in:
Fransolet Thomas 2022-02-10 21:10:05 +01:00
parent c4f2e29b9e
commit d145689ec3
5 changed files with 89 additions and 27 deletions

View File

@ -4,15 +4,15 @@ import 'package:fluttertoast/fluttertoast.dart';
import 'package:mqtt_client/mqtt_client.dart'; import 'package:mqtt_client/mqtt_client.dart';
import 'package:mqtt_client/mqtt_server_client.dart'; import 'package:mqtt_client/mqtt_server_client.dart';
import 'package:myhomie_app/Models/homieContext.dart'; import 'package:myhomie_app/Models/homieContext.dart';
import 'package:myhomie_app/app_context.dart';
class MQTTHelper { class MQTTHelper {
MQTTHelper._privateConstructor(); MQTTHelper._privateConstructor();
bool isInstantiated = false; bool isInstantiated = false;
static final MQTTHelper instance = MQTTHelper._privateConstructor(); static final MQTTHelper instance = MQTTHelper._privateConstructor();
String lastMessage;
void onConnected(dynamic appContext) { void onConnected(AppContext appContext) {
print('Connected !!!!!!!!!!!! ----------------------------------'); print('Connected !!!!!!!!!!!! ----------------------------------');
HomieAppContext homieAppContext = appContext.getContext(); HomieAppContext homieAppContext = appContext.getContext();
@ -26,7 +26,7 @@ class MQTTHelper {
final payload = MqttPublishPayload.bytesToStringAsString(message.payload.message); final payload = MqttPublishPayload.bytesToStringAsString(message.payload.message);
print('Received message:$payload from topic: ${c[0].topic}'); 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]; var topic = c[0].topic.split('/')[0];
@ -67,12 +67,6 @@ class MQTTHelper {
print('Unsubscribed topic: $topic'); print('Unsubscribed topic: $topic');
} }
String getLastMessage() {
print("LAST MESSAAAGE");
print(lastMessage);
return lastMessage;
}
Future<MqttServerClient> connect(dynamic appContext) async { Future<MqttServerClient> connect(dynamic appContext) async {
HomieAppContext homieAppContext = appContext.getContext(); HomieAppContext homieAppContext = appContext.getContext();

View File

@ -11,37 +11,57 @@ class DebugPage extends StatefulWidget {
} }
class _DebugPageState extends State<DebugPage> { class _DebugPageState extends State<DebugPage> {
List<String> lastMessages = [];
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
final appContext = Provider.of<AppContext>(context); final AppContext appContext = Provider.of<AppContext>(context);
String lastMessage = "test"; String lastMessage = "test";
//lastMessage = MQTTHelper.instance.getLastMessage() == null ? "rien" : MQTTHelper.instance.getLastMessage(); //lastMessage = MQTTHelper.instance.getLastMessage() == null ? "rien" : MQTTHelper.instance.getLastMessage();
return Scaffold( return Scaffold(
appBar: AppBar( appBar: AppBar(
title: Text("Debug TODO"), title: Text("Debug"),
), ),
body: Center( body: Center(
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
Text( new Expanded(
"Last Message", child: new ListView.separated
), (
Text( reverse: true,
MQTTHelper.instance.getLastMessage() == null ? "rien" : MQTTHelper.instance.getLastMessage(), separatorBuilder: (context, index) {
style: Theme.of(context).textTheme.headline4, 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: () => {}, onPressed: () => {},
tooltip: 'Increment', tooltip: 'Increment',
child: Icon(Icons.add), child: Icon(Icons.add),
), ),*/
); );
} }
Widget buildBody(BuildContext context, int index) {
return new Text(lastMessages[index]);
}
} }

View File

@ -136,7 +136,36 @@ class _HomePageState extends State<HomePage> {
], ],
), ),
), ),
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: () => { onPressed: () => {
Navigator.push( Navigator.push(
context, context,
@ -149,7 +178,7 @@ class _HomePageState extends State<HomePage> {
}, },
tooltip: 'Increment', tooltip: 'Increment',
child: Icon(Icons.add), child: Icon(Icons.add),
), ),*/
); );
} }
} }

View File

@ -6,6 +6,7 @@ import 'Models/homieContext.dart';
class AppContext with ChangeNotifier { class AppContext with ChangeNotifier {
HomieAppContext _homieContext; HomieAppContext _homieContext;
Client clientAPI; Client clientAPI;
List<String> _lastMQTTMessages = [];
AppContext(this._homieContext); AppContext(this._homieContext);
@ -15,4 +16,15 @@ class AppContext with ChangeNotifier {
notifyListeners(); notifyListeners();
} }
getLastMQTTMessages() => _lastMQTTMessages;
setLastMessage(String lastMessage) async {
_lastMQTTMessages.add(lastMessage);
if (_lastMQTTMessages.length > 10) {
_lastMQTTMessages.removeAt(0);
}
notifyListeners();
}
} }

View File

@ -63,7 +63,7 @@ packages:
name: cupertino_icons name: cupertino_icons
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.0.2" version: "1.0.4"
enum_to_string: enum_to_string:
dependency: "direct main" dependency: "direct main"
description: description:
@ -142,6 +142,13 @@ packages:
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.12.11" 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: meta:
dependency: transitive dependency: transitive
description: description:
@ -183,7 +190,7 @@ packages:
name: pedantic name: pedantic
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "1.11.0" version: "1.11.1"
provider: provider:
dependency: "direct main" dependency: "direct main"
description: description:
@ -216,14 +223,14 @@ packages:
name: sqflite name: sqflite
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.0+4" version: "2.0.2"
sqflite_common: sqflite_common:
dependency: transitive dependency: transitive
description: description:
name: sqflite_common name: sqflite_common
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "2.0.1+1" version: "2.2.0"
stack_trace: stack_trace:
dependency: transitive dependency: transitive
description: description:
@ -265,7 +272,7 @@ packages:
name: test_api name: test_api
url: "https://pub.dartlang.org" url: "https://pub.dartlang.org"
source: hosted source: hosted
version: "0.4.3" version: "0.4.8"
typed_data: typed_data:
dependency: transitive dependency: transitive
description: description:
@ -281,5 +288,5 @@ packages:
source: hosted source: hosted
version: "2.1.1" version: "2.1.1"
sdks: sdks:
dart: ">=2.12.0 <3.0.0" dart: ">=2.15.0 <3.0.0"
flutter: ">=1.16.0" flutter: ">=1.16.0"