ente/lib/ui/account/recovery_key_page.dart

269 lines
9.5 KiB
Dart
Raw Normal View History

// @dart=2.9
2022-04-06 17:41:24 +00:00
import 'dart:io' as io;
2022-07-03 06:49:00 +00:00
2022-04-06 17:41:24 +00:00
import 'package:bip39/bip39.dart' as bip39;
import 'package:dotted_border/dotted_border.dart';
2022-03-26 01:49:14 +00:00
import 'package:flutter/material.dart';
2022-04-06 17:51:08 +00:00
import 'package:flutter/services.dart';
2022-03-26 01:49:14 +00:00
import 'package:photos/core/configuration.dart';
2022-04-06 17:41:24 +00:00
import 'package:photos/core/constants.dart';
2022-04-08 05:59:20 +00:00
import 'package:photos/ente_theme_data.dart';
2022-07-03 10:09:01 +00:00
import 'package:photos/ui/common/gradient_button.dart';
2022-04-06 17:51:08 +00:00
import 'package:photos/utils/toast_util.dart';
2022-04-06 17:41:24 +00:00
import 'package:share_plus/share_plus.dart';
import 'package:step_progress_indicator/step_progress_indicator.dart';
2022-04-06 17:41:24 +00:00
class RecoveryKeyPage extends StatefulWidget {
final bool showAppBar;
final String recoveryKey;
final String doneText;
final Function() onDone;
final bool isDismissible;
final String title;
final String text;
final String subText;
final bool showProgressBar;
2022-04-06 17:41:24 +00:00
2022-06-11 08:23:52 +00:00
const RecoveryKeyPage(
this.recoveryKey,
this.doneText, {
Key key,
this.showAppBar,
this.onDone,
this.isDismissible,
this.title,
this.text,
this.subText,
this.showProgressBar = false,
}) : super(key: key);
2022-04-06 17:41:24 +00:00
@override
2022-07-03 09:45:00 +00:00
State<RecoveryKeyPage> createState() => _RecoveryKeyPageState();
2022-04-06 17:41:24 +00:00
}
2022-03-26 01:49:14 +00:00
2022-04-06 17:41:24 +00:00
class _RecoveryKeyPageState extends State<RecoveryKeyPage> {
bool _hasTriedToSave = false;
final _recoveryKeyFile = io.File(
2022-06-11 08:23:52 +00:00
Configuration.instance.getTempDirectory() + "ente-recovery-key.txt",
);
2022-03-26 01:49:14 +00:00
@override
Widget build(BuildContext context) {
2022-04-06 17:41:24 +00:00
final String recoveryKey = bip39.entropyToMnemonic(widget.recoveryKey);
if (recoveryKey.split(' ').length != mnemonicKeyWordCount) {
2022-04-06 17:41:24 +00:00
throw AssertionError(
'recovery code should have $mnemonicKeyWordCount words',
2022-06-11 08:23:52 +00:00
);
2022-04-06 17:41:24 +00:00
}
2022-09-17 04:31:28 +00:00
final double topPadding = widget.showAppBar
? 40
: widget.showProgressBar
? 32
: 120;
2022-04-06 17:41:24 +00:00
2022-03-26 01:49:14 +00:00
return Scaffold(
2022-11-06 08:22:54 +00:00
appBar: widget.showProgressBar
? AppBar(
automaticallyImplyLeading: false,
2022-11-06 08:22:54 +00:00
elevation: 0,
title: Hero(
tag: "recovery_key",
child: StepProgressIndicator(
totalSteps: 4,
currentStep: 3,
selectedColor: Theme.of(context).colorScheme.greenAlternative,
roundedEdges: const Radius.circular(10),
unselectedColor:
Theme.of(context).colorScheme.stepProgressUnselectedColor,
2022-09-17 04:31:28 +00:00
),
2022-11-06 08:22:54 +00:00
),
)
: widget.showAppBar
? AppBar(
elevation: 0,
title: Text(widget.title ?? "Recovery key"),
)
: null,
body: Padding(
padding: EdgeInsets.fromLTRB(20, topPadding, 20, 20),
child: LayoutBuilder(
builder: (context, constraints) {
return SingleChildScrollView(
child: ConstrainedBox(
constraints: BoxConstraints(
minWidth: constraints.maxWidth,
minHeight: constraints.maxHeight,
),
child: IntrinsicHeight(
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.start,
2022-11-06 08:22:54 +00:00
children: [
widget.showAppBar
? const SizedBox.shrink()
: Text(
widget.title ?? "Recovery key",
style: Theme.of(context).textTheme.headline4,
),
Padding(
padding: EdgeInsets.all(widget.showAppBar ? 0 : 12),
),
Text(
widget.text ??
"If you forget your password, the only way you can recover your data is with this key.",
style: Theme.of(context).textTheme.subtitle1,
),
const Padding(padding: EdgeInsets.only(top: 24)),
DottedBorder(
color: const Color.fromRGBO(17, 127, 56, 1),
//color of dotted/dash line
strokeWidth: 1,
//thickness of dash/dots
dashPattern: const [6, 6],
radius: const Radius.circular(8),
//dash patterns, 10 is dash width, 6 is space width
child: SizedBox(
//inner container
// height: 120, //height of inner container
width: double
.infinity, //width to 100% match to parent container.
// ignore: prefer_const_literals_to_create_immutables
child: Column(
children: [
GestureDetector(
onTap: () async {
await Clipboard.setData(
ClipboardData(text: recoveryKey),
);
showShortToast(
2022-11-06 08:22:54 +00:00
context,
"Recovery key copied to clipboard",
);
setState(() {
_hasTriedToSave = true;
});
},
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: const Color.fromRGBO(
49,
155,
86,
.2,
2022-09-17 04:31:28 +00:00
),
),
2022-11-06 08:22:54 +00:00
borderRadius: const BorderRadius.all(
Radius.circular(2),
2022-09-17 04:31:28 +00:00
),
2022-11-06 08:22:54 +00:00
color: Theme.of(context)
.colorScheme
.recoveryKeyBoxColor,
),
padding: const EdgeInsets.all(20),
width: double.infinity,
child: Text(
recoveryKey,
style:
Theme.of(context).textTheme.bodyText1,
2022-09-17 04:31:28 +00:00
),
),
2022-11-06 08:22:54 +00:00
),
],
2022-04-06 17:51:08 +00:00
),
2022-04-06 17:41:24 +00:00
),
2022-11-06 08:22:54 +00:00
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 20),
child: Text(
widget.subText ??
"We don't store this key, please save this 24 word key in a safe place.",
style: Theme.of(context).textTheme.bodyText1,
2022-04-06 17:41:24 +00:00
),
2022-11-06 08:22:54 +00:00
),
Expanded(
child: Container(
alignment: Alignment.bottomCenter,
width: double.infinity,
padding: const EdgeInsets.fromLTRB(10, 10, 10, 42),
child: Column(
mainAxisAlignment: MainAxisAlignment.end,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: _saveOptions(context, recoveryKey),
2022-09-17 04:31:28 +00:00
),
2022-11-06 08:22:54 +00:00
),
)
],
), // columnEnds
2022-06-11 08:23:52 +00:00
),
2022-11-06 08:22:54 +00:00
),
);
},
),
),
);
2022-03-26 01:49:14 +00:00
}
2022-04-06 17:41:24 +00:00
List<Widget> _saveOptions(BuildContext context, String recoveryKey) {
2022-08-29 14:43:31 +00:00
final List<Widget> childrens = [];
2022-04-06 17:41:24 +00:00
if (!_hasTriedToSave) {
2022-06-11 08:23:52 +00:00
childrens.add(
ElevatedButton(
style: Theme.of(context).colorScheme.optionalActionButtonStyle,
onPressed: () async {
await _saveKeys();
},
2022-07-04 06:02:17 +00:00
child: const Text('Do this later'),
2022-06-11 08:23:52 +00:00
),
);
2022-07-04 06:02:17 +00:00
childrens.add(const SizedBox(height: 10));
2022-03-26 01:49:14 +00:00
}
2022-04-06 17:41:24 +00:00
2022-06-11 08:23:52 +00:00
childrens.add(
GradientButton(
onTap: () async {
await _shareRecoveryKey(recoveryKey);
},
2022-07-04 04:45:32 +00:00
text: 'Save key',
2022-04-21 05:12:18 +00:00
),
2022-06-11 08:23:52 +00:00
);
2022-04-06 17:41:24 +00:00
if (_hasTriedToSave) {
2022-07-04 06:02:17 +00:00
childrens.add(const SizedBox(height: 10));
2022-06-11 08:23:52 +00:00
childrens.add(
ElevatedButton(
child: Text(widget.doneText),
onPressed: () async {
await _saveKeys();
},
),
);
2022-03-26 01:49:14 +00:00
}
2022-07-04 06:02:17 +00:00
childrens.add(const SizedBox(height: 12));
2022-04-06 17:41:24 +00:00
return childrens;
}
Future _shareRecoveryKey(String recoveryKey) async {
if (_recoveryKeyFile.existsSync()) {
await _recoveryKeyFile.delete();
2022-03-26 01:49:14 +00:00
}
2022-04-06 17:41:24 +00:00
_recoveryKeyFile.writeAsStringSync(recoveryKey);
await Share.shareFiles([_recoveryKeyFile.path]);
2022-07-04 06:02:17 +00:00
Future.delayed(const Duration(milliseconds: 500), () {
2022-04-06 17:41:24 +00:00
if (mounted) {
setState(() {
_hasTriedToSave = true;
});
}
});
}
Future<void> _saveKeys() async {
Navigator.of(context).pop();
if (_recoveryKeyFile.existsSync()) {
await _recoveryKeyFile.delete();
}
widget.onDone();
2022-03-26 01:49:14 +00:00
}
}