-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(time-slots): add the capability to select which days the time sl…
…ots are the most impacting
- Loading branch information
1 parent
753bd5b
commit a88ac68
Showing
26 changed files
with
624 additions
and
171 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import 'package:chabo/bloc/chabo_event.dart'; | ||
import 'package:chabo/const.dart'; | ||
import 'package:chabo/models/enums/day.dart'; | ||
import 'package:chabo/models/time_slot.dart'; | ||
import 'package:chabo/service/storage_service.dart'; | ||
import 'package:equatable/equatable.dart'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
|
||
part 'time_slots_event.dart'; | ||
part 'time_slots_state.dart'; | ||
|
||
class TimeSlotsBloc extends Bloc<TimeSlotsEvent, TimeSlotsState> { | ||
final StorageService storageService; | ||
|
||
TimeSlotsBloc({required this.storageService}) : super(TimeSlotsInitial()) { | ||
on<TimeSlotChanged>( | ||
_onTimeSlotChanged, | ||
); | ||
|
||
on<DaysChanged>( | ||
_onDaysChanged, | ||
); | ||
|
||
on<TimeSlotsAppEvent>( | ||
_onAppEvent, | ||
); | ||
} | ||
|
||
Future<void> _onTimeSlotChanged( | ||
TimeSlotChanged event, | ||
Emitter<TimeSlotsState> emit, | ||
) async { | ||
final timeSlots = List<TimeSlot>.from(state.timeSlots); | ||
timeSlots[event.index] = event.timeSlot; | ||
await storageService.saveTimeSlots( | ||
Const.notificationFavoriteSlotsValueKey, | ||
timeSlots, | ||
); | ||
HapticFeedback.lightImpact(); | ||
|
||
emit( | ||
state.copyWith( | ||
timeSlots: timeSlots, | ||
), | ||
); | ||
} | ||
|
||
Future<void> _onDaysChanged( | ||
DaysChanged event, | ||
Emitter<TimeSlotsState> emit, | ||
) async { | ||
final days = List<Day>.from(state.days); | ||
if (event.isSelected) { | ||
days.add(event.day); | ||
} else { | ||
days.remove(event.day); | ||
} | ||
|
||
await storageService.saveDays( | ||
Const.notificationFavoriteSlotsDaysValueKey, | ||
days, | ||
); | ||
HapticFeedback.lightImpact(); | ||
|
||
emit( | ||
state.copyWith( | ||
days: days, | ||
), | ||
); | ||
} | ||
|
||
void _onAppEvent( | ||
TimeSlotsAppEvent event, | ||
Emitter<TimeSlotsState> emit, | ||
) { | ||
final days = | ||
storageService.readDays(Const.notificationFavoriteSlotsDaysValueKey) ?? | ||
Const.notificationFavoriteSlotsDaysDefaultValue; | ||
|
||
final timeSlots = | ||
storageService.readTimeSlots(Const.notificationFavoriteSlotsValueKey) ?? | ||
Const.notificationFavoriteSlotsDefaultValue; | ||
|
||
emit( | ||
state.copyWith( | ||
days: days, | ||
timeSlots: timeSlots, | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
part of 'time_slots_bloc.dart'; | ||
|
||
class TimeSlotsEvent extends ChaboEvent {} | ||
|
||
class TimeSlotChanged extends TimeSlotsEvent { | ||
final TimeSlot timeSlot; | ||
final int index; | ||
|
||
TimeSlotChanged({ | ||
required this.timeSlot, | ||
required this.index, | ||
}) : super(); | ||
} | ||
|
||
class DaysChanged extends TimeSlotsEvent { | ||
final Day day; | ||
final bool isSelected; | ||
|
||
DaysChanged({ | ||
required this.day, | ||
required this.isSelected, | ||
}) : super(); | ||
} | ||
|
||
class TimeSlotsAppEvent extends TimeSlotsEvent { | ||
TimeSlotsAppEvent() : super(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
part of 'time_slots_bloc.dart'; | ||
|
||
class TimeSlotsState extends Equatable { | ||
final List<TimeSlot> timeSlots; | ||
final List<Day> days; | ||
|
||
const TimeSlotsState({ | ||
required this.timeSlots, | ||
required this.days, | ||
}); | ||
|
||
TimeSlotsState copyWith({ | ||
List<TimeSlot>? timeSlots, | ||
List<Day>? days, | ||
}) { | ||
return TimeSlotsState( | ||
timeSlots: timeSlots ?? this.timeSlots, | ||
days: days ?? this.days, | ||
); | ||
} | ||
|
||
@override | ||
List<Object?> get props => [timeSlots, days]; | ||
} | ||
|
||
class TimeSlotsInitial extends TimeSlotsState { | ||
TimeSlotsInitial() | ||
: super( | ||
timeSlots: Const.notificationFavoriteSlotsDefaultValue, | ||
days: Const.notificationFavoriteSlotsDaysDefaultValue, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.