code_widget.dart 12 KB

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