Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(mobile): Use switch expression when possible #15852

Merged
merged 2 commits into from
Feb 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions mobile/lib/entities/asset.entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -545,19 +545,13 @@ enum AssetType {
}

extension AssetTypeEnumHelper on AssetTypeEnum {
AssetType toAssetType() {
switch (this) {
case AssetTypeEnum.IMAGE:
return AssetType.image;
case AssetTypeEnum.VIDEO:
return AssetType.video;
case AssetTypeEnum.AUDIO:
return AssetType.audio;
case AssetTypeEnum.OTHER:
return AssetType.other;
}
throw Exception();
}
AssetType toAssetType() => switch (this) {
AssetTypeEnum.IMAGE => AssetType.image,
AssetTypeEnum.VIDEO => AssetType.video,
AssetTypeEnum.AUDIO => AssetType.audio,
AssetTypeEnum.OTHER => AssetType.other,
_ => throw Exception(),
};
}

/// Describes where the information of this asset came from:
Expand Down
27 changes: 9 additions & 18 deletions mobile/lib/entities/store.entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,16 @@ class StoreValue {
int? intValue;
String? strValue;

T? _extract<T>(StoreKey<T> key) {
switch (key.type) {
case const (int):
return intValue as T?;
case const (bool):
return intValue == null ? null : (intValue! == 1) as T;
case const (DateTime):
return intValue == null
T? _extract<T>(StoreKey<T> key) => switch (key.type) {
const (int) => intValue as T?,
const (bool) => intValue == null ? null : (intValue! == 1) as T,
const (DateTime) => intValue == null
? null
: DateTime.fromMicrosecondsSinceEpoch(intValue!) as T;
case const (String):
return strValue as T?;
default:
if (key.fromDb != null) {
return key.fromDb!.call(Store._db, intValue!);
}
}
throw TypeError();
}
: DateTime.fromMicrosecondsSinceEpoch(intValue!) as T,
const (String) => strValue as T?,
_ when key.fromDb != null => key.fromDb!.call(Store._db, intValue!),
_ => throw TypeError(),
};

static Future<StoreValue> _of<T>(T? value, StoreKey<T> key) async {
int? i;
Expand Down
75 changes: 26 additions & 49 deletions mobile/lib/entities/user.entity.dart
Original file line number Diff line number Diff line change
Expand Up @@ -149,56 +149,33 @@ enum AvatarColorEnum {
}

extension AvatarColorEnumHelper on UserAvatarColor {
AvatarColorEnum toAvatarColor() {
switch (this) {
case UserAvatarColor.primary:
return AvatarColorEnum.primary;
case UserAvatarColor.pink:
return AvatarColorEnum.pink;
case UserAvatarColor.red:
return AvatarColorEnum.red;
case UserAvatarColor.yellow:
return AvatarColorEnum.yellow;
case UserAvatarColor.blue:
return AvatarColorEnum.blue;
case UserAvatarColor.green:
return AvatarColorEnum.green;
case UserAvatarColor.purple:
return AvatarColorEnum.purple;
case UserAvatarColor.orange:
return AvatarColorEnum.orange;
case UserAvatarColor.gray:
return AvatarColorEnum.gray;
case UserAvatarColor.amber:
return AvatarColorEnum.amber;
}
return AvatarColorEnum.primary;
}
AvatarColorEnum toAvatarColor() => switch (this) {
UserAvatarColor.primary => AvatarColorEnum.primary,
UserAvatarColor.pink => AvatarColorEnum.pink,
UserAvatarColor.red => AvatarColorEnum.red,
UserAvatarColor.yellow => AvatarColorEnum.yellow,
UserAvatarColor.blue => AvatarColorEnum.blue,
UserAvatarColor.green => AvatarColorEnum.green,
UserAvatarColor.purple => AvatarColorEnum.purple,
UserAvatarColor.orange => AvatarColorEnum.orange,
UserAvatarColor.gray => AvatarColorEnum.gray,
UserAvatarColor.amber => AvatarColorEnum.amber,
_ => AvatarColorEnum.primary,
};
}

extension AvatarColorToColorHelper on AvatarColorEnum {
Color toColor([bool isDarkTheme = false]) {
switch (this) {
case AvatarColorEnum.primary:
return isDarkTheme ? const Color(0xFFABCBFA) : const Color(0xFF4250AF);
case AvatarColorEnum.pink:
return const Color.fromARGB(255, 244, 114, 182);
case AvatarColorEnum.red:
return const Color.fromARGB(255, 239, 68, 68);
case AvatarColorEnum.yellow:
return const Color.fromARGB(255, 234, 179, 8);
case AvatarColorEnum.blue:
return const Color.fromARGB(255, 59, 130, 246);
case AvatarColorEnum.green:
return const Color.fromARGB(255, 22, 163, 74);
case AvatarColorEnum.purple:
return const Color.fromARGB(255, 147, 51, 234);
case AvatarColorEnum.orange:
return const Color.fromARGB(255, 234, 88, 12);
case AvatarColorEnum.gray:
return const Color.fromARGB(255, 75, 85, 99);
case AvatarColorEnum.amber:
return const Color.fromARGB(255, 217, 119, 6);
}
}
Color toColor([bool isDarkTheme = false]) => switch (this) {
AvatarColorEnum.primary =>
isDarkTheme ? const Color(0xFFABCBFA) : const Color(0xFF4250AF),
AvatarColorEnum.pink => const Color.fromARGB(255, 244, 114, 182),
AvatarColorEnum.red => const Color.fromARGB(255, 239, 68, 68),
AvatarColorEnum.yellow => const Color.fromARGB(255, 234, 179, 8),
AvatarColorEnum.blue => const Color.fromARGB(255, 59, 130, 246),
AvatarColorEnum.green => const Color.fromARGB(255, 22, 163, 74),
AvatarColorEnum.purple => const Color.fromARGB(255, 147, 51, 234),
AvatarColorEnum.orange => const Color.fromARGB(255, 234, 88, 12),
AvatarColorEnum.gray => const Color.fromARGB(255, 75, 85, 99),
AvatarColorEnum.amber => const Color.fromARGB(255, 217, 119, 6),
};
}
37 changes: 12 additions & 25 deletions mobile/lib/pages/common/app_log.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,32 +36,19 @@ class AppLogPage extends HookConsumerWidget {
);
}

Widget buildLeadingIcon(LogLevel level) {
switch (level) {
case LogLevel.INFO:
return colorStatusIndicator(context.primaryColor);
case LogLevel.SEVERE:
return colorStatusIndicator(Colors.redAccent);
Widget buildLeadingIcon(LogLevel level) => switch (level) {
LogLevel.INFO => colorStatusIndicator(context.primaryColor),
LogLevel.SEVERE => colorStatusIndicator(Colors.redAccent),
LogLevel.WARNING => colorStatusIndicator(Colors.orangeAccent),
_ => colorStatusIndicator(Colors.grey),
};

case LogLevel.WARNING:
return colorStatusIndicator(Colors.orangeAccent);
default:
return colorStatusIndicator(Colors.grey);
}
}

getTileColor(LogLevel level) {
switch (level) {
case LogLevel.INFO:
return Colors.transparent;
case LogLevel.SEVERE:
return Colors.redAccent.withOpacity(0.25);
case LogLevel.WARNING:
return Colors.orangeAccent.withOpacity(0.25);
default:
return context.primaryColor.withOpacity(0.1);
}
}
Color getTileColor(LogLevel level) => switch (level) {
LogLevel.INFO => Colors.transparent,
LogLevel.SEVERE => Colors.redAccent.withOpacity(0.25),
LogLevel.WARNING => Colors.orangeAccent.withOpacity(0.25),
_ => context.primaryColor.withOpacity(0.1),
};

return Scaffold(
appBar: AppBar(
Expand Down
30 changes: 10 additions & 20 deletions mobile/lib/pages/common/download_panel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -74,26 +74,16 @@ class DownloadTaskTile extends StatelessWidget {
Widget build(BuildContext context) {
final progressPercent = (progress * 100).round();

getStatusText() {
switch (status) {
case TaskStatus.running:
return 'downloading'.tr();
case TaskStatus.complete:
return 'download_complete'.tr();
case TaskStatus.failed:
return 'download_failed'.tr();
case TaskStatus.canceled:
return 'download_canceled'.tr();
case TaskStatus.paused:
return 'download_paused'.tr();
case TaskStatus.enqueued:
return 'download_enqueue'.tr();
case TaskStatus.notFound:
return 'download_notfound'.tr();
case TaskStatus.waitingToRetry:
return 'download_waiting_to_retry'.tr();
}
}
String getStatusText() => switch (status) {
TaskStatus.running => 'downloading'.tr(),
TaskStatus.complete => 'download_complete'.tr(),
TaskStatus.failed => 'download_failed'.tr(),
TaskStatus.canceled => 'download_canceled'.tr(),
TaskStatus.paused => 'download_paused'.tr(),
TaskStatus.enqueued => 'download_enqueue'.tr(),
TaskStatus.notFound => 'download_notfound'.tr(),
TaskStatus.waitingToRetry => 'download_waiting_to_retry'.tr(),
};

return SizedBox(
key: const ValueKey('download_progress'),
Expand Down
30 changes: 8 additions & 22 deletions mobile/lib/pages/editing/crop.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -174,33 +174,19 @@ class _AspectRatioButton extends StatelessWidget {

@override
Widget build(BuildContext context) {
IconData iconData;
switch (label) {
case 'Free':
iconData = Icons.crop_free_rounded;
break;
case '1:1':
iconData = Icons.crop_square_rounded;
break;
case '16:9':
iconData = Icons.crop_16_9_rounded;
break;
case '3:2':
iconData = Icons.crop_3_2_rounded;
break;
case '7:5':
iconData = Icons.crop_7_5_rounded;
break;
default:
iconData = Icons.crop_free_rounded;
}

return Column(
mainAxisSize: MainAxisSize.min,
children: [
IconButton(
icon: Icon(
iconData,
switch (label) {
'Free' => Icons.crop_free_rounded,
'1:1' => Icons.crop_square_rounded,
'16:9' => Icons.crop_16_9_rounded,
'3:2' => Icons.crop_3_2_rounded,
'7:5' => Icons.crop_7_5_rounded,
_ => Icons.crop_free_rounded,
},
color: aspectRatio.value == ratio
? context.primaryColor
: context.themeData.iconTheme.color,
Expand Down
27 changes: 10 additions & 17 deletions mobile/lib/pages/onboarding/permission_onboarding.page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -136,23 +136,16 @@ class PermissionOnboardingPage extends HookConsumerWidget {
);
}

final Widget child;
switch (permission) {
case PermissionStatus.limited:
child = buildPermissionLimited();
break;
case PermissionStatus.denied:
child = buildRequestPermission();
break;
case PermissionStatus.granted:
case PermissionStatus.provisional:
child = buildPermissionGranted();
break;
case PermissionStatus.restricted:
case PermissionStatus.permanentlyDenied:
child = buildPermissionDenied();
break;
}
final Widget child = switch (permission) {
PermissionStatus.limited => buildPermissionLimited(),
PermissionStatus.denied => buildRequestPermission(),
PermissionStatus.granted ||
PermissionStatus.provisional =>
buildPermissionGranted(),
PermissionStatus.restricted ||
PermissionStatus.permanentlyDenied =>
buildPermissionDenied()
};

return Scaffold(
body: SafeArea(
Expand Down
31 changes: 10 additions & 21 deletions mobile/lib/repositories/album.repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,11 @@ class AlbumRepository extends DatabaseRepository implements IAlbumRepository {
@override
Future<int> count({bool? local}) {
final baseQuery = db.albums.where();
final QueryBuilder<Album, Album, QAfterWhereClause> query;
switch (local) {
case null:
query = baseQuery.noOp();
case true:
query = baseQuery.localIdIsNotNull();
case false:
query = baseQuery.remoteIdIsNotNull();
}
final QueryBuilder<Album, Album, QAfterWhereClause> query = switch (local) {
null => baseQuery.noOp(),
true => baseQuery.localIdIsNotNull(),
false => baseQuery.remoteIdIsNotNull(),
};
return query.count();
}

Expand Down Expand Up @@ -91,15 +87,11 @@ class AlbumRepository extends DatabaseRepository implements IAlbumRepository {
if (ownerId != null) {
filterQuery = filterQuery.owner((q) => q.isarIdEqualTo(ownerId));
}
final QueryBuilder<Album, Album, QAfterSortBy> query;
switch (sortBy) {
case null:
query = filterQuery.noOp();
case AlbumSort.remoteId:
query = filterQuery.sortByRemoteId();
case AlbumSort.localId:
query = filterQuery.sortByLocalId();
}
final QueryBuilder<Album, Album, QAfterSortBy> query = switch (sortBy) {
null => filterQuery.noOp(),
AlbumSort.remoteId => filterQuery.sortByRemoteId(),
AlbumSort.localId => filterQuery.sortByLocalId(),
};
return query.findAll();
}

Expand Down Expand Up @@ -150,14 +142,11 @@ class AlbumRepository extends DatabaseRepository implements IAlbumRepository {
query = query.owner(
(q) => q.not().isarIdEqualTo(Store.get(StoreKey.currentUser).isarId),
);
break;
case QuickFilterMode.myAlbums:
query = query.owner(
(q) => q.isarIdEqualTo(Store.get(StoreKey.currentUser).isarId),
);
break;
case QuickFilterMode.all:
default:
break;
}

Expand Down
Loading
Loading