[Android] Add cancel request method (#7)
* feat: add cancel connect for android * fix: cancel connect only for android * fix: clean up the project * chore: new fvm version * chore: code style and comments * chore: bump version to 0.0.8
This commit is contained in:
@@ -1,3 +1,7 @@
|
||||
## 0.0.8
|
||||
|
||||
- Add `cancelLastConnectionProcess` for Android manager
|
||||
|
||||
## 0.0.7
|
||||
|
||||
- Log all errors on the Android platform
|
||||
|
||||
@@ -203,6 +203,15 @@ class NearbyServiceManager(private var context: Context) {
|
||||
|
||||
}
|
||||
|
||||
fun cancelConnect(result: Result? = null) {
|
||||
val actionListener = getActionListener(
|
||||
result,
|
||||
"Last connection request was cancelled",
|
||||
"Failed to cancel the last connection process"
|
||||
)
|
||||
wifiManager.cancelConnect(wifiChannel, actionListener)
|
||||
}
|
||||
|
||||
|
||||
private fun addWifiActions() {
|
||||
intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION)
|
||||
|
||||
@@ -136,6 +136,14 @@ class NearbyServicePlugin : FlutterPlugin, MethodCallHandler, ActivityAware {
|
||||
}
|
||||
}
|
||||
|
||||
"cancelConnect" -> {
|
||||
try {
|
||||
manager.cancelConnect(result)
|
||||
} catch (e: Exception) {
|
||||
onError(result, e)
|
||||
}
|
||||
}
|
||||
|
||||
else -> {
|
||||
result.notImplemented()
|
||||
}
|
||||
|
||||
@@ -135,6 +135,10 @@ class AppService extends ChangeNotifier {
|
||||
|
||||
Future<void> stopDiscovery() async {
|
||||
try {
|
||||
final hasRunning = await hasRunningJobs();
|
||||
if (hasRunning) {
|
||||
await cancelConnect();
|
||||
}
|
||||
final result = await _nearbyService.stopDiscovery();
|
||||
if (result) {
|
||||
updateState(AppState.readyToDiscover);
|
||||
@@ -170,6 +174,17 @@ class AppService extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> cancelConnect() async {
|
||||
try {
|
||||
await _nearbyService.android?.cancelLastConnectionProcess();
|
||||
} on NearbyServiceBusyException catch (_) {
|
||||
_logBusyException();
|
||||
} catch (e, s) {
|
||||
_log(e, s);
|
||||
}
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
Future<void> stopListeningAll() async {
|
||||
await endCommunicationChannel();
|
||||
await stopListeningConnectedDevice();
|
||||
|
||||
@@ -57,7 +57,7 @@ class DevicePreview extends StatelessWidget {
|
||||
),
|
||||
);
|
||||
|
||||
final disconnectButton = device.status.isConnected
|
||||
final trailingButton = device.status.isConnected
|
||||
? TextButton(
|
||||
onPressed: () => context.read<AppService>().disconnect(device),
|
||||
style: TextButton.styleFrom(
|
||||
@@ -75,10 +75,10 @@ class DevicePreview extends StatelessWidget {
|
||||
name,
|
||||
const SizedBox(height: 5),
|
||||
id,
|
||||
if (disconnectButton != null)
|
||||
if (trailingButton != null)
|
||||
Padding(
|
||||
padding: const EdgeInsets.only(top: 10),
|
||||
child: disconnectButton,
|
||||
child: trailingButton,
|
||||
),
|
||||
],
|
||||
);
|
||||
@@ -86,7 +86,9 @@ class DevicePreview extends StatelessWidget {
|
||||
return InkWell(
|
||||
borderRadius: BorderRadius.circular(12),
|
||||
onTap: () {
|
||||
if (!device.status.isConnected) {
|
||||
if (device.status.isConnecting) {
|
||||
context.read<AppService>().cancelConnect();
|
||||
} else if (!device.status.isConnected) {
|
||||
context.read<AppService>().connect(device);
|
||||
} else {
|
||||
context.read<AppService>().startListeningConnectedDevice(device);
|
||||
@@ -115,7 +117,11 @@ class DevicePreview extends StatelessWidget {
|
||||
),
|
||||
Flexible(
|
||||
child: Text(
|
||||
device.status.isConnected ? 'Tap to chat' : 'Tap to connect',
|
||||
device.status.isConnected
|
||||
? 'Tap to chat'
|
||||
: device.status.isConnecting
|
||||
? 'Tap to cancel connection'
|
||||
: 'Tap to connect',
|
||||
style: const TextStyle(color: kGreenColor, fontSize: 12),
|
||||
textAlign: TextAlign.center,
|
||||
),
|
||||
|
||||
@@ -173,7 +173,6 @@ extension NearbyManager: MCNearbyServiceAdvertiserDelegate {
|
||||
self.invitationHandlers[peerID.displayName] = invitationHandler
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
extension NearbyManager: MCNearbyServiceBrowserDelegate {
|
||||
|
||||
@@ -69,6 +69,16 @@ class NearbyAndroidService extends NearbyService {
|
||||
return NearbyServiceAndroidPlatform.instance.disconnect();
|
||||
}
|
||||
|
||||
///
|
||||
/// On [NearbyServiceBusyException] you can check for running jobs
|
||||
/// (if any of devices has [NearbyDeviceStatus.connecting]).
|
||||
///
|
||||
/// If so, call [cancelLastConnectionProcess] to cancel last request
|
||||
///
|
||||
Future<bool> cancelLastConnectionProcess() {
|
||||
return NearbyServiceAndroidPlatform.instance.cancelConnect();
|
||||
}
|
||||
|
||||
///
|
||||
/// Starts a socket service to transfer data. Uses device with
|
||||
/// [NearbyCommunicationChannelData.connectedDeviceId].
|
||||
|
||||
@@ -55,6 +55,10 @@ abstract class NearbyServiceAndroidPlatform extends PlatformInterface {
|
||||
throw UnimplementedError('disconnect() has not been implemented.');
|
||||
}
|
||||
|
||||
Future<bool> cancelConnect() {
|
||||
throw UnimplementedError('cancelConnect() has not been implemented.');
|
||||
}
|
||||
|
||||
Stream<NearbyConnectionAndroidInfo?> getConnectionInfoStream() {
|
||||
throw UnimplementedError(
|
||||
'getConnectionInfoStream() has not been implemented.',
|
||||
|
||||
@@ -66,6 +66,12 @@ class MethodChannelAndroidNearbyService extends NearbyServiceAndroidPlatform {
|
||||
return _handleBooleanResult(result);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> cancelConnect() async {
|
||||
final result = await methodChannel.invokeMethod("cancelConnect");
|
||||
return _handleBooleanResult(result);
|
||||
}
|
||||
|
||||
@override
|
||||
Stream<NearbyConnectionAndroidInfo?> getConnectionInfoStream() {
|
||||
const connectedDeviceChannel = EventChannel(
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
name: nearby_service
|
||||
description: Nearby Service Flutter Plugin is used to create connections in a P2P network. Supports sending text messages and files.
|
||||
version: 0.0.7
|
||||
version: 0.0.8
|
||||
homepage: https://github.com/ksenia312/nearby_service
|
||||
repository: https://github.com/ksenia312/nearby_service
|
||||
|
||||
|
||||
Reference in New Issue
Block a user