-
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(opening_hours): update every minute; refactor (#554)
- Fix wrong text representation of opening hours in the indicator. - Add minute-wise update feature to the opening hours indicator. - Remove 'isOpen' check from cubit as this is a widget's job. - Update doc comments in Timeslot entity.
- Loading branch information
Showing
9 changed files
with
100 additions
and
114 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
11 changes: 0 additions & 11 deletions
11
lib/features/opening_hours/domain/usecases/check_open_status.dart
This file was deleted.
Oops, something went wrong.
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
97 changes: 71 additions & 26 deletions
97
lib/features/opening_hours/presentation/widgets/opening_hours_indicator.dart
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 |
---|---|---|
@@ -1,50 +1,95 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:coffeecard/core/strings.dart'; | ||
import 'package:coffeecard/core/styles/app_colors.dart'; | ||
import 'package:coffeecard/core/styles/app_text_styles.dart'; | ||
import 'package:coffeecard/features/opening_hours/domain/entities/timeslot.dart'; | ||
import 'package:coffeecard/features/opening_hours/presentation/cubit/opening_hours_cubit.dart'; | ||
import 'package:coffeecard/features/opening_hours/presentation/widgets/analog_icons.dart'; | ||
import 'package:flutter/widgets.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
import 'package:fpdart/fpdart.dart' show Option, Some; | ||
import 'package:gap/gap.dart'; | ||
import 'package:intl/intl.dart'; | ||
|
||
class OpeningHoursIndicator extends StatelessWidget { | ||
class OpeningHoursIndicator extends StatefulWidget { | ||
const OpeningHoursIndicator(); | ||
|
||
String get formatCurrentWeekday => DateFormat('EEEE') | ||
.format(DateTime.now()) | ||
.characters | ||
.getRange(0, 3) | ||
.toString(); | ||
@override | ||
State<OpeningHoursIndicator> createState() => _OpeningHoursIndicatorState(); | ||
} | ||
|
||
class _OpeningHoursIndicatorState extends State<OpeningHoursIndicator> { | ||
late Timer timer; | ||
final refreshDuration = const Duration(minutes: 1); | ||
|
||
@override | ||
void initState() { | ||
super.initState(); | ||
timer = Timer.periodic(refreshDuration, (_) => setState(() {})); | ||
} | ||
|
||
@override | ||
void dispose() { | ||
timer.cancel(); | ||
super.dispose(); | ||
} | ||
|
||
String get currentWeekday => DateFormat.E().format(DateTime.now()); | ||
|
||
(Color, String) openColorAndText(Timeslot timeslot) => ( | ||
AppColors.success, | ||
'$currentWeekday: ${timeslot.format(context)}', | ||
); | ||
(Color, String) get closedColorAndText => ( | ||
AppColors.errorOnBright, | ||
'${Strings.openingHoursIndicatorPrefix} ${Strings.closed}', | ||
); | ||
|
||
(Color, String) colorAndText(Option<Timeslot> todaysOpeningHours) { | ||
final isOpen = TimeOfDay.now().isInTimeslot; | ||
return switch (todaysOpeningHours) { | ||
Some(value: final hours) when isOpen(hours) => openColorAndText(hours), | ||
_ => closedColorAndText, | ||
}; | ||
} | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return BlocBuilder<OpeningHoursCubit, OpeningHoursState>( | ||
builder: (context, state) { | ||
if (state is! OpeningHoursLoaded) { | ||
return const SizedBox.shrink(); | ||
} | ||
builder: (_, state) { | ||
return switch (state) { | ||
OpeningHoursLoaded(:final today) => _Content(colorAndText(today)), | ||
_ => const SizedBox.shrink(), | ||
}; | ||
}, | ||
); | ||
} | ||
} | ||
|
||
final text = state.isOpen | ||
? '$formatCurrentWeekday: ${state.todaysOpeningHours}' | ||
: '${Strings.openingHoursIndicatorPrefix} ${Strings.closed}'; | ||
final color = | ||
state.isOpen ? AppColors.success : AppColors.errorOnBright; | ||
class _Content extends StatelessWidget { | ||
_Content((Color, String) colorAndText) | ||
: color = colorAndText.$1, | ||
text = colorAndText.$2; | ||
|
||
final textStyle = | ||
AppTextStyle.openingHoursIndicator.copyWith(color: color); | ||
final Color color; | ||
final String text; | ||
|
||
return Row( | ||
@override | ||
Widget build(BuildContext context) { | ||
return DefaultTextStyle( | ||
style: AppTextStyle.openingHoursIndicator.copyWith(color: color), | ||
child: Theme( | ||
data: Theme.of(context) | ||
.copyWith(iconTheme: IconThemeData(color: color, size: 18)), | ||
child: Row( | ||
children: [ | ||
Icon(AnalogIcons.closed, size: 18, color: color), | ||
const Icon(AnalogIcons.closed), | ||
const Gap(8), | ||
Text( | ||
text, | ||
style: textStyle, | ||
), | ||
Text(text), | ||
], | ||
); | ||
}, | ||
), | ||
), | ||
); | ||
} | ||
} |
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
35 changes: 0 additions & 35 deletions
35
test/features/opening_hours/domain/usecases/check_open_status_test.dart
This file was deleted.
Oops, something went wrong.
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