ente/lib/ui/settings/security_section_widget.dart

189 lines
5.8 KiB
Dart
Raw Normal View History

2021-06-29 09:48:01 +00:00
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:photos/core/configuration.dart';
2021-06-29 09:48:01 +00:00
import 'package:photos/core/event_bus.dart';
import 'package:photos/ente_theme_data.dart';
2021-06-29 09:48:01 +00:00
import 'package:photos/events/two_factor_status_change_event.dart';
import 'package:photos/services/local_authentication_service.dart';
import 'package:photos/services/user_service.dart';
2022-10-21 13:37:57 +00:00
import 'package:photos/theme/ente_theme.dart';
import 'package:photos/ui/account/sessions_page.dart';
import 'package:photos/ui/components/captioned_text_widget.dart';
import 'package:photos/ui/components/expandable_menu_item_widget.dart';
2023-01-31 12:21:57 +00:00
import 'package:photos/ui/components/menu_item_widget/menu_item_widget.dart';
2022-10-05 15:57:41 +00:00
import 'package:photos/ui/components/toggle_switch_widget.dart';
import 'package:photos/ui/settings/common_settings.dart';
class SecuritySectionWidget extends StatefulWidget {
2022-12-27 18:03:40 +00:00
const SecuritySectionWidget({Key? key}) : super(key: key);
@override
2022-07-03 09:45:00 +00:00
State<SecuritySectionWidget> createState() => _SecuritySectionWidgetState();
}
class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
final _config = Configuration.instance;
2022-12-27 18:03:40 +00:00
late StreamSubscription<TwoFactorStatusChangeEvent>
_twoFactorStatusChangeEvent;
2021-06-29 09:48:01 +00:00
@override
void initState() {
super.initState();
2021-06-29 09:48:01 +00:00
_twoFactorStatusChangeEvent =
Bus.instance.on<TwoFactorStatusChangeEvent>().listen((event) async {
if (mounted) {
setState(() {});
}
});
}
@override
void dispose() {
_twoFactorStatusChangeEvent.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
return ExpandableMenuItemWidget(
title: "Security",
selectionOptionsWidget: _getSectionOptions(context),
leadingIcon: Icons.local_police_outlined,
);
}
Widget _getSectionOptions(BuildContext context) {
final Completer completer = Completer();
final List<Widget> children = [];
if (_config.hasConfiguredAccount()) {
children.addAll(
[
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
title: "Two-factor",
),
2022-12-06 18:03:39 +00:00
trailingWidget: ToggleSwitchWidget(
value: () => UserService.instance.hasEnabledTwoFactor(),
onChanged: () async {
final hasAuthenticated = await LocalAuthenticationService
.instance
.requestLocalAuthentication(
context,
"Please authenticate to configure two-factor authentication",
);
final isTwoFactorEnabled =
UserService.instance.hasEnabledTwoFactor();
if (hasAuthenticated) {
if (isTwoFactorEnabled) {
await _disableTwoFactor();
2022-11-16 13:01:29 +00:00
completer.isCompleted ? null : completer.complete();
} else {
await UserService.instance
.setupTwoFactor(context, completer);
}
return completer.future;
}
},
),
),
sectionOptionSpacing,
],
);
}
children.addAll([
MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
2022-10-05 13:57:32 +00:00
title: "Lockscreen",
),
2022-12-06 18:03:39 +00:00
trailingWidget: ToggleSwitchWidget(
value: () => _config.shouldShowLockScreen(),
onChanged: () async {
await LocalAuthenticationService.instance
.requestLocalAuthForLockScreen(
context,
!_config.shouldShowLockScreen(),
"Please authenticate to change lockscreen setting",
"To enable lockscreen, please setup device passcode or screen lock in your system settings.",
);
},
),
),
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: const CaptionedTextWidget(
2022-12-07 05:38:02 +00:00
title: "View active sessions",
),
2022-10-21 13:37:57 +00:00
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
2022-09-06 08:35:34 +00:00
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
2022-09-03 03:02:42 +00:00
context,
"Please authenticate to view your active sessions",
);
2022-09-06 08:35:34 +00:00
if (hasAuthenticated) {
2022-11-06 10:36:33 +00:00
unawaited(
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return const SessionsPage();
},
),
2022-09-03 03:02:42 +00:00
),
);
}
},
),
sectionOptionSpacing,
]);
2021-07-28 15:41:45 +00:00
return Column(
children: children,
);
}
Future<void> _disableTwoFactor() async {
2022-08-29 14:43:31 +00:00
final AlertDialog alert = AlertDialog(
2022-07-04 06:02:17 +00:00
title: const Text("Disable two-factor"),
content: const Text(
"Are you sure you want to disable two-factor authentication?",
),
actions: [
TextButton(
child: Text(
2022-06-18 12:23:51 +00:00
"No",
style: TextStyle(
2022-07-12 06:30:02 +00:00
color: Theme.of(context).colorScheme.greenAlternative,
),
),
onPressed: () {
Navigator.of(context, rootNavigator: true).pop('dialog');
},
),
TextButton(
2022-07-04 06:02:17 +00:00
child: const Text(
2022-06-18 12:23:51 +00:00
"Yes",
style: TextStyle(
color: Colors.red,
),
),
onPressed: () async {
await UserService.instance.disableTwoFactor(context);
Navigator.of(context, rootNavigator: true).pop('dialog');
},
),
],
);
2022-11-16 11:54:48 +00:00
await showDialog(
context: context,
builder: (BuildContext context) {
return alert;
},
);
}
}