fix(query): isLoading & isRefreshing not working as expected
This commit is contained in:
@@ -29,13 +29,15 @@ class QueryPage extends StatelessWidget {
|
|||||||
'hello',
|
'hello',
|
||||||
() {
|
() {
|
||||||
return Future.delayed(
|
return Future.delayed(
|
||||||
const Duration(seconds: 6), () => 'Hello World! $value');
|
const Duration(seconds: 6),
|
||||||
|
() => 'Hello World! $value',
|
||||||
|
);
|
||||||
},
|
},
|
||||||
initial: 'Hello',
|
initial: 'Hello',
|
||||||
jsonConfig: JsonConfig(
|
// jsonConfig: JsonConfig(
|
||||||
fromJson: (json) => json['data'],
|
// fromJson: (json) => json['data'],
|
||||||
toJson: (data) => {'data': data},
|
// toJson: (data) => {'data': data},
|
||||||
),
|
// ),
|
||||||
onData: (value) {
|
onData: (value) {
|
||||||
debugPrint('onData: $value');
|
debugPrint('onData: $value');
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -21,23 +21,28 @@ class QueryState<DataType, ErrorType> with Invalidation {
|
|||||||
final DateTime updatedAt;
|
final DateTime updatedAt;
|
||||||
final Duration staleDuration;
|
final Duration staleDuration;
|
||||||
|
|
||||||
|
final bool _loading;
|
||||||
|
|
||||||
const QueryState({
|
const QueryState({
|
||||||
this.data,
|
this.data,
|
||||||
this.error,
|
this.error,
|
||||||
required this.updatedAt,
|
required this.updatedAt,
|
||||||
required this.staleDuration,
|
required this.staleDuration,
|
||||||
});
|
bool loading = false,
|
||||||
|
}) : _loading = loading;
|
||||||
|
|
||||||
QueryState<DataType, ErrorType> copyWith({
|
QueryState<DataType, ErrorType> copyWith({
|
||||||
DataType? data,
|
DataType? data,
|
||||||
ErrorType? error,
|
ErrorType? error,
|
||||||
DateTime? updatedAt,
|
DateTime? updatedAt,
|
||||||
|
bool? loading,
|
||||||
}) {
|
}) {
|
||||||
return QueryState<DataType, ErrorType>(
|
return QueryState<DataType, ErrorType>(
|
||||||
updatedAt: updatedAt ?? this.updatedAt,
|
updatedAt: updatedAt ?? this.updatedAt,
|
||||||
staleDuration: staleDuration,
|
staleDuration: staleDuration,
|
||||||
data: data ?? this.data,
|
data: data ?? this.data,
|
||||||
error: error ?? this.error,
|
error: error ?? this.error,
|
||||||
|
loading: loading ?? this._loading,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,9 +130,11 @@ class Query<DataType, ErrorType>
|
|||||||
StreamSubscription<bool>? _connectivitySubscription;
|
StreamSubscription<bool>? _connectivitySubscription;
|
||||||
|
|
||||||
bool get isInitial => hasData && state.data == _initial;
|
bool get isInitial => hasData && state.data == _initial;
|
||||||
bool get isLoading => isInitial ? _mutex.isLocked : !hasData && !hasError;
|
bool get isLoading =>
|
||||||
|
(isInitial && !hasError && state._loading) ||
|
||||||
|
(!hasData && !hasError && state._loading);
|
||||||
bool get isRefreshing =>
|
bool get isRefreshing =>
|
||||||
((!isInitial && hasData) || hasError) && _mutex.isLocked;
|
((!isInitial && hasData) || hasError) && state._loading;
|
||||||
bool get isInactive => !hasListeners;
|
bool get isInactive => !hasListeners;
|
||||||
bool get hasData => state.data != null;
|
bool get hasData => state.data != null;
|
||||||
bool get hasError => state.error != null;
|
bool get hasError => state.error != null;
|
||||||
@@ -145,7 +152,7 @@ class Query<DataType, ErrorType>
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
return _mutex.protect(() async {
|
return _mutex.protect(() async {
|
||||||
state = state.copyWith();
|
state = state.copyWith(loading: true);
|
||||||
_operation = cancellableRetryOperation(
|
_operation = cancellableRetryOperation(
|
||||||
_queryFn,
|
_queryFn,
|
||||||
config: retryConfig,
|
config: retryConfig,
|
||||||
@@ -154,6 +161,7 @@ class Query<DataType, ErrorType>
|
|||||||
data: data,
|
data: data,
|
||||||
error: null,
|
error: null,
|
||||||
updatedAt: DateTime.now(),
|
updatedAt: DateTime.now(),
|
||||||
|
loading: false,
|
||||||
);
|
);
|
||||||
if (data is DataType) {
|
if (data is DataType) {
|
||||||
_dataController.add(data);
|
_dataController.add(data);
|
||||||
@@ -166,7 +174,11 @@ class Query<DataType, ErrorType>
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
onFailed: (ErrorType? error) {
|
onFailed: (ErrorType? error) {
|
||||||
state = state.copyWith(error: error, updatedAt: DateTime.now());
|
state = state.copyWith(
|
||||||
|
error: error,
|
||||||
|
updatedAt: DateTime.now(),
|
||||||
|
loading: false,
|
||||||
|
);
|
||||||
if (error is ErrorType) _errorController.add(error);
|
if (error is ErrorType) _errorController.add(error);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
@@ -193,12 +205,20 @@ class Query<DataType, ErrorType>
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setData(DataType data) {
|
void setData(DataType data) {
|
||||||
state = state.copyWith(data: data, updatedAt: DateTime.now());
|
state = state.copyWith(
|
||||||
|
data: data,
|
||||||
|
updatedAt: DateTime.now(),
|
||||||
|
loading: false,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> reset() async {
|
Future<void> reset() async {
|
||||||
await _operation?.cancel();
|
await _operation?.cancel();
|
||||||
state = state.copyWith(data: _initial, updatedAt: DateTime.now());
|
state = state.copyWith(
|
||||||
|
data: _initial,
|
||||||
|
updatedAt: DateTime.now(),
|
||||||
|
loading: false,
|
||||||
|
);
|
||||||
_box.delete(key);
|
_box.delete(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user