fix: make the moxxmpp example work again

Note: to do this, I could not use the ExampleTcpSocketWrapper.
If I did, the app crashed on launch.

I also added some functionality: the header bar turns green when
connected, the FAB says what it does, and you can disconnect.

Signed-off-by: Blake Leonard <me@blakes.dev>
This commit is contained in:
Blake Leonard 2023-03-08 13:02:43 -05:00
parent 1000e0756b
commit 7b215d5c6e
No known key found for this signature in database
GPG Key ID: C1C57FDCC13B90D0

View File

@ -55,21 +55,25 @@ class MyHomePage extends StatefulWidget {
class _MyHomePageState extends State<MyHomePage> {
final XmppConnection connection = XmppConnection(
ExponentialBackoffReconnectionPolicy(),
ExampleTcpSocketWrapper(),
RandomBackoffReconnectionPolicy(1, 60),
AlwaysConnectedConnectivityManager(),
//ExampleTcpSocketWrapper(), // this causes the app to crash
TCPSocketWrapper(true), // Note: you probably want this to be false in a real app
);
TextEditingController jidController = TextEditingController();
TextEditingController passwordController = TextEditingController();
bool connected = false;
bool loading = false;
_MyHomePageState() : super() {
connection
..registerManagers([
StreamManagementManager(),
DiscoManager(),
RosterManager(),
DiscoManager([]),
RosterManager(TestingRosterStateManager("", [])),
PingManager(),
MessageManager(),
PresenceManager('http://moxxmpp.example'),
PresenceManager(),
])
..registerFeatureNegotiators([
ResourceBindingNegotiator(),
@ -85,15 +89,24 @@ class _MyHomePageState extends State<MyHomePage> {
}
Future<void> _buttonPressed() async {
if (connected) {
await connection.disconnect();
setState(() {connected = false;});
return;
}
setState(() {loading = true;});
connection.setConnectionSettings(
ConnectionSettings(
jid: JID.fromString(jidController.text),
password: passwordController.text,
useDirectTLS: true,
allowPlainAuth: false,
// otherwise, connecting to some servers will
// cause an app to hang
),
);
await connection.connect();
setState(() {connected = true; loading = false;});
}
@override
@ -101,18 +114,22 @@ class _MyHomePageState extends State<MyHomePage> {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
backgroundColor: connected ? Colors.green : Colors.deepPurple[800],
foregroundColor: connected ? Colors.black : Colors.white,
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
TextField(
enabled: !loading,
controller: jidController,
decoration: InputDecoration(
labelText: 'JID',
),
),
TextField(
enabled: !loading,
controller: passwordController,
decoration: InputDecoration(
labelText: 'Password',
@ -122,10 +139,13 @@ class _MyHomePageState extends State<MyHomePage> {
],
),
),
floatingActionButton: FloatingActionButton(
floatingActionButton: FloatingActionButton.extended(
onPressed: _buttonPressed,
label: Text(connected ? 'Disconnect' : 'Connect'),
backgroundColor: Colors.blue,
foregroundColor: Colors.white,
tooltip: 'Connect',
child: const Icon(Icons.add),
icon: const Icon(Icons.power),
),
);
}