Skip to content

Commit

Permalink
EntitasFF updated to Dart 3! Added new null safety methods.
Browse files Browse the repository at this point in the history
  • Loading branch information
chqs-git committed Apr 22, 2024
1 parent 9139310 commit 7da1044
Show file tree
Hide file tree
Showing 25 changed files with 522 additions and 382 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
[![Build Status](https://travis-ci.org/mzaks/entitas_ff.svg?branch=master)](https://travis-ci.org/mzaks/entitas_ff) [![codecov](https://codecov.io/gh/mzaks/entitas_ff/branch/master/graph/badge.svg)](https://codecov.io/gh/mzaks/entitas_ff)

Entitas for Flutter
This library brings __Entitas__ an implementation of Enitty Component System pattern to [Dart](https://www.dartlang.org) programming language and [Flutter](https://flutter.dev) platform.

If you are intrested how Entitas for Flutter can help you build reactive and testable application, please have a look at following articles:
This library brings __Entitas__ an implementation of Entity Component System pattern to [Dart](https://www.dartlang.org) programming language and [Flutter](https://flutter.dev) platform.

If you are interested how Entitas for Flutter can help you build reactive and testable application, please have a look at following articles:
- https://medium.com/@icex33/entitas-for-flutter-e1ea063a123c
- https://medium.com/@icex33/services-in-entitas-for-flutter-b131495d5536
- https://medium.com/@icex33/the-power-of-tick-eef6cd209d62
8 changes: 4 additions & 4 deletions example/counter_app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ class MyApp extends StatelessWidget {
Text(
'You have pushed the button this many times:',
),
// Text widget represnting the count built by [EntityObservingWidget]
// Text widget representing the count built by [EntityObservingWidget]
EntityObservingWidget(
provider: (em) => em.getUniqueEntity<CountComponent>(),
builder: (e, context) => Text(
e.get<CountComponent>().value.toString(),
style: Theme.of(context).textTheme.display1,
e.getOrNull<CountComponent>()?.value.toString() ?? "",
style: Theme.of(context).textTheme.displayMedium,
),
)
],
Expand All @@ -54,7 +54,7 @@ class MyApp extends StatelessWidget {
onPressed: () {
final entityManager =
EntityManagerProvider.of(context).entityManager;
final count = entityManager.getUnique<CountComponent>().value;
final count = entityManager.getUnique<CountComponent>()?.value ?? 0;
entityManager.setUnique(CountComponent(count + 1));
},
tooltip: 'increment count',
Expand Down
2 changes: 1 addition & 1 deletion example/counter_app_with_systems/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class MyApp extends StatelessWidget {
provider: (m) => m.getUniqueEntity<CountComponent>(),
builder: (e, context) => Text(
e.get<CountComponent>().value.toString(),
style: Theme.of(context).textTheme.display1,
style: Theme.of(context).textTheme.displayMedium,
),
)
],
Expand Down
4 changes: 2 additions & 2 deletions example/github_search/lib/github_search_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ class GithubService {
final Group results;

GithubService({
this.manager,
HttpClient client,
required this.manager,
HttpClient? client,
this.baseUrl = "https://api.github.com/search/repositories?q=",
}) : this.client = client ?? new HttpClient(),
this.results = manager.group(all: [NameComponent, UrlComponent, AvatarUrlComponent]) {
Expand Down
6 changes: 3 additions & 3 deletions example/github_search/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ Widget buildApp() {
}

class SearchScreen extends StatelessWidget {
SearchScreen({Key key}) : super(key: key);
SearchScreen() : super();

@override
Widget build(BuildContext context) {
Expand All @@ -53,9 +53,9 @@ class SearchScreen extends StatelessWidget {
EntityObservingWidget(
provider: (m) => m.getUniqueEntity<SearchStateComponent>(),
builder: (e, context) {
final state = e.get<SearchStateComponent>();
final state = e.getOrNull<SearchStateComponent>();
return Expanded(
child: overlay(state.value),
child: overlay(state?.value ?? SearchState.empty),
);
},
)
Expand Down
6 changes: 3 additions & 3 deletions example/github_search/lib/overlays.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SearchErrorWidget extends OverlayWithWarning {
String get message => "An error occured";

@override
Color get messageColor => Colors.red[300];
Color get messageColor => Colors.redAccent;
}

class SearchIntroWidget extends OverlayWithWarning {
Expand All @@ -54,7 +54,7 @@ class SearchIntroWidget extends OverlayWithWarning {
String get message => "Enter a search term to begin";

@override
Color get messageColor => Colors.green[100];
Color get messageColor => Colors.green;
}

class EmptyResultWidget extends OverlayWithWarning {
Expand All @@ -66,7 +66,7 @@ class EmptyResultWidget extends OverlayWithWarning {
String get message => "No results";

@override
Color get messageColor => Colors.yellow[100];
Color get messageColor => Colors.yellow;
}

class SearchLoadingWidget extends StatelessWidget {
Expand Down
2 changes: 1 addition & 1 deletion example/github_search/lib/search_result_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SearchResultWidget extends StatelessWidget {
new MaterialPageRoute<Null>(
builder: (BuildContext context) {
return new Scaffold(
resizeToAvoidBottomPadding: false,
resizeToAvoidBottomInset: false,
body: new GestureDetector(
key: new Key(avatarUrl),
onTap: () => Navigator.pop(context),
Expand Down
24 changes: 10 additions & 14 deletions example/github_search_with_systems/lib/github_search_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class GithubService {
final EntityMap<SearchResultComponent, String> _searchResultCache;

GithubService({
@required manager,
HttpClient client,
required manager,
HttpClient? client,
this.baseUrl = "https://api.github.com/search/repositories?q=",
}) : this._client = client ?? new HttpClient(),
this._manager = manager,
Expand All @@ -24,15 +24,13 @@ class GithubService {
}

search(String term) async {
var result = _searchResultCache[term];
if (result == null){
result = _manager.createEntity();
try {
var repositories = await _fetchResults(term);
result += SearchResultComponent(repositories, term);
} catch (error) {
result += SearchErrorComponent(error);
}
var result = _searchResultCache[term] ?? _manager.createEntity();

try {
var repositories = await _fetchResults(term);
result += SearchResultComponent(repositories, term);
} catch (error) {
result += SearchErrorComponent(error);
}
_manager.setUniqueOnEntity(CurrentResultFlagComponent(), result);
var tick = _manager.getUnique<CurrentTickComponent>().value;
Expand All @@ -44,9 +42,7 @@ class GithubService {
final response = await request.close();
final results = json.decode(await response.transform(utf8.decoder).join());
var list = (results['items'] as List);
if(list == null) {
throw results;
}

return list.cast<Map<String, Object>>();
}
}
6 changes: 3 additions & 3 deletions example/github_search_with_systems/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ Widget buildApp() {
}

class SearchScreen extends StatelessWidget {
SearchScreen({Key key}) : super(key: key);
SearchScreen() : super();

@override
Widget build(BuildContext context) {
Expand Down Expand Up @@ -68,9 +68,9 @@ class SearchScreen extends StatelessWidget {
EntityObservingWidget(
provider: (m) => m.getUniqueEntity<SearchStateComponent>(),
builder: (e, context) {
final state = e.get<SearchStateComponent>();
final state = e.getOrNull<SearchStateComponent>();
return Expanded(
child: overlay(state.value),
child: overlay(state?.value ?? SearchState.empty),
);
},
)
Expand Down
6 changes: 3 additions & 3 deletions example/github_search_with_systems/lib/overlays.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SearchErrorWidget extends OverlayWithWarning {
String get message => "An error occured";

@override
Color get messageColor => Colors.red[300];
Color get messageColor => Colors.red;
}

class SearchIntroWidget extends OverlayWithWarning {
Expand All @@ -54,7 +54,7 @@ class SearchIntroWidget extends OverlayWithWarning {
String get message => "Enter a search term to begin";

@override
Color get messageColor => Colors.green[100];
Color get messageColor => Colors.green;
}

class EmptyResultWidget extends OverlayWithWarning {
Expand All @@ -66,7 +66,7 @@ class EmptyResultWidget extends OverlayWithWarning {
String get message => "No results";

@override
Color get messageColor => Colors.yellow[100];
Color get messageColor => Colors.yellow;
}

class SearchLoadingWidget extends StatelessWidget {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class SearchResultWidget extends StatelessWidget {
new MaterialPageRoute<Null>(
builder: (BuildContext context) {
return new Scaffold(
resizeToAvoidBottomPadding: false,
resizeToAvoidBottomInset: false,
body: new GestureDetector(
key: new Key(avatarUrl),
onTap: () => Navigator.pop(context),
Expand Down
10 changes: 5 additions & 5 deletions example/github_search_with_systems/lib/systems.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ class TickSystem extends EntityManagerSystem implements InitSystem, ExecuteSyste
class ScheduleSearchSystem extends EntityManagerSystem implements ExecuteSystem {
@override
execute() {
var searchTermEntity = entityManager.getUniqueEntity<SearchTermComponent>();
var searchTermEntity = entityManager.getUniqueEntityOrNull<SearchTermComponent>();
if (searchTermEntity == null) return;
var tick = entityManager.getUnique<CurrentTickComponent>().value;
var searchTick = searchTermEntity.get<TickComponent>()?.value ?? 0;
var searchTick = searchTermEntity.getOrNull<TickComponent>()?.value ?? 0;
if (searchTick + 20 < tick) {
var term = searchTermEntity.get<SearchTermComponent>().value;
if (term.isEmpty && entityManager.group(all:[NameComponent, UrlComponent, AvatarUrlComponent]).isEmpty) {
Expand Down Expand Up @@ -63,7 +63,7 @@ class ProcessResultsSystem extends TriggeredSystem {
}

class ProcessErrorsSystem extends EntityManagerSystem implements InitSystem, ExecuteSystem, CleanupSystem {
Group _errors;
late Group _errors;
@override
init() {
_errors = entityManager.group(all: [SearchErrorComponent]);
Expand All @@ -72,7 +72,7 @@ class ProcessErrorsSystem extends EntityManagerSystem implements InitSystem, Exe
@override
execute() {
for (var e in _errors.entities) {
print(e.get<SearchErrorComponent>().value);
print(e.getOrNull<SearchErrorComponent>()?.value);
if (e.has(CurrentResultFlagComponent)) {
entityManager.setUnique(SearchStateComponent(SearchState.error));
}
Expand All @@ -86,7 +86,7 @@ class ProcessErrorsSystem extends EntityManagerSystem implements InitSystem, Exe
}

class RemoveOldResultsSystem extends EntityManagerSystem implements InitSystem, CleanupSystem {
Group _searchResults;
late Group _searchResults;

@override
init() {
Expand Down
34 changes: 14 additions & 20 deletions example/shopping_cart/lib/widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ class ProductSquare extends StatelessWidget {
final Entity product;

ProductSquare({
Key key,
@required this.product
}) : super(key: key);
required this.product
}) : super();

@override
Widget build(BuildContext context) {
Expand All @@ -24,7 +23,7 @@ class ProductSquare extends StatelessWidget {
color: color,
child: InkWell(
onTap: (){
final prevCount = product.get<CountComponent>()?.value ?? 0;
final prevCount = product.getOrNull<CountComponent>()?.value ?? 0;
// ignore: unnecessary_statements
product + CountComponent(prevCount + 1);
},
Expand All @@ -51,26 +50,22 @@ class CartButton extends StatefulWidget {
final Color badgeTextColor;

CartButton({
Key key,
@required this.itemCount,
this.onPressed,
this.badgeColor: Colors.red,
this.badgeTextColor: Colors.white,
required this.itemCount,
required this.onPressed,
this.badgeColor = Colors.red,
this.badgeTextColor = Colors.white,
}) : assert(itemCount >= 0),
assert(badgeColor != null),
assert(badgeTextColor != null),
super(key: key);
super();

@override
CartButtonState createState() {
return CartButtonState();
}
}

class CartButtonState extends State<CartButton>
with SingleTickerProviderStateMixin {
AnimationController _animationController;
Animation<double> _animation;
class CartButtonState extends State<CartButton> with SingleTickerProviderStateMixin {
late AnimationController _animationController;
late Animation<double> _animation;

final Tween<Offset> _badgePositionTween = Tween(
begin: const Offset(-0.5, 0.9),
Expand All @@ -82,8 +77,7 @@ class CartButtonState extends State<CartButton>

return IconButton(
icon: Stack(
overflow: Overflow.visible,
children: [
clipBehavior: Clip.none, children: [
Icon(Icons.shopping_cart),
Positioned(
top: -8.0,
Expand Down Expand Up @@ -154,7 +148,7 @@ class CartPage extends StatelessWidget {
),
body: group.isEmpty
? Center(
child: Text('Empty', style: Theme.of(context).textTheme.display1))
child: Text('Empty', style: Theme.of(context).textTheme.displayMedium))
: ListView(
children:
sortedEntries.map((item) => ItemTile(item: item)).toList()),
Expand All @@ -163,7 +157,7 @@ class CartPage extends StatelessWidget {
}

class ItemTile extends StatelessWidget {
ItemTile({this.item});
ItemTile({required this.item});
final Entity item;

@override
Expand Down
6 changes: 2 additions & 4 deletions example/shopping_cart_complex/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class MyHomePage extends StatelessWidget {
title: EntityObservingWidget(
provider: (em) => em.getUniqueEntity<TotalAmountLabelComponent>(),
builder: (e, context){
final value = e?.get<TotalAmountLabelComponent>()?.value ?? "0";
final value = e.getOrNull<TotalAmountLabelComponent>()?.value ?? "0";
return Text('Total: ($value)');
},
),
Expand Down Expand Up @@ -83,9 +83,7 @@ class MyHomePage extends StatelessWidget {

class ProductGrid extends StatelessWidget {

ProductGrid({
Key key,
}) : super(key: key);
ProductGrid() : super();

@override
Widget build(BuildContext context) {
Expand Down
Loading

0 comments on commit 7da1044

Please sign in to comment.