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