Modify Settings page to work for offline mode

This commit is contained in:
vishnukvmd
2023-08-16 13:28:09 +05:30
parent 54817cc100
commit 7d8a85b861
3 changed files with 187 additions and 160 deletions

View File

@@ -31,9 +31,11 @@ class SecuritySectionWidget extends StatefulWidget {
class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
final _config = Configuration.instance;
late bool _hasLoggedIn;
@override
void initState() {
_hasLoggedIn = _config.hasConfiguredAccount();
super.initState();
}
@@ -53,49 +55,103 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
}
Widget _getSectionOptions(BuildContext context) {
final bool? canDisableMFA = UserService.instance.canDisableEmailMFA();
if (canDisableMFA == null) {
// We don't know if the user can disable MFA yet, so we fetch the info
UserService.instance.getUserDetailsV2().ignore();
}
final l10n = context.l10n;
final List<Widget> children = [];
children.addAll([
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: l10n.recoveryKey,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
l10n.authToViewYourRecoveryKey,
);
if (hasAuthenticated) {
String recoveryKey;
try {
recoveryKey =
Sodium.bin2hex(Configuration.instance.getRecoveryKey());
} catch (e) {
showGenericErrorDialog(context: context);
return;
}
routeToPage(
if (_hasLoggedIn) {
final bool? canDisableMFA = UserService.instance.canDisableEmailMFA();
if (canDisableMFA == null) {
// We don't know if the user can disable MFA yet, so we fetch the info
UserService.instance.getUserDetailsV2().ignore();
}
children.addAll([
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: l10n.recoveryKey,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
RecoveryKeyPage(
recoveryKey,
l10n.ok,
showAppBar: true,
onDone: () {},
),
l10n.authToViewYourRecoveryKey,
);
}
},
),
if (hasAuthenticated) {
String recoveryKey;
try {
recoveryKey =
Sodium.bin2hex(Configuration.instance.getRecoveryKey());
} catch (e) {
showGenericErrorDialog(context: context);
return;
}
routeToPage(
context,
RecoveryKeyPage(
recoveryKey,
l10n.ok,
showAppBar: true,
onDone: () {},
),
);
}
},
),
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: l10n.emailVerificationToggle,
),
trailingWidget: ToggleSwitchWidget(
value: () => UserService.instance.hasEmailMFAEnabled(),
onChanged: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
l10n.authToChangeEmailVerificationSetting,
);
final isEmailMFAEnabled =
UserService.instance.hasEmailMFAEnabled();
if (hasAuthenticated) {
await updateEmailMFA(!isEmailMFAEnabled);
if (mounted) {
setState(() {});
}
}
},
),
),
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: context.l10n.viewActiveSessions,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
context.l10n.authToViewYourActiveSessions,
);
if (hasAuthenticated) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return const SessionsPage();
},
),
);
}
},
),
]);
} else {
children.add(sectionOptionSpacing);
}
children.addAll([
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: l10n.lockscreen,
@@ -117,54 +173,6 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
),
),
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: l10n.emailVerificationToggle,
),
trailingWidget: ToggleSwitchWidget(
value: () => UserService.instance.hasEmailMFAEnabled(),
onChanged: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
l10n.authToChangeEmailVerificationSetting,
);
final isEmailMFAEnabled = UserService.instance.hasEmailMFAEnabled();
if (hasAuthenticated) {
await updateEmailMFA(!isEmailMFAEnabled);
if (mounted) {
setState(() {});
}
}
},
),
),
sectionOptionSpacing,
MenuItemWidget(
captionedTextWidget: CaptionedTextWidget(
title: context.l10n.viewActiveSessions,
),
pressedColor: getEnteColorScheme(context).fillFaint,
trailingIcon: Icons.chevron_right_outlined,
trailingIconIsMuted: true,
onTap: () async {
final hasAuthenticated = await LocalAuthenticationService.instance
.requestLocalAuthentication(
context,
context.l10n.authToViewYourActiveSessions,
);
if (hasAuthenticated) {
Navigator.of(context).push(
MaterialPageRoute(
builder: (BuildContext context) {
return const SessionsPage();
},
),
);
}
},
),
sectionOptionSpacing,
]);
return Column(
children: children,
@@ -173,12 +181,19 @@ class _SecuritySectionWidgetState extends State<SecuritySectionWidget> {
Future<void> updateEmailMFA(bool isEnabled) async {
try {
final UserDetails details = await UserService.instance.getUserDetailsV2(memoryCount: false);
if(details.profileData?.canDisableEmailMFA == false) {
await routeToPage(context, RequestPasswordVerificationPage(onPasswordVerified: (Uint8List keyEncryptionKey) async {
final Uint8List loginKey = await CryptoUtil.deriveLoginKey(keyEncryptionKey);
await UserService.instance.registerOrUpdateSrp(loginKey);
},),);
final UserDetails details =
await UserService.instance.getUserDetailsV2(memoryCount: false);
if (details.profileData?.canDisableEmailMFA == false) {
await routeToPage(
context,
RequestPasswordVerificationPage(
onPasswordVerified: (Uint8List keyEncryptionKey) async {
final Uint8List loginKey =
await CryptoUtil.deriveLoginKey(keyEncryptionKey);
await UserService.instance.registerOrUpdateSrp(loginKey);
},
),
);
}
await UserService.instance.updateEmailMFA(isEnabled);
} catch (e) {

View File

@@ -1,5 +1,6 @@
import 'dart:io';
import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/l10n/l10n.dart';
import 'package:ente_auth/models/subscription.dart';
import 'package:ente_auth/services/billing_service.dart';
@@ -18,50 +19,53 @@ class SupportDevWidget extends StatelessWidget {
final l10n = context.l10n;
// fetch
return FutureBuilder<Subscription>(
future: BillingService.instance.getSubscription(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final subscription = snapshot.data;
if (subscription != null && subscription.productID == "free") {
return GestureDetector(
onTap: () {
launchUrl(Uri.parse("https://ente.io"));
},
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 12.0, horizontal: 6),
child: Column(
children: [
StyledText(
text: l10n.supportDevs,
tags: {
'bold-green': StyledTextTag(
style: TextStyle(
fontWeight: FontWeight.bold,
color: getEnteColorScheme(context).primaryGreen,
),
),
},
),
const Padding(padding: EdgeInsets.all(6)),
Platform.isAndroid
? Text(
l10n.supportDiscount,
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.grey,
if (Configuration.instance.hasConfiguredAccount()) {
return FutureBuilder<Subscription>(
future: BillingService.instance.getSubscription(),
builder: (context, snapshot) {
if (snapshot.hasData) {
final subscription = snapshot.data;
if (subscription != null && subscription.productID == "free") {
return GestureDetector(
onTap: () {
launchUrl(Uri.parse("https://ente.io"));
},
child: Padding(
padding:
const EdgeInsets.symmetric(vertical: 12.0, horizontal: 6),
child: Column(
children: [
StyledText(
text: l10n.supportDevs,
tags: {
'bold-green': StyledTextTag(
style: TextStyle(
fontWeight: FontWeight.bold,
color: getEnteColorScheme(context).primaryGreen,
),
)
: const SizedBox.shrink(),
],
),
},
),
const Padding(padding: EdgeInsets.all(6)),
Platform.isAndroid
? Text(
l10n.supportDiscount,
textAlign: TextAlign.center,
style: const TextStyle(
color: Colors.grey,
),
)
: const SizedBox.shrink(),
],
),
),
),
);
);
}
}
}
return const SizedBox.shrink();
},
);
return const SizedBox.shrink();
},
);
}
return const SizedBox.shrink();
}
}

View File

@@ -1,5 +1,6 @@
import 'dart:io';
import 'package:ente_auth/core/configuration.dart';
import 'package:ente_auth/services/user_service.dart';
import 'package:ente_auth/theme/colors.dart';
import 'package:ente_auth/theme/ente_theme.dart';
@@ -18,12 +19,15 @@ import 'package:flutter/material.dart';
class SettingsPage extends StatelessWidget {
final ValueNotifier<String?> emailNotifier;
const SettingsPage({Key? key, required this.emailNotifier}) : super(key: key);
final _hasLoggedIn = Configuration.instance.hasConfiguredAccount();
SettingsPage({Key? key, required this.emailNotifier}) : super(key: key);
@override
Widget build(BuildContext context) {
UserService.instance.getUserDetailsV2().ignore();
if (_hasLoggedIn) {
UserService.instance.getUserDetailsV2().ignore();
}
final enteColorScheme = getEnteColorScheme(context);
return Scaffold(
body: Container(
@@ -35,33 +39,37 @@ class SettingsPage extends StatelessWidget {
Widget _getBody(BuildContext context, EnteColorScheme colorScheme) {
final enteTextTheme = getEnteTextTheme(context);
const sectionSpacing = SizedBox(height: 8);
final List<Widget> contents = [];
contents.add(
Container(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Align(
alignment: Alignment.centerLeft,
child: AnimatedBuilder(
// [AnimatedBuilder] accepts any [Listenable] subtype.
animation: emailNotifier,
builder: (BuildContext context, Widget? child) {
return Text(
emailNotifier.value!,
style: enteTextTheme.body.copyWith(
color: colorScheme.textMuted,
overflow: TextOverflow.ellipsis,
),
);
},
if (_hasLoggedIn) {
contents.add(
Container(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Align(
alignment: Alignment.centerLeft,
child: AnimatedBuilder(
// [AnimatedBuilder] accepts any [Listenable] subtype.
animation: emailNotifier,
builder: (BuildContext context, Widget? child) {
return Text(
emailNotifier.value!,
style: enteTextTheme.body.copyWith(
color: colorScheme.textMuted,
overflow: TextOverflow.ellipsis,
),
);
},
),
),
),
),
);
const sectionSpacing = SizedBox(height: 8);
contents.add(const SizedBox(height: 12));
);
contents.addAll([
const SizedBox(height: 12),
AccountSectionWidget(),
sectionSpacing,
]);
}
contents.addAll([
AccountSectionWidget(),
sectionSpacing,
DataSectionWidget(),
sectionSpacing,
const SecuritySectionWidget(),