Skip to content

Commit

Permalink
feat(connectivity)!: support multiple ConnectivityResult values at th…
Browse files Browse the repository at this point in the history
…e same time

BREAKING CHANGES: checkConnectivity and onConnectivityChanged returns list of connectivityResult. Users will need to update their apps to handle the new list of ConnectivityResult types returned by checkConnectivity and onConnectivityChanged methods.

Signed-off-by: George Kutsurua <[email protected]>
  • Loading branch information
suquant committed Feb 22, 2024
1 parent 689e6b8 commit 20b2450
Show file tree
Hide file tree
Showing 16 changed files with 75 additions and 69 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#Tue Feb 20 19:54:18 GST 2024
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.6.1-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.2-bin.zip
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public class Connectivity {
static final String CONNECTIVITY_ETHERNET = "ethernet";
static final String CONNECTIVITY_BLUETOOTH = "bluetooth";
static final String CONNECTIVITY_VPN = "vpn";
static final String CONNECTIVITY_OTHER = "other";
private final ConnectivityManager connectivityManager;

public Connectivity(ConnectivityManager connectivityManager) {
Expand All @@ -30,11 +31,13 @@ List<String> getNetworkTypes() {
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
Network network = connectivityManager.getActiveNetwork();
NetworkCapabilities capabilities = connectivityManager.getNetworkCapabilities(network);
if (capabilities == null) {
if (capabilities == null
|| !capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
types.add(CONNECTIVITY_NONE);
return types;
}
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)) {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI)
|| capabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI_AWARE)) {
types.add(CONNECTIVITY_WIFI);
}
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)) {
Expand All @@ -49,9 +52,12 @@ List<String> getNetworkTypes() {
if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_BLUETOOTH)) {
types.add(CONNECTIVITY_BLUETOOTH);
}
if (types.isEmpty()
&& capabilities.hasCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)) {
types.add(CONNECTIVITY_OTHER);
}
if (types.isEmpty()) {
types.add(CONNECTIVITY_NONE);
return types;
}
} else {
// For legacy versions, return a single type as before or adapt similarly if multiple types need to be supported
Expand Down Expand Up @@ -91,7 +97,7 @@ private List<String> getNetworkTypesLegacy() {
types.add(CONNECTIVITY_MOBILE);
break;
default:
types.add(CONNECTIVITY_NONE);
types.add(CONNECTIVITY_OTHER);
}
return types;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,12 @@ public void onCancel(Object arguments) {
@Override
public void onReceive(Context context, Intent intent) {
if (events != null) {
events.success(String.join(",", connectivity.getNetworkTypes()));
events.success(connectivity.getNetworkTypes());
}
}

private void sendEvent() {
Runnable runnable = () -> events.success(String.join(",", connectivity.getNetworkTypes()));
Runnable runnable = () -> events.success(connectivity.getNetworkTypes());
mainHandler.post(runnable);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ConnectivityMethodChannelHandler implements MethodChannel.MethodCallHandle
@Override
public void onMethodCall(MethodCall call, @NonNull MethodChannel.Result result) {
if ("check".equals(call.method)) {
result.success(String.join(",", connectivity.getNetworkTypes()));
result.success(connectivity.getNetworkTypes());
} else {
result.notImplemented();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@
97C146E61CF9000F007C117D /* Project object */ = {
isa = PBXProject;
attributes = {
LastUpgradeCheck = 1430;
LastUpgradeCheck = 1510;
ORGANIZATIONNAME = "";
TargetAttributes = {
331C8080294A63A400263BE5 = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1430"
LastUpgradeVersion = "1510"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class PathMonitorConnectivityProvider: NSObject, ConnectivityProvider {

private let queue = DispatchQueue.global(qos: .background)

private var _pathMonitor: NWPathMonitor?
private var pathMonitor: NWPathMonitor?

public var currentConnectivityTypes: [ConnectivityType] {
let path = ensurePathMonitor().currentPath
Expand Down Expand Up @@ -43,19 +43,19 @@ public class PathMonitorConnectivityProvider: NSObject, ConnectivityProvider {
}

public func stop() {
_pathMonitor?.cancel()
_pathMonitor = nil
pathMonitor?.cancel()
pathMonitor = nil
}

@discardableResult
private func ensurePathMonitor() -> NWPathMonitor {
if (_pathMonitor == nil) {
if (pathMonitor == nil) {
let pathMonitor = NWPathMonitor()
pathMonitor.start(queue: queue)
pathMonitor.pathUpdateHandler = pathUpdateHandler
_pathMonitor = pathMonitor
self.pathMonitor = pathMonitor
}
return _pathMonitor!
return self.pathMonitor!
}

private func pathUpdateHandler(path: NWPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import Foundation
import Reachability

public class ReachabilityConnectivityProvider: NSObject, ConnectivityProvider {
private var _reachability: Reachability?
private var reachability: Reachability?

public var currentConnectivityTypes: [ConnectivityType] {
guard let reachability = _reachability else {
guard let reachability = reachability else {
return [.none]
}

// Supported types https://github.com/ashleymills/Reachability.swift/blob/master/Sources/Reachability.swift#L99
switch reachability.connection {
case .wifi:
return [.wifi]
Expand Down Expand Up @@ -42,23 +43,23 @@ public class ReachabilityConnectivityProvider: NSObject, ConnectivityProvider {
NotificationCenter.default.removeObserver(
self,
name: .reachabilityChanged,
object: _reachability)
object: reachability)

_reachability?.stopNotifier()
_reachability = nil
reachability?.stopNotifier()
reachability = nil
}

private func ensureReachability() -> Reachability {
if (_reachability == nil) {
if (reachability == nil) {
let reachability = try? Reachability()
_reachability = reachability
self.reachability = reachability
}
return _reachability!
return reachability!
}

@objc private func reachabilityChanged(notification: NSNotification) {
if let reachability = notification.object as? Reachability {
_reachability = reachability
self.reachability = reachability
connectivityUpdateHandler?(currentConnectivityTypes)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,10 @@ public class SwiftConnectivityPlusPlugin: NSObject, FlutterPlugin, FlutterStream
}
}

private func statusFrom(connectivityTypes: [ConnectivityType]) -> String {
private func statusFrom(connectivityTypes: [ConnectivityType]) -> [String] {
return connectivityTypes.map {
self.statusFrom(connectivityType: $0)
}.joined(separator: ",")
}
}

public func onListen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,10 @@ public class ConnectivityPlugin: NSObject, FlutterPlugin, FlutterStreamHandler {
}
}

private func statusFrom(connectivityTypes: [ConnectivityType]) -> String {
private func statusFrom(connectivityTypes: [ConnectivityType]) -> [String] {
return connectivityTypes.map {
self.statusFrom(connectivityType: $0)
}.joined(separator: ",")
}
}

public func onListen(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public class PathMonitorConnectivityProvider: NSObject, ConnectivityProvider {

private let queue = DispatchQueue.global(qos: .background)

private var _pathMonitor: NWPathMonitor?
private var pathMonitor: NWPathMonitor?

public var currentConnectivityTypes: [ConnectivityType] {
let path = ensurePathMonitor().currentPath
Expand Down Expand Up @@ -43,18 +43,18 @@ public class PathMonitorConnectivityProvider: NSObject, ConnectivityProvider {
}

public func stop() {
_pathMonitor?.cancel()
_pathMonitor = nil
pathMonitor?.cancel()
pathMonitor = nil
}

private func ensurePathMonitor() -> NWPathMonitor {
if (_pathMonitor == nil) {
if (pathMonitor == nil) {
let pathMonitor = NWPathMonitor()
pathMonitor.start(queue: queue)
pathMonitor.pathUpdateHandler = pathUpdateHandler
_pathMonitor = pathMonitor
self.pathMonitor = pathMonitor
}
return _pathMonitor!
return self.pathMonitor!
}

private func pathUpdateHandler(path: NWPath) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import Foundation
import Reachability

public class ReachabilityConnectivityProvider: NSObject, ConnectivityProvider {
private var _reachability: Reachability?
private var reachability: Reachability?

public var currentConnectivityTypes: [ConnectivityType] {
guard let reachability = _reachability else {
guard let reachability = reachability else {
return [.none]
}

Expand Down Expand Up @@ -42,23 +42,23 @@ public class ReachabilityConnectivityProvider: NSObject, ConnectivityProvider {
NotificationCenter.default.removeObserver(
self,
name: .reachabilityChanged,
object: _reachability)
object: reachability)

_reachability?.stopNotifier()
_reachability = nil
reachability?.stopNotifier()
reachability = nil
}

private func ensureReachability() -> Reachability {
if (_reachability == nil) {
if (reachability == nil) {
let reachability = try? Reachability()
_reachability = reachability
self.reachability = reachability
}
return _reachability!
return self.reachability!
}

@objc private func reachabilityChanged(notification: NSNotification) {
if let reachability = notification.object as? Reachability {
_reachability = reachability
self.reachability = reachability
connectivityUpdateHandler?(currentConnectivityTypes)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,29 +114,29 @@ static std::string ConnectivityToString(ConnectivityType connectivityType) {
}
}

static std::string
ConnectivityToString(std::set<ConnectivityType> connectivityTypes) {
static flutter::EncodableList
EncodeConnectivityTypes(std::set<ConnectivityType> connectivityTypes) {
flutter::EncodableList encodedList;

if (connectivityTypes.empty()) {
return "none";
encodedList.push_back(
flutter::EncodableValue(ConnectivityToString(ConnectivityType::None)));
return encodedList;
}

std::string connectivity;
for (auto type : connectivityTypes) {
if (!connectivity.empty()) {
connectivity += ',';
}
connectivity += ConnectivityToString(type);
for (const auto &type : connectivityTypes) {
std::string connectivityString = ConnectivityToString(type);
encodedList.push_back(flutter::EncodableValue(connectivityString));
}
return connectivity;

return encodedList;
}

void ConnectivityPlusWindowsPlugin::HandleMethodCall(
const flutter::MethodCall<flutter::EncodableValue> &method_call,
std::unique_ptr<flutter::MethodResult<flutter::EncodableValue>> result) {
if (method_call.method_name().compare("check") == 0) {
std::string connectivity =
ConnectivityToString(manager->GetConnectivityTypes());
result->Success(flutter::EncodableValue(connectivity));
result->Success(EncodeConnectivityTypes(manager->GetConnectivityTypes()));
} else {
result->NotImplemented();
}
Expand All @@ -149,9 +149,7 @@ ConnectivityStreamHandler::ConnectivityStreamHandler(
ConnectivityStreamHandler::~ConnectivityStreamHandler() {}

void ConnectivityStreamHandler::AddConnectivityEvent() {
std::string connectivity =
ConnectivityToString(manager->GetConnectivityTypes());
sink->Success(flutter::EncodableValue(connectivity));
sink->Success(EncodeConnectivityTypes(manager->GetConnectivityTypes()));
}

std::unique_ptr<FlStreamHandlerError>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,15 @@ class MethodChannelConnectivity extends ConnectivityPlatform {
Stream<List<ConnectivityResult>> get onConnectivityChanged {
_onConnectivityChanged ??= eventChannel
.receiveBroadcastStream()
.map((dynamic result) => result.toString())
.map((dynamic result) => List<String>.from(result))
.map(parseConnectivityResults);
return _onConnectivityChanged!;
}

@override
Future<List<ConnectivityResult>> checkConnectivity() {
return methodChannel
.invokeMethod<String>('check')
.then((value) => parseConnectivityResults(value ?? ''));
.invokeListMethod<String>('check')
.then((value) => parseConnectivityResults(value ?? []));
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:connectivity_plus_platform_interface/connectivity_plus_platform_interface.dart';

/// Convert a comma-separated String to a list of ConnectivityResult values.
List<ConnectivityResult> parseConnectivityResults(String states) {
return states.split(',').map((state) {
List<ConnectivityResult> parseConnectivityResults(List<String> states) {
return states.map((state) {
switch (state.trim()) {
case 'bluetooth':
return ConnectivityResult.bluetooth;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void main() {
log.add(methodCall);
switch (methodCall.method) {
case 'check':
// Simulate returning a comma-separated string of connectivity statuses
return 'wifi,mobile';
// Simulate returning a list of string of connectivity statuses
return ['wifi', 'mobile'];
default:
return null;
}
Expand All @@ -45,7 +45,7 @@ void main() {
.handlePlatformMessage(
methodChannelConnectivity.eventChannel.name,
methodChannelConnectivity.eventChannel.codec
.encodeSuccessEnvelope('wifi,mobile'),
.encodeSuccessEnvelope(['wifi', 'mobile']),
(_) {},
);
break;
Expand Down

0 comments on commit 20b2450

Please sign in to comment.