code_widget.dart 6.9 KB

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