Fix logout crash

This commit is contained in:
Christian Pauly 2020-05-12 11:58:47 +02:00
parent e736d32322
commit bed0d821e3
2 changed files with 249 additions and 225 deletions

View file

@ -20,6 +20,7 @@ import '../utils/app_route.dart';
import '../utils/url_launcher.dart'; import '../utils/url_launcher.dart';
import '../utils/client_presence_extension.dart'; import '../utils/client_presence_extension.dart';
import 'archive.dart'; import 'archive.dart';
import 'homeserver_picker.dart';
import 'new_group.dart'; import 'new_group.dart';
import 'new_private_chat.dart'; import 'new_private_chat.dart';
import 'settings.dart'; import 'settings.dart';
@ -222,236 +223,263 @@ class _ChatListState extends State<ChatList> {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return StreamBuilder( return StreamBuilder<LoginState>(
stream: Matrix.of(context).onShareContentChanged.stream, stream: Matrix.of(context).client.onLoginStateChanged.stream,
builder: (context, snapshot) { builder: (context, snapshot) {
final selectMode = Matrix.of(context).shareContent == null if (snapshot.data == LoginState.loggedOut) {
? SelectMode.normal Timer(Duration(seconds: 1), () {
: SelectMode.share; Matrix.of(context).clean();
return Scaffold( Navigator.of(context).pushAndRemoveUntil(
drawer: selectMode == SelectMode.share AppRoute.defaultRoute(context, HomeserverPicker()),
? null (r) => false);
: Drawer( });
child: SafeArea( }
child: ListView( return StreamBuilder(
padding: EdgeInsets.zero, stream: Matrix.of(context).onShareContentChanged.stream,
children: <Widget>[ builder: (context, snapshot) {
ListTile( final selectMode = Matrix.of(context).shareContent == null
leading: Icon(Icons.edit), ? SelectMode.normal
title: Text(L10n.of(context).setStatus), : SelectMode.share;
onTap: () => _setStatus(context), return Scaffold(
), drawer: selectMode == SelectMode.share
Divider(height: 1), ? null
ListTile( : Drawer(
leading: Icon(Icons.people_outline), child: SafeArea(
title: Text(L10n.of(context).createNewGroup), child: ListView(
onTap: () => _drawerTapAction(NewGroupView()), padding: EdgeInsets.zero,
),
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<void>(
future: waitForFirstSync(context),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
List<Room> rooms =
List<Room>.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: <Widget>[ children: <Widget>[
Icon( ListTile(
searchMode leading: Icon(Icons.edit),
? Icons.search title: Text(L10n.of(context).setStatus),
: Icons.chat_bubble_outline, onTap: () => _setStatus(context),
size: 80, ),
color: Colors.grey, 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 = appBar: AppBar(
(publicRoomsResponse?.publicRooms?.length ?? 0); elevation: _scrolledToTop ? 0 : null,
final int totalCount = rooms.length + publicRoomsCount; leading: selectMode != SelectMode.share
return ListView.separated( ? null
controller: _scrollController, : IconButton(
separatorBuilder: (BuildContext context, int i) => icon: Icon(Icons.close),
i == totalCount - publicRoomsCount onPressed: () =>
? Material( Matrix.of(context).shareContent = null,
elevation: 2, ),
child: ListTile( titleSpacing: 0,
title: Text( title: selectMode == SelectMode.share
L10n.of(context).publicRooms), ? 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(), : Icon(Icons.search),
itemCount: totalCount + 1, contentPadding: EdgeInsets.all(9),
itemBuilder: (BuildContext context, int i) { border: InputBorder.none,
if (i == 0) { hintText: L10n.of(context).searchForAChat,
return (Matrix.of(context) ),
.client ),
.statusList ),
.isEmpty || ),
selectMode == SelectMode.share) floatingActionButton:
? Container() (AdaptivePageLayout.columnMode(context) ||
: PreferredSize( selectMode == SelectMode.share)
preferredSize: Size.fromHeight(89), ? null
child: Container( : SpeedDial(
height: 81, child: Icon(Icons.add),
child: ListView.builder( overlayColor: blackWhiteColor(context),
scrollDirection: Axis.horizontal, foregroundColor: Colors.white,
itemCount: Matrix.of(context) backgroundColor: Theme.of(context).primaryColor,
.client children: [
.statusList SpeedDialChild(
.length, child: Icon(Icons.people_outline),
itemBuilder: foregroundColor: Colors.white,
(BuildContext context, int i) => backgroundColor: Colors.blue,
PresenceListItem( label: L10n.of(context).createNewGroup,
Matrix.of(context) labelStyle: TextStyle(
.client fontSize: 18.0, color: Colors.black),
.statusList[i]), 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<void>(
future: waitForFirstSync(context),
builder: (BuildContext context, snapshot) {
if (snapshot.hasData) {
List<Room> rooms = List<Room>.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: <Widget>[
Icon(
searchMode
? Icons.search
: Icons.chat_bubble_outline,
size: 80,
color: Colors.grey,
),
Text(searchMode
? L10n.of(context).noRoomsFound
: L10n.of(context)
.startYourFirstChat),
],
),
);
} }
i--; final int publicRoomsCount =
return i < rooms.length (publicRoomsResponse?.publicRooms?.length ??
? ChatListItem( 0);
rooms[i], final int totalCount =
activeChat: rooms.length + publicRoomsCount;
widget.activeChat == rooms[i].id, return ListView.separated(
) controller: _scrollController,
: PublicRoomListItem(publicRoomsResponse separatorBuilder:
.publicRooms[i - rooms.length]); (BuildContext context, int i) =>
}); i == totalCount - publicRoomsCount
} else { ? Material(
return Center( elevation: 2,
child: CircularProgressIndicator(), 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(),
);
}
},
); );
} }),
}, );
); });
}),
);
}); });
} }
} }

View file

@ -2,7 +2,6 @@ import 'dart:io';
import 'package:famedlysdk/famedlysdk.dart'; import 'package:famedlysdk/famedlysdk.dart';
import 'package:fluffychat/components/settings_themes.dart'; import 'package:fluffychat/components/settings_themes.dart';
import 'package:fluffychat/views/homeserver_picker.dart';
import 'package:fluffychat/views/settings_devices.dart'; import 'package:fluffychat/views/settings_devices.dart';
import 'package:flutter/foundation.dart'; import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
@ -46,9 +45,6 @@ class _SettingsState extends State<Settings> {
MatrixState matrix = Matrix.of(context); MatrixState matrix = Matrix.of(context);
await SimpleDialogs(context) await SimpleDialogs(context)
.tryRequestWithLoadingDialog(matrix.client.logout()); .tryRequestWithLoadingDialog(matrix.client.logout());
matrix.clean();
await Navigator.of(context).pushAndRemoveUntil(
AppRoute.defaultRoute(context, HomeserverPicker()), (r) => false);
} }
void setJitsiInstanceAction(BuildContext context) async { void setJitsiInstanceAction(BuildContext context) async {