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 SnackBar snack = SnackBar(
content: Text(text),
duration: const Duration(seconds: 2),
behavior: behavior,
);
state.showSnackBar(snack);
}

View File

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

View File

@ -52,114 +52,117 @@ class _SnapshotModalState extends State<SnapshotModal> {
.state
.getServiceById(widget.snapshot.serviceId);
return ListView(
controller: widget.scrollController,
padding: const EdgeInsets.all(16),
children: [
const SizedBox(height: 16),
Text(
'backup.snapshot_modal_heading'.tr(),
style: Theme.of(context).textTheme.headlineSmall,
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
ListTile(
leading: service != null
? SvgPicture.string(
service.svgIcon,
height: 24,
width: 24,
colorFilter: ColorFilter.mode(
Theme.of(context).colorScheme.onSurface,
BlendMode.srcIn,
return Scaffold(
backgroundColor: Colors.transparent,
body: ListView(
controller: widget.scrollController,
padding: const EdgeInsets.all(16),
children: [
const SizedBox(height: 16),
Text(
'backup.snapshot_modal_heading'.tr(),
style: Theme.of(context).textTheme.headlineSmall,
textAlign: TextAlign.center,
),
const SizedBox(height: 16),
ListTile(
leading: service != null
? SvgPicture.string(
service.svgIcon,
height: 24,
width: 24,
colorFilter: ColorFilter.mode(
Theme.of(context).colorScheme.onSurface,
BlendMode.srcIn,
),
)
: const Icon(
Icons.question_mark_outlined,
),
)
: const Icon(
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(),
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(),
),
)
],
),
);
}
}