code_widget.dart 5.8 KB

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