Fix cancel button not working in download notification

This commit is contained in:
Ming Ming 2023-07-26 01:41:14 +08:00
parent 3817b16847
commit 068fef8fde
5 changed files with 27 additions and 28 deletions

View file

@ -4,6 +4,7 @@ import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import com.nkming.nc_photos.plugin.NcPhotosPlugin
import io.flutter.plugin.common.EventChannel
class DownloadEventCancelChannelHandler(context: Context) : BroadcastReceiver(),
@ -15,14 +16,14 @@ class DownloadEventCancelChannelHandler(context: Context) : BroadcastReceiver(),
}
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.action != K.ACTION_DOWNLOAD_CANCEL || !intent.hasExtra(
K.EXTRA_NOTIFICATION_ID
if (intent?.action != NcPhotosPlugin.ACTION_DOWNLOAD_CANCEL || !intent.hasExtra(
NcPhotosPlugin.EXTRA_NOTIFICATION_ID
)
) {
return
}
val id = intent.getIntExtra(K.EXTRA_NOTIFICATION_ID, 0)
val id = intent.getIntExtra(NcPhotosPlugin.EXTRA_NOTIFICATION_ID, 0)
_eventSink?.success(
mapOf(
"notificationId" to id
@ -32,7 +33,7 @@ class DownloadEventCancelChannelHandler(context: Context) : BroadcastReceiver(),
override fun onListen(arguments: Any?, events: EventChannel.EventSink) {
_context.registerReceiver(
this, IntentFilter(K.ACTION_DOWNLOAD_CANCEL)
this, IntentFilter(NcPhotosPlugin.ACTION_DOWNLOAD_CANCEL)
)
_eventSink = events
}

View file

@ -2,8 +2,5 @@ package com.nkming.nc_photos
interface K {
companion object {
const val ACTION_DOWNLOAD_CANCEL = "com.nkming.nc_photos.ACTION_DOWNLOAD_CANCEL"
const val EXTRA_NOTIFICATION_ID = "com.nkming.nc_photos.EXTRA_NOTIFICATION_ID"
}
}

View file

@ -83,12 +83,11 @@ class _DownlaodHandlerAndroid extends _DownloadHandlerBase {
StreamSubscription<DownloadCancelEvent>? subscription;
try {
bool isCancel = false;
subscription = DownloadEvent.listenDownloadCancel()
..onData((data) {
if (data.notificationId == id) {
isCancel = true;
}
});
subscription = DownloadEvent.downloadCancelStream().listen((data) {
if (data.notificationId == id) {
isCancel = true;
}
});
int count = 0;
for (final f in files) {
@ -110,13 +109,13 @@ class _DownlaodHandlerAndroid extends _DownloadHandlerBase {
parentDir: parentDir,
shouldNotify: false,
);
itemSubscription = DownloadEvent.listenDownloadCancel()
..onData((data) {
if (data.notificationId == id) {
_log.info("[downloadFiles] Cancel requested");
download.cancel();
}
});
itemSubscription =
DownloadEvent.downloadCancelStream().listen((data) {
if (data.notificationId == id) {
_log.info("[downloadFiles] Cancel requested");
download.cancel();
}
});
final result = await download();
successes.add(Tuple2(f, result));
} on PermissionException catch (_) {

View file

@ -3,20 +3,18 @@ import 'dart:async';
import 'package:flutter/services.dart';
class DownloadEvent {
static StreamSubscription<DownloadCancelEvent> listenDownloadCancel() =>
_cancelStream.listen(null);
static Stream<DownloadCancelEvent> downloadCancelStream() =>
_downloadCancelChannel
.receiveBroadcastStream()
.map((data) => DownloadCancelEvent(
data["notificationId"],
));
/// User canceled the download job
static const exceptionCodeUserCanceled = "userCanceled";
static const _downloadCancelChannel = EventChannel(
"com.nkming.nc_photos/download_event/action_download_cancel");
static final _cancelStream = _downloadCancelChannel
.receiveBroadcastStream()
.map((data) => DownloadCancelEvent(
data["notificationId"],
));
}
class DownloadCancelEvent {

View file

@ -21,6 +21,10 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
K.ACTION_SHOW_IMAGE_PROCESSOR_RESULT
const val EXTRA_IMAGE_RESULT_URI = K.EXTRA_IMAGE_RESULT_URI
const val ACTION_DOWNLOAD_CANCEL =
K.ACTION_DOWNLOAD_CANCEL
const val EXTRA_NOTIFICATION_ID = K.EXTRA_NOTIFICATION_ID
private const val TAG = "NcPhotosPlugin"
}