Skip to content

Commit

Permalink
refactor: migrate WidgetsBindingObserver to AppLifecycleListener
Browse files Browse the repository at this point in the history
  • Loading branch information
S-ecki authored and domesticmouse committed Dec 5, 2023
1 parent 8b31ff0 commit 1f2fceb
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 59 deletions.
18 changes: 6 additions & 12 deletions samples/ads/lib/app_lifecycle/app_lifecycle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import 'package:provider/provider.dart';

typedef AppLifecycleStateNotifier = ValueNotifier<AppLifecycleState>;

// TODO(filiph): use the new AppLifecycleListener class, e.g.
// https://kazlauskas.dev/flutter-app-lifecycle-listener-overview/
class AppLifecycleObserver extends StatefulWidget {
final Widget child;

Expand All @@ -19,9 +17,9 @@ class AppLifecycleObserver extends StatefulWidget {
State<AppLifecycleObserver> createState() => _AppLifecycleObserverState();
}

class _AppLifecycleObserverState extends State<AppLifecycleObserver>
with WidgetsBindingObserver {
class _AppLifecycleObserverState extends State<AppLifecycleObserver> {
static final _log = Logger('AppLifecycleObserver');
late final AppLifecycleListener _appLifecycleListener;

final ValueNotifier<AppLifecycleState> lifecycleListenable =
ValueNotifier(AppLifecycleState.inactive);
Expand All @@ -46,22 +44,18 @@ class _AppLifecycleObserverState extends State<AppLifecycleObserver>
);
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_log.info(() => 'didChangeAppLifecycleState: $state');
lifecycleListenable.value = state;
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_appLifecycleListener.dispose();
super.dispose();
}

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_appLifecycleListener = AppLifecycleListener(
onStateChange: (s) => lifecycleListenable.value = s,
);
_log.info('Subscribed to app lifecycle updates');
}
}
18 changes: 6 additions & 12 deletions samples/multiplayer/lib/app_lifecycle/app_lifecycle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import 'package:provider/provider.dart';

typedef AppLifecycleStateNotifier = ValueNotifier<AppLifecycleState>;

// TODO(filiph): use the new AppLifecycleListener class, e.g.
// https://kazlauskas.dev/flutter-app-lifecycle-listener-overview/
class AppLifecycleObserver extends StatefulWidget {
final Widget child;

Expand All @@ -19,9 +17,9 @@ class AppLifecycleObserver extends StatefulWidget {
State<AppLifecycleObserver> createState() => _AppLifecycleObserverState();
}

class _AppLifecycleObserverState extends State<AppLifecycleObserver>
with WidgetsBindingObserver {
class _AppLifecycleObserverState extends State<AppLifecycleObserver> {
static final _log = Logger('AppLifecycleObserver');
late final AppLifecycleListener _appLifecycleListener;

final ValueNotifier<AppLifecycleState> lifecycleListenable =
ValueNotifier(AppLifecycleState.inactive);
Expand All @@ -46,22 +44,18 @@ class _AppLifecycleObserverState extends State<AppLifecycleObserver>
);
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_log.info(() => 'didChangeAppLifecycleState: $state');
lifecycleListenable.value = state;
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_appLifecycleListener.dispose();
super.dispose();
}

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_appLifecycleListener = AppLifecycleListener(
onStateChange: (s) => lifecycleListenable.value = s,
);
_log.info('Subscribed to app lifecycle updates');
}
}
18 changes: 6 additions & 12 deletions templates/basic/lib/app_lifecycle/app_lifecycle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import 'package:provider/provider.dart';

typedef AppLifecycleStateNotifier = ValueNotifier<AppLifecycleState>;

// TODO(filiph): use the new AppLifecycleListener class, e.g.
// https://kazlauskas.dev/flutter-app-lifecycle-listener-overview/
class AppLifecycleObserver extends StatefulWidget {
final Widget child;

Expand All @@ -19,9 +17,9 @@ class AppLifecycleObserver extends StatefulWidget {
State<AppLifecycleObserver> createState() => _AppLifecycleObserverState();
}

class _AppLifecycleObserverState extends State<AppLifecycleObserver>
with WidgetsBindingObserver {
class _AppLifecycleObserverState extends State<AppLifecycleObserver> {
static final _log = Logger('AppLifecycleObserver');
late final AppLifecycleListener _appLifecycleListener;

final ValueNotifier<AppLifecycleState> lifecycleListenable =
ValueNotifier(AppLifecycleState.inactive);
Expand All @@ -46,22 +44,18 @@ class _AppLifecycleObserverState extends State<AppLifecycleObserver>
);
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_log.info(() => 'didChangeAppLifecycleState: $state');
lifecycleListenable.value = state;
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_appLifecycleListener.dispose();
super.dispose();
}

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_appLifecycleListener = AppLifecycleListener(
onStateChange: (s) => lifecycleListenable.value = s,
);
_log.info('Subscribed to app lifecycle updates');
}
}
18 changes: 6 additions & 12 deletions templates/card/lib/app_lifecycle/app_lifecycle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import 'package:provider/provider.dart';

typedef AppLifecycleStateNotifier = ValueNotifier<AppLifecycleState>;

// TODO(filiph): use the new AppLifecycleListener class, e.g.
// https://kazlauskas.dev/flutter-app-lifecycle-listener-overview/
class AppLifecycleObserver extends StatefulWidget {
final Widget child;

Expand All @@ -19,9 +17,9 @@ class AppLifecycleObserver extends StatefulWidget {
State<AppLifecycleObserver> createState() => _AppLifecycleObserverState();
}

class _AppLifecycleObserverState extends State<AppLifecycleObserver>
with WidgetsBindingObserver {
class _AppLifecycleObserverState extends State<AppLifecycleObserver> {
static final _log = Logger('AppLifecycleObserver');
late final AppLifecycleListener _appLifecycleListener;

final ValueNotifier<AppLifecycleState> lifecycleListenable =
ValueNotifier(AppLifecycleState.inactive);
Expand All @@ -46,22 +44,18 @@ class _AppLifecycleObserverState extends State<AppLifecycleObserver>
);
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_log.info(() => 'didChangeAppLifecycleState: $state');
lifecycleListenable.value = state;
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_appLifecycleListener.dispose();
super.dispose();
}

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_appLifecycleListener = AppLifecycleListener(
onStateChange: (s) => lifecycleListenable.value = s,
);
_log.info('Subscribed to app lifecycle updates');
}
}
18 changes: 7 additions & 11 deletions templates/endless_runner/lib/app_lifecycle/app_lifecycle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import 'package:provider/provider.dart';

typedef AppLifecycleStateNotifier = ValueNotifier<AppLifecycleState>;

// TODO(filiph): use the new AppLifecycleListener class, e.g.
// https://kazlauskas.dev/flutter-app-lifecycle-listener-overview/
class AppLifecycleObserver extends StatefulWidget {
final Widget child;

Expand All @@ -14,8 +12,9 @@ class AppLifecycleObserver extends StatefulWidget {
State<AppLifecycleObserver> createState() => _AppLifecycleObserverState();
}

class _AppLifecycleObserverState extends State<AppLifecycleObserver>
with WidgetsBindingObserver {
class _AppLifecycleObserverState extends State<AppLifecycleObserver> {
late final AppLifecycleListener _appLifecycleListener;

final ValueNotifier<AppLifecycleState> lifecycleListenable =
ValueNotifier(AppLifecycleState.inactive);

Expand All @@ -39,20 +38,17 @@ class _AppLifecycleObserverState extends State<AppLifecycleObserver>
);
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
lifecycleListenable.value = state;
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
_appLifecycleListener.dispose();
super.dispose();
}

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);
_appLifecycleListener = AppLifecycleListener(
onStateChange: (s) => lifecycleListenable.value = s,
);
}
}

0 comments on commit 1f2fceb

Please sign in to comment.