code_widget.dart 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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. _copyToClipboard();
  74. },
  75. onLongPress: () {
  76. _copyToClipboard();
  77. },
  78. child: SizedBox(
  79. child: Column(
  80. crossAxisAlignment: CrossAxisAlignment.start,
  81. mainAxisAlignment: MainAxisAlignment.center,
  82. children: [
  83. FAProgressBar(
  84. currentValue:
  85. _timeRemaining / widget.code.period * 100,
  86. size: 4,
  87. animatedDuration: const Duration(milliseconds: 200),
  88. progressColor: Colors.orange,
  89. changeColorValue: 40,
  90. changeProgressColor: Colors.green,
  91. ),
  92. const SizedBox(
  93. height: 16,
  94. ),
  95. Padding(
  96. padding: const EdgeInsets.only(left: 16, right: 16),
  97. child: Text(
  98. Uri.decodeFull(widget.code.issuer),
  99. style: Theme.of(context).textTheme.headline6,
  100. ),
  101. ),
  102. Container(
  103. padding: const EdgeInsets.only(right: 16),
  104. child: Row(
  105. mainAxisAlignment: MainAxisAlignment.end,
  106. children: [
  107. Text(
  108. "next",
  109. style: Theme.of(context).textTheme.caption,
  110. ),
  111. ],
  112. ),
  113. ),
  114. Container(
  115. padding: const EdgeInsets.only(left: 16, right: 16),
  116. child: Row(
  117. mainAxisAlignment: MainAxisAlignment.start,
  118. children: [
  119. Expanded(
  120. child: Text(
  121. _getTotp(),
  122. style: const TextStyle(fontSize: 24),
  123. ),
  124. ),
  125. Text(
  126. _getNextTotp(),
  127. style: const TextStyle(
  128. fontSize: 24,
  129. color: Colors.grey,
  130. ),
  131. ),
  132. ],
  133. ),
  134. ),
  135. const SizedBox(
  136. height: 20,
  137. ),
  138. ],
  139. ),
  140. ),
  141. ),
  142. ),
  143. ),
  144. ),
  145. ),
  146. ),
  147. );
  148. }
  149. void _copyToClipboard() {
  150. FlutterClipboard.copy(_getTotp()).then(
  151. (value) => showToast(context, "Copied to clipboard"),
  152. );
  153. }
  154. void _onDeletePressed(_) {
  155. final AlertDialog alert = AlertDialog(
  156. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
  157. title: Text(
  158. "Delete code?",
  159. style: Theme.of(context).textTheme.headline6,
  160. ),
  161. content: const Text(
  162. "Are you sure you want to delete this code? This action is irreversible.",
  163. ),
  164. actions: [
  165. TextButton(
  166. child: const Text(
  167. "Delete",
  168. style: TextStyle(
  169. color: Colors.red,
  170. ),
  171. ),
  172. onPressed: () {
  173. CodeStore.instance.removeCode(widget.code);
  174. Navigator.of(context, rootNavigator: true).pop('dialog');
  175. },
  176. ),
  177. TextButton(
  178. child: Text(
  179. "Cancel",
  180. style: TextStyle(
  181. color: Theme.of(context).colorScheme.onSurface,
  182. ),
  183. ),
  184. onPressed: () {
  185. Navigator.of(context, rootNavigator: true).pop('dialog');
  186. },
  187. ),
  188. ],
  189. );
  190. showDialog(
  191. context: context,
  192. builder: (BuildContext context) {
  193. return alert;
  194. },
  195. barrierColor: Colors.black12,
  196. );
  197. }
  198. String _getTotp() {
  199. try {
  200. return getTotp(widget.code);
  201. } catch (e) {
  202. return "Error";
  203. }
  204. }
  205. String _getNextTotp() {
  206. try {
  207. return getNextTotp(widget.code);
  208. } catch (e) {
  209. return "Error";
  210. }
  211. }
  212. }