Skip to content

Commit

Permalink
Merge pull request #179 from LezdCS/feat/logging
Browse files Browse the repository at this point in the history
Setup Talker Flutter for logging
  • Loading branch information
LezdCS authored May 16, 2024
2 parents 7df5b9c + 597d441 commit e40f468
Show file tree
Hide file tree
Showing 17 changed files with 262 additions and 76 deletions.
6 changes: 6 additions & 0 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ PODS:
- PromisesSwift (2.3.1):
- PromisesObjC (= 2.3.1)
- ReachabilitySwift (5.0.0)
- share_plus (0.0.1):
- Flutter
- shared_preferences_foundation (0.0.1):
- Flutter
- FlutterMacOS
Expand Down Expand Up @@ -237,6 +239,7 @@ DEPENDENCIES:
- move_to_background (from `.symlinks/plugins/move_to_background/ios`)
- package_info_plus (from `.symlinks/plugins/package_info_plus/ios`)
- path_provider_foundation (from `.symlinks/plugins/path_provider_foundation/darwin`)
- share_plus (from `.symlinks/plugins/share_plus/ios`)
- shared_preferences_foundation (from `.symlinks/plugins/shared_preferences_foundation/darwin`)
- url_launcher_ios (from `.symlinks/plugins/url_launcher_ios/ios`)
- wakelock (from `.symlinks/plugins/wakelock/ios`)
Expand Down Expand Up @@ -309,6 +312,8 @@ EXTERNAL SOURCES:
:path: ".symlinks/plugins/package_info_plus/ios"
path_provider_foundation:
:path: ".symlinks/plugins/path_provider_foundation/darwin"
share_plus:
:path: ".symlinks/plugins/share_plus/ios"
shared_preferences_foundation:
:path: ".symlinks/plugins/shared_preferences_foundation/darwin"
url_launcher_ios:
Expand Down Expand Up @@ -364,6 +369,7 @@ SPEC CHECKSUMS:
PromisesObjC: c50d2056b5253dadbd6c2bea79b0674bd5a52fa4
PromisesSwift: 28dca69a9c40779916ac2d6985a0192a5cb4a265
ReachabilitySwift: 985039c6f7b23a1da463388634119492ff86c825
share_plus: 8875f4f2500512ea181eef553c3e27dba5135aad
shared_preferences_foundation: b4c3b4cddf1c21f02770737f147a3f5da9d39695
url_launcher_ios: 6116280ddcfe98ab8820085d8d76ae7449447586
wakelock: d0fc7c864128eac40eba1617cb5264d9c940b46f
Expand Down
19 changes: 17 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import 'package:get_storage/get_storage.dart';
import 'package:irllink/routes/app_pages.dart';
import 'package:irllink/src/bindings/login_bindings.dart';
import 'package:irllink/src/core/resources/themes.dart';
import 'package:irllink/src/core/utils/crashlytics_talker_observer.dart';
import 'package:irllink/src/presentation/views/login_view.dart';
import 'package:kick_chat/kick_chat.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:talker_flutter/talker_flutter.dart';
import 'package:upgrader/upgrader.dart';
import 'package:wakelock/wakelock.dart';
import 'firebase_options.dart';
Expand All @@ -22,6 +24,11 @@ import 'src/core/utils/globals.dart' as globals;

void main() async {
WidgetsFlutterBinding.ensureInitialized();
final crashlyticsTalkerObserver = CrashlyticsTalkerObserver();
final talker = TalkerFlutter.init(
settings: TalkerSettings(),
observer: crashlyticsTalkerObserver,
);
await initializeService();
await GetStorage.init();
await Wakelock.enable();
Expand All @@ -34,8 +41,9 @@ void main() async {
globals.buildNumber = packageInfo.buildNumber;
globals.appName = packageInfo.appName;
globals.packageName = packageInfo.packageName;
globals.talker = talker;
AppTranslations.initLanguages();
runApp(const Main());
runApp(Main(talker: talker,));
}

const notificationChannelId = 'irllink_foreground';
Expand Down Expand Up @@ -171,7 +179,11 @@ void notificationTapBackground(NotificationResponse notificationResponse) {
}

class Main extends StatelessWidget {
const Main({super.key});
const Main({
super.key,
required this.talker,
});
final Talker talker;

@override
Widget build(BuildContext context) {
Expand All @@ -190,6 +202,9 @@ class Main extends StatelessWidget {
translations: AppTranslations(),
locale: Get.deviceLocale,
fallbackLocale: const Locale('en', 'US'),
navigatorObservers: [
TalkerRouteObserver(talker),
],
);
}
}
24 changes: 24 additions & 0 deletions lib/src/core/utils/crashlytics_talker_observer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:talker_flutter/talker_flutter.dart';

class CrashlyticsTalkerObserver extends TalkerObserver {
CrashlyticsTalkerObserver();

@override
void onError(err) {
FirebaseCrashlytics.instance.recordError(
err.error,
err.stackTrace,
reason: err.message,
);
}

@override
void onException(err) {
FirebaseCrashlytics.instance.recordError(
err.exception,
err.stackTrace,
reason: err.message,
);
}
}
3 changes: 3 additions & 0 deletions lib/src/core/utils/globals.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
library globals;

import 'package:talker_flutter/talker_flutter.dart';

String appName = "IRL Link";
String packageName = "com.irllink";
String version = "1.0.0";
String buildNumber = "1";
Talker? talker;
21 changes: 21 additions & 0 deletions lib/src/core/utils/init_dio.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:talker_dio_logger/talker_dio_logger_interceptor.dart';
import 'package:talker_dio_logger/talker_dio_logger_settings.dart';
import 'package:irllink/src/core/utils/globals.dart' as globals;

Dio initDio() {
final talker = globals.talker;
var dio = Dio();
dio.interceptors.add(
TalkerDioLogger(
talker: talker,
settings: TalkerDioLoggerSettings(
requestFilter: (RequestOptions options) => kDebugMode ? true : !options.path.contains('api.twitch.tv'),
responseFilter: (response) => kDebugMode ? true : ![200, 202].contains(response.statusCode),
),
),
);

return dio;
}
11 changes: 6 additions & 5 deletions lib/src/core/utils/twitch_event_sub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import 'dart:convert';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:get/get.dart';
import 'package:irllink/src/core/utils/init_dio.dart';
import 'package:irllink/src/data/entities/twitch/twitch_poll_dto.dart';
import 'package:irllink/src/data/entities/twitch/twitch_prediction_dto.dart';
import 'package:irllink/src/domain/entities/twitch/twitch_hype_train.dart';
import 'package:irllink/src/domain/entities/twitch/twitch_poll.dart';
import 'package:irllink/src/domain/entities/twitch/twitch_prediction.dart';
import 'package:twitch_chat/twitch_chat.dart';
import 'package:web_socket_channel/io.dart';
import 'package:irllink/src/core/utils/globals.dart' as globals;

import '../../data/entities/twitch/twitch_hype_train_dto.dart';
import 'constants.dart';
Expand Down Expand Up @@ -137,13 +139,12 @@ class TwitchEventSub {
}

void _onDone() {
debugPrint("Twitch Sub Event: Connection closed");
globals.talker?.debug("Twitch Sub Event: Connection closed");
close();
}

void _onError(Object o, StackTrace s) {
debugPrint('Twitch event sub error: $o');
debugPrint('Twitch event sub error: $s');
globals.talker?.error("Twitch Sub Event: error", o, s);
}

Future<String> _getChannelId() async {
Expand All @@ -154,7 +155,7 @@ class TwitchEventSub {

void subscribeToEvent(String type, String version, String sessionId,
Map<String, String> condition) async {
var dio = Dio();
var dio = initDio();
try {
dio.options.headers['Client-Id'] = kTwitchAuthClientId;
dio.options.headers["authorization"] = "Bearer $accessToken";
Expand All @@ -167,7 +168,7 @@ class TwitchEventSub {
"transport": {"method": "websocket", "session_id": sessionId}
});
} on DioException catch (e) {
debugPrint(e.response.toString());
globals.talker?.error(e.response.toString());
}
}
}
29 changes: 11 additions & 18 deletions lib/src/data/repositories/streamelements_repository_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'package:irllink/src/core/params/streamelements_auth_params.dart';
import 'package:irllink/src/core/resources/data_state.dart';
import 'package:irllink/src/core/utils/init_dio.dart';
import 'package:irllink/src/data/entities/stream_elements/se_activity_dto.dart';
import 'package:irllink/src/data/entities/stream_elements/se_me_dto.dart';
import 'package:irllink/src/data/entities/stream_elements/se_overlay_dto.dart';
Expand Down Expand Up @@ -49,7 +50,7 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {

@override
Future<void> replayActivity(String token, SeActivity activity) async {
var dio = Dio();
var dio = initDio();
try {
dio.options.headers["Authorization"] = "Bearer $token";
await dio.post(
Expand All @@ -63,7 +64,7 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {
@override
Future<DataState<List<SeActivity>>> getLastActivities(
String token, String channel) async {
var dio = Dio();
var dio = initDio();
Response response;
List<SeActivity> activities = [];
try {
Expand All @@ -89,18 +90,14 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {
);
return DataSuccess(activities);
} on DioException catch (e) {
debugPrint(e.toString());
debugPrint(e.response.toString());
debugPrint(e.error.toString());

return DataFailed(e.toString());
}
}

@override
Future<DataState<List<SeOverlay>>> getOverlays(
String token, String channel) async {
var dio = Dio();
var dio = initDio();
List<SeOverlay> overlays = [];
try {
dio.options.headers["Authorization"] = "Bearer $token";
Expand All @@ -115,14 +112,13 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {
);
return DataSuccess(overlays);
} on DioException catch (e) {
debugPrint(e.toString());
return DataFailed(e.toString());
}
}

@override
Future<DataState<SeMe>> getMe(String token) async {
var dio = Dio();
var dio = initDio();
late SeMe me;
try {
dio.options.headers["Authorization"] = "Bearer $token";
Expand All @@ -134,14 +130,13 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {

return DataSuccess(me);
} on DioException catch (e) {
debugPrint(e.toString());
return DataFailed(e.toString());
}
}

@override
Future<void> nextSong(String token, String userId) async {
var dio = Dio();
var dio = initDio();
try {
dio.options.headers["Authorization"] = "Bearer $token";
await dio.post(
Expand All @@ -154,7 +149,7 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {

@override
Future<void> removeSong(String token, String userId, String songId) async {
var dio = Dio();
var dio = initDio();
try {
dio.options.headers["Authorization"] = "Bearer $token";
await dio.delete(
Expand All @@ -167,7 +162,7 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {

@override
Future<void> resetQueue(String token, String userId) async {
var dio = Dio();
var dio = initDio();
try {
dio.options.headers["Authorization"] = "Bearer $token";
await dio.delete(
Expand All @@ -182,7 +177,7 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {
Future<DataState<List<SeSong>>> getSongQueue(
String token, String userId) async {
List<SeSong> songs = [];
var dio = Dio();
var dio = initDio();
try {
dio.options.headers["Authorization"] = "Bearer $token";
Response response = await dio.get(
Expand All @@ -203,14 +198,13 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {

return DataSuccess(songs);
} on DioException catch (e) {
debugPrint(e.toString());
return DataFailed(e.toString());
}
}

@override
Future<DataState<SeSong>> getSongPlaying(String token, String userId) async {
var dio = Dio();
var dio = initDio();
try {
dio.options.headers["Authorization"] = "Bearer $token";
Response response = await dio.get(
Expand All @@ -227,14 +221,13 @@ class StreamelementsRepositoryImpl extends StreamelementsRepository {

return DataSuccess(song);
} on DioException catch (e) {
debugPrint(e.toString());
return DataFailed(e.toString());
}
}

@override
Future<void> updatePlayerState(String token, String userId, String state) async {
var dio = Dio();
var dio = initDio();
try {
dio.options.headers["Authorization"] = "Bearer $token";
await dio.post(
Expand Down
Loading

0 comments on commit e40f468

Please sign in to comment.