Skip to content

Commit

Permalink
Refactor code to use new switch statements
Browse files Browse the repository at this point in the history
  • Loading branch information
CodeDoctorDE committed Oct 15, 2023
1 parent bee3bef commit 166e1d4
Show file tree
Hide file tree
Showing 19 changed files with 214 additions and 395 deletions.
4 changes: 2 additions & 2 deletions app/android/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ GEM
artifactory (3.0.15)
atomos (0.1.3)
aws-eventstream (1.2.0)
aws-partitions (1.834.0)
aws-partitions (1.835.0)
aws-sdk-core (3.185.1)
aws-eventstream (~> 1, >= 1.0.2)
aws-partitions (~> 1, >= 1.651.0)
Expand Down Expand Up @@ -107,7 +107,7 @@ GEM
xcpretty (~> 0.3.0)
xcpretty-travis-formatter (>= 0.0.3)
gh_inspector (1.1.3)
google-apis-androidpublisher_v3 (0.50.0)
google-apis-androidpublisher_v3 (0.51.0)
google-apis-core (>= 0.11.0, < 2.a)
google-apis-core (0.11.1)
addressable (~> 2.5, >= 2.5.1)
Expand Down
16 changes: 5 additions & 11 deletions app/lib/dialogs/file_system/move.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,11 @@ class _FileSystemAssetMoveDialogState extends State<FileSystemAssetMoveDialog> {

@override
Widget build(BuildContext context) {
String title;
switch (widget.moveMode) {
case MoveMode.duplicate:
title = AppLocalizations.of(context).duplicate;
break;
case MoveMode.move:
title = AppLocalizations.of(context).move;
break;
default:
title = AppLocalizations.of(context).changeDocumentPath;
}
String title = switch (widget.moveMode) {
MoveMode.duplicate => AppLocalizations.of(context).duplicate,
MoveMode.move => AppLocalizations.of(context).move,
_ => AppLocalizations.of(context).changeDocumentPath,
};
return AlertDialog(
actions: [
TextButton(
Expand Down
36 changes: 11 additions & 25 deletions app/lib/dialogs/packs/select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,17 @@ class SelectPackAssetDialog extends StatelessWidget {
this.selected,
});

List<PackAssetLocation> _getAssets(NoteData document) {
final packs = document
.getPacks()
.map((e) => document.getPack(e))
.whereNotNull()
.toList();
switch (type) {
case PackAssetType.component:
return packs
.expand((pack) => pack
.getComponents()
.map((e) => PackAssetLocation(pack.name!, e)))
.toList();
case PackAssetType.style:
return packs
.expand((pack) =>
pack.getStyles().map((e) => PackAssetLocation(pack.name!, e)))
.toList();
case PackAssetType.palette:
return packs
.expand((pack) =>
pack.getPalettes().map((e) => PackAssetLocation(pack.name!, e)))
.toList();
}
}
List<PackAssetLocation> _getAssets(NoteData document) => document
.getPacks()
.map((e) => document.getPack(e))
.whereNotNull()
.expand((pack) => switch (type) {
PackAssetType.component => pack.getComponents(),
PackAssetType.style => pack.getStyles(),
PackAssetType.palette => pack.getPalettes(),
}
.map((e) => PackAssetLocation(pack.name!, e)))
.toList();

NoteData _createAsset(NoteData pack, String name) => switch (type) {
PackAssetType.component =>
Expand Down
33 changes: 7 additions & 26 deletions app/lib/dialogs/packs/styles/text.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:butterfly/visualizer/text.dart';
import 'package:butterfly_api/butterfly_text.dart' as text;
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
Expand Down Expand Up @@ -181,32 +182,12 @@ class _TextStyleViewState extends State<TextStyleView> {
initialSelection: widget.value.decorationStyle,
dropdownMenuEntries: [
...text.TextDecorationStyle.values
.map((style) {
String name;
switch (style) {
case text.TextDecorationStyle.solid:
name =
AppLocalizations.of(context).solid;
break;
case text.TextDecorationStyle.dashed:
name =
AppLocalizations.of(context).dashed;
break;
case text.TextDecorationStyle.dotted:
name =
AppLocalizations.of(context).dotted;
break;
case text.TextDecorationStyle.double:
name =
AppLocalizations.of(context).double;
break;
case text.TextDecorationStyle.wavy:
name =
AppLocalizations.of(context).wavy;
}
return DropdownMenuEntry(
value: style, label: name);
}).toList(),
.map((style) => DropdownMenuEntry(
value: style,
label:
style.getLocalizedName(context),
))
.toList(),
DropdownMenuEntry(
value: null,
label: AppLocalizations.of(context).notSet,
Expand Down
33 changes: 11 additions & 22 deletions app/lib/handlers/select.dart
Original file line number Diff line number Diff line change
Expand Up @@ -120,28 +120,17 @@ extension HandTransformCornerExtension on HandTransformCorner {
}
}

Offset getFromRect(Rect rect) {
switch (this) {
case HandTransformCorner.topLeft:
return rect.topLeft;
case HandTransformCorner.topCenter:
return rect.topCenter;
case HandTransformCorner.topRight:
return rect.topRight;
case HandTransformCorner.centerLeft:
return rect.centerLeft;
case HandTransformCorner.centerRight:
return rect.centerRight;
case HandTransformCorner.bottomLeft:
return rect.bottomLeft;
case HandTransformCorner.bottomCenter:
return rect.bottomCenter;
case HandTransformCorner.bottomRight:
return rect.bottomRight;
case HandTransformCorner.center:
return rect.center;
}
}
Offset getFromRect(Rect rect) => switch (this) {
HandTransformCorner.topLeft => rect.topLeft,
HandTransformCorner.topCenter => rect.topCenter,
HandTransformCorner.topRight => rect.topRight,
HandTransformCorner.centerLeft => rect.centerLeft,
HandTransformCorner.centerRight => rect.centerRight,
HandTransformCorner.bottomLeft => rect.bottomLeft,
HandTransformCorner.bottomCenter => rect.bottomCenter,
HandTransformCorner.bottomRight => rect.bottomRight,
HandTransformCorner.center => rect.center,
};
}

class SelectHandler extends Handler<SelectTool> {
Expand Down
13 changes: 5 additions & 8 deletions app/lib/models/label.dart
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,10 @@ extension LabelElementLayouter on LabelElement {
Point<double> getOffset(double height) {
final align = areaProperty.alignment;
final current = position;
switch (align) {
case txt.VerticalAlignment.top:
return current;
case txt.VerticalAlignment.bottom:
return current + Point(0, height);
case txt.VerticalAlignment.center:
return current + Point(0, height / 2);
}
return switch (align) {
txt.VerticalAlignment.top => current,
txt.VerticalAlignment.bottom => current + Point(0, height),
txt.VerticalAlignment.center => current + Point(0, height / 2),
};
}
}
27 changes: 10 additions & 17 deletions app/lib/services/import.dart
Original file line number Diff line number Diff line change
Expand Up @@ -85,23 +85,16 @@ class ImportService {
}

Future<NoteData?> import(
AssetFileType type, Uint8List bytes, NoteData document,
{Offset? position}) async {
switch (type) {
case AssetFileType.note:
return importBfly(bytes, document, position);
case AssetFileType.image:
return importImage(bytes, document, position);
case AssetFileType.svg:
return importSvg(bytes, document, position);
case AssetFileType.markdown:
return importMarkdown(bytes, document, position);
case AssetFileType.pdf:
return importPdf(bytes, document, position, true);
case AssetFileType.page:
return importPage(bytes, document, position);
}
}
AssetFileType type, Uint8List bytes, NoteData document,
{Offset? position}) async =>
switch (type) {
AssetFileType.note => importBfly(bytes, document, position),
AssetFileType.image => importImage(bytes, document, position),
AssetFileType.svg => importSvg(bytes, document, position),
AssetFileType.markdown => importMarkdown(bytes, document, position),
AssetFileType.pdf => importPdf(bytes, document, position, true),
AssetFileType.page => importPage(bytes, document, position),
};

FutureOr<NoteData?> importBfly(Uint8List bytes,
[NoteData? document, Offset? position]) async {
Expand Down
74 changes: 26 additions & 48 deletions app/lib/settings/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,22 @@ enum SettingsView {
personalization,
connections;

String getLocalizedName(BuildContext context) {
switch (this) {
case SettingsView.general:
return AppLocalizations.of(context).general;
case SettingsView.data:
return AppLocalizations.of(context).data;
case SettingsView.behaviors:
return AppLocalizations.of(context).behaviors;
case SettingsView.personalization:
return AppLocalizations.of(context).personalization;
case SettingsView.connections:
return AppLocalizations.of(context).connections;
}
}

PhosphorIconData getIcon() {
switch (this) {
case SettingsView.general:
return PhosphorIconsLight.gear;
case SettingsView.data:
return PhosphorIconsLight.database;
case SettingsView.behaviors:
return PhosphorIconsLight.faders;
case SettingsView.personalization:
return PhosphorIconsLight.monitor;
case SettingsView.connections:
return PhosphorIconsLight.cloud;
}
}
String getLocalizedName(BuildContext context) => switch (this) {
SettingsView.general => AppLocalizations.of(context).general,
SettingsView.data => AppLocalizations.of(context).data,
SettingsView.behaviors => AppLocalizations.of(context).behaviors,
SettingsView.personalization =>
AppLocalizations.of(context).personalization,
SettingsView.connections => AppLocalizations.of(context).connections,
};

IconGetter get icon => switch (this) {
SettingsView.general => PhosphorIcons.gear,
SettingsView.data => PhosphorIcons.database,
SettingsView.behaviors => PhosphorIcons.faders,
SettingsView.personalization => PhosphorIcons.monitor,
SettingsView.connections => PhosphorIcons.cloud,
};
String get path => '/settings/$name';
}

Expand Down Expand Up @@ -107,7 +93,8 @@ class _SettingsPageState extends State<SettingsPage> {
shrinkWrap: true,
children: [
...SettingsView.values.map((view) => ListTile(
leading: PhosphorIcon(view.getIcon()),
leading: PhosphorIcon(
view.icon(PhosphorIconsStyle.light)),
title: Text(view.getLocalizedName(context)),
onTap: () => navigateTo(view),
selected: _view == view && !isMobile,
Expand Down Expand Up @@ -146,24 +133,15 @@ class _SettingsPageState extends State<SettingsPage> {
if (isMobile) {
return navigation;
}
Widget content;
switch (_view) {
case SettingsView.general:
content = const GeneralSettingsPage(inView: true);
break;
case SettingsView.data:
content = const DataSettingsPage(inView: true);
break;
case SettingsView.behaviors:
content = const BehaviorsSettingsPage(inView: true);
break;
case SettingsView.personalization:
content = const PersonalizationSettingsPage(inView: true);
break;
case SettingsView.connections:
content = const ConnectionsSettingsPage(inView: true);
break;
}
final content = switch (_view) {
SettingsView.general => const GeneralSettingsPage(inView: true),
SettingsView.data => const DataSettingsPage(inView: true),
SettingsView.behaviors => const BehaviorsSettingsPage(inView: true),
SettingsView.personalization =>
const PersonalizationSettingsPage(inView: true),
SettingsView.connections =>
const ConnectionsSettingsPage(inView: true),
};
return Row(crossAxisAlignment: CrossAxisAlignment.stretch, children: [
SizedBox(width: 300, child: navigation),
Expanded(child: content),
Expand Down
17 changes: 5 additions & 12 deletions app/lib/settings/personalization.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,11 @@ class PersonalizationSettingsPage extends StatelessWidget {
final bool inView;
const PersonalizationSettingsPage({super.key, this.inView = false});

String _getThemeName(BuildContext context, ThemeMode mode) {
switch (mode) {
case ThemeMode.system:
return AppLocalizations.of(context).systemTheme;
case ThemeMode.light:
return AppLocalizations.of(context).lightTheme;
case ThemeMode.dark:
return AppLocalizations.of(context).darkTheme;
default:
return AppLocalizations.of(context).systemTheme;
}
}
String _getThemeName(BuildContext context, ThemeMode mode) => switch (mode) {
ThemeMode.system => AppLocalizations.of(context).systemTheme,
ThemeMode.light => AppLocalizations.of(context).lightTheme,
ThemeMode.dark => AppLocalizations.of(context).darkTheme,
};

String _getPlatformThemeName(BuildContext context, PlatformTheme theme) =>
switch (theme) {
Expand Down
15 changes: 5 additions & 10 deletions app/lib/views/files.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,11 @@ class _FilesViewState extends State<FilesView> {
}
}

String getLocalizedNameOfSortBy(SortBy sortBy) {
switch (sortBy) {
case SortBy.name:
return AppLocalizations.of(context).name;
case SortBy.created:
return AppLocalizations.of(context).created;
case SortBy.modified:
return AppLocalizations.of(context).modified;
}
}
String getLocalizedNameOfSortBy(SortBy sortBy) => switch (sortBy) {
SortBy.name => AppLocalizations.of(context).name,
SortBy.created => AppLocalizations.of(context).created,
SortBy.modified => AppLocalizations.of(context).modified,
};

void _setFilesFuture() {
_fileSystem = DocumentFileSystem.fromPlatform(remote: _remote);
Expand Down
Loading

0 comments on commit 166e1d4

Please sign in to comment.