Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/core/lib/utils/lifecycle/impl.dart
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export 'default.dart' if (dart.library.io) 'fgbg.dart';
Copy link
Contributor

Choose a reason for hiding this comment

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

since fgbg is not being used anymore should we just remove it? and also remove the dependency on flutter_fgbg?

export 'default.dart' if (dart.library.io) 'widget_observer.dart';
86 changes: 86 additions & 0 deletions packages/core/lib/utils/lifecycle/widget_observer.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:async';
import 'package:flutter/widgets.dart';

enum AppStatus { foreground, background }

abstract class LifeCycle extends Stream<AppStatus> {}

class LifeCycleImpl extends LifeCycle with WidgetsBindingObserver {
final StreamController<AppLifecycleState> _streamController = StreamController<AppLifecycleState>.broadcast();

LifeCycleImpl() {
WidgetsBinding.instance.addObserver(this);
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
_streamController.add(state);
}

@override
StreamSubscription<AppStatus> listen(void Function(AppStatus event)? onData, {Function? onError, void Function()? onDone, bool? cancelOnError}) {
final subscription = _streamController.stream.listen((event) => event == AppLifecycleState.resumed ? AppStatus.foreground : AppStatus.background);
return _LifeCycleSuscription(subscription);
}
}

class _LifeCycleSuscription extends StreamSubscription<AppStatus> {
final StreamSubscription<AppLifecycleState> _subscription;

_LifeCycleSuscription(this._subscription);

@override
Future<E> asFuture<E>([E? futureValue]) {
return _subscription.asFuture(futureValue);
}

@override
Future<void> cancel() {
return _subscription.cancel();
}

@override
bool get isPaused => _subscription.isPaused;

@override
void onData(void Function(AppStatus data)? handleData) {
_subscription.onData(handleData == null
? null
: (data) {
if (data == AppLifecycleState.resumed) {
handleData(AppStatus.foreground);
} else if (data == AppLifecycleState.paused || data == AppLifecycleState.inactive) {
handleData(AppStatus.background);
}
});
}

@override
void onDone(void Function()? handleDone) {
_subscription.onData(handleDone == null
? null
: (data) {
handleDone();
});
}

@override
void onError(Function? handleError) {
_subscription.onData(handleError == null
? null
: (error) {
handleError(error);
});
}

@override
void pause([Future<void>? resumeSignal]) {
_subscription.pause(resumeSignal);
}

@override
void resume() {
_subscription.resume();
}
}

1 change: 0 additions & 1 deletion packages/core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ dependencies:
plugin_platform_interface: ^2.0.2
uuid: ^3.0.7
json_annotation: ^4.8.0
flutter_fgbg: ^0.2.2
state_notifier: ^0.7.2
http: ">=0.13.6 <2.0.0"
logger: ^1.1.0
Expand Down