refactor: Use snackbar to show snapshot id copy notification

pull/325/head
Inex Code 2023-09-07 14:35:42 +03:00
parent ffa985aba2
commit fe93360870
3 changed files with 120 additions and 138 deletions

View File

@ -21,11 +21,12 @@ class NavigationService {
); );
} }
void showSnackBar(final String text) { void showSnackBar(final String text, {final SnackBarBehavior? behavior}) {
final ScaffoldMessengerState state = scaffoldMessengerKey.currentState!; final ScaffoldMessengerState state = scaffoldMessengerKey.currentState!;
final SnackBar snack = SnackBar( final SnackBar snack = SnackBar(
content: Text(text), content: Text(text),
duration: const Duration(seconds: 2), duration: const Duration(seconds: 2),
behavior: behavior,
); );
state.showSnackBar(snack); state.showSnackBar(snack);
} }

View File

@ -1,10 +1,9 @@
import 'dart:async';
import 'package:easy_localization/easy_localization.dart'; import 'package:easy_localization/easy_localization.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:selfprivacy/config/get_it_config.dart';
class SnapshotIdListTile extends StatefulWidget { class SnapshotIdListTile extends StatelessWidget {
const SnapshotIdListTile({ const SnapshotIdListTile({
required this.snapshotId, required this.snapshotId,
super.key, super.key,
@ -12,41 +11,20 @@ class SnapshotIdListTile extends StatefulWidget {
final String snapshotId; final String snapshotId;
@override
State<SnapshotIdListTile> createState() => _SnapshotIdListTileState();
}
class _SnapshotIdListTileState extends State<SnapshotIdListTile> {
bool copiedToClipboard = false;
void handleTimeout() {
setState(() {
copiedToClipboard = false;
});
}
@override @override
Widget build(final BuildContext context) => ListTile( Widget build(final BuildContext context) => ListTile(
onLongPress: () { onLongPress: () {
if (copiedToClipboard == false) { Clipboard.setData(ClipboardData(text: snapshotId));
Clipboard.setData(ClipboardData(text: widget.snapshotId)); getIt<NavigationService>().showSnackBar(
Timer(const Duration(seconds: 2), handleTimeout); 'basis.copied_to_clipboard'.tr(),
setState(() { behavior: SnackBarBehavior.floating,
copiedToClipboard = true; );
});
}
}, },
leading: Icon( leading: Icon(
Icons.numbers_outlined, Icons.numbers_outlined,
color: Theme.of(context).colorScheme.onSurface, color: Theme.of(context).colorScheme.onSurface,
), ),
title: Text( title: Text('backup.snapshot_id_title'.tr()),
copiedToClipboard subtitle: Text(snapshotId),
? 'basis.copied_to_clipboard'.tr()
: 'backup.snapshot_id_title'.tr(),
),
subtitle: Text(
copiedToClipboard ? '' : widget.snapshotId,
),
); );
} }

View File

@ -52,114 +52,117 @@ class _SnapshotModalState extends State<SnapshotModal> {
.state .state
.getServiceById(widget.snapshot.serviceId); .getServiceById(widget.snapshot.serviceId);
return ListView( return Scaffold(
controller: widget.scrollController, backgroundColor: Colors.transparent,
padding: const EdgeInsets.all(16), body: ListView(
children: [ controller: widget.scrollController,
const SizedBox(height: 16), padding: const EdgeInsets.all(16),
Text( children: [
'backup.snapshot_modal_heading'.tr(), const SizedBox(height: 16),
style: Theme.of(context).textTheme.headlineSmall, Text(
textAlign: TextAlign.center, 'backup.snapshot_modal_heading'.tr(),
), style: Theme.of(context).textTheme.headlineSmall,
const SizedBox(height: 16), textAlign: TextAlign.center,
ListTile( ),
leading: service != null const SizedBox(height: 16),
? SvgPicture.string( ListTile(
service.svgIcon, leading: service != null
height: 24, ? SvgPicture.string(
width: 24, service.svgIcon,
colorFilter: ColorFilter.mode( height: 24,
Theme.of(context).colorScheme.onSurface, width: 24,
BlendMode.srcIn, colorFilter: ColorFilter.mode(
Theme.of(context).colorScheme.onSurface,
BlendMode.srcIn,
),
)
: const Icon(
Icons.question_mark_outlined,
), ),
) title: Text(
: const Icon( 'backup.snapshot_service_title'.tr(),
Icons.question_mark_outlined,
),
title: Text(
'backup.snapshot_service_title'.tr(),
),
subtitle: Text(
service?.displayName ?? widget.snapshot.fallbackServiceName,
),
),
ListTile(
leading: Icon(
Icons.access_time_outlined,
color: Theme.of(context).colorScheme.onSurface,
),
title: Text(
'backup.snapshot_creation_time_title'.tr(),
),
subtitle: Text(
'${MaterialLocalizations.of(context).formatShortDate(widget.snapshot.time)} ${TimeOfDay.fromDateTime(widget.snapshot.time).format(context)}',
),
),
SnapshotIdListTile(snapshotId: widget.snapshot.id),
if (service != null)
Column(
children: [
const SizedBox(height: 8),
Text(
'backup.snapshot_modal_select_strategy'.tr(),
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
_BackupStrategySelectionCard(
isSelected: selectedStrategy ==
BackupRestoreStrategy.downloadVerifyOverwrite,
onTap: () {
setState(() {
selectedStrategy =
BackupRestoreStrategy.downloadVerifyOverwrite;
});
},
title:
'backup.snapshot_modal_download_verify_option_title'.tr(),
subtitle:
'backup.snapshot_modal_download_verify_option_description'
.tr(),
),
const SizedBox(height: 8),
_BackupStrategySelectionCard(
isSelected: selectedStrategy == BackupRestoreStrategy.inplace,
onTap: () {
setState(() {
selectedStrategy = BackupRestoreStrategy.inplace;
});
},
title: 'backup.snapshot_modal_inplace_option_title'.tr(),
subtitle:
'backup.snapshot_modal_inplace_option_description'.tr(),
),
const SizedBox(height: 8),
// Restore backup button
BrandButton.filled(
onPressed: isServiceBusy
? null
: () {
context.read<BackupsCubit>().restoreBackup(
widget.snapshot.id,
selectedStrategy,
);
Navigator.of(context).pop();
getIt<NavigationService>()
.showSnackBar('backup.restore_started'.tr());
},
text: 'backup.restore'.tr(),
),
],
)
else
Padding(
padding: const EdgeInsets.all(16.0),
child: InfoBox(
isWarning: true,
text: 'backup.snapshot_modal_service_not_found'.tr(),
), ),
) subtitle: Text(
], service?.displayName ?? widget.snapshot.fallbackServiceName,
),
),
ListTile(
leading: Icon(
Icons.access_time_outlined,
color: Theme.of(context).colorScheme.onSurface,
),
title: Text(
'backup.snapshot_creation_time_title'.tr(),
),
subtitle: Text(
'${MaterialLocalizations.of(context).formatShortDate(widget.snapshot.time)} ${TimeOfDay.fromDateTime(widget.snapshot.time).format(context)}',
),
),
SnapshotIdListTile(snapshotId: widget.snapshot.id),
if (service != null)
Column(
children: [
const SizedBox(height: 8),
Text(
'backup.snapshot_modal_select_strategy'.tr(),
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 8),
_BackupStrategySelectionCard(
isSelected: selectedStrategy ==
BackupRestoreStrategy.downloadVerifyOverwrite,
onTap: () {
setState(() {
selectedStrategy =
BackupRestoreStrategy.downloadVerifyOverwrite;
});
},
title:
'backup.snapshot_modal_download_verify_option_title'.tr(),
subtitle:
'backup.snapshot_modal_download_verify_option_description'
.tr(),
),
const SizedBox(height: 8),
_BackupStrategySelectionCard(
isSelected: selectedStrategy == BackupRestoreStrategy.inplace,
onTap: () {
setState(() {
selectedStrategy = BackupRestoreStrategy.inplace;
});
},
title: 'backup.snapshot_modal_inplace_option_title'.tr(),
subtitle:
'backup.snapshot_modal_inplace_option_description'.tr(),
),
const SizedBox(height: 8),
// Restore backup button
BrandButton.filled(
onPressed: isServiceBusy
? null
: () {
context.read<BackupsCubit>().restoreBackup(
widget.snapshot.id,
selectedStrategy,
);
Navigator.of(context).pop();
getIt<NavigationService>()
.showSnackBar('backup.restore_started'.tr());
},
text: 'backup.restore'.tr(),
),
],
)
else
Padding(
padding: const EdgeInsets.all(16.0),
child: InfoBox(
isWarning: true,
text: 'backup.snapshot_modal_service_not_found'.tr(),
),
)
],
),
); );
} }
} }