Skip to content

Commit f35270c

Browse files
FeichtmeierCopilot
andauthored
chore: refactor navigation handling to use teleport method and add create recovery key localization (#149)
Co-authored-by: Copilot <copilot@github.com>
1 parent ea86d08 commit f35270c

25 files changed

Lines changed: 257 additions & 191 deletions

lib/authentication/view/chat_login_page.dart

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,7 @@ class _ChatLoginPageState extends State<ChatLoginPage> {
4242
select: (AuthenticationManager m) => m.loginCommand,
4343
handler: (context, userId, cancel) {
4444
if (userId != null && context.mounted) {
45-
Navigator.of(context).pushAndRemoveUntil(
46-
MaterialPageRoute(builder: (_) => const CheckEncryptionSetupPage()),
47-
(route) => false,
48-
);
45+
context.teleport((_) => const CheckEncryptionSetupPage());
4946
}
5047
},
5148
);

lib/authentication/view/chat_matrix_id_login_page.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import 'package:flutter/material.dart';
2-
import 'package:matrix/matrix.dart';
32
import 'package:flutter_it/flutter_it.dart';
3+
import 'package:matrix/matrix.dart';
44
import 'package:yaru/yaru.dart';
55

66
import '../../app/app_config.dart';
77
import '../../common/constants.dart';
8+
import '../../common/view/build_context_x.dart';
89
import '../../common/view/theme.dart';
910
import '../../encryption/view/check_encryption_setup_page.dart';
1011
import '../../l10n/l10n.dart';
@@ -57,10 +58,7 @@ class _ChatMatrixIdLoginPageState extends State<ChatMatrixIdLoginPage> {
5758
select: (AuthenticationManager m) => m.loginCommand,
5859
handler: (context, userId, cancel) {
5960
if (userId != null && context.mounted) {
60-
Navigator.of(context).pushAndRemoveUntil(
61-
MaterialPageRoute(builder: (_) => const CheckEncryptionSetupPage()),
62-
(route) => false,
63-
);
61+
context.teleport((_) => const CheckEncryptionSetupPage());
6462
}
6563
},
6664
);

lib/common/view/build_context_x.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:flutter/material.dart';
22

3+
import '../../extensions/navigator_x.dart';
34
import 'ui_constants.dart';
45

56
extension BuildContextX on BuildContext {
@@ -9,4 +10,9 @@ extension BuildContextX on BuildContext {
910
ThemeData get theme => Theme.of(this);
1011
ColorScheme get colorScheme => theme.colorScheme;
1112
TextTheme get textTheme => theme.textTheme;
13+
NavigatorState get navigator => Navigator.of(this);
14+
Future<T?> teleport<T extends Object?>(
15+
Widget Function(BuildContext) builder,
16+
) => navigator.teleport(builder);
17+
void pop<T extends Object?>([T? result]) => navigator.pop(result);
1218
}

lib/encryption/encryption_manager.dart

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ class EncryptionManager {
1111
required Client client,
1212
required FlutterSecureStorage secureStorage,
1313
}) : _client = client,
14-
_secureStorage = secureStorage;
14+
_secureStorage = secureStorage {
15+
loadRecoveryKeyFromSecureStorageCommand.run();
16+
}
1517

1618
final Client _client;
1719
final FlutterSecureStorage _secureStorage;
@@ -96,6 +98,11 @@ class EncryptionManager {
9698
initialValue: null,
9799
);
98100

101+
late final Command<void, void> deleteRecoveryKeyFromSecureStorageCommand =
102+
Command.createAsyncNoParamNoResult(
103+
() => _secureStorage.delete(key: secureStorageKey),
104+
);
105+
99106
String get secureStorageKey => 'ssss_recovery_key_${_client.userID}';
100107

101108
Future<KeyVerification> startKeyVerification() async {

lib/encryption/view/chat_global_handlers.dart

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import 'package:flutter/material.dart';
21
import 'package:flutter_it/flutter_it.dart';
32
import 'package:matrix/matrix.dart';
43

54
import '../../authentication/authentication_service.dart';
65
import '../../authentication/view/chat_login_page.dart';
76
import '../../authentication/view/uia_request_handler.dart';
7+
import '../../common/view/build_context_x.dart';
88

99
mixin ChatGlobalHandlerMixin {
1010
void registerGlobalChatHandlers() {
@@ -25,10 +25,7 @@ mixin ChatGlobalHandlerMixin {
2525
select: (AuthenticationService m) => m.loginStateStream,
2626
handler: (context, newValue, cancel) {
2727
if (newValue.hasData && newValue.data != LoginState.loggedIn) {
28-
Navigator.of(context).pushAndRemoveUntil(
29-
MaterialPageRoute(builder: (_) => const ChatLoginPage()),
30-
(route) => false,
31-
);
28+
context.teleport((_) => const ChatLoginPage());
3229
}
3330
},
3431
);

lib/encryption/view/check_encryption_setup_page.dart

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'package:flutter_it/flutter_it.dart';
33

44
import '../../app/view/splash_page.dart';
55
import '../../chat_master/view/chat_master_detail_page.dart';
6+
import '../../common/view/build_context_x.dart';
67
import '../../l10n/l10n.dart';
78
import '../encryption_manager.dart';
89
import 'encryption_setup_error_page.dart';
@@ -23,25 +24,16 @@ class CheckEncryptionSetupPage extends StatelessWidget with WatchItMixin {
2324
m.checkIfEncryptionSetupIsNeededCommand.results,
2425
handler: (context, newValue, cancel) {
2526
if (newValue.error != null) {
26-
Navigator.of(context).pushAndRemoveUntil(
27-
MaterialPageRoute(
28-
builder: (_) => EncryptionSetupErrorPage(error: newValue.error),
29-
),
30-
(route) => false,
27+
context.teleport(
28+
(_) => EncryptionSetupErrorPage(error: newValue.error),
3129
);
3230
} else if (newValue.data != null) {
3331
final cryptoIdentityState = newValue.data!;
3432
if (cryptoIdentityState.connected &&
3533
cryptoIdentityState.initialized) {
36-
Navigator.of(context).pushAndRemoveUntil(
37-
MaterialPageRoute(builder: (_) => const ChatMasterDetailPage()),
38-
(route) => false,
39-
);
34+
context.teleport((_) => const ChatMasterDetailPage());
4035
} else {
41-
Navigator.of(context).pushAndRemoveUntil(
42-
MaterialPageRoute(builder: (_) => const UnlockChatPage()),
43-
(route) => false,
44-
);
36+
context.teleport((_) => const UnlockChatPage());
4537
}
4638
}
4739
},

lib/encryption/view/encryption_setup_error_page.dart

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,8 @@ class EncryptionSetupErrorPage extends StatelessWidget {
3535
),
3636
ElevatedButton.icon(
3737
icon: const Icon(YaruIcons.refresh),
38-
onPressed: () {
39-
Navigator.of(context).pushAndRemoveUntil(
40-
MaterialPageRoute(
41-
builder: (_) => const CheckEncryptionSetupPage(),
42-
),
43-
(route) => false,
44-
);
45-
},
38+
onPressed: () =>
39+
context.teleport((_) => const CheckEncryptionSetupPage()),
4640
label: Text(l10n.tryAgain),
4741
),
4842
const ChatSettingsLogoutButton(),

lib/encryption/view/init_crypto_identity_button.dart

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class InitCryptoIdentityButton extends StatelessWidget {
3535
context: context,
3636
builder: (context) => InitCryptoIdentityConfirmationDialog(
3737
title: dialogTitle ?? l10n.resetRecoveryKeyTitle,
38+
confirmLabel: dialogTitle,
3839
),
3940
);
4041
},

lib/encryption/view/init_crypto_identity_confirmation_dialog.dart

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,17 @@ import '../../common/view/confirm.dart';
77
import '../../common/view/ui_constants.dart';
88
import '../../l10n/l10n.dart';
99
import '../encryption_manager.dart';
10-
import 'init_crypto_identify_progress_page.dart';
10+
import 'init_crypto_identity_progress_page.dart';
1111

1212
class InitCryptoIdentityConfirmationDialog extends StatefulWidget {
13-
const InitCryptoIdentityConfirmationDialog({super.key, required this.title});
13+
const InitCryptoIdentityConfirmationDialog({
14+
super.key,
15+
required this.title,
16+
this.confirmLabel,
17+
});
1418

1519
final String title;
20+
final String? confirmLabel;
1621

1722
@override
1823
State<InitCryptoIdentityConfirmationDialog> createState() =>
@@ -158,7 +163,8 @@ class _InitCryptoIdentityConfirmationDialogState
158163
),
159164
],
160165
),
161-
confirmLabel: l10n.resetRecoveryKeyConfirmationLabel,
166+
confirmLabel:
167+
widget.confirmLabel ?? l10n.resetRecoveryKeyConfirmationLabel,
162168
onConfirm: () {
163169
di<EncryptionManager>().initCryptoIdentityCommand.run(
164170
NewCryptoIdentityCapsule(
@@ -175,12 +181,7 @@ class _InitCryptoIdentityConfirmationDialogState
175181

176182
Navigator.of(context).pop();
177183

178-
Navigator.of(context).pushAndRemoveUntil(
179-
MaterialPageRoute(
180-
builder: (_) => const InitCryptoIdentifyProgressPage(),
181-
),
182-
(route) => false,
183-
);
184+
context.teleport((_) => const InitCryptoIdentityProgressPage());
184185
},
185186
);
186187
}

lib/encryption/view/init_crypto_identify_progress_page.dart renamed to lib/encryption/view/init_crypto_identity_progress_page.dart

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@ import 'package:flutter/material.dart';
22
import 'package:flutter_it/flutter_it.dart';
33

44
import '../../app/view/splash_page.dart';
5+
import '../../common/view/build_context_x.dart';
56
import '../../l10n/l10n.dart';
67
import '../encryption_manager.dart';
78
import 'chat_global_handlers.dart';
89
import 'encryption_setup_error_page.dart';
910
import 'new_key_created_page.dart';
1011

11-
class InitCryptoIdentifyProgressPage extends StatelessWidget
12+
class InitCryptoIdentityProgressPage extends StatelessWidget
1213
with WatchItMixin, ChatGlobalHandlerMixin {
13-
const InitCryptoIdentifyProgressPage({super.key});
14+
const InitCryptoIdentityProgressPage({super.key});
1415

1516
@override
1617
Widget build(BuildContext context) {
@@ -20,21 +21,12 @@ class InitCryptoIdentifyProgressPage extends StatelessWidget
2021
select: (EncryptionManager m) => m.initCryptoIdentityCommand.results,
2122
handler: (context, results, cancel) {
2223
if (results.error != null) {
23-
Navigator.of(context).pushAndRemoveUntil(
24-
MaterialPageRoute(
25-
builder: (context) =>
26-
EncryptionSetupErrorPage(error: results.error!),
27-
),
28-
(route) => false,
24+
context.teleport(
25+
(context) => EncryptionSetupErrorPage(error: results.error!),
2926
);
3027
} else if (results.data != null) {
31-
final newSsssKey = results.data!;
32-
Navigator.of(context).pushAndRemoveUntil(
33-
MaterialPageRoute(
34-
builder: (context) =>
35-
NewKeyCreatedPage(encryptionKey: newSsssKey),
36-
),
37-
(route) => false,
28+
context.teleport(
29+
(context) => NewKeyCreatedPage(encryptionKey: results.data!),
3830
);
3931
}
4032
},

0 commit comments

Comments
 (0)