[94] chore: remove excessive logs

This commit is contained in:
kanat
2023-05-10 13:39:20 -07:00
parent 13f923ab60
commit 78aa631164
2 changed files with 55 additions and 122 deletions
+54 -86
View File
@@ -8,6 +8,7 @@ import 'package:example/state/init_data.dart';
import 'package:example/utils/app_config.dart';
import 'package:example/utils/local_notification_observer.dart';
import 'package:example/utils/localizations.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
@@ -16,7 +17,6 @@ import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:go_router/go_router.dart';
import 'package:provider/provider.dart';
import 'package:sentry_flutter/sentry_flutter.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:stream_chat_flutter/stream_chat_flutter.dart';
import 'package:stream_chat_localizations/stream_chat_localizations.dart';
import 'package:stream_chat_persistence/stream_chat_persistence.dart';
@@ -25,12 +25,10 @@ import 'package:streaming_shared_preferences/streaming_shared_preferences.dart';
import 'firebase_options.dart';
@pragma('vm:entry-point')
Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
Future<void> _onFirebaseBackgroundMessage(RemoteMessage message) async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
debugPrint('[onBackgroundMessage] #firebase; message: ${message.toMap()}');
final data = message.data;
if (data['type'] != 'message.new') {
return;
@@ -42,7 +40,6 @@ Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
userId = await secureStorage.read(key: kStreamUserId);
token = await secureStorage.read(key: kStreamToken);
}
debugPrint('[onBackgroundMessage] #firebase; apiKey: $apiKey, userId: $userId, token: $token');
if (userId == null || token == null) {
return;
}
@@ -59,18 +56,7 @@ Future<void> _firebaseMessagingBackgroundHandler(RemoteMessage message) async {
final messageId = data['id'];
final cid = data['cid'];
final response = await client.getMessage(messageId);
debugPrint('[onBackgroundMessage] #firebase; response: $response');
await persistenceClient.updateMessages(cid, [response.message]);
debugPrint('[onBackgroundMessage] #firebase; saved');
}
Future<void> _firebaseMessagingForegroundHandler(RemoteMessage message) async {
debugPrint('[onForegroundMessage] #firebase; message: ${message.toMap()}');
}
Future<void> _firebaseMessagingOpenedHandler(RemoteMessage message) async {
debugPrint('[onOpenedMessage] #firebase; message: ${message.toMap()}');
}
final chatPersistentClient = StreamChatPersistenceClient(
@@ -127,7 +113,6 @@ class _StreamChatSampleAppState extends State<StreamChatSampleApp>
userId = await secureStorage.read(key: kStreamUserId);
token = await secureStorage.read(key: kStreamToken);
}
debugPrint('[initConnection] #firebase; apiKey: $apiKey, userId: $userId, token: $token');
final client = buildStreamChatClient(apiKey ?? kDefaultStreamApiKey);
if (userId != null && token != null) {
@@ -148,71 +133,55 @@ class _StreamChatSampleAppState extends State<StreamChatSampleApp>
.map((it) => it?.id)
.distinct()
.listen((userId) async {
debugPrint('[onUserIdSet] #firebase; userId: "$userId"');
if (userId != null) {
FirebaseMessaging.onBackgroundMessage(
_firebaseMessagingBackgroundHandler);
firebaseSubscriptions.add(FirebaseMessaging.onMessage
.listen(_firebaseMessagingForegroundHandler));
firebaseSubscriptions
.add(FirebaseMessaging.onMessageOpenedApp.listen((message) async {
debugPrint(
'[onOpenedMessage] #firebase; message: ${message.toMap()}');
final channelCid = (message.data['cid'] as String?) ?? '';
final channelType = (message.data['channel_type'] as String?) ?? '';
final channelId = (message.data['channel_id'] as String?) ?? '';
debugPrint('[onOpenedMessage] #firebase; channelCid; $channelCid, channelType: $channelType, channelId: $channelId');
var channel = client.state.channels[channelCid];
debugPrint('[onOpenedMessage] #firebase; channel1: $channel');
if (channel == null) {
channel = client.channel(
channelId,
id: channelId,
);
final state = await channel.watch();
debugPrint('[onOpenedMessage] #firebase; channelState: $state');
}
debugPrint('[onOpenedMessage] #firebase; channel2: $channel');
debugPrint('[onOpenedMessage] #firebase; #1');
WidgetsBinding.instance.addPostFrameCallback((timeStamp) async {
if (channel == null) {
debugPrint('[onOpenedMessage] #firebase; rejected (channel is null)');
return;
}
try {
debugPrint('[onOpenedMessage] #firebase; #2: ${_navigatorKey.currentContext}');
final router = GoRouter.of(_navigatorKey.currentContext!);
debugPrint('[onOpenedMessage] #firebase; #3: $router');
router.pushNamed(
Routes.CHANNEL_PAGE.name,
params: Routes.CHANNEL_PAGE.params(channel),
);
debugPrint('[onOpenedMessage] #firebase; #4');
} catch (e, stk) {
debugPrint('[onOpenedMessage] #firebase; failed: $e; $stk');
}
});
}));
firebaseSubscriptions.add(
FirebaseMessaging.instance.onTokenRefresh.listen((token) async {
debugPrint('[onTokenRefresh] #firebase; token: "$token"');
await client.addDevice(token, PushProvider.firebase);
debugPrint('[onTokenRefresh] #firebase; token set: $token');
}));
await FirebaseMessaging.instance.requestPermission();
FirebaseMessaging.onBackgroundMessage(_onFirebaseBackgroundMessage);
firebaseSubscriptions.add(FirebaseMessaging.onMessageOpenedApp
.listen(_onFirebaseMessageOpenedApp(client)));
firebaseSubscriptions.add(FirebaseMessaging.instance.onTokenRefresh
.listen(_onFirebaseTokenRefresh(client)));
final token = await FirebaseMessaging.instance.getToken();
debugPrint('[initFirebaseMessaging] #firebase; token: "$token"');
if (token != null) {
await client.addDevice(token, PushProvider.firebase);
debugPrint('[initFirebaseMessaging] #firebase; token set: $token');
}
} else {
firebaseSubscriptions.cancelAll();
final token = await FirebaseMessaging.instance.getToken();
if (token != null) {
await client.removeDevice(token);
}
}
});
}
OnRemoteMessage _onFirebaseMessageOpenedApp(StreamChatClient client) {
return (message) async {
final channelType = (message.data['channel_type'] as String?) ?? '';
final channelId = (message.data['channel_id'] as String?) ?? '';
final channelCid = (message.data['cid'] as String?) ?? '';
var channel = client.state.channels[channelCid];
if (channel == null) {
channel = client.channel(
channelType,
id: channelId,
);
await channel.watch();
}
GoRouter.of(_navigatorKey.currentContext!).pushNamed(
Routes.CHANNEL_PAGE.name,
params: Routes.CHANNEL_PAGE.params(channel),
);
};
}
Future<void> Function(String) _onFirebaseTokenRefresh(
StreamChatClient client,
) {
return (token) async {
await client.addDevice(token, PushProvider.firebase);
};
}
@override
void initState() {
final timeOfStartMs = DateTime.now().millisecondsSinceEpoch;
@@ -227,19 +196,11 @@ class _StreamChatSampleAppState extends State<StreamChatSampleApp>
if (now - timeOfStartMs > 1500) {
SchedulerBinding.instance.addPostFrameCallback((timeStamp) {
debugPrint('[forwardAnimations] #firebase; context1: $context');
debugPrint('[forwardAnimations] #firebase; _navigatorKey.currentContext1: ${_navigatorKey.currentContext}');
forwardAnimations();
final router = GoRouter.of(_navigatorKey.currentContext!);
debugPrint('[forwardAnimations] #firebase; router1: ${router}');
});
} else {
Future.delayed(const Duration(milliseconds: 1500)).then((value) {
debugPrint('[forwardAnimations] #firebase; context2: $context');
debugPrint('[forwardAnimations] #firebase; _navigatorKey.currentContext2: ${_navigatorKey.currentContext}');
forwardAnimations();
final router = GoRouter.of(_navigatorKey.currentContext!);
debugPrint('[forwardAnimations] #firebase; router2: ${router}');
});
}
_initFirebaseMessaging(initData.client);
@@ -253,10 +214,7 @@ class _StreamChatSampleAppState extends State<StreamChatSampleApp>
void dispose() {
super.dispose();
userIdSubscription?.cancel();
for (final subscription in firebaseSubscriptions) {
unawaited(subscription.cancel());
}
firebaseSubscriptions.clear();
firebaseSubscriptions.cancelAll();
}
final GlobalKey<NavigatorState> _navigatorKey = GlobalKey();
@@ -300,7 +258,6 @@ class _StreamChatSampleAppState extends State<StreamChatSampleApp>
@override
Widget build(BuildContext context) {
debugPrint('[AppState.build] #firebase; context: $context');
return Stack(
alignment: Alignment.center,
children: [
@@ -348,3 +305,14 @@ class _StreamChatSampleAppState extends State<StreamChatSampleApp>
);
}
}
typedef OnRemoteMessage = Future<void> Function(RemoteMessage);
extension on List<StreamSubscription> {
void cancelAll() {
for (final subscription in this) {
unawaited(subscription.cancel());
}
clear();
}
}
@@ -1,13 +1,12 @@
import 'dart:async';
import 'package:example/app.dart';
import 'package:example/state/init_data.dart';
import 'package:example/pages/user_mentions_page.dart';
import 'package:example/routes/routes.dart';
import 'package:example/state/init_data.dart';
import 'package:example/utils/app_config.dart';
import 'package:example/utils/localizations.dart';
import 'package:example/widgets/channel_list.dart';
import 'package:firebase_messaging/firebase_messaging.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_app_badger/flutter_app_badger.dart';
@@ -69,7 +68,6 @@ class _ChannelListPageState extends State<ChannelListPage> {
@override
Widget build(BuildContext context) {
print(">>>>>>>>>> ChannelListPage");
final user = StreamChat.of(context).currentUser;
if (user == null) {
return const Offstage();
@@ -78,7 +76,6 @@ class _ChannelListPageState extends State<ChannelListPage> {
backgroundColor: StreamChatTheme.of(context).colorTheme.appBg,
appBar: StreamChannelListHeader(
onNewChatButtonTap: () {
print(">>>>>>>>>> onNewChatButtonTap");
GoRouter.of(context).pushNamed(Routes.NEW_CHAT.name);
},
preNavigationCallback: () =>
@@ -129,39 +126,10 @@ class _ChannelListPageState extends State<ChannelListPage> {
FlutterAppBadger.removeBadge();
}
});
unawaited(_setupPushNotifications(StreamChat.of(context).client));
}
super.initState();
}
Future<void> _setupPushNotifications(StreamChatClient client) async {
FirebaseMessaging messaging = FirebaseMessaging.instance;
NotificationSettings settings = await messaging.requestPermission(
alert: true,
announcement: false,
badge: true,
carPlay: false,
criticalAlert: false,
provisional: false,
sound: true,
);
print('[setupPushNotifications] #firebase; settings: ${settings.authorizationStatus}');
String? token = await FirebaseMessaging.instance.getToken();
if (token != null) {
final result = await client.addDevice(token, PushProvider.firebase);
print('[setupPushNotifications] #firebase; token registered: $token');
}
FirebaseMessaging.instance.onTokenRefresh.listen((token) async {
final result = await client.addDevice(token, PushProvider.firebase);
print('[setupPushNotifications] #firebase; token refreshed: $token');
});
}
@override
void dispose() {
badgeListener?.cancel();
@@ -223,7 +191,6 @@ class LeftDrawer extends StatelessWidget {
.withOpacity(.5),
),
onTap: () {
print(">>>>>>>>>> NEW_CHAT");
Navigator.of(context).pop();
GoRouter.of(context).pushNamed(Routes.NEW_CHAT.name);
},
@@ -242,7 +209,6 @@ class LeftDrawer extends StatelessWidget {
.withOpacity(.5),
),
onTap: () {
print(">>>>>>>>>> NEW_GROUP_CHAT");
Navigator.of(context).pop();
GoRouter.of(context).pushNamed(Routes.NEW_GROUP_CHAT.name);
},
@@ -258,7 +224,6 @@ class LeftDrawer extends StatelessWidget {
alignment: Alignment.bottomCenter,
child: ListTile(
onTap: () async {
print(">>>>>>>>>> CHOOSE_USER");
final client = StreamChat.of(context).client;
final router = GoRouter.of(context);
final initNotifier = context.read<InitNotifier>();