Skip to content
This repository has been archived by the owner on Nov 7, 2023. It is now read-only.

Commit

Permalink
refactor: use switch exprs
Browse files Browse the repository at this point in the history
  • Loading branch information
zyrouge committed Nov 7, 2023
1 parent c6a200c commit 0274481
Show file tree
Hide file tree
Showing 18 changed files with 164 additions and 240 deletions.
125 changes: 42 additions & 83 deletions lib/core/anilist/translations.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,41 +33,28 @@ extension TenkaTypeAnilistUtils on TenkaType {
}

extension AnimeSeasonsTUtils on AnimeSeasons {
String getTitleCase(final Translation translation) {
switch (this) {
case AnimeSeasons.winter:
return translation.winter;

case AnimeSeasons.spring:
return translation.spring;

case AnimeSeasons.summer:
return translation.summer;

case AnimeSeasons.fall:
return translation.fall;
}
}
String getTitleCase(final Translation translation) => switch (this) {
AnimeSeasons.winter => translation.winter,
AnimeSeasons.spring => translation.spring,
AnimeSeasons.summer => translation.summer,
AnimeSeasons.fall => translation.fall,
};
}

extension AnilistMediaTUtils on AnilistMedia {
String getWatchtime(final Translation translation) {
switch (type) {
case AnilistMediaType.anime:
if (format == AnilistMediaFormat.movie) {
return duration != null
String getWatchtime(final Translation translation) => switch (type) {
AnilistMediaType.anime when format == AnilistMediaFormat.movie =>
duration != null
? PrettyDurations.prettyHoursMinutesShort(
translation,
Duration(minutes: duration!),
)
: translation.nMins(Translation.unk);
}
return translation.nEps(episodes?.toString() ?? Translation.unk);

case AnilistMediaType.manga:
return translation.nChs(chapters?.toString() ?? Translation.unk);
}
}
: translation.nMins(Translation.unk),
AnilistMediaType.anime =>
translation.nEps(episodes?.toString() ?? Translation.unk),
AnilistMediaType.manga =>
translation.nChs(chapters?.toString() ?? Translation.unk),
};

String get airdate {
final String startDatePretty = startDate?.maybePretty ?? Translation.unk;
Expand All @@ -88,65 +75,37 @@ extension AnilistCharacterRoleTUtils on AnilistCharacterRole {
}

extension AnilistMediaStatusTUtils on AnilistMediaStatus {
String getTitleCase(final Translation translation) {
switch (this) {
case AnilistMediaStatus.cancelled:
return translation.cancelled;

case AnilistMediaStatus.releasing:
return translation.releasing;

case AnilistMediaStatus.notYetReleased:
return translation.notYetReleased;

case AnilistMediaStatus.finished:
return translation.finished;

case AnilistMediaStatus.hiatus:
return translation.hiatus;
}
}
String getTitleCase(final Translation translation) => switch (this) {
AnilistMediaStatus.cancelled => translation.cancelled,
AnilistMediaStatus.releasing => translation.releasing,
AnilistMediaStatus.notYetReleased => translation.notYetReleased,
AnilistMediaStatus.finished => translation.finished,
AnilistMediaStatus.hiatus => translation.hiatus,
};
}

const Map<AnilistMediaFormat, String> _anilistMediaFormatTitleMap =
<AnilistMediaFormat, String>{
AnilistMediaFormat.tv: 'TV',
AnilistMediaFormat.tvShort: 'TV (Short)',
AnilistMediaFormat.movie: 'Movie',
AnilistMediaFormat.special: 'Special',
AnilistMediaFormat.ova: 'OVA',
AnilistMediaFormat.ona: 'ONA',
AnilistMediaFormat.music: 'Music',
AnilistMediaFormat.manga: 'Manga',
AnilistMediaFormat.novel: 'Novel',
AnilistMediaFormat.oneshot: 'OneShot',
};

extension AnilistMediaFormatTUtils on AnilistMediaFormat {
String getTitleCase(final Translation translation) =>
_anilistMediaFormatTitleMap[this]!;
String getTitleCase(final Translation translation) => switch (this) {
AnilistMediaFormat.tv => 'TV',
AnilistMediaFormat.tvShort => 'TV (Short)',
AnilistMediaFormat.movie => 'Movie',
AnilistMediaFormat.special => 'Special',
AnilistMediaFormat.ova => 'OVA',
AnilistMediaFormat.ona => 'ONA',
AnilistMediaFormat.music => 'Music',
AnilistMediaFormat.manga => 'Manga',
AnilistMediaFormat.novel => 'Novel',
AnilistMediaFormat.oneshot => 'OneShot',
};
}

extension AnilistMediaListStatusTUtils on AnilistMediaListStatus {
String getTitleCase(final Translation translation) {
switch (this) {
case AnilistMediaListStatus.current:
return translation.current;

case AnilistMediaListStatus.planning:
return translation.planning;

case AnilistMediaListStatus.completed:
return translation.completed;

case AnilistMediaListStatus.dropped:
return translation.dropped;

case AnilistMediaListStatus.paused:
return translation.paused;

case AnilistMediaListStatus.repeating:
return translation.repeating;
}
}
String getTitleCase(final Translation translation) => switch (this) {
AnilistMediaListStatus.current => translation.current,
AnilistMediaListStatus.planning => translation.planning,
AnilistMediaListStatus.completed => translation.completed,
AnilistMediaListStatus.dropped => translation.dropped,
AnilistMediaListStatus.paused => translation.paused,
AnilistMediaListStatus.repeating => translation.repeating,
};
}
2 changes: 1 addition & 1 deletion lib/core/database/settings/schema.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:kazahana/ui/utils/relative_size.dart';
import 'package:kazahana/ui/utils/relative_scale.dart';
import '../../utils/exports.dart';

part 'schema.g.dart';
Expand Down
13 changes: 4 additions & 9 deletions lib/core/tenka/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,8 @@ import 'package:tenka/tenka.dart';
import '../translator/exports.dart';

extension TenkaTypeUtils on TenkaType {
String getTitleCase(final Translation translation) {
switch (this) {
case TenkaType.anime:
return translation.anime;

case TenkaType.manga:
return translation.manga;
}
}
String getTitleCase(final Translation translation) => switch (this) {
TenkaType.anime => translation.anime,
TenkaType.manga => translation.manga,
};
}
81 changes: 24 additions & 57 deletions lib/core/themes/colors.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,61 +44,28 @@ abstract class ForegroundColors {

static List<String> names() => colors.keys.toList();

static String getTitleCase(final Translation translation, final String name) {
switch (name) {
case 'red':
return translation.red;

case 'orange':
return translation.orange;

case 'amber':
return translation.amber;

case 'yellow':
return translation.yellow;

case 'lime':
return translation.lime;

case 'green':
return translation.green;

case 'emerald':
return translation.emerald;

case 'teal':
return translation.teal;

case 'cyan':
return translation.cyan;

case 'sky':
return translation.sky;

case 'blue':
return translation.blue;

case 'indigo':
return translation.indigo;

case 'violet':
return translation.violet;

case 'purple':
return translation.purple;

case 'fuchsia':
return translation.fuchsia;

case 'pink':
return translation.pink;

case 'rose':
return translation.rose;

default:
return name;
}
}
static String getTitleCase(
final Translation translation,
final String name,
) =>
switch (name) {
'red' => translation.red,
'orange' => translation.orange,
'amber' => translation.amber,
'yellow' => translation.yellow,
'lime' => translation.lime,
'green' => translation.green,
'emerald' => translation.emerald,
'teal' => translation.teal,
'cyan' => translation.cyan,
'sky' => translation.sky,
'blue' => translation.blue,
'indigo' => translation.indigo,
'violet' => translation.violet,
'purple' => translation.purple,
'fuchsia' => translation.fuchsia,
'pink' => translation.pink,
'rose' => translation.rose,
_ => name,
};
}
15 changes: 6 additions & 9 deletions lib/ui/pages/_home/components/body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ class UnderScoreHomePageBody extends StatelessWidget {
final UnderScoreHomePageProvider provider =
context.watch<UnderScoreHomePageProvider>();

switch (provider.type) {
case TenkaType.anime:
return Column(
return switch (provider.type) {
TenkaType.anime => Column(
key: const ValueKey<TenkaType>(TenkaType.anime),
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
Expand All @@ -79,10 +78,8 @@ class UnderScoreHomePageBody extends StatelessWidget {
results: provider.mostPopularAnime,
),
],
);

case TenkaType.manga:
return Column(
),
TenkaType.manga => Column(
key: const ValueKey<TenkaType>(TenkaType.manga),
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
Expand All @@ -100,8 +97,8 @@ class UnderScoreHomePageBody extends StatelessWidget {
results: provider.mostPopularManga,
),
],
);
}
),
};
}

@override
Expand Down
20 changes: 8 additions & 12 deletions lib/ui/pages/anilist/components/body/profile/body.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,14 @@ class AnilistPageProfileBodyBody extends StatelessWidget {
),
);

String getMediaProgressText(final AnilistMedia media) {
switch (media.type) {
case AnilistMediaType.anime:
return '${media.mediaListEntry?.progress ?? 0}/${media.episodes ?? Translation.unk}';

case AnilistMediaType.manga:
return <String>[
'${media.mediaListEntry?.progress ?? 0}/${media.episodes ?? 0}',
'(${media.mediaListEntry?.progressVolumes ?? 0}/${media.volumes ?? Translation.unk})',
].join(' ');
}
}
String getMediaProgressText(final AnilistMedia media) => switch (media.type) {
AnilistMediaType.anime =>
'${media.mediaListEntry?.progress ?? 0}/${media.episodes ?? Translation.unk}',
AnilistMediaType.manga => <String>[
'${media.mediaListEntry?.progress ?? 0}/${media.episodes ?? 0}',
'(${media.mediaListEntry?.progressVolumes ?? 0}/${media.volumes ?? Translation.unk})',
].join(' '),
};

List<Widget> buildTiles({
required final BuildContext context,
Expand Down
2 changes: 0 additions & 2 deletions lib/ui/pages/anilist/components/body/profile/hero.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class AnilistPageProfileBodyHero extends StatelessWidget {
switch (provider.category.type) {
case AnilistMediaType.anime:
final AnilistUserStatistics? stats = user.animeStatistics;

return <Widget>[
buildStatisticsChild(
context: context,
Expand Down Expand Up @@ -69,7 +68,6 @@ class AnilistPageProfileBodyHero extends StatelessWidget {

case AnilistMediaType.manga:
final AnilistUserStatistics? stats = user.mangaStatistics;

return <Widget>[
buildStatisticsChild(
context: context,
Expand Down
5 changes: 4 additions & 1 deletion lib/ui/pages/settings/components/tiles/choice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ class _MultiChoiceListTileState<T> extends State<MultiChoiceListTile<T>> {
subtitle: widget.items[widget.value],
onTap: () async {
WidgetsBinding.instance.addPostFrameCallback((final _) {
Scrollable.ensureVisible(_initActiveOptionKey.currentContext!);
Scrollable.ensureVisible(
_initActiveOptionKey.currentContext!,
duration: AnimationDurations.defaultNormalAnimation,
);
});
final T? value = await showModalBottomSheet<T>(
context: context,
Expand Down
18 changes: 6 additions & 12 deletions lib/ui/pages/settings/view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,9 @@ enum _SettingsCategory {
}

extension on _SettingsCategory {
String getTitleCase(final Translation translation) {
switch (this) {
case _SettingsCategory.appearance:
return translation.appearance;
}
}
String getTitleCase(final Translation translation) => switch (this) {
_SettingsCategory.appearance => translation.appearance,
};
}

class _SettingsPageState extends State<SettingsPage> {
Expand Down Expand Up @@ -80,12 +77,9 @@ class _SettingsPageState extends State<SettingsPage> {
);
}

Widget buildBody(final BuildContext context) {
switch (category) {
case _SettingsCategory.appearance:
return const ApperanceSettings();
}
}
Widget buildBody(final BuildContext context) => switch (category) {
_SettingsCategory.appearance => const ApperanceSettings(),
};

@override
Widget build(final BuildContext context) => Scaffold(
Expand Down
Loading

0 comments on commit 0274481

Please sign in to comment.