refactor: use gorouter for navigation
This commit is contained in:
@@ -0,0 +1,69 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:example/routes/routes.dart';
|
||||
import 'package:example/utils/notifications_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
|
||||
|
||||
class LocalNotificationObserver extends NavigatorObserver {
|
||||
Route? currentRoute;
|
||||
late final StreamSubscription _subscription;
|
||||
|
||||
LocalNotificationObserver(
|
||||
StreamChatClient client,
|
||||
GlobalKey<NavigatorState> navigatorKey,
|
||||
) {
|
||||
_subscription = client
|
||||
.on(
|
||||
EventType.messageNew,
|
||||
EventType.notificationMessageNew,
|
||||
)
|
||||
.listen((event) {
|
||||
_handleEvent(event, client, navigatorKey);
|
||||
});
|
||||
}
|
||||
|
||||
void _handleEvent(Event event, StreamChatClient client,
|
||||
GlobalKey<NavigatorState> navigatorKey) {
|
||||
if (event.message?.user?.id == client.state.currentUser?.id) {
|
||||
return;
|
||||
}
|
||||
final channelId = event.cid;
|
||||
if (currentRoute?.settings.name == Routes.CHANNEL_PAGE.name) {
|
||||
final args = currentRoute?.settings.arguments as Map<String, String>;
|
||||
if (args['cid'] == channelId) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
showLocalNotification(
|
||||
event,
|
||||
client.state.currentUser!.id,
|
||||
navigatorKey.currentState!.context,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void didPop(Route route, Route? previousRoute) {
|
||||
currentRoute = route;
|
||||
}
|
||||
|
||||
@override
|
||||
void didPush(Route route, Route? previousRoute) {
|
||||
currentRoute = route;
|
||||
}
|
||||
|
||||
@override
|
||||
void didRemove(Route route, Route? previousRoute) {
|
||||
currentRoute = route;
|
||||
}
|
||||
|
||||
@override
|
||||
void didReplace({Route? newRoute, Route? oldRoute}) {
|
||||
currentRoute = newRoute;
|
||||
}
|
||||
|
||||
void dispose() {
|
||||
_subscription.cancel();
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
import 'package:example/pages/channel_page.dart';
|
||||
import 'package:example/utils/localizations.dart';
|
||||
import 'package:example/routes/routes.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_local_notifications/flutter_local_notifications.dart'
|
||||
hide Message;
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
|
||||
|
||||
void showLocalNotification(
|
||||
@@ -35,7 +35,7 @@ void showLocalNotification(
|
||||
onSelectNotification: (channelCid) async {
|
||||
if (channelCid != null) {
|
||||
final client = StreamChat.of(context).client;
|
||||
final navigator = Navigator.of(context);
|
||||
final router = GoRouter.of(context);
|
||||
|
||||
var channel = client.state.channels[channelCid];
|
||||
|
||||
@@ -50,11 +50,9 @@ void showLocalNotification(
|
||||
await channel.watch();
|
||||
}
|
||||
|
||||
navigator.pushNamed(
|
||||
Routes.CHANNEL_PAGE,
|
||||
arguments: ChannelPageArgs(
|
||||
channel: channel,
|
||||
),
|
||||
router.pushNamed(
|
||||
Routes.CHANNEL_PAGE.name,
|
||||
params: Routes.CHANNEL_PAGE.params(channel),
|
||||
);
|
||||
}
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user