Skip to content

Commit

Permalink
use user defined locale instead of default locale
Browse files Browse the repository at this point in the history
  • Loading branch information
julien4215 committed Aug 29, 2024
1 parent c1ddc11 commit 82063f6
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 35 deletions.
20 changes: 20 additions & 0 deletions lib/src/utils/current_locale.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:intl/intl.dart';
import 'package:lichess_mobile/src/model/settings/general_preferences.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'current_locale.g.dart';

@riverpod
String currentLocale(CurrentLocaleRef ref) {
return ref.watch(generalPreferencesProvider).locale?.languageCode ??
Intl.getCurrentLocale();
}

extension LocaleWidgetRefExtension on WidgetRef {
/// Runs [fn] with the current locale.
T withLocale<T>(T Function(String) fn) {
final currentLocale = watch(currentLocaleProvider);
return fn(currentLocale);
}
}
8 changes: 4 additions & 4 deletions lib/src/view/broadcast/broadcast_overview_tab.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ import 'package:intl/intl.dart';
import 'package:lichess_mobile/src/model/broadcast/broadcast_providers.dart';
import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/current_locale.dart';
import 'package:url_launcher/url_launcher.dart';

final _dateFormatter = DateFormat.MMMd(Intl.getCurrentLocale());

/// A tab that displays the overview of a broadcast.
class BroadcastOverviewTab extends ConsumerWidget {
final BroadcastTournamentId tournamentId;
Expand All @@ -19,6 +18,7 @@ class BroadcastOverviewTab extends ConsumerWidget {
@override
Widget build(BuildContext context, WidgetRef ref) {
final tournament = ref.watch(broadcastTournamentProvider(tournamentId));
final dateFormatter = ref.withLocale((locale) => DateFormat.MMMd(locale));

return SafeArea(
child: Padding(
Expand All @@ -37,8 +37,8 @@ class BroadcastOverviewTab extends ConsumerWidget {
BroadcastOverviewCard(
CupertinoIcons.calendar,
information.dates!.endsAt == null
? _dateFormatter.format(information.dates!.startsAt)
: '${_dateFormatter.format(information.dates!.startsAt)} - ${_dateFormatter.format(information.dates!.endsAt!)}',
? dateFormatter.format(information.dates!.startsAt)
: '${dateFormatter.format(information.dates!.startsAt)} - ${dateFormatter.format(information.dates!.endsAt!)}',
),
if (information.format != null)
BroadcastOverviewCard(
Expand Down
8 changes: 5 additions & 3 deletions lib/src/view/broadcast/broadcasts_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import 'package:lichess_mobile/src/model/common/id.dart';
import 'package:lichess_mobile/src/styles/lichess_colors.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/styles/transparent_image.dart';
import 'package:lichess_mobile/src/utils/current_locale.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/navigation.dart';
import 'package:lichess_mobile/src/view/broadcast/broadcast_screen.dart';
Expand All @@ -16,8 +17,6 @@ import 'package:lichess_mobile/src/widgets/buttons.dart';
import 'package:lichess_mobile/src/widgets/platform.dart';
import 'package:lichess_mobile/src/widgets/shimmer.dart';

final _dateFormatter = DateFormat.MMMd(Intl.getCurrentLocale()).add_Hm();

/// A screen that displays a paginated list of broadcasts.
class BroadcastsListScreen extends StatelessWidget {
const BroadcastsListScreen({super.key});
Expand Down Expand Up @@ -221,6 +220,9 @@ class BroadcastGridItem extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final dateFormatter =
ref.withLocale((locale) => DateFormat.yMMMd(locale).add_Hm());

return AdaptiveInkWell(
borderRadius: BorderRadius.circular(20),
onTap: () {
Expand Down Expand Up @@ -293,7 +295,7 @@ class BroadcastGridItem extends ConsumerWidget {
)
else if (broadcast.round.startsAt != null)
Text(
_dateFormatter.format(broadcast.round.startsAt!),
dateFormatter.format(broadcast.round.startsAt!),
style: Theme.of(context)
.textTheme
.labelSmall
Expand Down
7 changes: 4 additions & 3 deletions lib/src/view/game/game_list_tile.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import 'package:lichess_mobile/src/model/game/game_share_service.dart';
import 'package:lichess_mobile/src/model/game/game_status.dart';
import 'package:lichess_mobile/src/styles/lichess_colors.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/current_locale.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/navigation.dart';
import 'package:lichess_mobile/src/utils/share.dart';
Expand All @@ -25,8 +26,6 @@ import 'package:lichess_mobile/src/widgets/list.dart';
import 'package:lichess_mobile/src/widgets/user_full_name.dart';
import 'package:timeago/timeago.dart' as timeago;

final _dateFormatter = DateFormat.yMMMd(Intl.getCurrentLocale()).add_Hm();

/// A list tile that shows game info.
class GameListTile extends StatelessWidget {
const GameListTile({
Expand Down Expand Up @@ -110,6 +109,8 @@ class _ContextMenu extends ConsumerWidget {
final orientation = mySide;

final customColors = Theme.of(context).extension<CustomColors>();
final dateFormatter =
ref.withLocale((locale) => DateFormat.yMMMd(locale).add_Hm());

return DraggableScrollableSheet(
initialChildSize: .7,
Expand Down Expand Up @@ -178,7 +179,7 @@ class _ContextMenu extends ConsumerWidget {
),
),
Text(
_dateFormatter.format(game.lastMoveAt),
dateFormatter.format(game.lastMoveAt),
style: TextStyle(
color: textShade(
context,
Expand Down
6 changes: 3 additions & 3 deletions lib/src/view/puzzle/puzzle_history_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:lichess_mobile/src/model/puzzle/puzzle_activity.dart';
import 'package:lichess_mobile/src/model/puzzle/puzzle_angle.dart';
import 'package:lichess_mobile/src/model/puzzle/puzzle_theme.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/current_locale.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/navigation.dart';
import 'package:lichess_mobile/src/utils/screen.dart';
Expand All @@ -19,8 +20,6 @@ import 'package:lichess_mobile/src/widgets/feedback.dart';
import 'package:lichess_mobile/src/widgets/platform.dart';
import 'package:timeago/timeago.dart' as timeago;

final _dateFormatter = DateFormat.yMMMd(Intl.getCurrentLocale());

class PuzzleHistoryScreen extends StatelessWidget {
@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -128,6 +127,7 @@ class _BodyState extends ConsumerState<_Body> {
@override
Widget build(BuildContext context) {
final historyState = ref.watch(puzzleActivityProvider);
final dateFormatter = ref.withLocale((locale) => DateFormat.yMMMd(locale));

return historyState.when(
data: (state) {
Expand Down Expand Up @@ -182,7 +182,7 @@ class _BodyState extends ConsumerState<_Body> {
);
} else if (element is DateTime) {
final title = DateTime.now().difference(element).inDays >= 15
? _dateFormatter.format(element)
? dateFormatter.format(element)
: timeago.format(element);
return Padding(
padding: const EdgeInsets.only(left: _kPuzzlePadding)
Expand Down
40 changes: 21 additions & 19 deletions lib/src/view/user/perf_stats_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import 'package:lichess_mobile/src/model/user/user.dart';
import 'package:lichess_mobile/src/model/user/user_repository_providers.dart';
import 'package:lichess_mobile/src/styles/lichess_icons.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/current_locale.dart';
import 'package:lichess_mobile/src/utils/duration.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/utils/navigation.dart';
Expand All @@ -34,9 +35,6 @@ import 'package:lichess_mobile/src/widgets/rating.dart';
import 'package:lichess_mobile/src/widgets/stat_card.dart';
import 'package:lichess_mobile/src/widgets/user_full_name.dart';

final _currentLocale = Intl.getCurrentLocale();
final _dateFormatter = DateFormat.yMMMd(_currentLocale);

const _customOpacity = 0.6;
const _defaultStatFontSize = 12.0;
const _defaultValueFontSize = 18.0;
Expand Down Expand Up @@ -164,6 +162,7 @@ class _Body extends ConsumerWidget {
final loggedInUser = ref.watch(authSessionProvider);
const statGroupSpace = SizedBox(height: 15.0);
const subStatSpace = SizedBox(height: 10);
final currentLocale = ref.watch(currentLocaleProvider);

return perfStats.when(
data: (data) {
Expand Down Expand Up @@ -238,9 +237,8 @@ class _Body extends ConsumerWidget {
context.l10n.rank,
value: data.rank == null
? '?'
: NumberFormat.decimalPattern(
Intl.getCurrentLocale(),
).format(data.rank),
: NumberFormat.decimalPattern(currentLocale)
.format(data.rank),
),
StatCard(
context.l10n
Expand Down Expand Up @@ -485,25 +483,26 @@ class _ProgressionWidget extends StatelessWidget {
}
}

class _UserGameWidget extends StatelessWidget {
class _UserGameWidget extends ConsumerWidget {
final UserPerfGame? game;

const _UserGameWidget(this.game);

@override
Widget build(BuildContext context) {
Widget build(BuildContext context, WidgetRef ref) {
// TODO: Implement functionality to view game on tap.
// (Return a button? Wrap with InkWell?)
const defaultDateFontSize = 16.0;
final defaultDateStyle = TextStyle(
color: Theme.of(context).colorScheme.tertiary,
fontSize: defaultDateFontSize,
);
final dateFormatter = ref.withLocale((locale) => DateFormat.yMMMd(locale));

return game == null
? Text('?', style: defaultDateStyle)
: Text(
_dateFormatter.format(game!.finishedAt),
dateFormatter.format(game!.finishedAt),
style: defaultDateStyle,
);
}
Expand Down Expand Up @@ -686,6 +685,8 @@ class _GameListWidget extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final dateFormatter = ref.withLocale((locale) => DateFormat.yMMMd(locale));

return ListSection(
header: header,
margin: const EdgeInsets.only(top: 10.0),
Expand Down Expand Up @@ -725,7 +726,7 @@ class _GameListWidget extends ConsumerWidget {
rating: game.opponentRating,
),
subtitle: Text(
_dateFormatter.format(game.finishedAt),
dateFormatter.format(game.finishedAt),
),
),
],
Expand Down Expand Up @@ -761,16 +762,16 @@ class _GameListTile extends StatelessWidget {
}
}

class _EloChart extends StatefulWidget {
class _EloChart extends ConsumerStatefulWidget {
final UserRatingHistoryPerf value;

const _EloChart(this.value);

@override
State<_EloChart> createState() => _EloChartState();
ConsumerState<_EloChart> createState() => _EloChartState();
}

class _EloChartState extends State<_EloChart> {
class _EloChartState extends ConsumerState<_EloChart> {
late DateRange _selectedRange;

late List<FlSpot> _allFlSpot;
Expand Down Expand Up @@ -860,20 +861,21 @@ class _EloChartState extends State<_EloChart> {
final borderColor =
Theme.of(context).colorScheme.onSurface.withOpacity(0.5);
final chartColor = Theme.of(context).colorScheme.tertiary;
final currentLocale = ref.watch(currentLocaleProvider);
final chartDateFormatter = switch (_selectedRange) {
DateRange.oneWeek => DateFormat.MMMd(_currentLocale),
DateRange.oneMonth => DateFormat.MMMd(_currentLocale),
DateRange.threeMonths => DateFormat.yMMM(_currentLocale),
DateRange.oneYear => DateFormat.yMMM(_currentLocale),
DateRange.allTime => DateFormat.yMMM(_currentLocale),
DateRange.oneWeek => DateFormat.MMMd(currentLocale),
DateRange.oneMonth => DateFormat.MMMd(currentLocale),
DateRange.threeMonths => DateFormat.yMMM(currentLocale),
DateRange.oneYear => DateFormat.yMMM(currentLocale),
DateRange.allTime => DateFormat.yMMM(currentLocale),
};

String formatDateFromTimestamp(double nbDays) => chartDateFormatter.format(
_firstDate.add(Duration(days: nbDays.toInt())),
);

String formatDateFromTimestampForTooltip(double nbDays) =>
DateFormat.yMMMd(_currentLocale).format(
DateFormat.yMMMd(currentLocale).format(
_firstDate.add(Duration(days: nbDays.toInt())),
);

Expand Down
7 changes: 4 additions & 3 deletions lib/src/view/user/user_activity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import 'package:lichess_mobile/src/model/user/user_repository.dart';
import 'package:lichess_mobile/src/styles/lichess_colors.dart';
import 'package:lichess_mobile/src/styles/lichess_icons.dart';
import 'package:lichess_mobile/src/styles/styles.dart';
import 'package:lichess_mobile/src/utils/current_locale.dart';
import 'package:lichess_mobile/src/utils/l10n_context.dart';
import 'package:lichess_mobile/src/view/account/rating_pref_aware.dart';
import 'package:lichess_mobile/src/widgets/list.dart';
Expand All @@ -19,8 +20,6 @@ import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'user_activity.g.dart';

final _dateFormatter = DateFormat.yMMMd(Intl.getCurrentLocale());

@riverpod
Future<IList<UserActivity>> _userActivity(
_UserActivityRef ref, {
Expand Down Expand Up @@ -98,6 +97,8 @@ class UserActivityEntry extends ConsumerWidget {
final redColor = theme.extension<CustomColors>()?.error;
final greenColor = theme.extension<CustomColors>()?.good;

final dateFormatter = ref.withLocale((locale) => DateFormat.yMMMd(locale));

return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expand All @@ -109,7 +110,7 @@ class UserActivityEntry extends ConsumerWidget {
bottom: 4.0,
),
child: Text(
_dateFormatter.format(entry.startTime),
dateFormatter.format(entry.startTime),
style: TextStyle(
color: context.lichessColors.brag,
fontWeight: FontWeight.bold,
Expand Down

0 comments on commit 82063f6

Please sign in to comment.