refactor: Migrate routes to go router

This commit is contained in:
krille-chan 2023-08-07 18:40:02 +02:00
commit ee957ab1f6
No known key found for this signature in database
52 changed files with 584 additions and 612 deletions

View file

@ -37,7 +37,7 @@ import 'package:fluffychat/utils/matrix_sdk_extensions/client_stories_extension.
import 'package:fluffychat/utils/push_helper.dart';
import '../config/app_config.dart';
import '../config/setting_keys.dart';
import '../widgets/fluffy_chat_app.dart';
import '../widgets/matrix.dart';
import 'famedlysdk_store.dart';
import 'platform_infos.dart';
@ -52,8 +52,8 @@ class BackgroundPush {
final FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin =
FlutterLocalNotificationsPlugin();
Client client;
BuildContext? context;
GlobalKey<VRouterState> get router => FluffyChatApp.routerKey;
MatrixState? matrix;
BuildContext? get context => matrix?.navigatorContext;
String? _fcmToken;
void Function(String errorMsg, {Uri? link})? onFcmError;
L10n? l10n;
@ -77,6 +77,7 @@ class BackgroundPush {
onRoomSync ??= client.onSync.stream
.where((s) => s.hasRoomUpdate)
.listen((s) => _onClearingPush(getFromServer: false));
final context = this.context;
firebase?.setListeners(
onMessage: (message) => pushHelper(
PushNotification.fromJson(
@ -84,7 +85,9 @@ class BackgroundPush {
),
client: client,
l10n: l10n,
activeRoomId: router.currentState?.pathParameters['roomid'],
activeRoomId: context == null
? null
: GoRouterState.of(context).pathParameters['roomid'],
onSelectNotification: goToRoom,
),
);
@ -104,12 +107,11 @@ class BackgroundPush {
}
factory BackgroundPush(
Client client,
BuildContext context, {
MatrixState matrix, {
final void Function(String errorMsg, {Uri? link})? onFcmError,
}) {
final instance = BackgroundPush.clientOnly(client);
instance.context = context;
final instance = BackgroundPush.clientOnly(matrix.client);
instance.matrix = matrix;
// ignore: prefer_initializing_formals
instance.onFcmError = onFcmError;
return instance;
@ -250,8 +252,7 @@ class BackgroundPush {
.then((details) {
if (details == null ||
!details.didNotificationLaunchApp ||
_wentToRoomOnStartup ||
router.currentState == null) {
_wentToRoomOnStartup) {
return;
}
_wentToRoomOnStartup = true;
@ -303,7 +304,7 @@ class BackgroundPush {
try {
final roomId = response?.payload;
Logs().v('[Push] Attempting to go to room $roomId...');
if (router.currentState == null || roomId == null) {
if (roomId == null) {
return;
}
await client.roomsLoading;
@ -314,7 +315,7 @@ class BackgroundPush {
?.content
.tryGet<String>('type') ==
ClientStoriesExtension.storiesRoomType;
router.currentState!.toSegments([isStory ? 'stories' : 'rooms', roomId]);
context?.go(['', isStory ? 'stories' : 'rooms', roomId].join('/'));
} catch (e, s) {
Logs().e('[Push] Failed to open room', e, s);
}
@ -390,11 +391,14 @@ class BackgroundPush {
);
// UP may strip the devices list
data['devices'] ??= [];
final context = this.context;
await pushHelper(
PushNotification.fromJson(data),
client: client,
l10n: l10n,
activeRoomId: router.currentState?.pathParameters['roomid'],
activeRoomId: context == null
? null
: GoRouterState.of(context).pathParameters['roomid'],
);
}

View file

@ -46,20 +46,29 @@ abstract class PlatformInfos {
final version = await PlatformInfos.getVersion();
showAboutDialog(
context: context,
useRootNavigator: false,
children: [
Text('Version: $version'),
OutlinedButton(
TextButton.icon(
onPressed: () => launchUrlString(AppConfig.sourceCodeUrl),
child: Text(L10n.of(context)!.sourceCode),
icon: const Icon(Icons.source_outlined),
label: Text(L10n.of(context)!.sourceCode),
),
OutlinedButton(
TextButton.icon(
onPressed: () => launchUrlString(AppConfig.emojiFontUrl),
child: const Text(AppConfig.emojiFontName),
icon: const Icon(Icons.emoji_emotions_outlined),
label: const Text(AppConfig.emojiFontName),
),
OutlinedButton(
onPressed: () => VRouter.of(context).to('logs'),
child: const Text('Logs'),
Builder(
builder: (context) {
return TextButton.icon(
onPressed: () {
Navigator.of(context).pop();
context.go('/logs');
},
icon: const Icon(Icons.list_outlined),
label: const Text('Logs'),
);
},
),
],
applicationIcon: Image.asset(

View file

@ -49,7 +49,7 @@ extension UiaRequestManager on MatrixState {
case AuthenticationTypes.emailIdentity:
if (currentThreepidCreds == null) {
return uiaRequest.cancel(
UiaException(L10n.of(widget.context)!.serverRequiresEmail),
UiaException(L10n.of(navigatorContext)!.serverRequiresEmail),
);
}
final auth = AuthenticationThreePidCreds(

View file

@ -172,17 +172,20 @@ class UrlLauncher {
if (room != null) {
if (room.isSpace) {
// TODO: Implement navigate to space
VRouter.of(context).toSegments(['rooms']);
context.go(['', 'rooms'].join('/'));
return;
}
// we have the room, so....just open it
if (event != null) {
VRouter.of(context).toSegments(
['rooms', room.id],
queryParameters: {'event': event},
context.go(
Uri(
pathSegments: ['rooms', room.id],
queryParameters: {'event': event},
).toString(),
);
} else {
VRouter.of(context).toSegments(['rooms', room.id]);
context.go(['', 'rooms', room.id].join('/'));
}
return;
} else {
@ -216,12 +219,14 @@ class UrlLauncher {
future: () => Future.delayed(const Duration(seconds: 2)),
);
if (event != null) {
VRouter.of(context).toSegments(
['rooms', response.result!],
queryParameters: {'event': event},
context.go(
Uri(
pathSegments: ['rooms', response.result!],
queryParameters: {'event': event},
).toString(),
);
} else {
VRouter.of(context).toSegments(['rooms', response.result!]);
context.go(['', 'rooms', response.result!].join('/'));
}
}
}

View file

@ -12,14 +12,15 @@ import 'package:webrtc_interface/webrtc_interface.dart' hide Navigator;
import 'package:fluffychat/pages/chat_list/chat_list.dart';
import 'package:fluffychat/pages/dialer/dialer.dart';
import 'package:fluffychat/utils/platform_infos.dart';
import 'package:fluffychat/widgets/fluffy_chat_app.dart';
import '../../utils/famedlysdk_store.dart';
import '../../utils/voip/callkeep_manager.dart';
import '../../utils/voip/user_media_manager.dart';
import '../widgets/matrix.dart';
class VoipPlugin with WidgetsBindingObserver implements WebRTCDelegate {
final Client client;
VoipPlugin(this.client) {
final MatrixState matrix;
Client get client => matrix.client;
VoipPlugin(this.matrix) {
voip = VoIP(client, this);
Connectivity()
.onConnectivityChanged
@ -40,6 +41,7 @@ class VoipPlugin with WidgetsBindingObserver implements WebRTCDelegate {
late VoIP voip;
ConnectivityResult? _currentConnectivity;
OverlayEntry? overlayEntry;
BuildContext get context => matrix.navigatorContext;
void _handleNetworkChanged(ConnectivityResult result) async {
/// Got a new connectivity status!
@ -59,9 +61,8 @@ class VoipPlugin with WidgetsBindingObserver implements WebRTCDelegate {
}
void addCallingOverlay(String callId, CallSession call) {
final context = kIsWeb
? ChatList.contextForVoip!
: FluffyChatApp.matrixKey.currentContext!; // web is weird
final context =
kIsWeb ? ChatList.contextForVoip! : this.context; // web is weird
if (overlayEntry != null) {
Logs().e('[VOIP] addCallingOverlay: The call session already exists?');
@ -166,8 +167,7 @@ class VoipPlugin with WidgetsBindingObserver implements WebRTCDelegate {
addCallingOverlay(call.callId, call);
try {
if (!hasCallingAccount) {
ScaffoldMessenger.of(FluffyChatApp.matrixKey.currentContext!)
.showSnackBar(
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(
content: Text(
'No calling accounts found (used for native calls UI)',