code_widget.dart 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. SizedBox _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. Container _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 Text(
  187. value,
  188. style: const TextStyle(fontSize: 24),
  189. );
  190. },
  191. ),
  192. ),
  193. widget.code.type == Type.totp
  194. ? Column(
  195. crossAxisAlignment: CrossAxisAlignment.end,
  196. children: [
  197. Text(
  198. l10n.nextTotpTitle,
  199. style: Theme.of(context).textTheme.bodySmall,
  200. ),
  201. ValueListenableBuilder<String>(
  202. valueListenable: _nextCode,
  203. builder: (context, value, child) {
  204. return Text(
  205. value,
  206. style: const TextStyle(
  207. fontSize: 18,
  208. color: Colors.grey,
  209. ),
  210. );
  211. },
  212. ),
  213. ],
  214. )
  215. : Column(
  216. crossAxisAlignment: CrossAxisAlignment.end,
  217. children: [
  218. Text(
  219. l10n.nextTotpTitle,
  220. style: Theme.of(context).textTheme.bodySmall,
  221. ),
  222. InkWell(
  223. onTap: _onNextHotpTapped,
  224. child: const Icon(
  225. Icons.forward_outlined,
  226. size: 32,
  227. color: Colors.grey,
  228. ),
  229. ),
  230. ],
  231. ),
  232. ],
  233. ),
  234. );
  235. }
  236. Padding _getTopRow() {
  237. return Padding(
  238. padding: const EdgeInsets.only(left: 16, right: 16),
  239. child: Row(
  240. mainAxisAlignment: MainAxisAlignment.spaceBetween,
  241. crossAxisAlignment: CrossAxisAlignment.start,
  242. children: [
  243. Column(
  244. crossAxisAlignment: CrossAxisAlignment.start,
  245. children: [
  246. Text(
  247. safeDecode(widget.code.issuer).trim(),
  248. style: Theme.of(context).textTheme.titleLarge,
  249. ),
  250. const SizedBox(height: 2),
  251. Text(
  252. safeDecode(widget.code.account).trim(),
  253. style: Theme.of(context).textTheme.bodySmall?.copyWith(
  254. fontSize: 12,
  255. color: Colors.grey,
  256. ),
  257. ),
  258. ],
  259. ),
  260. Row(
  261. mainAxisAlignment: MainAxisAlignment.end,
  262. children: [
  263. (widget.code.hasSynced != null && widget.code.hasSynced!) ||
  264. !hasConfiguredAccount
  265. ? const SizedBox.shrink()
  266. : const Icon(
  267. Icons.sync_disabled,
  268. size: 20,
  269. color: Colors.amber,
  270. ),
  271. const SizedBox(width: 12),
  272. _shouldShowLargeIcon ? const SizedBox.shrink() : _getIcon(),
  273. ],
  274. ),
  275. ],
  276. ),
  277. );
  278. }
  279. Widget _getIcon() {
  280. return Padding(
  281. padding: _shouldShowLargeIcon
  282. ? const EdgeInsets.only(left: 16)
  283. : const EdgeInsets.all(0),
  284. child: GestureDetector(
  285. onTap: () {
  286. PreferenceService.instance.setShowLargeIcons(!_shouldShowLargeIcon);
  287. },
  288. child: IconUtils.instance.getIcon(
  289. safeDecode(widget.code.issuer).trim(),
  290. width: _shouldShowLargeIcon ? 42 : 24,
  291. ),
  292. ),
  293. );
  294. }
  295. void _copyToClipboard() {
  296. FlutterClipboard.copy(_getCurrentOTP())
  297. .then((value) => showToast(context, context.l10n.copiedToClipboard));
  298. }
  299. void _onNextHotpTapped() {
  300. if (widget.code.type == Type.hotp) {
  301. CodeStore.instance
  302. .addCode(
  303. widget.code.copyWith(counter: widget.code.counter + 1),
  304. shouldSync: true,
  305. )
  306. .ignore();
  307. }
  308. }
  309. Future<void> _onEditPressed(_) async {
  310. final Code? code = await Navigator.of(context).push(
  311. MaterialPageRoute(
  312. builder: (BuildContext context) {
  313. return SetupEnterSecretKeyPage(code: widget.code);
  314. },
  315. ),
  316. );
  317. if (code != null) {
  318. CodeStore.instance.addCode(code);
  319. }
  320. }
  321. Future<void> _onShowQrPressed(_) async {
  322. // ignore: unused_local_variable
  323. final Code? code = await Navigator.of(context).push(
  324. MaterialPageRoute(
  325. builder: (BuildContext context) {
  326. return ViewQrPage(code: widget.code);
  327. },
  328. ),
  329. );
  330. }
  331. void _onDeletePressed(_) async {
  332. final l10n = context.l10n;
  333. await showChoiceActionSheet(
  334. context,
  335. title: l10n.deleteCodeTitle,
  336. body: l10n.deleteCodeMessage,
  337. firstButtonLabel: l10n.delete,
  338. isCritical: true,
  339. firstButtonOnTap: () async {
  340. await CodeStore.instance.removeCode(widget.code);
  341. },
  342. );
  343. }
  344. String _getCurrentOTP() {
  345. try {
  346. return getOTP(widget.code);
  347. } catch (e) {
  348. return context.l10n.error;
  349. }
  350. }
  351. String _getNextTotp() {
  352. try {
  353. assert(widget.code.type == Type.totp);
  354. return getNextTotp(widget.code);
  355. } catch (e) {
  356. return context.l10n.error;
  357. }
  358. }
  359. }