Nikolas Rimikis
1 year ago
19 changed files with 114 additions and 74 deletions
@ -1,28 +1,51 @@ |
|||||||
part of '../../neon.dart'; |
part of '../../neon.dart'; |
||||||
|
|
||||||
class ResultBuilder<R> extends StatelessWidget { |
typedef ResultWidgetBuilder<T> = Widget Function(BuildContext context, Result<T> snapshot); |
||||||
|
|
||||||
|
class ResultBuilder<T> extends StreamBuilderBase<Result<T>, Result<T>> { |
||||||
const ResultBuilder({ |
const ResultBuilder({ |
||||||
required this.stream, |
|
||||||
required this.builder, |
required this.builder, |
||||||
|
this.initialData, |
||||||
|
super.stream, |
||||||
super.key, |
super.key, |
||||||
}); |
}); |
||||||
|
|
||||||
final Stream<Result<R>?>? stream; |
ResultBuilder.behaviorSubject({ |
||||||
|
required this.builder, |
||||||
|
BehaviorSubject<Result<T>>? super.stream, |
||||||
|
super.key, |
||||||
|
}) : initialData = stream?.valueOrNull; |
||||||
|
|
||||||
|
final ResultWidgetBuilder<T> builder; |
||||||
|
final Result<T>? initialData; |
||||||
|
|
||||||
final Widget Function(BuildContext, Result<R>) builder; |
@override |
||||||
|
Result<T> initial() => initialData?.asLoading() ?? Result<T>.loading(); |
||||||
|
|
||||||
@override |
@override |
||||||
Widget build(final BuildContext context) => StreamBuilder( |
Result<T> afterData(final Result<T> current, final Result<T> data) { |
||||||
stream: stream, |
// prevent rebuild when only the cache state cahnges |
||||||
builder: (final context, final snapshot) { |
if (current == data) { |
||||||
if (snapshot.hasError) { |
return current; |
||||||
return builder(context, Result.error(snapshot.error!)); |
} |
||||||
|
|
||||||
|
return data; |
||||||
} |
} |
||||||
if (snapshot.hasData) { |
|
||||||
return builder(context, snapshot.data!); |
@override |
||||||
|
Result<T> afterError(final Result<T> current, final Object error, final StackTrace stackTrace) { |
||||||
|
if (current.hasError) { |
||||||
|
return current; |
||||||
} |
} |
||||||
|
|
||||||
return builder(context, Result.loading()); |
return Result( |
||||||
}, |
current.data, |
||||||
|
error, |
||||||
|
isLoading: false, |
||||||
|
isCached: false, |
||||||
); |
); |
||||||
} |
} |
||||||
|
|
||||||
|
@override |
||||||
|
Widget build(final BuildContext context, final Result<T> currentSummary) => builder(context, currentSummary); |
||||||
|
} |
||||||
|
Loading…
Reference in new issue