From 8dc06464c7c9eb2228d14de0304bfdfced70e454 Mon Sep 17 00:00:00 2001 From: Hubert <70658889+HubertJan@users.noreply.github.com> Date: Sun, 1 Oct 2023 18:38:54 +0200 Subject: [PATCH] Add minimize on copy --- lib/l10n/arb/app_en.arb | 3 +- lib/services/preference_service.dart | 13 ++++++++ lib/ui/code_widget.dart | 33 ++++++++++++++++----- lib/ui/settings/general_section_widget.dart | 19 ++++++++++++ 4 files changed, 60 insertions(+), 8 deletions(-) diff --git a/lib/l10n/arb/app_en.arb b/lib/l10n/arb/app_en.arb index e5cc9b7ab..b572856ed 100644 --- a/lib/l10n/arb/app_en.arb +++ b/lib/l10n/arb/app_en.arb @@ -332,5 +332,6 @@ "showLargeIcons": "Show large icons", "shouldHideCode": "Hide codes", "focusOnSearchBar": "Focus search on app start", - "confirmUpdatingkey": "Are you sure you want to update the secret key?" + "confirmUpdatingkey": "Are you sure you want to update the secret key?", + "minimizeAppOnCopy": "Minimize app on copy" } diff --git a/lib/services/preference_service.dart b/lib/services/preference_service.dart index cad38143d..66d96c7fd 100644 --- a/lib/services/preference_service.dart +++ b/lib/services/preference_service.dart @@ -13,6 +13,7 @@ class PreferenceService { static const kShouldShowLargeIconsKey = "should_show_large_icons"; static const kShouldHideCodesKey = "should_hide_codes"; static const kShouldAutoFocusOnSearchBar = "should_auto_focus_on_search_bar"; + static const kShouldMinimizeOnCopy = "should_minimize_on_copy"; Future init() async { _prefs = await SharedPreferences.getInstance(); @@ -64,4 +65,16 @@ class PreferenceService { await _prefs.setBool(kShouldAutoFocusOnSearchBar, value); Bus.instance.fire(IconsChangedEvent()); } + + bool shouldMinimizeOnCopy() { + if (_prefs.containsKey(kShouldMinimizeOnCopy)) { + return _prefs.getBool(kShouldMinimizeOnCopy)!; + } else { + return false; + } + } + + Future setShouldMinimizeOnCopy(bool value) async { + await _prefs.setBool(kShouldMinimizeOnCopy, value); + } } diff --git a/lib/ui/code_widget.dart b/lib/ui/code_widget.dart index b3f240224..a26cc20b2 100644 --- a/lib/ui/code_widget.dart +++ b/lib/ui/code_widget.dart @@ -1,4 +1,5 @@ import 'dart:async'; +import 'dart:io'; import 'package:clipboard/clipboard.dart'; import 'package:ente_auth/core/configuration.dart'; @@ -17,6 +18,7 @@ import 'package:ente_auth/utils/totp_util.dart'; import 'package:flutter/material.dart'; import 'package:flutter_slidable/flutter_slidable.dart'; import 'package:logging/logging.dart'; +import 'package:move_to_background/move_to_background.dart'; class CodeWidget extends StatefulWidget { final Code code; @@ -141,7 +143,7 @@ class _CodeWidgetState extends State { borderRadius: BorderRadius.circular(10), ), onTap: () { - _copyToClipboard(); + _copyCurrentOTPToClipboard(); }, onDoubleTap: isMaskingEnabled ? () { @@ -153,7 +155,7 @@ class _CodeWidgetState extends State { } : null, onLongPress: () { - _copyToClipboard(); + _copyCurrentOTPToClipboard(); }, child: _getCardContents(l10n), ), @@ -328,17 +330,34 @@ class _CodeWidgetState extends State { ); } - void _copyToClipboard() { - FlutterClipboard.copy(_getCurrentOTP()) - .then((value) => showToast(context, context.l10n.copiedToClipboard)); + void _copyCurrentOTPToClipboard() async { + _copyToClipboard( + _getCurrentOTP(), + confirmationMessage: context.l10n.copiedToClipboard, + ); } void _copyNextToClipboard() { - FlutterClipboard.copy(_getNextTotp()).then( - (value) => showToast(context, context.l10n.copiedNextToClipboard), + _copyToClipboard( + _getNextTotp(), + confirmationMessage: context.l10n.copiedNextToClipboard, ); } + void _copyToClipboard( + String content, { + required String confirmationMessage, + }) async { + final shouldMinimizeOnCopy = + PreferenceService.instance.shouldMinimizeOnCopy(); + + await FlutterClipboard.copy(content); + showToast(context, confirmationMessage); + if (Platform.isAndroid && shouldMinimizeOnCopy) { + MoveToBackground.moveTaskToBack(); + } + } + void _onNextHotpTapped() { if (widget.code.type == Type.hotp) { CodeStore.instance diff --git a/lib/ui/settings/general_section_widget.dart b/lib/ui/settings/general_section_widget.dart index 428e76357..986ef3a8e 100644 --- a/lib/ui/settings/general_section_widget.dart +++ b/lib/ui/settings/general_section_widget.dart @@ -1,3 +1,5 @@ +import 'dart:io'; + import 'package:ente_auth/app/view/app.dart'; import 'package:ente_auth/core/logging/super_logging.dart'; import 'package:ente_auth/l10n/l10n.dart'; @@ -120,6 +122,23 @@ class _AdvancedSectionWidgetState extends State { ), ), sectionOptionSpacing, + if (Platform.isAndroid) ...[ + MenuItemWidget( + captionedTextWidget: CaptionedTextWidget( + title: l10n.minimizeAppOnCopy, + ), + trailingWidget: ToggleSwitchWidget( + value: () => PreferenceService.instance.shouldMinimizeOnCopy(), + onChanged: () async { + await PreferenceService.instance.setShouldMinimizeOnCopy( + !PreferenceService.instance.shouldMinimizeOnCopy(), + ); + setState(() {}); + }, + ), + ), + sectionOptionSpacing, + ], ], ); }