code_widget.dart 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import 'dart:async';
  2. import 'package:clipboard/clipboard.dart';
  3. import 'package:ente_auth/models/code.dart';
  4. import 'package:ente_auth/store/code_store.dart';
  5. import 'package:ente_auth/utils/toast_util.dart';
  6. import 'package:ente_auth/utils/totp_util.dart';
  7. import 'package:flutter/material.dart';
  8. import 'package:flutter_animation_progress_bar/flutter_animation_progress_bar.dart';
  9. // import 'package:flutter_animation_progress_bar/flutter_animation_progress_bar.dart';
  10. import 'package:flutter_slidable/flutter_slidable.dart';
  11. class CodeWidget extends StatefulWidget {
  12. final Code code;
  13. const CodeWidget(this.code, {Key? key}) : super(key: key);
  14. @override
  15. State<CodeWidget> createState() => _CodeWidgetState();
  16. }
  17. class _CodeWidgetState extends State<CodeWidget> {
  18. Timer? _everySecondTimer;
  19. int _timeRemaining = 30;
  20. @override
  21. void initState() {
  22. super.initState();
  23. _updateTimeRemaining();
  24. _everySecondTimer =
  25. Timer.periodic(const Duration(milliseconds: 200), (Timer t) {
  26. setState(() {
  27. _updateTimeRemaining();
  28. });
  29. });
  30. }
  31. void _updateTimeRemaining() {
  32. _timeRemaining =
  33. widget.code.period - (DateTime.now().second % widget.code.period);
  34. }
  35. @override
  36. void dispose() {
  37. _everySecondTimer?.cancel();
  38. super.dispose();
  39. }
  40. @override
  41. Widget build(BuildContext context) {
  42. return Slidable(
  43. key: ValueKey(widget.code.hashCode),
  44. endActionPane: ActionPane(
  45. motion: const ScrollMotion(),
  46. children: [
  47. SlidableAction(
  48. onPressed: _onDeletePressed,
  49. backgroundColor: Colors.grey.withOpacity(0.1),
  50. borderRadius: const BorderRadius.all(Radius.circular(12.0)),
  51. foregroundColor: const Color(0xFFFE4A49),
  52. icon: Icons.delete,
  53. label: 'Delete',
  54. ),
  55. ],
  56. ),
  57. child: InkWell(
  58. onTap: () {
  59. FlutterClipboard.copy(_getTotp())
  60. .then((value) => showToast(context, "Copied to clipboard"));
  61. },
  62. child: SizedBox(
  63. child: Column(
  64. crossAxisAlignment: CrossAxisAlignment.start,
  65. mainAxisAlignment: MainAxisAlignment.center,
  66. children: [
  67. FAProgressBar(
  68. currentValue: _timeRemaining / widget.code.period * 100,
  69. size: 4,
  70. animatedDuration: const Duration(milliseconds: 200),
  71. progressColor: Colors.orange,
  72. changeColorValue: 40,
  73. changeProgressColor: Colors.green,
  74. ),
  75. const SizedBox(
  76. height: 10,
  77. ),
  78. Padding(
  79. padding: const EdgeInsets.only(left: 16, right: 16),
  80. child: Text(
  81. Uri.decodeFull(widget.code.issuer),
  82. style: Theme.of(context).textTheme.headline6,
  83. ),
  84. ),
  85. Container(
  86. padding: const EdgeInsets.only(right: 16),
  87. child: Row(
  88. mainAxisAlignment: MainAxisAlignment.end,
  89. children: [
  90. Text(
  91. "next",
  92. style: Theme.of(context).textTheme.caption,
  93. ),
  94. ],
  95. ),
  96. ),
  97. Container(
  98. padding: const EdgeInsets.only(left: 16, right: 16),
  99. child: Row(
  100. mainAxisAlignment: MainAxisAlignment.start,
  101. children: [
  102. Expanded(
  103. child: Text(
  104. _getTotp(),
  105. style: const TextStyle(fontSize: 24),
  106. ),
  107. ),
  108. Text(
  109. _getNextTotp(),
  110. style: const TextStyle(
  111. fontSize: 24,
  112. color: Colors.grey,
  113. ),
  114. ),
  115. ],
  116. ),
  117. ),
  118. const SizedBox(
  119. height: 32,
  120. ),
  121. ],
  122. ),
  123. ),
  124. ),
  125. );
  126. }
  127. void _onDeletePressed(_) {
  128. final AlertDialog alert = AlertDialog(
  129. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
  130. title: Text(
  131. "Delete code?",
  132. style: Theme.of(context).textTheme.headline6,
  133. ),
  134. content: const Text(
  135. "Are you sure you want to delete this code? This action is irreversible.",
  136. ),
  137. actions: [
  138. TextButton(
  139. child: const Text(
  140. "Delete",
  141. style: TextStyle(
  142. color: Colors.red,
  143. ),
  144. ),
  145. onPressed: () {
  146. CodeStore.instance.removeCode(widget.code);
  147. Navigator.of(context, rootNavigator: true).pop('dialog');
  148. },
  149. ),
  150. TextButton(
  151. child: Text(
  152. "Cancel",
  153. style: TextStyle(
  154. color: Theme.of(context).colorScheme.onSurface,
  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. barrierColor: Colors.black12,
  169. );
  170. }
  171. String _getTotp() {
  172. try {
  173. return getTotp(widget.code);
  174. } catch (e) {
  175. return "Error";
  176. }
  177. }
  178. String _getNextTotp() {
  179. try {
  180. return getNextTotp(widget.code);
  181. } catch (e) {
  182. return "Error";
  183. }
  184. }
  185. Color _getProgressColor() {
  186. final progress = _timeRemaining / widget.code.period;
  187. if (progress > 0.6) {
  188. return Colors.green;
  189. } else if (progress > 0.4) {
  190. return Colors.yellow;
  191. } else if (progress > 2) {
  192. return Colors.orange;
  193. }
  194. return Colors.red;
  195. }
  196. }