[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:
@@ -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,
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user