-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
260 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 24 additions & 35 deletions
59
...esentation/features/presentation/cubit/internet_connection/internet_connection_cubit.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,36 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:connectivity_plus/connectivity_plus.dart'; | ||
import 'package:flutter_bloc/flutter_bloc.dart'; | ||
|
||
part 'internet_connection_state.dart'; | ||
|
||
class InternetConnectionCubit extends Cubit<InternetConnectionState> { | ||
InternetConnectionCubit() : super(CheckConnectionLoading()); | ||
|
||
static InternetConnectionCubit get(context) => BlocProvider.of(context); | ||
|
||
final _connectivity = Connectivity(); | ||
bool? hasConnection; | ||
|
||
Future<void> init() async { | ||
_connectivity.onConnectivityChanged.listen(_connectionChange); | ||
_checkConnection(await _connectivity.checkConnectivity()); | ||
} | ||
|
||
void _connectionChange(ConnectivityResult result) { | ||
_checkConnection(result); | ||
} | ||
|
||
Future<bool?> _checkConnection(ConnectivityResult result) async { | ||
bool? previousConnection; | ||
|
||
if (hasConnection != null) previousConnection = hasConnection; | ||
|
||
if (result == ConnectivityResult.none) { | ||
hasConnection = false; | ||
if (previousConnection != hasConnection) { | ||
_connectionChangeController(hasConnection!); | ||
final Connectivity connectivity; | ||
late StreamSubscription connectivityStreamSubscription; | ||
|
||
InternetConnectionCubit({required this.connectivity}) | ||
: super(InternetLoading()) { | ||
connectivityStreamSubscription = | ||
connectivity.onConnectivityChanged.listen((connectivityResult) { | ||
if (connectivityResult == ConnectivityResult.wifi) { | ||
emitInternetConnected(ConnectionType.wifi); | ||
} else if (connectivityResult == ConnectivityResult.mobile) { | ||
emitInternetConnected(ConnectionType.mobile); | ||
} else if (connectivityResult == ConnectivityResult.none) { | ||
emitInternetDisconnected(); | ||
} | ||
return hasConnection; | ||
} | ||
return hasConnection; | ||
}); | ||
} | ||
|
||
bool showDialog = false; | ||
void emitInternetConnected(ConnectionType connectionType) => | ||
emit(InternetConnected(connectionType: connectionType)); | ||
|
||
void emitInternetDisconnected() => emit(InternetDisconnected()); | ||
|
||
void _connectionChangeController(bool hasConnection) { | ||
if (hasConnection) { | ||
emit(InternetConnected()); | ||
} else { | ||
emit(InternetDisconnected()); | ||
} | ||
@override | ||
Future<void> close() { | ||
connectivityStreamSubscription.cancel(); | ||
return super.close(); | ||
} | ||
} |
15 changes: 11 additions & 4 deletions
15
...esentation/features/presentation/cubit/internet_connection/internet_connection_state.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,18 @@ | ||
part of 'internet_connection_cubit.dart'; | ||
|
||
enum ConnectionType { | ||
wifi, | ||
mobile, | ||
} | ||
|
||
abstract class InternetConnectionState {} | ||
|
||
class CheckConnectionLoading extends InternetConnectionState {} | ||
class InternetLoading extends InternetConnectionState {} | ||
|
||
class InternetConnected extends InternetConnectionState {} | ||
class InternetConnected extends InternetConnectionState { | ||
final ConnectionType connectionType; | ||
|
||
class InternetDisconnected extends InternetConnectionState {} | ||
InternetConnected({required this.connectionType}); | ||
} | ||
|
||
class UnknownInternetError extends InternetConnectionState {} | ||
class InternetDisconnected extends InternetConnectionState {} |
Oops, something went wrong.