implemented new loadable state concept

This commit is contained in:
2024-05-05 22:58:40 +02:00
parent f58a2ec8cd
commit 6ad8203b6a
31 changed files with 1085 additions and 214 deletions

View File

@ -0,0 +1,26 @@
import 'dart:async';
import 'package:bloc/bloc.dart';
import 'package:connectivity_plus/connectivity_plus.dart';
import 'loadable_state_state.dart';
class LoadableStateBloc extends Bloc<void, LoadableStateState> {
late StreamSubscription<List<ConnectivityResult>> _updateStream;
LoadableStateBloc() : super(const LoadableStateState(connections: null)) {
emitState(List<ConnectivityResult> v) => emit(state.copyWith(connections: v));
Connectivity().checkConnectivity().then(emitState);
_updateStream = Connectivity().onConnectivityChanged.listen(emitState);
}
bool connectivityStatusKnown() => state.connections != null;
bool isConnected({bool? def}) => !(state.connections?.contains(ConnectivityResult.none) ?? def!);
@override
Future<void> close() {
_updateStream.cancel();
return super.close();
}
}

View File

@ -0,0 +1,11 @@
import 'package:connectivity_plus/connectivity_plus.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
part 'loadable_state_state.freezed.dart';
@freezed
class LoadableStateState with _$LoadableStateState {
const factory LoadableStateState({
required List<ConnectivityResult>? connections,
}) = _LoadableStateState;
}

View File

@ -0,0 +1,147 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark
part of 'loadable_state_state.dart';
// **************************************************************************
// FreezedGenerator
// **************************************************************************
T _$identity<T>(T value) => value;
final _privateConstructorUsedError = UnsupportedError(
'It seems like you constructed your class using `MyClass._()`. This constructor is only meant to be used by freezed and you are not supposed to need it nor use it.\nPlease check the documentation here for more information: https://github.com/rrousselGit/freezed#adding-getters-and-methods-to-our-models');
/// @nodoc
mixin _$LoadableStateState {
List<ConnectivityResult>? get connections =>
throw _privateConstructorUsedError;
@JsonKey(ignore: true)
$LoadableStateStateCopyWith<LoadableStateState> get copyWith =>
throw _privateConstructorUsedError;
}
/// @nodoc
abstract class $LoadableStateStateCopyWith<$Res> {
factory $LoadableStateStateCopyWith(
LoadableStateState value, $Res Function(LoadableStateState) then) =
_$LoadableStateStateCopyWithImpl<$Res, LoadableStateState>;
@useResult
$Res call({List<ConnectivityResult>? connections});
}
/// @nodoc
class _$LoadableStateStateCopyWithImpl<$Res, $Val extends LoadableStateState>
implements $LoadableStateStateCopyWith<$Res> {
_$LoadableStateStateCopyWithImpl(this._value, this._then);
// ignore: unused_field
final $Val _value;
// ignore: unused_field
final $Res Function($Val) _then;
@pragma('vm:prefer-inline')
@override
$Res call({
Object? connections = freezed,
}) {
return _then(_value.copyWith(
connections: freezed == connections
? _value.connections
: connections // ignore: cast_nullable_to_non_nullable
as List<ConnectivityResult>?,
) as $Val);
}
}
/// @nodoc
abstract class _$$LoadableStateStateImplCopyWith<$Res>
implements $LoadableStateStateCopyWith<$Res> {
factory _$$LoadableStateStateImplCopyWith(_$LoadableStateStateImpl value,
$Res Function(_$LoadableStateStateImpl) then) =
__$$LoadableStateStateImplCopyWithImpl<$Res>;
@override
@useResult
$Res call({List<ConnectivityResult>? connections});
}
/// @nodoc
class __$$LoadableStateStateImplCopyWithImpl<$Res>
extends _$LoadableStateStateCopyWithImpl<$Res, _$LoadableStateStateImpl>
implements _$$LoadableStateStateImplCopyWith<$Res> {
__$$LoadableStateStateImplCopyWithImpl(_$LoadableStateStateImpl _value,
$Res Function(_$LoadableStateStateImpl) _then)
: super(_value, _then);
@pragma('vm:prefer-inline')
@override
$Res call({
Object? connections = freezed,
}) {
return _then(_$LoadableStateStateImpl(
connections: freezed == connections
? _value._connections
: connections // ignore: cast_nullable_to_non_nullable
as List<ConnectivityResult>?,
));
}
}
/// @nodoc
class _$LoadableStateStateImpl implements _LoadableStateState {
const _$LoadableStateStateImpl(
{required final List<ConnectivityResult>? connections})
: _connections = connections;
final List<ConnectivityResult>? _connections;
@override
List<ConnectivityResult>? get connections {
final value = _connections;
if (value == null) return null;
if (_connections is EqualUnmodifiableListView) return _connections;
// ignore: implicit_dynamic_type
return EqualUnmodifiableListView(value);
}
@override
String toString() {
return 'LoadableStateState(connections: $connections)';
}
@override
bool operator ==(Object other) {
return identical(this, other) ||
(other.runtimeType == runtimeType &&
other is _$LoadableStateStateImpl &&
const DeepCollectionEquality()
.equals(other._connections, _connections));
}
@override
int get hashCode => Object.hash(
runtimeType, const DeepCollectionEquality().hash(_connections));
@JsonKey(ignore: true)
@override
@pragma('vm:prefer-inline')
_$$LoadableStateStateImplCopyWith<_$LoadableStateStateImpl> get copyWith =>
__$$LoadableStateStateImplCopyWithImpl<_$LoadableStateStateImpl>(
this, _$identity);
}
abstract class _LoadableStateState implements LoadableStateState {
const factory _LoadableStateState(
{required final List<ConnectivityResult>? connections}) =
_$LoadableStateStateImpl;
@override
List<ConnectivityResult>? get connections;
@override
@JsonKey(ignore: true)
_$$LoadableStateStateImplCopyWith<_$LoadableStateStateImpl> get copyWith =>
throw _privateConstructorUsedError;
}