Refresh UI when icon settings are updated

This commit is contained in:
vishnukvmd 2023-09-08 18:02:10 +05:30
parent 7fb3ab02f9
commit 1697547091
2 changed files with 42 additions and 7 deletions

View file

@ -7,6 +7,7 @@ import 'package:ente_auth/l10n/l10n.dart';
import 'package:ente_auth/models/code.dart';
import 'package:ente_auth/onboarding/view/setup_enter_secret_key_page.dart';
import 'package:ente_auth/onboarding/view/view_qr_page.dart';
import 'package:ente_auth/services/preference_service.dart';
import 'package:ente_auth/store/code_store.dart';
import 'package:ente_auth/ui/code_timer_progress.dart';
import 'package:ente_auth/ui/utils/icon_utils.dart';
@ -33,6 +34,7 @@ class _CodeWidgetState extends State<CodeWidget> {
final Logger logger = Logger("_CodeWidgetState");
bool _isInitialized = false;
late bool hasConfiguredAccount;
late bool _shouldShowLargeIcon;
@override
void initState() {
@ -61,6 +63,7 @@ class _CodeWidgetState extends State<CodeWidget> {
@override
Widget build(BuildContext context) {
_shouldShowLargeIcon = PreferenceService.instance.shouldShowLargeIcons();
if (!_isInitialized) {
_currentCode.value = _getCurrentOTP();
if (widget.code.type == Type.totp) {
@ -158,9 +161,20 @@ class _CodeWidgetState extends State<CodeWidget> {
const SizedBox(
height: 16,
),
_getTopRow(),
const SizedBox(height: 4),
_getBottomRow(l10n),
Row(
children: [
_shouldShowLargeIcon ? _getIcon() : const SizedBox.shrink(),
Expanded(
child: Column(
children: [
_getTopRow(),
const SizedBox(height: 4),
_getBottomRow(l10n),
],
),
),
],
),
const SizedBox(
height: 20,
),
@ -267,9 +281,7 @@ class _CodeWidgetState extends State<CodeWidget> {
color: Colors.amber,
),
const SizedBox(width: 12),
IconUtils.instance.getIcon(
safeDecode(widget.code.issuer).trim(),
),
_shouldShowLargeIcon ? const SizedBox.shrink() : _getIcon(),
],
),
],
@ -277,6 +289,23 @@ class _CodeWidgetState extends State<CodeWidget> {
);
}
Widget _getIcon() {
return Padding(
padding: _shouldShowLargeIcon
? const EdgeInsets.only(left: 16)
: const EdgeInsets.all(0),
child: GestureDetector(
onTap: () {
PreferenceService.instance.setShowLargeIcons(!_shouldShowLargeIcon);
},
child: IconUtils.instance.getIcon(
safeDecode(widget.code.issuer).trim(),
width: _shouldShowLargeIcon ? 42 : 24,
),
),
);
}
void _copyToClipboard() {
FlutterClipboard.copy(_getCurrentOTP())
.then((value) => showToast(context, context.l10n.copiedToClipboard));

View file

@ -5,6 +5,7 @@ import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/core/event_bus.dart';
import 'package:ente_auth/ente_theme_data.dart';
import 'package:ente_auth/events/codes_updated_event.dart';
import 'package:ente_auth/events/icons_changed_event.dart';
import 'package:ente_auth/events/trigger_logout_event.dart';
import "package:ente_auth/l10n/l10n.dart";
import 'package:ente_auth/models/code.dart';
@ -51,6 +52,7 @@ class _HomePageState extends State<HomePage> {
List<Code> _filteredCodes = [];
StreamSubscription<CodesUpdatedEvent>? _streamSubscription;
StreamSubscription<TriggerLogoutEvent>? _triggerLogoutEvent;
StreamSubscription<IconsChangedEvent>? _iconsChangedEvent;
@override
void initState() {
@ -69,6 +71,9 @@ class _HomePageState extends State<HomePage> {
const Duration(seconds: 1),
() async => await CodeStore.instance.importOfflineCodes(),
);
_iconsChangedEvent = Bus.instance.on<IconsChangedEvent>().listen((event) {
setState(() {});
});
}
void _loadCodes() {
@ -100,6 +105,7 @@ class _HomePageState extends State<HomePage> {
void dispose() {
_streamSubscription?.cancel();
_triggerLogoutEvent?.cancel();
_iconsChangedEvent?.cancel();
_textController.removeListener(_applyFilteringAndRefresh);
super.dispose();
}
@ -228,7 +234,7 @@ class _HomePageState extends State<HomePage> {
itemBuilder: ((context, index) {
try {
return CodeWidget(_filteredCodes[index]);
} catch(e) {
} catch (e) {
return const Text("Failed");
}
}),