Skip to content

Commit

Permalink
Merge pull request #311 from woltapp/coffee-maker-demo-app-add-lifecy…
Browse files Browse the repository at this point in the history
…cle-observer

[Coffee Maker App] Introduce AppLifecycleObserver Widget for App Lifecycle Management
  • Loading branch information
ulusoyca authored Aug 20, 2024
2 parents 9d88159 + 5dbf7c8 commit ac17031
Show file tree
Hide file tree
Showing 27 changed files with 576 additions and 446 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
<application
android:label="coffee_maker_navigator_2"
android:name="${applicationName}"
android:enableOnBackInvokedCallback="true"
android:icon="@mipmap/ic_launcher">
<activity
android:name=".MainActivity"
Expand Down
15 changes: 9 additions & 6 deletions coffee_maker_navigator_2/lib/app/app.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:coffee_maker_navigator_2/app/app_lifecycle/ui/app_lifecycle_listenable.dart';
import 'package:wolt_di/wolt_di.dart';
import 'package:coffee_maker_navigator_2/app/di/coffee_maker_app_level_dependency_container.dart';
import 'package:demo_ui_components/demo_ui_components.dart';
Expand All @@ -13,12 +14,14 @@ class CoffeeMakerApp extends StatelessWidget {
final appLevelDependencyContainer = DependencyInjector.container<
CoffeeMakerAppLevelDependencyContainer>(context);

return MaterialApp.router(
debugShowCheckedModeBanner: false,
theme: AppThemeData.themeData(context),
routerDelegate: appLevelDependencyContainer.appRouterDelegate,
backButtonDispatcher:
appLevelDependencyContainer.backButtonDispatcher,
return AppLifecycleObserver(
child: MaterialApp.router(
debugShowCheckedModeBanner: false,
theme: AppThemeData.themeData(context),
routerDelegate: appLevelDependencyContainer.appRouterDelegate,
backButtonDispatcher:
appLevelDependencyContainer.backButtonDispatcher,
),
);
}),
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'package:flutter/foundation.dart';

enum AppLifeCycleState {
foreground,
background,
}

class AppLifeCycleService {
late ValueNotifier<AppLifeCycleState> lifeCycleState;

ValueListenable<AppLifeCycleState> get appLifeStateListenable =>
lifeCycleState;

AppLifeCycleService() {
lifeCycleState = ValueNotifier(AppLifeCycleState.foreground);
}

void onForegrounded() {
lifeCycleState.value = AppLifeCycleState.foreground;
}

void onBackgrounded() {
lifeCycleState.value = AppLifeCycleState.background;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import 'package:coffee_maker_navigator_2/app/di/coffee_maker_app_level_dependency_container.dart';
import 'package:flutter/widgets.dart';
import 'package:wolt_di/wolt_di.dart';

class AppLifecycleObserver extends StatefulWidget {
const AppLifecycleObserver({required this.child, super.key});

final Widget child;

@override
State<AppLifecycleObserver> createState() => _AppLifecycleObserverState();
}

class _AppLifecycleObserverState extends State<AppLifecycleObserver> {
late final AppLifecycleListener listener;

@override
void initState() {
super.initState();
final appLevelDependencyContainer =
DependencyInjector.container<CoffeeMakerAppLevelDependencyContainer>(
context);
final appLifeCycleService = appLevelDependencyContainer.appLifeCycleService;
listener = AppLifecycleListener(
onShow: () {
debugPrint('AppLifecycleListener: onForegrounded');
appLifeCycleService.onForegrounded();
},
onHide: () {
debugPrint('AppLifecycleListener: onBackgrounded');
appLifeCycleService.onBackgrounded();
},
);
}

@override
void dispose() {
listener.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return widget.child;
}
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:coffee_maker_navigator_2/app/app_lifecycle/domain/app_lifecyle_service.dart';
import 'package:coffee_maker_navigator_2/app/auth/data/local/auth_local_data_source.dart';
import 'package:coffee_maker_navigator_2/app/auth/data/repository/auth_repository.dart';
import 'package:coffee_maker_navigator_2/app/auth/domain/auth_service.dart';
Expand All @@ -24,6 +25,8 @@ class CoffeeMakerAppLevelDependencyContainer
late final _onboardingRepository = _createOnboardingRepository();
late final _onboardingService = _createOnboardingService();

late final _appLifeCycleService = _createAppLifeCycleService();

late final _appRouterDelegate = _createAppRouterDelegate();
late final _backButtonDispatcher = _createRootBackButtonDispatcher();
late final _routerViewModel = _createRouterViewModel();
Expand All @@ -32,6 +35,7 @@ class CoffeeMakerAppLevelDependencyContainer
BackButtonDispatcher get backButtonDispatcher => _backButtonDispatcher;
RouterViewModel get routerViewModel => _routerViewModel;
AuthService get authService => _authService;
AppLifeCycleService get appLifeCycleService => _appLifeCycleService;

CoffeeMakerAppLevelDependencyContainer();

Expand Down Expand Up @@ -78,4 +82,8 @@ class CoffeeMakerAppLevelDependencyContainer
onboardingService: _onboardingService,
);
}

AppLifeCycleService _createAppLifeCycleService() {
return AppLifeCycleService();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import 'package:coffee_maker_navigator_2/features/add_water/domain/add_water_ser
import 'package:coffee_maker_navigator_2/features/orders/domain/orders_service.dart';
import 'package:coffee_maker_navigator_2/features/add_water/ui/view_model/add_water_view_model.dart';

class AddWaterDependencyContainer
extends FeatureWithViewModelDependencyContainer {
class AddWaterDependencyContainer extends FeatureLevelDependencyContainer {
// Just in the sake to show that we can use a method here to make the service lazily initialized.
late final AddWaterService _addWaterService = _createAddWaterService();
// This is an example to non-lazy initialization.
Expand All @@ -20,7 +19,6 @@ class AddWaterDependencyContainer
return AddWaterService();
}

@override
AddWaterViewModel createViewModel() => AddWaterViewModel(
addWaterService: _addWaterService,
ordersService: _ordersService,
Expand Down
Loading

0 comments on commit ac17031

Please sign in to comment.