Skip to content

Commit

Permalink
State management experiements
Browse files Browse the repository at this point in the history
Add stateful value notifiwe

Add wolt state management
  • Loading branch information
ulusoyca committed Nov 20, 2024
1 parent e5e79c8 commit 26fff29
Show file tree
Hide file tree
Showing 18 changed files with 1,912 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class OrdersDependencyContainer extends FeatureLevelDependencyContainer {

@override
void dispose() {
unbindFrom<CoffeeMakerAppLevelDependencyContainer>();
_ordersService.dispose();
_ordersRemoteDataSource.dispose();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:coffee_maker_navigator_2/features/orders/ui/widgets/top_bar.dart
import 'package:demo_ui_components/demo_ui_components.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:wolt_state_management/wolt_state_management.dart';

typedef OnCoffeeOrderUpdate = void Function(String coffeeOrderId);

Expand All @@ -28,7 +29,7 @@ class OrderScreenContent extends StatelessWidget {
});

final ValueListenable<CoffeeMakerStep> selectedNavBarTabListenable;
final ValueListenable<GroupedCoffeeOrders> groupedCoffeeOrders;
final StatefulValueListenable<GroupedCoffeeOrders> groupedCoffeeOrders;
final OnOrderScreenBottomNavBarItemSelected onNavBarItemSelected;
final OnCoffeeOrderUpdate onGrindCoffeeStepSelected;
final OnCoffeeOrderUpdate onAddWaterCoffeeStepSelected;
Expand All @@ -47,9 +48,15 @@ class OrderScreenContent extends StatelessWidget {
children: [
TopBar(selectedTab: selectedTab),
Expanded(
child: ValueListenableBuilder(
child: StatefulValueListenableBuilder(
valueListenable: groupedCoffeeOrders,
builder: (context, orders, _) {
idleBuilder:
(BuildContext context, GroupedCoffeeOrders? orders) {
if (orders == null) {
return const Center(
child: Text('No orders available'));
}

return CoffeeOrderListViewForStep(
groupedCoffeeOrders: orders,
selectedBottomNavBarItem: selectedTab,
Expand All @@ -59,6 +66,31 @@ class OrderScreenContent extends StatelessWidget {
onReadyCoffeeStepSelected: onReadyCoffeeStepSelected,
);
},
loadingBuilder:
(BuildContext context, GroupedCoffeeOrders? value) {
return const Center(child: CircularProgressIndicator());
},
errorBuilder: (
BuildContext context,
Object? error,
GroupedCoffeeOrders? lastKnownValue,
) {
return Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Error: $error'),
ElevatedButton(
onPressed: () {
/// TODO: call view model for retry.
// Retry by setting the last known value, call view model
},
child: const Text('Retry'),
),
],
),
);
},
),
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:coffee_maker_navigator_2/features/orders/ui/widgets/coffee_maker
import 'package:demo_ui_components/demo_ui_components.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:wolt_state_management/wolt_state_management.dart';

/// A custom bottom navigation bar for the home screen.
///
Expand All @@ -19,7 +20,7 @@ class OrdersScreenBottomNavigationBar extends StatelessWidget {
super.key,
});

final ValueListenable<GroupedCoffeeOrders> groupedCoffeeOrders;
final StatefulValueListenable<GroupedCoffeeOrders> groupedCoffeeOrders;
final ValueListenable<CoffeeMakerStep> selectedBottomNavBarItem;
final OnOrderScreenBottomNavBarItemSelected onBottomNavBarItemSelected;

Expand All @@ -28,9 +29,13 @@ class OrdersScreenBottomNavigationBar extends StatelessWidget {
return ValueListenableBuilder(
valueListenable: selectedBottomNavBarItem,
builder: (context, selectedTab, _) {
return ValueListenableBuilder(
return StatefulValueListenableBuilder(
valueListenable: groupedCoffeeOrders,
builder: (context, orders, _) {
idleBuilder: (BuildContext context, GroupedCoffeeOrders? orders) {
if (orders == null) {
return const SizedBox.shrink();
}

return Column(
mainAxisSize: MainAxisSize.min,
children: <Widget>[
Expand All @@ -52,6 +57,13 @@ class OrdersScreenBottomNavigationBar extends StatelessWidget {
],
);
},
loadingBuilder: (BuildContext context, GroupedCoffeeOrders? value) {
return const SizedBox.shrink();
},
errorBuilder: (BuildContext context, Object? error,
GroupedCoffeeOrders? lastKnownValue) {
return const SizedBox.shrink();
},
);
},
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
import 'package:coffee_maker_navigator_2/features/orders/domain/entities/coffee_maker_step.dart';
import 'package:coffee_maker_navigator_2/features/orders/domain/entities/grouped_coffee_orders.dart';
import 'package:coffee_maker_navigator_2/features/orders/domain/orders_service.dart';
import 'package:flutter/foundation.dart';
import 'package:wolt_state_management/wolt_state_management.dart';

class OrdersScreenViewModel {
OrdersScreenViewModel({required OrdersService ordersService})
: _ordersService = ordersService {
final currentOrders = _ordersService.orders.value;
_groupedCoffeeOrders.value =
GroupedCoffeeOrders.fromCoffeeOrders(currentOrders);
_groupedCoffeeOrders.setLoading();
_ordersService.orders.addListener(_onOrdersReceived);
}

final OrdersService _ordersService;

final _groupedCoffeeOrders = ValueNotifier(GroupedCoffeeOrders.empty());
final _groupedCoffeeOrders =
StatefulValueNotifier.idle(GroupedCoffeeOrders.empty());

ValueListenable<GroupedCoffeeOrders> get groupedCoffeeOrders =>
StatefulValueListenable<GroupedCoffeeOrders> get groupedCoffeeOrders =>
_groupedCoffeeOrders;

void dispose() {
Expand All @@ -29,7 +28,8 @@ class OrdersScreenViewModel {

void _onOrdersReceived() {
final orders = _ordersService.orders.value;
_groupedCoffeeOrders.value = GroupedCoffeeOrders.fromCoffeeOrders(orders);
_groupedCoffeeOrders.setIdle(
value: GroupedCoffeeOrders.fromCoffeeOrders(orders));
}

bool orderExists(String orderId, CoffeeMakerStep step) {
Expand Down
2 changes: 2 additions & 0 deletions coffee_maker_navigator_2/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ dependencies:
path: ../demo_ui_components
assets:
path: assets
wolt_state_management:
path: ../wolt_state_management

dev_dependencies:
flutter_lints: ^2.0.1
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:coffee_maker_navigator_2/features/orders/ui/view/widgets/orders_
import 'package:flutter/material.dart';
import 'package:widgetbook/widgetbook.dart';
import 'package:widgetbook_annotation/widgetbook_annotation.dart';
import 'package:wolt_state_management/wolt_state_management.dart';

@UseCase(
name: 'Filled',
Expand All @@ -14,7 +15,7 @@ import 'package:widgetbook_annotation/widgetbook_annotation.dart';
Widget ordersScreenBottomNavigationBarFilled(BuildContext context) {
return Scaffold(
bottomNavigationBar: OrdersScreenBottomNavigationBar(
ValueNotifier(
StatefulValueNotifier.idle(
const GroupedCoffeeOrders(
grindStateOrders: [
CoffeeOrder(
Expand Down Expand Up @@ -61,7 +62,7 @@ Widget ordersScreenBottomNavigationBarFilled(BuildContext context) {
Widget ordersScreenBottomNavigationBarEmpty(BuildContext context) {
return Scaffold(
bottomNavigationBar: OrdersScreenBottomNavigationBar(
ValueNotifier(GroupedCoffeeOrders.empty()),
StatefulValueNotifier.idle(GroupedCoffeeOrders.empty()),
(_) {},
ValueNotifier(
context.knobs.list(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import 'package:coffee_maker_navigator_2/features/orders/ui/view/widgets/order_s
import 'package:flutter/widgets.dart';
import 'package:widgetbook/widgetbook.dart';
import 'package:widgetbook_annotation/widgetbook_annotation.dart';
import 'package:wolt_state_management/wolt_state_management.dart';

@UseCase(
name: 'OrderScreenContent',
Expand All @@ -19,7 +20,7 @@ Widget orderScreenContent(BuildContext context) {
options: CoffeeMakerStep.values,
),
),
groupedCoffeeOrders: ValueNotifier(
groupedCoffeeOrders: StatefulValueNotifier.idle(
GroupedCoffeeOrders.fromCoffeeOrders(
const [
CoffeeOrder(
Expand Down
2 changes: 2 additions & 0 deletions coffee_maker_navigator_2/widgetbook/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ dependencies:
sdk: flutter
widgetbook: ^3.10.0
widgetbook_annotation: ^3.2.0
wolt_state_management:
path: ../../wolt_state_management
wolt_modal_sheet:
path: ../..
coffee_maker_navigator_2:
Expand Down
1 change: 1 addition & 0 deletions melos.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ packages:
- playground
- playground_navigator2
- wolt_di
- wolt_state_management

command:
bootstrap:
Expand Down
Loading

0 comments on commit 26fff29

Please sign in to comment.