Frank Moreno

onDelete moved into GetLifeCycle

import 'package:flutter/material.dart';
/// Special callable class to keep the contract of a regular method, and avoid
/// overrides if you extend the class that uses it, as Dart has no final
/// methods.
... ... @@ -6,11 +8,19 @@
class _InternalFinalCallback<T> {
T Function() callback;
_InternalFinalCallback();
_InternalFinalCallback({this.callback});
T call() => callback.call();
}
class _InternalFinalPrivateCallback<T> {
T Function() _callback;
_InternalFinalPrivateCallback();
T call() => _callback.call();
}
mixin GetLifeCycle {
/// Called at the exact moment the widget is allocated in memory.
/// It uses an internal "callable" type, to avoid any @overrides in subclases.
... ... @@ -18,11 +28,15 @@ mixin GetLifeCycle {
/// lifetime cycle of the subclass.
final onStart = _InternalFinalCallback<void>();
final onDelete = _InternalFinalCallback<void>();
/// Internal callback that starts the cycle of this controller.
final onDelete = _InternalFinalPrivateCallback<void>();
/// Called immediately after the widget is allocated in memory.
/// You might use this to initialize something for the controller.
void onInit() {}
@mustCallSuper
void onInit() {
onDelete._callback = _onDelete;
}
/// Called 1 frame after onInit(). It is the perfect place to enter
/// navigation events, like snackbar, dialogs, or a new route, or
... ... @@ -36,6 +50,18 @@ mixin GetLifeCycle {
/// like TextEditingControllers, AnimationControllers.
/// Might be useful as well to persist some data on disk.
void onClose() {}
bool _isClosed = false;
/// Checks whether the controller has already been closed.
bool get isClosed => _isClosed;
// Internal callback that starts the cycle of this controller.
void _onDelete() {
if (_isClosed) return;
_isClosed = true;
onClose();
}
}
/// Allow track difference between GetxServices and GetxControllers
... ...
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import '../../../get_instance/src/lifecycle.dart';
... ... @@ -15,14 +16,8 @@ abstract class DisposableInterface with GetLifeCycle {
/// Checks whether the controller has already been initialized.
bool get initialized => _initialized;
bool _isClosed = false;
/// Checks whether the controller has already been closed.
bool get isClosed => _isClosed;
DisposableInterface() {
onStart.callback = _onStart;
onDelete.callback = _onDelete;
}
// Internal callback that starts the cycle of this controller.
... ... @@ -33,22 +28,17 @@ abstract class DisposableInterface with GetLifeCycle {
SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());
}
// Internal callback that starts the cycle of this controller.
void _onDelete() {
if (_isClosed) return;
_isClosed = true;
onClose();
}
/// Called immediately after the widget is allocated in memory.
/// You might use this to initialize something for the controller.
@override
void onInit() {}
@mustCallSuper
void onInit() => super.onInit();
/// Called 1 frame after onInit(). It is the perfect place to enter
/// navigation events, like snackbar, dialogs, or a new route, or
/// async request.
@override
@mustCallSuper
void onReady() {}
/// Called before [onDelete] method. [onClose] might be used to
... ... @@ -58,5 +48,6 @@ abstract class DisposableInterface with GetLifeCycle {
/// like TextEditingControllers, AnimationControllers.
/// Might be useful as well to persist some data on disk.
@override
@mustCallSuper
void onClose() {}
}
... ...
... ... @@ -39,7 +39,6 @@ typedef Condition = bool Function();
abstract class GetNotifier<T> extends Value<T> with GetLifeCycle {
GetNotifier(T initial) : super(initial) {
onStart.callback = _onStart;
onDelete.callback = _onDelete;
_fillEmptyStatus();
}
... ... @@ -48,11 +47,6 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycle {
/// Checks whether the controller has already been initialized.
bool get initialized => _initialized;
bool _isClosed = false;
/// Checks whether the controller has already been closed.
bool get isClosed => _isClosed;
// Internal callback that starts the cycle of this controller.
void _onStart() {
if (_initialized) return;
... ... @@ -61,13 +55,6 @@ abstract class GetNotifier<T> extends Value<T> with GetLifeCycle {
SchedulerBinding.instance?.addPostFrameCallback((_) => onReady());
}
// Internal callback that starts the cycle of this controller.
void _onDelete() {
if (_isClosed) return;
_isClosed = true;
onClose();
}
RxStatus _status;
bool get isNullOrEmpty {
... ...
... ... @@ -12,7 +12,6 @@ class Mock {
class DisposableController with GetLifeCycle {
DisposableController() {
onStart.callback = _onStart;
onDelete.callback = _onDelete;
}
// Internal callback that starts the cycle of this controller.
... ... @@ -21,18 +20,13 @@ class DisposableController with GetLifeCycle {
onInit();
}
// Internal callback that starts the cycle of this controller.
void _onDelete() {
if (isClosed) return;
isClosed = true;
onClose();
}
bool initialized = false;
bool isClosed = false;
@override
void onInit() async {
super.onInit();
initialized = true;
}
}
... ...