Skip to content

Commit

Permalink
feat(notifications): automatic check if the notifications are enabled
Browse files Browse the repository at this point in the history
  • Loading branch information
vareversat committed May 8, 2023
1 parent f51c2f7 commit baf2d9f
Show file tree
Hide file tree
Showing 9 changed files with 127 additions and 88 deletions.
1 change: 1 addition & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>


<queries>
Expand Down
75 changes: 63 additions & 12 deletions lib/bloc/notification/notification_bloc.dart
Original file line number Diff line number Diff line change
@@ -1,22 +1,28 @@
import 'dart:async';

import 'package:bloc_concurrency/bloc_concurrency.dart';
import 'package:chabo/bloc/chabo_event.dart';
import 'package:chabo/const.dart';
import 'package:chabo/models/abstract_chaban_bridge_forecast.dart';
import 'package:chabo/models/enums/day.dart';
import 'package:chabo/models/time_slot.dart';
import 'package:chabo/service/notification_service.dart';
import 'package:chabo/service/storage_service.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

part 'notification_event.dart';

part 'notification_state.dart';

class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
final StorageService storageService;
final NotificationService notificationService;

NotificationBloc({required this.storageService})
: super(
NotificationBloc({
required this.storageService,
required this.notificationService,
}) : super(
NotificationState(
durationNotificationEnabled:
Const.notificationDurationEnabledDefaultValue,
Expand All @@ -35,6 +41,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
timeSlotsEnabledForNotifications:
Const.notificationFavoriteSlotsEnabledDefaultValue,
timeSlotsValue: Const.notificationFavoriteSlotsDefaultValue,
notificationEnabled: false,
),
) {
on<OpeningNotificationStateEvent>(
Expand Down Expand Up @@ -81,6 +88,10 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
_onTimeSlotsEventValue,
transformer: sequential(),
);
on<ComputeNotificationEvent>(
_onComputeNotificationEvent,
transformer: sequential(),
);
on<AppEvent>(
_onAppEvent,
transformer: sequential(),
Expand All @@ -97,7 +108,9 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
);
HapticFeedback.lightImpact();
emit(
state.copyWith(openingNotificationEnabled: event.enabled),
state.copyWith(
openingNotificationEnabled: event.enabled,
),
);
}

Expand All @@ -109,9 +122,14 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
Const.notificationClosingEnabledKey,
event.enabled,
);

HapticFeedback.lightImpact();
final enabled = await notificationService.areNotificationsEnabled();
emit(
state.copyWith(closingNotificationEnabled: event.enabled),
state.copyWith(
closingNotificationEnabled: event.enabled,
notificationEnabled: enabled,
),
);
}

Expand All @@ -124,19 +142,28 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
event.enabled,
);
HapticFeedback.lightImpact();
final enabled = await notificationService.areNotificationsEnabled();
emit(
state.copyWith(dayNotificationEnabled: event.enabled),
state.copyWith(
dayNotificationEnabled: event.enabled,
notificationEnabled: enabled,
),
);
}

Future<void> _onDayNotificationValueEvent(
DayNotificationValueEvent event,
Emitter<NotificationState> emit,
) async {
await storageService.saveDay(Const.notificationDayValueKey, event.day);
await storageService.saveDay(
Const.notificationDayValueKey,
event.day,
);
HapticFeedback.lightImpact();
emit(
state.copyWith(dayNotificationValue: event.day),
state.copyWith(
dayNotificationValue: event.day,
),
);
}

Expand All @@ -150,7 +177,9 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
);
HapticFeedback.lightImpact();
emit(
state.copyWith(dayNotificationTimeValue: event.time),
state.copyWith(
dayNotificationTimeValue: event.time,
),
);
}

Expand All @@ -163,8 +192,12 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
event.enabled,
);
HapticFeedback.lightImpact();
final enabled = await notificationService.areNotificationsEnabled();
emit(
state.copyWith(timeNotificationEnabled: event.enabled),
state.copyWith(
timeNotificationEnabled: event.enabled,
notificationEnabled: enabled,
),
);
}

Expand All @@ -177,7 +210,9 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
event.time,
);
emit(
state.copyWith(timeNotificationValue: event.time),
state.copyWith(
timeNotificationValue: event.time,
),
);
}

Expand All @@ -190,9 +225,11 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
event.enabled,
);
HapticFeedback.lightImpact();
final enabled = await notificationService.areNotificationsEnabled();
emit(
state.copyWith(
durationNotificationEnabled: event.enabled,
notificationEnabled: enabled,
),
);
}
Expand Down Expand Up @@ -244,10 +281,21 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
);
}

Future<void> _onComputeNotificationEvent(
ComputeNotificationEvent event,
Emitter<NotificationState> emit,
) async {
await notificationService.computeNotifications(
event.forecasts,
state,
event.context,
);
}

void _onAppEvent(
AppEvent event,
Emitter<NotificationState> emit,
) {
) async {
final durationNotificationEnabled =
storageService.readBool(Const.notificationDurationEnabledKey) ??
Const.notificationDurationEnabledDefaultValue;
Expand Down Expand Up @@ -292,6 +340,8 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
storageService.readBool(Const.notificationFavoriteSlotsEnabledKey) ??
Const.notificationFavoriteSlotsEnabledDefaultValue;

final enabled = await notificationService.areNotificationsEnabled();

emit(
state.copyWith(
durationNotificationEnabled: durationNotificationEnabled,
Expand All @@ -305,6 +355,7 @@ class NotificationBloc extends Bloc<NotificationEvent, NotificationState> {
closingNotificationEnabled: closingNotificationEnabled,
timeSlotsValue: timeSlots,
timeSlotsEnabledForNotifications: enabledForNotifications,
notificationEnabled: enabled,
),
);
}
Expand Down
8 changes: 8 additions & 0 deletions lib/bloc/notification/notification_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,14 @@ class ValueTimeSlotEvent extends NotificationEvent {
ValueTimeSlotEvent({required this.timeSlot, required this.index}) : super();
}

class ComputeNotificationEvent extends NotificationEvent {
final List<AbstractChabanBridgeForecast> forecasts;
final BuildContext context;

ComputeNotificationEvent({required this.forecasts, required this.context})
: super();
}

class AppEvent extends NotificationEvent {
AppEvent() : super();
}
4 changes: 4 additions & 0 deletions lib/bloc/notification/notification_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ class NotificationState {
final bool closingNotificationEnabled;
final bool timeSlotsEnabledForNotifications;
final List<TimeSlot> timeSlotsValue;
final bool notificationEnabled;

NotificationState({
required this.notificationEnabled,
required this.durationNotificationEnabled,
required this.durationNotificationValue,
required this.timeNotificationEnabled,
Expand All @@ -39,6 +41,7 @@ class NotificationState {
bool? closingNotificationEnabled,
bool? timeSlotsEnabledForNotifications,
List<TimeSlot>? timeSlotsValue,
bool? notificationEnabled,
}) {
return NotificationState(
durationNotificationEnabled:
Expand All @@ -61,6 +64,7 @@ class NotificationState {
timeSlotsEnabledForNotifications: timeSlotsEnabledForNotifications ??
this.timeSlotsEnabledForNotifications,
timeSlotsValue: timeSlotsValue ?? this.timeSlotsValue,
notificationEnabled: notificationEnabled ?? this.notificationEnabled,
);
}
}
9 changes: 1 addition & 8 deletions lib/chabo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import 'package:chabo/bloc/notification/notification_bloc.dart';
import 'package:chabo/bloc/scroll_status/scroll_status_bloc.dart';
import 'package:chabo/bloc/theme/theme_bloc.dart';
import 'package:chabo/cubits/floating_actions_cubit.dart';
import 'package:chabo/cubits/notification_service_cubit.dart';
import 'package:chabo/screens/chaban_bridge_forecast_screen.dart';
import 'package:chabo/service/notification_service.dart';
import 'package:chabo/service/storage_service.dart';
Expand Down Expand Up @@ -37,13 +36,6 @@ class Chabo extends StatelessWidget {
),
),

/// Bloc intended to manage the Notifications service
BlocProvider(
create: (_) => NotificationServiceCubit(
notificationService,
),
),

/// Bloc intended to manage the FloatingActions
BlocProvider(
create: (_) => FloatingActionsCubit(
Expand Down Expand Up @@ -77,6 +69,7 @@ class Chabo extends StatelessWidget {
BlocProvider(
create: (_) => NotificationBloc(
storageService: storageService,
notificationService: notificationService,
)..add(
AppEvent(),
),
Expand Down
6 changes: 0 additions & 6 deletions lib/cubits/notification_service_cubit.dart

This file was deleted.

18 changes: 8 additions & 10 deletions lib/screens/chaban_bridge_forecast_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'package:chabo/bloc/chaban_bridge_status/chaban_bridge_status_bloc.dart';
import 'package:chabo/bloc/notification/notification_bloc.dart';
import 'package:chabo/bloc/scroll_status/scroll_status_bloc.dart';
import 'package:chabo/cubits/floating_actions_cubit.dart';
import 'package:chabo/cubits/notification_service_cubit.dart';
import 'package:chabo/custom_widget_state.dart';
import 'package:chabo/misc/no_scaling_animation.dart';
import 'package:chabo/screens/error_screen.dart';
Expand Down Expand Up @@ -78,16 +77,15 @@ class _ChabanBridgeForecastScreenState
duration: state.durationNotificationValue,
),
);
context
.read<NotificationServiceCubit>()
.state
.computeNotifications(
BlocProvider.of<ChabanBridgeForecastBloc>(
context,
).state.chabanBridgeForecasts,
state,
BlocProvider.of<NotificationBloc>(context).add(
ComputeNotificationEvent(
forecasts:
BlocProvider.of<ChabanBridgeForecastBloc>(
context,
);
).state.chabanBridgeForecasts,
context: context,
),
);
},
),
],
Expand Down
Loading

0 comments on commit baf2d9f

Please sign in to comment.