Skip to content

Commit

Permalink
Minor logging improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Wolfteam committed Jul 9, 2021
1 parent 401d0f3 commit 197913a
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 12 deletions.
2 changes: 2 additions & 0 deletions lib/application/notification/notification_bloc.dart
Expand Up @@ -361,6 +361,8 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {

if (state.key == null) {
await _telemetryService.trackNotificationCreated(state.type);
}else{
await _telemetryService.trackNotificationUpdated(state.type);
}
} catch (e, s) {
_loggingService.error(runtimeType, '_saveChanges: Unknown error while saving changes', e, s);
Expand Down
7 changes: 6 additions & 1 deletion lib/application/notifications/notifications_bloc.dart
Expand Up @@ -7,6 +7,7 @@ import 'package:genshindb/domain/models/models.dart';
import 'package:genshindb/domain/services/data_service.dart';
import 'package:genshindb/domain/services/notification_service.dart';
import 'package:genshindb/domain/services/settings_service.dart';
import 'package:genshindb/domain/services/telemetry_service.dart';
import 'package:meta/meta.dart';

part 'notifications_bloc.freezed.dart';
Expand All @@ -19,10 +20,11 @@ class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {
final DataService _dataService;
final NotificationService _notificationService;
final SettingsService _settingsService;
final TelemetryService _telemetryService;

Timer? _timer;

NotificationsBloc(this._dataService, this._notificationService, this._settingsService) : super(_initialState);
NotificationsBloc(this._dataService, this._notificationService, this._settingsService, this._telemetryService) : super(_initialState);

@override
Stream<NotificationsState> mapEventToState(NotificationsEvent event) async* {
Expand Down Expand Up @@ -66,19 +68,22 @@ class NotificationsBloc extends Bloc<NotificationsEvent, NotificationsState> {
await _notificationService.cancelNotification(key, type);
final notifications = [...state.notifications];
notifications.removeWhere((el) => el.key == key && el.type == type);
await _telemetryService.trackNotificationDeleted(type);
return state.copyWith.call(notifications: notifications);
}

Future<NotificationsState> _resetNotification(int key, AppNotificationType type) async {
final notif = await _dataService.resetNotification(key, type, _settingsService.serverResetTime);
await _notificationService.cancelNotification(key, type);
await _notificationService.scheduleNotification(key, type, notif.title, notif.body, notif.completesAt);
await _telemetryService.trackNotificationRestarted(type);
return _afterUpdatingNotification(notif);
}

Future<NotificationsState> _stopNotification(int key, AppNotificationType type) async {
final notif = await _dataService.stopNotification(key, type);
await _notificationService.cancelNotification(key, type);
await _telemetryService.trackNotificationStopped(type);
return _afterUpdatingNotification(notif);
}

Expand Down
8 changes: 8 additions & 0 deletions lib/domain/services/telemetry_service.dart
Expand Up @@ -55,4 +55,12 @@ abstract class TelemetryService {
Future<void> trackItemDeletedFromInventory(String key);

Future<void> trackNotificationCreated(AppNotificationType type);

Future<void> trackNotificationUpdated(AppNotificationType type);

Future<void> trackNotificationDeleted(AppNotificationType type);

Future<void> trackNotificationRestarted(AppNotificationType type);

Future<void> trackNotificationStopped(AppNotificationType type);
}
8 changes: 4 additions & 4 deletions lib/infrastructure/logging_service.dart
Expand Up @@ -65,10 +65,10 @@ class LoggingServiceImpl implements LoggingService {

Map<String, String> _buildError(String tag, String msg, [dynamic ex, StackTrace? trace]) {
final map = {
'tag': tag,
'msg': msg,
'ex': ex?.toString() ?? 'No exception available',
'trace': trace?.toString() ?? 'No trace available',
'Tag': tag,
'Msg': msg,
'Ex': ex?.toString() ?? 'No exception available',
'Trace': trace?.toString() ?? 'No trace available',
};

map.addAll(_deviceInfoService.deviceInfo);
Expand Down
13 changes: 10 additions & 3 deletions lib/infrastructure/notification_service.dart
Expand Up @@ -31,11 +31,13 @@ class NotificationServiceImpl implements NotificationService {
final currentTimeZone = await FlutterNativeTimezone.getLocalTimezone();
_location = tz.getLocation(currentTimeZone);
tz.setLocalLocation(_location);
} catch (e, s) {
} on tz.LocationNotFoundException catch (e) {
//https://github.com/srawlins/timezone/issues/92
_loggingService.info(runtimeType, 'init: ${e.msg}, assigning the generic one...');
_setDefaultTimeZone();
} catch (e, s) {
_loggingService.error(runtimeType, 'init: Failed to get timezone or device is GMT or UTC, assigning the generic one...', e, s);
_location = tz.getLocation(_fallbackTimeZone);
tz.setLocalLocation(_location);
_setDefaultTimeZone();
}
}

Expand Down Expand Up @@ -107,6 +109,11 @@ class NotificationServiceImpl implements NotificationService {
);
}

void _setDefaultTimeZone() {
_location = tz.getLocation(_fallbackTimeZone);
tz.setLocalLocation(_location);
}

NotificationDetails _getPlatformChannelSpecifics(AppNotificationType type, String body) {
final style = body.length < 40 ? null : BigTextStyleInformation(body);
final _androidPlatformChannelSpecifics = AndroidNotificationDetails(
Expand Down
21 changes: 18 additions & 3 deletions lib/infrastructure/telemetry/telemetry_service.dart
Expand Up @@ -142,7 +142,22 @@ class TelemetryServiceImpl implements TelemetryService {
Future<void> trackItemUpdatedInInventory(String key, int quantity) => trackEventAsync('MyInventory-Updated', {'Key_Qty': '${key}_$quantity'});

@override
Future<void> trackNotificationCreated(AppNotificationType type) => trackEventAsync('Notification-Created', {
'Type': EnumToString.convertToString(type),
});
Future<void> trackNotificationCreated(AppNotificationType type) =>
trackEventAsync('Notification-Created', {'Type': EnumToString.convertToString(type)});

@override
Future<void> trackNotificationDeleted(AppNotificationType type) =>
trackEventAsync('Notification-Deleted', {'Type': EnumToString.convertToString(type)});

@override
Future<void> trackNotificationRestarted(AppNotificationType type) =>
trackEventAsync('Notification-Restarted', {'Type': EnumToString.convertToString(type)});

@override
Future<void> trackNotificationStopped(AppNotificationType type) =>
trackEventAsync('Notification-Stopped', {'Type': EnumToString.convertToString(type)});

@override
Future<void> trackNotificationUpdated(AppNotificationType type) =>
trackEventAsync('Notification-Updated', {'Type': EnumToString.convertToString(type)});
}
3 changes: 2 additions & 1 deletion lib/main.dart
Expand Up @@ -231,7 +231,8 @@ class MyApp extends StatelessWidget {
final dataService = getIt<DataService>();
final notificationService = getIt<NotificationService>();
final settingsService = getIt<SettingsService>();
return NotificationsBloc(dataService, notificationService, settingsService);
final telemetryService = getIt<TelemetryService>();
return NotificationsBloc(dataService, notificationService, settingsService, telemetryService);
},
),
BlocProvider(
Expand Down

0 comments on commit 197913a

Please sign in to comment.