code_widget.dart 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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/onboarding/view/setup_enter_secret_key_page.dart';
  6. import 'package:ente_auth/store/code_store.dart';
  7. import 'package:ente_auth/utils/toast_util.dart';
  8. import 'package:ente_auth/utils/totp_util.dart';
  9. import 'package:flutter/material.dart';
  10. import 'package:flutter_animation_progress_bar/flutter_animation_progress_bar.dart';
  11. import 'package:flutter_slidable/flutter_slidable.dart';
  12. class CodeWidget extends StatefulWidget {
  13. final Code code;
  14. const CodeWidget(this.code, {Key? key}) : super(key: key);
  15. @override
  16. State<CodeWidget> createState() => _CodeWidgetState();
  17. }
  18. class _CodeWidgetState extends State<CodeWidget> {
  19. Timer? _everySecondTimer;
  20. int _timeRemaining = 30;
  21. @override
  22. void initState() {
  23. super.initState();
  24. _updateTimeRemaining();
  25. _everySecondTimer =
  26. Timer.periodic(const Duration(milliseconds: 200), (Timer t) {
  27. setState(() {
  28. _updateTimeRemaining();
  29. });
  30. });
  31. }
  32. void _updateTimeRemaining() {
  33. _timeRemaining =
  34. widget.code.period - (DateTime.now().second % widget.code.period);
  35. }
  36. @override
  37. void dispose() {
  38. _everySecondTimer?.cancel();
  39. super.dispose();
  40. }
  41. @override
  42. Widget build(BuildContext context) {
  43. return Container(
  44. margin: const EdgeInsets.only(left: 16, right: 16, bottom: 8, top: 8),
  45. child: Slidable(
  46. key: ValueKey(widget.code.hashCode),
  47. endActionPane: ActionPane(
  48. motion: const ScrollMotion(),
  49. children: [
  50. SlidableAction(
  51. onPressed: _onEditPressed,
  52. backgroundColor: Colors.grey.withOpacity(0.1),
  53. borderRadius: const BorderRadius.all(Radius.circular(12.0)),
  54. foregroundColor:
  55. Theme.of(context).colorScheme.inverseBackgroundColor,
  56. icon: Icons.edit_outlined,
  57. label: 'Edit',
  58. padding: const EdgeInsets.only(left: 4, right: 0),
  59. spacing: 8,
  60. ),
  61. const SizedBox(
  62. width: 4,
  63. ),
  64. SlidableAction(
  65. onPressed: _onDeletePressed,
  66. backgroundColor: Colors.grey.withOpacity(0.1),
  67. borderRadius: const BorderRadius.all(Radius.circular(12.0)),
  68. foregroundColor: const Color(0xFFFE4A49),
  69. icon: Icons.delete,
  70. label: 'Delete',
  71. padding: const EdgeInsets.only(left: 0, right: 0),
  72. spacing: 8,
  73. ),
  74. ],
  75. ),
  76. child: Container(
  77. margin: const EdgeInsets.only(right: 10),
  78. child: ClipRRect(
  79. borderRadius: BorderRadius.circular(8),
  80. child: Container(
  81. color: Theme.of(context).colorScheme.codeCardBackgroundColor,
  82. child: Material(
  83. color: Colors.transparent,
  84. child: InkWell(
  85. customBorder: RoundedRectangleBorder(
  86. borderRadius: BorderRadius.circular(10),
  87. ),
  88. onTap: () {
  89. _copyToClipboard();
  90. },
  91. onLongPress: () {
  92. _copyToClipboard();
  93. },
  94. child: SizedBox(
  95. child: Column(
  96. crossAxisAlignment: CrossAxisAlignment.start,
  97. mainAxisAlignment: MainAxisAlignment.center,
  98. children: [
  99. FAProgressBar(
  100. currentValue:
  101. _timeRemaining / widget.code.period * 100,
  102. size: 4,
  103. animatedDuration: const Duration(milliseconds: 200),
  104. progressColor: Colors.orange,
  105. changeColorValue: 40,
  106. changeProgressColor: Colors.green,
  107. ),
  108. const SizedBox(
  109. height: 16,
  110. ),
  111. Padding(
  112. padding: const EdgeInsets.only(left: 16, right: 16),
  113. child: Row(
  114. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  115. crossAxisAlignment: CrossAxisAlignment.start,
  116. children: [
  117. Column(
  118. crossAxisAlignment: CrossAxisAlignment.start,
  119. children: [
  120. Text(
  121. Uri.decodeFull(widget.code.issuer).trim(),
  122. style:
  123. Theme.of(context).textTheme.headline6,
  124. ),
  125. const SizedBox(height: 2),
  126. Text(
  127. Uri.decodeFull(
  128. widget.code.account,
  129. ).trim(),
  130. style: Theme.of(context)
  131. .textTheme
  132. .caption
  133. ?.copyWith(
  134. fontSize: 12,
  135. color: Colors.grey,
  136. ),
  137. ),
  138. ],
  139. ),
  140. widget.code.hasSynced != null &&
  141. widget.code.hasSynced!
  142. ? Container()
  143. : const Icon(
  144. Icons.sync_disabled,
  145. size: 20,
  146. color: Colors.amber,
  147. ),
  148. ],
  149. ),
  150. ),
  151. const SizedBox(height: 4),
  152. Container(
  153. padding: const EdgeInsets.only(left: 16, right: 16),
  154. child: Row(
  155. mainAxisAlignment: MainAxisAlignment.start,
  156. crossAxisAlignment: CrossAxisAlignment.end,
  157. children: [
  158. Expanded(
  159. child: Text(
  160. _getTotp(),
  161. style: const TextStyle(fontSize: 24),
  162. ),
  163. ),
  164. Column(
  165. crossAxisAlignment: CrossAxisAlignment.end,
  166. children: [
  167. Text(
  168. "next",
  169. style: Theme.of(context).textTheme.caption,
  170. ),
  171. Text(
  172. _getNextTotp(),
  173. style: const TextStyle(
  174. fontSize: 18,
  175. color: Colors.grey,
  176. ),
  177. ),
  178. ],
  179. ),
  180. ],
  181. ),
  182. ),
  183. const SizedBox(
  184. height: 20,
  185. ),
  186. ],
  187. ),
  188. ),
  189. ),
  190. ),
  191. ),
  192. ),
  193. ),
  194. ),
  195. );
  196. }
  197. void _copyToClipboard() {
  198. FlutterClipboard.copy(_getTotp()).then(
  199. (value) => showToast(context, "Copied to clipboard"),
  200. );
  201. }
  202. Future<void> _onEditPressed(_) async {
  203. final Code? code = await Navigator.of(context).push(
  204. MaterialPageRoute(
  205. builder: (BuildContext context) {
  206. return SetupEnterSecretKeyPage(code: widget.code);
  207. },
  208. ),
  209. );
  210. if (code != null) {
  211. CodeStore.instance.addCode(code);
  212. }
  213. }
  214. void _onDeletePressed(_) {
  215. final AlertDialog alert = AlertDialog(
  216. shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(10)),
  217. title: Text(
  218. "Delete code?",
  219. style: Theme.of(context).textTheme.headline6,
  220. ),
  221. content: const Text(
  222. "Are you sure you want to delete this code? This action is irreversible.",
  223. ),
  224. actions: [
  225. TextButton(
  226. child: const Text(
  227. "Delete",
  228. style: TextStyle(
  229. color: Colors.red,
  230. ),
  231. ),
  232. onPressed: () {
  233. CodeStore.instance.removeCode(widget.code);
  234. Navigator.of(context, rootNavigator: true).pop('dialog');
  235. },
  236. ),
  237. TextButton(
  238. child: Text(
  239. "Cancel",
  240. style: TextStyle(
  241. color: Theme.of(context).colorScheme.onSurface,
  242. ),
  243. ),
  244. onPressed: () {
  245. Navigator.of(context, rootNavigator: true).pop('dialog');
  246. },
  247. ),
  248. ],
  249. );
  250. showDialog(
  251. context: context,
  252. builder: (BuildContext context) {
  253. return alert;
  254. },
  255. barrierColor: Colors.black12,
  256. );
  257. }
  258. String _getTotp() {
  259. try {
  260. return getTotp(widget.code);
  261. } catch (e) {
  262. return "Error";
  263. }
  264. }
  265. String _getNextTotp() {
  266. try {
  267. return getNextTotp(widget.code);
  268. } catch (e) {
  269. return "Error";
  270. }
  271. }
  272. }