diff --git a/example/lib/main.dart b/example/lib/main.dart index 8107c6f..710257c 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -55,21 +55,25 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { 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 { } Future _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 { 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: [ 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 { ], ), ), - 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), ), ); }