ente/lib/ui/sessions_page.dart

221 lines
6.1 KiB
Dart
Raw Normal View History

import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:logging/logging.dart';
2021-11-23 20:05:42 +00:00
import 'package:photos/core/configuration.dart';
import 'package:photos/ente_theme_data.dart';
import 'package:photos/models/sessions.dart';
import 'package:photos/services/user_service.dart';
import 'package:photos/ui/loading_widget.dart';
import 'package:photos/utils/date_time_util.dart';
2021-11-23 19:54:47 +00:00
import 'package:photos/utils/dialog_util.dart';
import 'package:photos/utils/toast_util.dart';
class SessionsPage extends StatefulWidget {
SessionsPage({Key key}) : super(key: key);
@override
_SessionsPageState createState() => _SessionsPageState();
}
class _SessionsPageState extends State<SessionsPage> {
Sessions _sessions;
final Logger _logger = Logger("SessionsPageState");
@override
void initState() {
_fetchActiveSessions();
super.initState();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
elevation: 0,
title: Text("Active sessions"),
),
body: _getBody(),
);
}
Widget _getBody() {
if (_sessions == null) {
return Center(child: loadWidget);
}
List<Widget> rows = [];
2021-11-26 04:48:04 +00:00
rows.add(Padding(padding: const EdgeInsets.all(4)));
for (final session in _sessions.sessions) {
rows.add(_getSessionWidget(session));
}
2021-11-24 05:40:07 +00:00
return SingleChildScrollView(
child: Column(
children: rows,
),
);
}
Widget _getSessionWidget(Session session) {
final lastUsedTime =
DateTime.fromMicrosecondsSinceEpoch(session.lastUsedTime);
2021-11-23 20:10:51 +00:00
return Column(
children: [
InkWell(
onTap: () async {
_showSessionTerminationDialog(session);
},
child: Padding(
padding: const EdgeInsets.all(16),
2021-11-24 05:40:07 +00:00
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
2021-11-23 19:54:47 +00:00
children: [
2021-11-24 05:40:07 +00:00
_getUAWidget(session),
Padding(padding: EdgeInsets.all(4)),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
2021-11-23 19:54:47 +00:00
children: [
2021-11-24 11:31:08 +00:00
Flexible(
child: Text(
session.ip,
style: TextStyle(
color: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.8),
2021-11-24 11:31:08 +00:00
fontSize: 14,
),
2021-11-24 05:40:07 +00:00
),
),
2021-11-24 11:31:08 +00:00
Padding(padding: EdgeInsets.all(8)),
Flexible(
child: Text(
getFormattedTime(lastUsedTime),
style: TextStyle(
color: Theme.of(context)
.colorScheme
.onSurface
.withOpacity(0.8),
2021-11-24 11:31:08 +00:00
fontSize: 12,
),
2021-11-23 19:54:47 +00:00
),
),
2021-11-23 19:54:47 +00:00
],
),
],
),
),
2021-11-23 20:10:51 +00:00
),
Divider(),
],
);
}
2021-11-23 19:54:47 +00:00
Future<void> _terminateSession(Session session) async {
2022-05-17 11:38:21 +00:00
final dialog = createProgressDialog(context, "Please wait...");
2021-11-23 19:54:47 +00:00
await dialog.show();
try {
await UserService.instance.terminateSession(session.token);
await _fetchActiveSessions();
await dialog.hide();
} catch (e, s) {
await dialog.hide();
_logger.severe('failed to terminate', e, s);
showErrorDialog(
context, 'Oops', "Something went wrong, please try again",);
}
2021-11-23 19:54:47 +00:00
}
Future<void> _fetchActiveSessions() async {
_sessions = await UserService.instance
.getActiveSessions()
.onError((error, stackTrace) {
2022-06-10 14:29:56 +00:00
showToast(context, "Failed to fetch active sessions");
throw error;
});
2021-11-23 20:05:42 +00:00
_sessions.sessions.sort((first, second) {
return second.lastUsedTime.compareTo(first.lastUsedTime);
});
setState(() {});
}
2021-11-23 19:54:47 +00:00
void _showSessionTerminationDialog(Session session) {
2021-11-23 20:05:42 +00:00
final isLoggingOutFromThisDevice =
session.token == Configuration.instance.getToken();
Widget text;
if (isLoggingOutFromThisDevice) {
text = Text(
2022-05-30 01:25:22 +00:00
"This will log you out of this device!",
2021-11-23 20:05:42 +00:00
);
} else {
text = SingleChildScrollView(
child: Column(
children: [
Text(
2022-05-30 01:25:22 +00:00
"This will log you out of the following device:",
2021-11-23 20:05:42 +00:00
),
Padding(padding: EdgeInsets.all(8)),
Text(
2021-11-24 05:40:07 +00:00
session.ua,
style: Theme.of(context).textTheme.caption,
2021-11-23 20:05:42 +00:00
),
],
),
);
}
2021-11-23 19:54:47 +00:00
AlertDialog alert = AlertDialog(
2022-05-30 01:25:22 +00:00
title: Text("Terminate session?"),
2021-11-23 20:05:42 +00:00
content: text,
2021-11-23 19:54:47 +00:00
actions: [
TextButton(
child: Text(
2022-05-30 01:25:22 +00:00
"Terminate",
2021-11-23 19:54:47 +00:00
style: TextStyle(
color: Colors.red,
),
),
onPressed: () async {
Navigator.of(context, rootNavigator: true).pop('dialog');
2021-11-23 20:05:42 +00:00
if (isLoggingOutFromThisDevice) {
await UserService.instance.logout(context);
} else {
_terminateSession(session);
}
2021-11-23 19:54:47 +00:00
},
),
TextButton(
child: Text(
2022-05-30 01:25:22 +00:00
"Cancel",
2021-11-23 19:54:47 +00:00
style: TextStyle(
2021-11-23 20:05:42 +00:00
color: isLoggingOutFromThisDevice
? Theme.of(context).buttonColor
: Theme.of(context).colorScheme.defaultTextColor,
2021-11-23 19:54:47 +00:00
),
),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop('dialog');
},
),
],
);
showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
2021-11-23 20:05:42 +00:00
Widget _getUAWidget(Session session) {
if (session.token == Configuration.instance.getToken()) {
return Text(
2022-05-30 01:25:22 +00:00
"This device",
2021-11-23 20:05:42 +00:00
style: TextStyle(
fontWeight: FontWeight.bold,
color: Theme.of(context).buttonColor,
),
);
}
2021-11-24 05:40:07 +00:00
return Text(session.prettyUA);
}
}