sessions_page.dart 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. Text(
  64. session.ip,
  65. style: TextStyle(
  66. color: Colors.white.withOpacity(0.8),
  67. fontSize: 14,
  68. ),
  69. ),
  70. Text(
  71. getFormattedTime(lastUsedTime),
  72. style: TextStyle(
  73. color: Colors.white.withOpacity(0.8),
  74. fontSize: 12,
  75. ),
  76. ),
  77. ],
  78. ),
  79. ],
  80. ),
  81. ),
  82. ),
  83. Divider(),
  84. ],
  85. );
  86. }
  87. Future<void> _terminateSession(Session session) async {
  88. final dialog = createProgressDialog(context, "please wait...");
  89. await dialog.show();
  90. await UserService.instance.terminateSession(session.token);
  91. await _fetchActiveSessions();
  92. await dialog.hide();
  93. }
  94. Future<void> _fetchActiveSessions() async {
  95. _sessions = await UserService.instance.getActiveSessions();
  96. _sessions.sessions.sort((first, second) {
  97. return second.lastUsedTime.compareTo(first.lastUsedTime);
  98. });
  99. setState(() {});
  100. }
  101. void _showSessionTerminationDialog(Session session) {
  102. final isLoggingOutFromThisDevice =
  103. session.token == Configuration.instance.getToken();
  104. Widget text;
  105. if (isLoggingOutFromThisDevice) {
  106. text = Text(
  107. "this will log you out of this device!",
  108. );
  109. } else {
  110. text = SingleChildScrollView(
  111. child: Column(
  112. children: [
  113. Text(
  114. "this will log you out of the following device:",
  115. ),
  116. Padding(padding: EdgeInsets.all(8)),
  117. Text(
  118. session.ua,
  119. style: TextStyle(
  120. color: Colors.white.withOpacity(0.7),
  121. fontSize: 14,
  122. ),
  123. ),
  124. ],
  125. ),
  126. );
  127. }
  128. AlertDialog alert = AlertDialog(
  129. title: Text("terminate session?"),
  130. content: text,
  131. actions: [
  132. TextButton(
  133. child: Text(
  134. "terminate",
  135. style: TextStyle(
  136. color: Colors.red,
  137. ),
  138. ),
  139. onPressed: () async {
  140. Navigator.of(context, rootNavigator: true).pop('dialog');
  141. if (isLoggingOutFromThisDevice) {
  142. await UserService.instance.logout(context);
  143. } else {
  144. _terminateSession(session);
  145. }
  146. },
  147. ),
  148. TextButton(
  149. child: Text(
  150. "cancel",
  151. style: TextStyle(
  152. color: isLoggingOutFromThisDevice
  153. ? Theme.of(context).buttonColor
  154. : Colors.white,
  155. ),
  156. ),
  157. onPressed: () {
  158. Navigator.of(context, rootNavigator: true).pop('dialog');
  159. },
  160. ),
  161. ],
  162. );
  163. showDialog(
  164. context: context,
  165. builder: (BuildContext context) {
  166. return alert;
  167. },
  168. );
  169. }
  170. Widget _getUAWidget(Session session) {
  171. if (session.token == Configuration.instance.getToken()) {
  172. return Text(
  173. "this device",
  174. style: TextStyle(
  175. fontWeight: FontWeight.bold,
  176. color: Theme.of(context).buttonColor,
  177. ),
  178. );
  179. }
  180. return Text(session.prettyUA);
  181. }
  182. }