Commit a9d84e49 authored by Wilko Manger's avatar Wilko Manger

Add ability to logout

parent 8ce2e048
......@@ -55,5 +55,9 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
} else if (event is LoggedIn) {
yield Authenticated(event.user, fromStore: false);
}
if (event is LoggedOut) {
yield NotAuthenticated();
}
}
}
......@@ -16,3 +16,5 @@ class LoggedIn extends AuthEvent {
@override
List<Object> get props => [user];
}
class LoggedOut extends AuthEvent {}
......@@ -46,10 +46,10 @@ class Matrix {
MyUser _user;
MyUser get user => _user;
final Completer<void> _firstSyncCompleter = Completer();
var _firstSyncCompleter = Completer<void>();
Future<void> get firstSync => _firstSyncCompleter.future;
final Completer<void> _userAvailable = Completer();
var _userAvailable = Completer<void>();
Future<void> get userAvaible => _userAvailable.future;
var _chats = <RoomId, Chat>{};
......@@ -59,6 +59,20 @@ class Matrix {
_authBloc.listen(_processAuthState);
}
Future<void> logout() async {
// Reset completers
_userAvailable = Completer<void>();
_firstSyncCompleter = Completer<void>();
await _user.logout();
final temp = await getTemporaryDirectory();
final data = await getApplicationDocumentsDirectory();
await temp.delete(recursive: true);
await data.delete(recursive: true);
}
Future<void> _processAuthState(AuthState state) async {
if (state is Authenticated) {
_processUser(state.user);
......
......@@ -319,6 +319,11 @@
"type": "text",
"placeholders": {}
},
"Logout": "Logout",
"@Logout": {
"type": "text",
"placeholders": {}
},
"_Error_connectionLost": "Connection has been lost.\nMake sure your phone has an active internet connection.",
"@_Error_connectionLost": {
"type": "text",
......
......@@ -319,6 +319,11 @@
"type": "text",
"placeholders": {}
},
"Logout": "Uitloggen",
"@Logout": {
"type": "text",
"placeholders": {}
},
"_Error_connectionLost": "De verbinding is verloren.\nControleer of je telefoon een actieve internetverbinding heeft.",
"@_Error_connectionLost": {
"type": "text",
......
......@@ -773,6 +773,13 @@ class _Settings extends _Category {
locale: _localeName,
);
}
String get logoutButton {
return Intl.message(
'Logout',
locale: _localeName,
);
}
}
class _Error extends _Category {
......
......@@ -78,6 +78,7 @@ class MessageLookup extends MessageLookupByLibrary {
final messages = _notInlinedMessages(_notInlinedMessages);
static _notInlinedMessages(_) => <String, Function>{
"Logout": MessageLookupByLibrary.simpleMessage("Logout"),
"Participants": MessageLookupByLibrary.simpleMessage("Participants"),
"Profile": MessageLookupByLibrary.simpleMessage("Profile"),
"_ChatDetails_description":
......
......@@ -14,6 +14,7 @@
//
// You should have received a copy of the GNU Affero General Public License
// along with Pattle. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/material.dart';
import 'profile/tile.dart';
......@@ -23,21 +24,19 @@ import '../../../resources/theme.dart';
import '../../../app.dart';
class SettingsPage extends StatefulWidget {
SettingsPage({Key key});
import 'widgets/logout_button/widget.dart';
@override
State<StatefulWidget> createState() => _SettingsPageState();
}
class SettingsPage extends StatelessWidget {
SettingsPage({Key key});
class _SettingsPageState extends State<SettingsPage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(context.intl.settings.title),
),
body: ListView(
body: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
ProfileSettingTile.withBloc(),
Divider(height: 1),
......@@ -51,7 +50,14 @@ class _SettingsPageState extends State<SettingsPage> {
onTap: () => Navigator.of(context).pushNamed(
Routes.settingsAppearance,
),
)
),
Expanded(
child: Align(
alignment: Alignment.bottomCenter,
child: LogoutButton.withBloc(),
),
),
SizedBox(height: 24),
],
),
);
......
......@@ -100,7 +100,7 @@ class _ProfilePageState extends State<ProfilePage> {
)
],
),
)
),
],
),
);
......
// Copyright (C) 2019 Wilko Manger
//
// This file is part of Pattle.
//
// Pattle is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pattle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Pattle. If not, see <https://www.gnu.org/licenses/>.
import 'package:bloc/bloc.dart';
import '../../../../../auth/bloc.dart' as auth;
import '../../../../../matrix.dart';
import 'event.dart';
import 'state.dart';
export 'event.dart';
export 'state.dart';
class LogoutBloc extends Bloc<LogoutEvent, LogoutState> {
final Matrix _matrix;
final auth.AuthBloc _authBloc;
LogoutBloc(this._matrix, this._authBloc);
@override
LogoutState get initialState => LogoutState();
@override
Stream<LogoutState> mapEventToState(LogoutEvent event) async* {
if (event is Logout) {
yield LoggingOut();
await _matrix.logout();
_authBloc.add(auth.LoggedOut());
yield LoggedOut();
}
}
}
import 'package:equatable/equatable.dart';
abstract class LogoutEvent extends Equatable {
@override
List<Object> get props => [];
}
class Logout extends LogoutEvent {}
// Copyright (C) 2020 Wilko Manger
//
// This file is part of Pattle.
//
// Pattle is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pattle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Pattle. If not, see <https://www.gnu.org/licenses/>.
import 'package:meta/meta.dart';
@immutable
class LogoutState {}
class LoggingOut extends LogoutState {}
class LoggedOut extends LogoutState {}
// Copyright (C) 2019 Wilko Manger
//
// This file is part of Pattle.
//
// Pattle is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Pattle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with Pattle. If not, see <https://www.gnu.org/licenses/>.
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import '../../../../../auth/bloc.dart' hide LoggedOut;
import '../../../../../app.dart';
import '../../../../../matrix.dart';
import 'bloc.dart';
class LogoutButton extends StatelessWidget {
LogoutButton._({Key key});
static Widget withBloc() {
return BlocProvider<LogoutBloc>(
create: (context) => LogoutBloc(
Matrix.of(context),
context.bloc<AuthBloc>(),
),
child: LogoutButton._(),
);
}
void _onStateChange(BuildContext context, LogoutState state) {
if (state is LoggedOut) {
Navigator.pushNamedAndRemoveUntil(
context,
Routes.login,
(_) => false,
);
}
}
void _logout(BuildContext context) {
context.bloc<LogoutBloc>().add(Logout());
}
@override
Widget build(BuildContext context) {
return BlocConsumer<LogoutBloc, LogoutState>(
listener: _onStateChange,
builder: (context, state) {
final isLoggingOut = state is LoggingOut;
const iconSize = 24.0;
return FlatButton.icon(
onPressed: !isLoggingOut ? () => _logout(context) : null,
icon: AnimatedCrossFade(
duration: Duration(milliseconds: 300),
crossFadeState: !isLoggingOut
? CrossFadeState.showFirst
: CrossFadeState.showSecond,
firstChild: SizedBox.fromSize(
size: Size(iconSize, iconSize),
child: Icon(Icons.exit_to_app, size: iconSize),
),
secondChild: SizedBox.fromSize(
size: Size(iconSize, iconSize),
child: Padding(
padding: EdgeInsets.all(4),
child: Center(
child: CircularProgressIndicator(),
),
),
),
),
label: Text('Logout'.toUpperCase()),
);
},
);
}
}
......@@ -309,8 +309,8 @@ packages:
dependency: "direct main"
description:
path: "."
ref: "0318f6487c547061ee29ef8cfe56dd1e8494f98f"
resolved-ref: "0318f6487c547061ee29ef8cfe56dd1e8494f98f"
ref: b83a9df02dfa1e2933710eb71da530704096dd49
resolved-ref: b83a9df02dfa1e2933710eb71da530704096dd49
url: "https://git.pattle.im/pattle/library/matrix-dart-sdk.git"
source: git
version: "0.0.0"
......
......@@ -13,7 +13,7 @@ dependencies:
matrix_sdk:
git:
url: https://git.pattle.im/pattle/library/matrix-dart-sdk.git
ref: 0318f6487c547061ee29ef8cfe56dd1e8494f98f
ref: b83a9df02dfa1e2933710eb71da530704096dd49
async: ^2.3.0
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment