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

Feature/extensions #7

Merged
merged 16 commits into from Jun 10, 2022
Merged

Feature/extensions #7

merged 16 commits into from Jun 10, 2022

Conversation

Rongix
Copy link
Collaborator

@Rongix Rongix commented Jun 8, 2022

Add extensions to access theme/theme properties to ThemeData / BuildContext

ThemeGetter.onBuildContextProps is selected as a default option, this is a subject to change. Also we may allow to generate few of these at once.

Generated extensions examples:

/// ThemeGetter.onBuildContext
extension SimpleThemeBuildContextExtension on BuildContext {
  SimpleTheme get simpleTheme => Theme.of(this).extension<SimpleTheme>()!;
}

/// ThemeGetter.onBuildContextProps
extension SimpleThemeBuildContextExtension on BuildContext {
  SimpleTheme get _simpleTheme => Theme.of(this).extension<SimpleTheme>()!;
  Color get background => _simpleTheme.background;
  Color get surface => _simpleTheme.surface;
  Color get appBar => _simpleTheme.appBar;
  TextStyle get h1 => _simpleTheme.h1;
  TextStyle get h2 => _simpleTheme.h2;
}

/// ThemeGetter.onThemeData
extension SimpleThemeThemeDataExtension on ThemeData {
  SimpleTheme get simpleTheme => extension<SimpleTheme>()!;
}

/// ThemeGetter.onThemeDataProps
extension SimpleThemeThemeDataExtension on ThemeData {
  SimpleTheme get _simpleTheme => extension<SimpleTheme>()!;
  Color get background => _simpleTheme.background;
  Color get surface => _simpleTheme.surface;
  Color get appBar => _simpleTheme.appBar;
  TextStyle get h1 => _simpleTheme.h1;
  TextStyle get h2 => _simpleTheme.h2;
}

@Rongix Rongix marked this pull request as ready for review June 9, 2022 07:31
Comment on lines +33 to +52
extension StringExtension on String {
String trimFirst() => isEmpty ? this : substring(1);

String firstToUpper() => _safeSubstring(toUpper: true);
String firstToLower() => _safeSubstring(toUpper: false);

String firstToUpperUnsafe() => this[0].toUpperCase() + substring(1);
String firstToLowerUnsafe() => this[0].toLowerCase() + substring(1);

String get firstAsUpper => this[0].toUpperCase();
String get firstAsLower => this[0].toLowerCase();

bool _canRunSubstring(String val) => val.length > 1;
String _safeSubstring({required bool toUpper}) {
late final first = toUpper ? firstAsUpper : firstAsLower;
if (isEmpty) return this;
if (!_canRunSubstring(this)) return first;
return first + substring(1);
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Można to znacząco skrócić:

extension StringExtension on String {
  String firstToUpper() => length > 0 ? '$firstAsUpper${substring(1)}' : '';

  String firstToLower() => length > 0 ? '$firstAsLower${substring(1)}' : '';

  String trimFirst() => isEmpty ? this : substring(1);

  String get firstAsUpper => this[0].toUpperCase();

  String get firstAsLower => this[0].toLowerCase();
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

String firstToUpper() => length > 0 ? '$firstAsUpper${substring(1)}' : '';
W przypadku gdy coś ma length 1 (np.: "a") to wyrzuci RangeError (jest uwzględniony ten przypadek w testach)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sprawdziłem to, testy z tą implementacją też przechodzą
Screenshot 2022-06-10 at 13 25 57

@jakubtiteo
Copy link
Contributor

Wygląda na to że example nie chce się poprawnie skompilować po wygenerowaniu kodu - w wygenerowanym kodzie mamy _SimpleTheme zamiast użytego w main.dart SimpleTheme, co bardziej zwróciło moją uwagę to nazwa metody w wygenerowanym kodzie extension na BuildContext:

extension _SimpleThemeBuildContext on BuildContext {
  _SimpleTheme get _SimpleThemeTheme =>
      Theme.of(this).extension<_SimpleTheme>()!;
}

@Rongix
Copy link
Collaborator Author

Rongix commented Jun 10, 2022

Fixed test for generated class name. Now it will correctly trim first 2 special symbols -> _$ || \$_
main.tailor.dart and SimpleTheme are now generated correctly

@Rongix Rongix merged commit 74b0d98 into main Jun 10, 2022
@Rongix Rongix deleted the feature/extensions branch June 29, 2022 09:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants