Skip to content

Commit

Permalink
feat: add the user the ability to choose the time format
Browse files Browse the repository at this point in the history
  • Loading branch information
vareversat committed Sep 9, 2023
1 parent c88e78f commit 02addf7
Show file tree
Hide file tree
Showing 31 changed files with 844 additions and 548 deletions.
1 change: 0 additions & 1 deletion lib/bloc/scroll_status/scroll_status_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import 'package:flutter/widgets.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

part 'scroll_status_event.dart';

part 'scroll_status_state.dart';

class ScrollStatusBloc extends Bloc<ScrollStatusEvent, ScrollStatusState> {
Expand Down
6 changes: 0 additions & 6 deletions lib/bloc/theme/theme_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,4 @@ class ThemeState {
themeData: themeData ?? this.themeData,
);
}

IconData getIconData() {
return themeData == AppTheme.lightTheme
? Icons.brightness_low
: Icons.dark_mode_outlined;
}
}
10 changes: 10 additions & 0 deletions lib/chabo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import 'package:chabo/bloc/status/status_bloc.dart';
import 'package:chabo/bloc/theme/theme_bloc.dart';
import 'package:chabo/bloc/time_slots/time_slots_bloc.dart';
import 'package:chabo/cubits/floating_actions_cubit.dart';
import 'package:chabo/cubits/time_format_cubit.dart';
import 'package:chabo/helpers/device_helper.dart';
import 'package:chabo/models/enums/time_format.dart';
import 'package:chabo/screens/forecast_screen.dart';
import 'package:chabo/service/notification_service.dart';
import 'package:chabo/service/storage_service.dart';
Expand Down Expand Up @@ -50,6 +52,14 @@ class Chabo extends StatelessWidget {
)..init(),
),

/// Bloc intended to manage the displayed time format
BlocProvider(
create: (_) => TimeFormatCubit(
storageService,
const TimeFormatState(timeFormat: TimeFormat.twentyFourHours),
)..init(),
),

/// Bloc intended to manage the forecast displayed
BlocProvider(
create: (_) => ForecastBloc(
Expand Down
3 changes: 3 additions & 0 deletions lib/const.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:chabo/models/enums/day.dart';
import 'package:chabo/models/enums/time_format.dart';
import 'package:chabo/models/time_slot.dart';
import 'package:chabo/models/web_link_icon.dart';
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -83,6 +84,7 @@ class Const {
'NOTIFICATION_FAVORITE_SLOTS_SETTINGS_VALUE';
static const String notificationFavoriteSlotsDaysValueKey =
'NOTIFICATION_FAVORITE_SLOTS_DAYS_SETTINGS_VALUE';
static const String timeFormatKey = 'TIME_FORMAT';

/// Notifications
static const String androidAppLogoPath =
Expand Down Expand Up @@ -128,6 +130,7 @@ class Const {

/// UI
static const bool isRightHandedDefaultValue = true;
static const TimeFormat timeFormatDefaultValue = TimeFormat.twentyFourHours;

/// Android Notifications
static const String androidTicket = 'ticker';
Expand Down
50 changes: 50 additions & 0 deletions lib/cubits/time_format_cubit.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import 'package:chabo/const.dart';
import 'package:chabo/models/enums/time_format.dart';
import 'package:chabo/service/storage_service.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class TimeFormatCubit extends Cubit<TimeFormatState> {
final StorageService storageService;

TimeFormatCubit(this.storageService, super.initialState);

void setTimeFormat() {
final newDateFormat = state.timeFormat == TimeFormat.twentyFourHours
? TimeFormat.twelveHours
: TimeFormat.twentyFourHours;
storageService.saveTimeFormat(Const.timeFormatKey, newDateFormat);
emit(
state.copyWith(
timeFormat: newDateFormat,
),
);
}

void init() {
final timeFormat = storageService.readTimeFormat(Const.timeFormatKey) ??
Const.timeFormatDefaultValue;
emit(
state.copyWith(
timeFormat: timeFormat,
),
);
}
}

class TimeFormatState extends Equatable {
final TimeFormat timeFormat;

const TimeFormatState({
required this.timeFormat,
});

TimeFormatState copyWith({TimeFormat? timeFormat}) {
return TimeFormatState(
timeFormat: timeFormat ?? this.timeFormat,
);
}

@override
List<Object?> get props => [timeFormat];
}
147 changes: 77 additions & 70 deletions lib/dialogs/days_of_the_week_dialog.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import 'package:chabo/bloc/notification/notification_bloc.dart';
import 'package:chabo/cubits/time_format_cubit.dart';
import 'package:chabo/custom_properties.dart';
import 'package:chabo/extensions/time_of_day_extension.dart';
import 'package:chabo/models/enums/day.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
Expand All @@ -19,81 +21,86 @@ class DaysOfTheWeekDialog extends StatelessWidget {
),
content: BlocBuilder<NotificationBloc, NotificationState>(
builder: (context, state) {
return Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 5,
runSpacing: 10,
children: [
ElevatedButton(
onPressed: () {}, // ignore: no-empty-block
child: DropdownButtonHideUnderline(
child: DropdownButton(
borderRadius: BorderRadius.circular(12.0),
onChanged: (Day? value) {
if (value != null) {
BlocProvider.of<NotificationBloc>(context).add(
DayNotificationValueEvent(
day: value,
),
);
}
},
value: state.dayNotificationValue,
items: Day.values
.map(
(day) => DropdownMenuItem<Day>(
value: day,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
day.localizedName(context),
style: const TextStyle(
fontWeight: FontWeight.bold,
return BlocBuilder<TimeFormatCubit, TimeFormatState>(
builder: (context, timeFormatState) {
return Wrap(
alignment: WrapAlignment.center,
crossAxisAlignment: WrapCrossAlignment.center,
spacing: 5,
runSpacing: 10,
children: [
ElevatedButton(
onPressed: () {}, // ignore: no-empty-block
child: DropdownButtonHideUnderline(
child: DropdownButton(
borderRadius: BorderRadius.circular(12.0),
onChanged: (Day? value) {
if (value != null) {
BlocProvider.of<NotificationBloc>(context).add(
DayNotificationValueEvent(
day: value,
),
);
}
},
value: state.dayNotificationValue,
items: Day.values
.map(
(day) => DropdownMenuItem<Day>(
value: day,
child: Padding(
padding: const EdgeInsets.all(8.0),
child: Text(
day.localizedName(context),
style: const TextStyle(
fontWeight: FontWeight.bold,
),
),
),
),
),
),
)
.toList(),
)
.toList(),
),
),
),
),
),
Text(
' ${AppLocalizations.of(context)!.dayNotificationAt} ',
style: Theme.of(context).textTheme.titleMedium,
),
ElevatedButton(
onPressed: () {
showTimePicker(
initialEntryMode: TimePickerEntryMode.dial,
context: context,
initialTime: state.dayNotificationTimeValue,
builder: (BuildContext context, Widget? child) {
return MediaQuery(
data: MediaQuery.of(context),
child: child!,
);
},
).then(
(value) => {
if (value != null)
{
BlocProvider.of<NotificationBloc>(context).add(
DayNotificationTimeValueEvent(
time: value,
),
),
Text(
' ${AppLocalizations.of(context)!.dayNotificationAt} ',
style: Theme.of(context).textTheme.titleMedium,
),
ElevatedButton(
onPressed: () {
showTimePicker(
initialEntryMode: TimePickerEntryMode.dial,
context: context,
initialTime: state.dayNotificationTimeValue,
builder: (BuildContext context, Widget? child) {
return MediaQuery(
data: MediaQuery.of(context),
child: child!,
);
},
).then(
(value) => {
if (value != null)
{
BlocProvider.of<NotificationBloc>(context).add(
DayNotificationTimeValueEvent(
time: value,
),
),
},
},
);
},
);
},
child: Text(
state.dayNotificationTimeValue.format(context),
style: Theme.of(context).textTheme.titleMedium,
),
),
],
child: Text(
state.dayNotificationTimeValue
.toFormattedString(timeFormatState.timeFormat),
style: Theme.of(context).textTheme.titleMedium,
),
),
],
);
},
);
},
),
Expand Down
Loading

0 comments on commit 02addf7

Please sign in to comment.