code_widget.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. import 'dart:async';
  2. import 'package:clipboard/clipboard.dart';
  3. import 'package:ente_auth/core/configuration.dart';
  4. import 'package:ente_auth/ente_theme_data.dart';
  5. import 'package:ente_auth/l10n/l10n.dart';
  6. import 'package:ente_auth/models/code.dart';
  7. import 'package:ente_auth/onboarding/view/setup_enter_secret_key_page.dart';
  8. import 'package:ente_auth/onboarding/view/view_qr_page.dart';
  9. import 'package:ente_auth/store/code_store.dart';
  10. import 'package:ente_auth/ui/code_timer_progress.dart';
  11. import 'package:ente_auth/ui/utils/icon_utils.dart';
  12. import 'package:ente_auth/utils/dialog_util.dart';
  13. import 'package:ente_auth/utils/toast_util.dart';
  14. import 'package:ente_auth/utils/totp_util.dart';
  15. import 'package:flutter/material.dart';
  16. import 'package:flutter_slidable/flutter_slidable.dart';
  17. import 'package:logging/logging.dart';
  18. class CodeWidget extends StatefulWidget {
  19. final Code code;
  20. const CodeWidget(this.code, {Key? key}) : super(key: key);
  21. @override
  22. State<CodeWidget> createState() => _CodeWidgetState();
  23. }
  24. class _CodeWidgetState extends State<CodeWidget> {
  25. Timer? _everySecondTimer;
  26. final ValueNotifier<String> _currentCode = ValueNotifier<String>("");
  27. final ValueNotifier<String> _nextCode = ValueNotifier<String>("");
  28. final Logger logger = Logger("_CodeWidgetState");
  29. bool _isInitialized = false;
  30. late bool hasConfiguredAccount;
  31. @override
  32. void initState() {
  33. super.initState();
  34. _everySecondTimer =
  35. Timer.periodic(const Duration(milliseconds: 500), (Timer t) {
  36. String newCode = _getCurrentOTP();
  37. if (newCode != _currentCode.value) {
  38. _currentCode.value = newCode;
  39. if (widget.code.type == Type.totp) {
  40. _nextCode.value = _getNextTotp();
  41. }
  42. }
  43. });
  44. hasConfiguredAccount = Configuration.instance.hasConfiguredAccount();
  45. }
  46. @override
  47. void dispose() {
  48. _everySecondTimer?.cancel();
  49. _currentCode.dispose();
  50. _nextCode.dispose();
  51. super.dispose();
  52. }
  53. @override
  54. Widget build(BuildContext context) {
  55. if (!_isInitialized) {
  56. _currentCode.value = _getCurrentOTP();
  57. if (widget.code.type == Type.totp) {
  58. _nextCode.value = _getNextTotp();
  59. }
  60. _isInitialized = true;
  61. }
  62. final l10n = context.l10n;
  63. return Container(
  64. margin: const EdgeInsets.only(left: 16, right: 16, bottom: 8, top: 8),
  65. child: Slidable(
  66. key: ValueKey(widget.code.hashCode),
  67. endActionPane: ActionPane(
  68. extentRatio: 0.60,
  69. motion: const ScrollMotion(),
  70. children: [
  71. const SizedBox(
  72. width: 4,
  73. ),
  74. SlidableAction(
  75. onPressed: _onShowQrPressed,
  76. backgroundColor: Colors.grey.withOpacity(0.1),
  77. borderRadius: const BorderRadius.all(Radius.circular(12.0)),
  78. foregroundColor:
  79. Theme.of(context).colorScheme.inverseBackgroundColor,
  80. icon: Icons.qr_code_2_outlined,
  81. label: "QR",
  82. padding: const EdgeInsets.only(left: 4, right: 0),
  83. spacing: 8,
  84. ),
  85. const SizedBox(
  86. width: 4,
  87. ),
  88. SlidableAction(
  89. onPressed: _onEditPressed,
  90. backgroundColor: Colors.grey.withOpacity(0.1),
  91. borderRadius: const BorderRadius.all(Radius.circular(12.0)),
  92. foregroundColor:
  93. Theme.of(context).colorScheme.inverseBackgroundColor,
  94. icon: Icons.edit_outlined,
  95. label: l10n.edit,
  96. padding: const EdgeInsets.only(left: 4, right: 0),
  97. spacing: 8,
  98. ),
  99. const SizedBox(
  100. width: 4,
  101. ),
  102. SlidableAction(
  103. onPressed: _onDeletePressed,
  104. backgroundColor: Colors.grey.withOpacity(0.1),
  105. borderRadius: const BorderRadius.all(Radius.circular(12.0)),
  106. foregroundColor: const Color(0xFFFE4A49),
  107. icon: Icons.delete,
  108. label: l10n.delete,
  109. padding: const EdgeInsets.only(left: 0, right: 0),
  110. spacing: 8,
  111. ),
  112. ],
  113. ),
  114. child: ClipRRect(
  115. borderRadius: BorderRadius.circular(8),
  116. child: Container(
  117. color: Theme.of(context).colorScheme.codeCardBackgroundColor,
  118. child: Material(
  119. color: Colors.transparent,
  120. child: InkWell(
  121. customBorder: RoundedRectangleBorder(
  122. borderRadius: BorderRadius.circular(10),
  123. ),
  124. onTap: () {
  125. _copyToClipboard();
  126. },
  127. onLongPress: () {
  128. _copyToClipboard();
  129. },
  130. child: SizedBox(
  131. child: Column(
  132. crossAxisAlignment: CrossAxisAlignment.start,
  133. mainAxisAlignment: MainAxisAlignment.center,
  134. children: [
  135. if (widget.code.type == Type.totp)
  136. CodeTimerProgress(
  137. period: widget.code.period,
  138. ),
  139. const SizedBox(
  140. height: 16,
  141. ),
  142. Padding(
  143. padding: const EdgeInsets.only(left: 16, right: 16),
  144. child: Row(
  145. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  146. crossAxisAlignment: CrossAxisAlignment.start,
  147. children: [
  148. Column(
  149. crossAxisAlignment: CrossAxisAlignment.start,
  150. children: [
  151. Text(
  152. safeDecode(widget.code.issuer).trim(),
  153. style: Theme.of(context).textTheme.titleLarge,
  154. ),
  155. const SizedBox(height: 2),
  156. Text(
  157. safeDecode(widget.code.account).trim(),
  158. style: Theme.of(context)
  159. .textTheme
  160. .bodySmall
  161. ?.copyWith(
  162. fontSize: 12,
  163. color: Colors.grey,
  164. ),
  165. ),
  166. ],
  167. ),
  168. Row(
  169. mainAxisAlignment: MainAxisAlignment.end,
  170. children: [
  171. (widget.code.hasSynced != null &&
  172. widget.code.hasSynced!) || !hasConfiguredAccount
  173. ? const SizedBox.shrink()
  174. : const Icon(
  175. Icons.sync_disabled,
  176. size: 20,
  177. color: Colors.amber,
  178. ),
  179. const SizedBox(width: 12),
  180. IconUtils.instance.getIcon(
  181. safeDecode(widget.code.issuer).trim(),
  182. ),
  183. ],
  184. ),
  185. ],
  186. ),
  187. ),
  188. const SizedBox(height: 4),
  189. Container(
  190. padding: const EdgeInsets.only(left: 16, right: 16),
  191. child: Row(
  192. mainAxisAlignment: MainAxisAlignment.start,
  193. crossAxisAlignment: CrossAxisAlignment.end,
  194. children: [
  195. Expanded(
  196. child: ValueListenableBuilder<String>(
  197. valueListenable: _currentCode,
  198. builder: (context, value, child) {
  199. return Text(
  200. value,
  201. style: const TextStyle(fontSize: 24),
  202. );
  203. },
  204. ),
  205. ),
  206. widget.code.type == Type.totp
  207. ? Column(
  208. crossAxisAlignment: CrossAxisAlignment.end,
  209. children: [
  210. Text(
  211. l10n.nextTotpTitle,
  212. style:
  213. Theme.of(context).textTheme.bodySmall,
  214. ),
  215. ValueListenableBuilder<String>(
  216. valueListenable: _nextCode,
  217. builder: (context, value, child) {
  218. return Text(
  219. value,
  220. style: const TextStyle(
  221. fontSize: 18,
  222. color: Colors.grey,
  223. ),
  224. );
  225. },
  226. ),
  227. ],
  228. )
  229. : Column(
  230. crossAxisAlignment: CrossAxisAlignment.end,
  231. children: [
  232. Text(
  233. l10n.nextTotpTitle,
  234. style:
  235. Theme.of(context).textTheme.bodySmall,
  236. ),
  237. InkWell(
  238. onTap: _onNextHotpTapped,
  239. child: const Icon(
  240. Icons.forward_outlined,
  241. size: 32,
  242. color: Colors.grey,
  243. ),
  244. ),
  245. ],
  246. ),
  247. ],
  248. ),
  249. ),
  250. const SizedBox(
  251. height: 20,
  252. ),
  253. ],
  254. ),
  255. ),
  256. ),
  257. ),
  258. ),
  259. ),
  260. ),
  261. );
  262. }
  263. void _copyToClipboard() {
  264. FlutterClipboard.copy(_getCurrentOTP())
  265. .then((value) => showToast(context, context.l10n.copiedToClipboard));
  266. }
  267. void _onNextHotpTapped() {
  268. if (widget.code.type == Type.hotp) {
  269. CodeStore.instance
  270. .addCode(
  271. widget.code.copyWith(counter: widget.code.counter + 1),
  272. shouldSync: true,
  273. )
  274. .ignore();
  275. }
  276. }
  277. Future<void> _onEditPressed(_) async {
  278. final Code? code = await Navigator.of(context).push(
  279. MaterialPageRoute(
  280. builder: (BuildContext context) {
  281. return SetupEnterSecretKeyPage(code: widget.code);
  282. },
  283. ),
  284. );
  285. if (code != null) {
  286. CodeStore.instance.addCode(code);
  287. }
  288. }
  289. Future<void> _onShowQrPressed(_) async {
  290. final Code? code = await Navigator.of(context).push(
  291. MaterialPageRoute(
  292. builder: (BuildContext context) {
  293. return ViewQrPage(code: widget.code);
  294. },
  295. ),
  296. );
  297. }
  298. void _onDeletePressed(_) async {
  299. final l10n = context.l10n;
  300. await showChoiceActionSheet(
  301. context,
  302. title: l10n.deleteCodeTitle,
  303. body: l10n.deleteCodeMessage,
  304. firstButtonLabel: l10n.delete,
  305. isCritical: true,
  306. firstButtonOnTap: () async {
  307. await CodeStore.instance.removeCode(widget.code);
  308. },
  309. );
  310. }
  311. String _getCurrentOTP() {
  312. try {
  313. return getOTP(widget.code);
  314. } catch (e) {
  315. return context.l10n.error;
  316. }
  317. }
  318. String _getNextTotp() {
  319. try {
  320. assert(widget.code.type == Type.totp);
  321. return getNextTotp(widget.code);
  322. } catch (e) {
  323. return context.l10n.error;
  324. }
  325. }
  326. }