From bed0d821e3ca6a16283d891056d8ca85416b70b4 Mon Sep 17 00:00:00 2001 From: Christian Pauly Date: Tue, 12 May 2020 11:58:47 +0200 Subject: [PATCH] Fix logout crash --- lib/views/chat_list.dart | 470 +++++++++++++++++++++------------------ lib/views/settings.dart | 4 - 2 files changed, 249 insertions(+), 225 deletions(-) diff --git a/lib/views/chat_list.dart b/lib/views/chat_list.dart index 48503566..d6230798 100644 --- a/lib/views/chat_list.dart +++ b/lib/views/chat_list.dart @@ -20,6 +20,7 @@ import '../utils/app_route.dart'; import '../utils/url_launcher.dart'; import '../utils/client_presence_extension.dart'; import 'archive.dart'; +import 'homeserver_picker.dart'; import 'new_group.dart'; import 'new_private_chat.dart'; import 'settings.dart'; @@ -222,236 +223,263 @@ class _ChatListState extends State { @override Widget build(BuildContext context) { - return StreamBuilder( - stream: Matrix.of(context).onShareContentChanged.stream, + return StreamBuilder( + stream: Matrix.of(context).client.onLoginStateChanged.stream, builder: (context, snapshot) { - final selectMode = Matrix.of(context).shareContent == null - ? SelectMode.normal - : SelectMode.share; - return Scaffold( - drawer: selectMode == SelectMode.share - ? null - : Drawer( - child: SafeArea( - child: ListView( - padding: EdgeInsets.zero, - children: [ - ListTile( - leading: Icon(Icons.edit), - title: Text(L10n.of(context).setStatus), - onTap: () => _setStatus(context), - ), - Divider(height: 1), - ListTile( - leading: Icon(Icons.people_outline), - title: Text(L10n.of(context).createNewGroup), - onTap: () => _drawerTapAction(NewGroupView()), - ), - ListTile( - leading: Icon(Icons.person_add), - title: Text(L10n.of(context).newPrivateChat), - onTap: () => _drawerTapAction(NewPrivateChatView()), - ), - Divider(height: 1), - ListTile( - leading: Icon(Icons.archive), - title: Text(L10n.of(context).archive), - onTap: () => _drawerTapAction( - Archive(), - ), - ), - ListTile( - leading: Icon(Icons.settings), - title: Text(L10n.of(context).settings), - onTap: () => _drawerTapAction( - SettingsView(), - ), - ), - Divider(height: 1), - ListTile( - leading: Icon(Icons.share), - title: Text(L10n.of(context).inviteContact), - onTap: () { - Navigator.of(context).pop(); - Share.share(L10n.of(context).inviteText( - Matrix.of(context).client.userID, - "https://matrix.to/#/${Matrix.of(context).client.userID}")); - }, - ), - ], - ), - ), - ), - appBar: AppBar( - elevation: _scrolledToTop ? 0 : null, - leading: selectMode != SelectMode.share - ? null - : IconButton( - icon: Icon(Icons.close), - onPressed: () => Matrix.of(context).shareContent = null, - ), - titleSpacing: 0, - title: selectMode == SelectMode.share - ? Text(L10n.of(context).share) - : Container( - padding: EdgeInsets.all(8), - height: 42, - margin: EdgeInsets.only(right: 8), - decoration: BoxDecoration( - color: Theme.of(context).secondaryHeaderColor, - borderRadius: BorderRadius.circular(90), - ), - child: TextField( - autocorrect: false, - controller: searchController, - decoration: InputDecoration( - suffixIcon: loadingPublicRooms - ? Container( - alignment: Alignment.centerRight, - child: Container( - width: 20, - height: 20, - child: CircularProgressIndicator(), - ), - ) - : Icon(Icons.search), - contentPadding: EdgeInsets.all(9), - border: InputBorder.none, - hintText: L10n.of(context).searchForAChat, - ), - ), - ), - ), - floatingActionButton: (AdaptivePageLayout.columnMode(context) || - selectMode == SelectMode.share) - ? null - : SpeedDial( - child: Icon(Icons.add), - overlayColor: blackWhiteColor(context), - foregroundColor: Colors.white, - backgroundColor: Theme.of(context).primaryColor, - children: [ - SpeedDialChild( - child: Icon(Icons.people_outline), - foregroundColor: Colors.white, - backgroundColor: Colors.blue, - label: L10n.of(context).createNewGroup, - labelStyle: - TextStyle(fontSize: 18.0, color: Colors.black), - onTap: () => Navigator.of(context).pushAndRemoveUntil( - AppRoute.defaultRoute(context, NewGroupView()), - (r) => r.isFirst), - ), - SpeedDialChild( - child: Icon(Icons.person_add), - foregroundColor: Colors.white, - backgroundColor: Colors.green, - label: L10n.of(context).newPrivateChat, - labelStyle: - TextStyle(fontSize: 18.0, color: Colors.black), - onTap: () => Navigator.of(context).pushAndRemoveUntil( - AppRoute.defaultRoute( - context, NewPrivateChatView()), - (r) => r.isFirst), - ), - ], - ), - body: StreamBuilder( - stream: Matrix.of(context).client.onSync.stream, - builder: (context, snapshot) { - return FutureBuilder( - future: waitForFirstSync(context), - builder: (BuildContext context, snapshot) { - if (snapshot.hasData) { - List rooms = - List.from(Matrix.of(context).client.rooms); - rooms.removeWhere((Room room) => - searchMode && - !room.displayname.toLowerCase().contains( - searchController.text.toLowerCase() ?? "")); - if (rooms.isEmpty && - (!searchMode || publicRoomsResponse == null)) { - return Center( - child: Column( - mainAxisSize: MainAxisSize.min, + if (snapshot.data == LoginState.loggedOut) { + Timer(Duration(seconds: 1), () { + Matrix.of(context).clean(); + Navigator.of(context).pushAndRemoveUntil( + AppRoute.defaultRoute(context, HomeserverPicker()), + (r) => false); + }); + } + return StreamBuilder( + stream: Matrix.of(context).onShareContentChanged.stream, + builder: (context, snapshot) { + final selectMode = Matrix.of(context).shareContent == null + ? SelectMode.normal + : SelectMode.share; + return Scaffold( + drawer: selectMode == SelectMode.share + ? null + : Drawer( + child: SafeArea( + child: ListView( + padding: EdgeInsets.zero, children: [ - Icon( - searchMode - ? Icons.search - : Icons.chat_bubble_outline, - size: 80, - color: Colors.grey, + ListTile( + leading: Icon(Icons.edit), + title: Text(L10n.of(context).setStatus), + onTap: () => _setStatus(context), + ), + Divider(height: 1), + ListTile( + leading: Icon(Icons.people_outline), + title: Text(L10n.of(context).createNewGroup), + onTap: () => _drawerTapAction(NewGroupView()), + ), + ListTile( + leading: Icon(Icons.person_add), + title: Text(L10n.of(context).newPrivateChat), + onTap: () => + _drawerTapAction(NewPrivateChatView()), + ), + Divider(height: 1), + ListTile( + leading: Icon(Icons.archive), + title: Text(L10n.of(context).archive), + onTap: () => _drawerTapAction( + Archive(), + ), + ), + ListTile( + leading: Icon(Icons.settings), + title: Text(L10n.of(context).settings), + onTap: () => _drawerTapAction( + SettingsView(), + ), + ), + Divider(height: 1), + ListTile( + leading: Icon(Icons.share), + title: Text(L10n.of(context).inviteContact), + onTap: () { + Navigator.of(context).pop(); + Share.share(L10n.of(context).inviteText( + Matrix.of(context).client.userID, + "https://matrix.to/#/${Matrix.of(context).client.userID}")); + }, ), - Text(searchMode - ? L10n.of(context).noRoomsFound - : L10n.of(context).startYourFirstChat), ], ), - ); - } - final int publicRoomsCount = - (publicRoomsResponse?.publicRooms?.length ?? 0); - final int totalCount = rooms.length + publicRoomsCount; - return ListView.separated( - controller: _scrollController, - separatorBuilder: (BuildContext context, int i) => - i == totalCount - publicRoomsCount - ? Material( - elevation: 2, - child: ListTile( - title: Text( - L10n.of(context).publicRooms), + ), + ), + appBar: AppBar( + elevation: _scrolledToTop ? 0 : null, + leading: selectMode != SelectMode.share + ? null + : IconButton( + icon: Icon(Icons.close), + onPressed: () => + Matrix.of(context).shareContent = null, + ), + titleSpacing: 0, + title: selectMode == SelectMode.share + ? Text(L10n.of(context).share) + : Container( + padding: EdgeInsets.all(8), + height: 42, + margin: EdgeInsets.only(right: 8), + decoration: BoxDecoration( + color: Theme.of(context).secondaryHeaderColor, + borderRadius: BorderRadius.circular(90), + ), + child: TextField( + autocorrect: false, + controller: searchController, + decoration: InputDecoration( + suffixIcon: loadingPublicRooms + ? Container( + alignment: Alignment.centerRight, + child: Container( + width: 20, + height: 20, + child: CircularProgressIndicator(), ), ) - : Container(), - itemCount: totalCount + 1, - itemBuilder: (BuildContext context, int i) { - if (i == 0) { - return (Matrix.of(context) - .client - .statusList - .isEmpty || - selectMode == SelectMode.share) - ? Container() - : PreferredSize( - preferredSize: Size.fromHeight(89), - child: Container( - height: 81, - child: ListView.builder( - scrollDirection: Axis.horizontal, - itemCount: Matrix.of(context) - .client - .statusList - .length, - itemBuilder: - (BuildContext context, int i) => - PresenceListItem( - Matrix.of(context) - .client - .statusList[i]), - ), - ), - ); + : Icon(Icons.search), + contentPadding: EdgeInsets.all(9), + border: InputBorder.none, + hintText: L10n.of(context).searchForAChat, + ), + ), + ), + ), + floatingActionButton: + (AdaptivePageLayout.columnMode(context) || + selectMode == SelectMode.share) + ? null + : SpeedDial( + child: Icon(Icons.add), + overlayColor: blackWhiteColor(context), + foregroundColor: Colors.white, + backgroundColor: Theme.of(context).primaryColor, + children: [ + SpeedDialChild( + child: Icon(Icons.people_outline), + foregroundColor: Colors.white, + backgroundColor: Colors.blue, + label: L10n.of(context).createNewGroup, + labelStyle: TextStyle( + fontSize: 18.0, color: Colors.black), + onTap: () => Navigator.of(context) + .pushAndRemoveUntil( + AppRoute.defaultRoute( + context, NewGroupView()), + (r) => r.isFirst), + ), + SpeedDialChild( + child: Icon(Icons.person_add), + foregroundColor: Colors.white, + backgroundColor: Colors.green, + label: L10n.of(context).newPrivateChat, + labelStyle: TextStyle( + fontSize: 18.0, color: Colors.black), + onTap: () => Navigator.of(context) + .pushAndRemoveUntil( + AppRoute.defaultRoute( + context, NewPrivateChatView()), + (r) => r.isFirst), + ), + ], + ), + body: StreamBuilder( + stream: Matrix.of(context).client.onSync.stream, + builder: (context, snapshot) { + return FutureBuilder( + future: waitForFirstSync(context), + builder: (BuildContext context, snapshot) { + if (snapshot.hasData) { + List rooms = List.from( + Matrix.of(context).client.rooms); + rooms.removeWhere((Room room) => + searchMode && + !room.displayname.toLowerCase().contains( + searchController.text.toLowerCase() ?? + "")); + if (rooms.isEmpty && + (!searchMode || + publicRoomsResponse == null)) { + return Center( + child: Column( + mainAxisSize: MainAxisSize.min, + children: [ + Icon( + searchMode + ? Icons.search + : Icons.chat_bubble_outline, + size: 80, + color: Colors.grey, + ), + Text(searchMode + ? L10n.of(context).noRoomsFound + : L10n.of(context) + .startYourFirstChat), + ], + ), + ); } - i--; - return i < rooms.length - ? ChatListItem( - rooms[i], - activeChat: - widget.activeChat == rooms[i].id, - ) - : PublicRoomListItem(publicRoomsResponse - .publicRooms[i - rooms.length]); - }); - } else { - return Center( - child: CircularProgressIndicator(), + final int publicRoomsCount = + (publicRoomsResponse?.publicRooms?.length ?? + 0); + final int totalCount = + rooms.length + publicRoomsCount; + return ListView.separated( + controller: _scrollController, + separatorBuilder: + (BuildContext context, int i) => + i == totalCount - publicRoomsCount + ? Material( + elevation: 2, + child: ListTile( + title: Text(L10n.of(context) + .publicRooms), + ), + ) + : Container(), + itemCount: totalCount + 1, + itemBuilder: (BuildContext context, int i) { + if (i == 0) { + return (Matrix.of(context) + .client + .statusList + .isEmpty || + selectMode == SelectMode.share) + ? Container() + : PreferredSize( + preferredSize: + Size.fromHeight(89), + child: Container( + height: 81, + child: ListView.builder( + scrollDirection: + Axis.horizontal, + itemCount: Matrix.of(context) + .client + .statusList + .length, + itemBuilder: (BuildContext + context, + int i) => + PresenceListItem( + Matrix.of(context) + .client + .statusList[i]), + ), + ), + ); + } + i--; + return i < rooms.length + ? ChatListItem( + rooms[i], + activeChat: widget.activeChat == + rooms[i].id, + ) + : PublicRoomListItem(publicRoomsResponse + .publicRooms[i - rooms.length]); + }); + } else { + return Center( + child: CircularProgressIndicator(), + ); + } + }, ); - } - }, - ); - }), - ); + }), + ); + }); }); } } diff --git a/lib/views/settings.dart b/lib/views/settings.dart index 014bdc04..fcba2579 100644 --- a/lib/views/settings.dart +++ b/lib/views/settings.dart @@ -2,7 +2,6 @@ import 'dart:io'; import 'package:famedlysdk/famedlysdk.dart'; import 'package:fluffychat/components/settings_themes.dart'; -import 'package:fluffychat/views/homeserver_picker.dart'; import 'package:fluffychat/views/settings_devices.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; @@ -46,9 +45,6 @@ class _SettingsState extends State { MatrixState matrix = Matrix.of(context); await SimpleDialogs(context) .tryRequestWithLoadingDialog(matrix.client.logout()); - matrix.clean(); - await Navigator.of(context).pushAndRemoveUntil( - AppRoute.defaultRoute(context, HomeserverPicker()), (r) => false); } void setJitsiInstanceAction(BuildContext context) async {