sessions_page.dart 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/widgets.dart';
  3. import 'package:photos/core/configuration.dart';
  4. import 'package:photos/models/sessions.dart';
  5. import 'package:photos/services/user_service.dart';
  6. import 'package:photos/ui/loading_widget.dart';
  7. import 'package:photos/utils/date_time_util.dart';
  8. import 'package:photos/utils/dialog_util.dart';
  9. class SessionsPage extends StatefulWidget {
  10. SessionsPage({Key key}) : super(key: key);
  11. @override
  12. _SessionsPageState createState() => _SessionsPageState();
  13. }
  14. class _SessionsPageState extends State<SessionsPage> {
  15. Sessions _sessions;
  16. @override
  17. void initState() {
  18. _fetchActiveSessions();
  19. super.initState();
  20. }
  21. @override
  22. Widget build(BuildContext context) {
  23. return Scaffold(
  24. appBar: AppBar(
  25. title: Text("active sessions"),
  26. ),
  27. body: _getBody(),
  28. );
  29. }
  30. Widget _getBody() {
  31. if (_sessions == null) {
  32. return Center(child: loadWidget);
  33. }
  34. List<Widget> rows = [];
  35. for (final session in _sessions.sessions) {
  36. rows.add(_getSessionWidget(session));
  37. }
  38. return SingleChildScrollView(
  39. child: Column(
  40. children: rows,
  41. ),
  42. );
  43. }
  44. Widget _getSessionWidget(Session session) {
  45. final lastUsedTime =
  46. DateTime.fromMicrosecondsSinceEpoch(session.lastUsedTime);
  47. return Column(
  48. children: [
  49. InkWell(
  50. onTap: () async {
  51. _showSessionTerminationDialog(session);
  52. },
  53. child: Padding(
  54. padding: const EdgeInsets.all(16),
  55. child: Column(
  56. crossAxisAlignment: CrossAxisAlignment.start,
  57. children: [
  58. _getUAWidget(session),
  59. Padding(padding: EdgeInsets.all(4)),
  60. Row(
  61. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  62. children: [
  63. Flexible(
  64. child: Text(
  65. session.ip,
  66. style: TextStyle(
  67. color: Colors.white.withOpacity(0.8),
  68. fontSize: 14,
  69. ),
  70. ),
  71. ),
  72. Padding(padding: EdgeInsets.all(8)),
  73. Flexible(
  74. child: Text(
  75. getFormattedTime(lastUsedTime),
  76. style: TextStyle(
  77. color: Colors.white.withOpacity(0.8),
  78. fontSize: 12,
  79. ),
  80. ),
  81. ),
  82. ],
  83. ),
  84. ],
  85. ),
  86. ),
  87. ),
  88. Divider(),
  89. ],
  90. );
  91. }
  92. Future<void> _terminateSession(Session session) async {
  93. final dialog = createProgressDialog(context, "please wait...");
  94. await dialog.show();
  95. await UserService.instance.terminateSession(session.token);
  96. await _fetchActiveSessions();
  97. await dialog.hide();
  98. }
  99. Future<void> _fetchActiveSessions() async {
  100. _sessions = await UserService.instance.getActiveSessions();
  101. _sessions.sessions.sort((first, second) {
  102. return second.lastUsedTime.compareTo(first.lastUsedTime);
  103. });
  104. setState(() {});
  105. }
  106. void _showSessionTerminationDialog(Session session) {
  107. final isLoggingOutFromThisDevice =
  108. session.token == Configuration.instance.getToken();
  109. Widget text;
  110. if (isLoggingOutFromThisDevice) {
  111. text = Text(
  112. "this will log you out of this device!",
  113. );
  114. } else {
  115. text = SingleChildScrollView(
  116. child: Column(
  117. children: [
  118. Text(
  119. "this will log you out of the following device:",
  120. ),
  121. Padding(padding: EdgeInsets.all(8)),
  122. Text(
  123. session.ua,
  124. style: TextStyle(
  125. color: Colors.white.withOpacity(0.7),
  126. fontSize: 14,
  127. ),
  128. ),
  129. ],
  130. ),
  131. );
  132. }
  133. AlertDialog alert = AlertDialog(
  134. title: Text("terminate session?"),
  135. content: text,
  136. actions: [
  137. TextButton(
  138. child: Text(
  139. "terminate",
  140. style: TextStyle(
  141. color: Colors.red,
  142. ),
  143. ),
  144. onPressed: () async {
  145. Navigator.of(context, rootNavigator: true).pop('dialog');
  146. if (isLoggingOutFromThisDevice) {
  147. await UserService.instance.logout(context);
  148. } else {
  149. _terminateSession(session);
  150. }
  151. },
  152. ),
  153. TextButton(
  154. child: Text(
  155. "cancel",
  156. style: TextStyle(
  157. color: isLoggingOutFromThisDevice
  158. ? Theme.of(context).buttonColor
  159. : Colors.white,
  160. ),
  161. ),
  162. onPressed: () {
  163. Navigator.of(context, rootNavigator: true).pop('dialog');
  164. },
  165. ),
  166. ],
  167. );
  168. showDialog(
  169. context: context,
  170. builder: (BuildContext context) {
  171. return alert;
  172. },
  173. );
  174. }
  175. Widget _getUAWidget(Session session) {
  176. if (session.token == Configuration.instance.getToken()) {
  177. return Text(
  178. "this device",
  179. style: TextStyle(
  180. fontWeight: FontWeight.bold,
  181. color: Theme.of(context).buttonColor,
  182. ),
  183. );
  184. }
  185. return Text(session.prettyUA);
  186. }
  187. }