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

View file

@ -2,8 +2,5 @@ package com.nkming.nc_photos
interface K { interface K {
companion object { 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; StreamSubscription<DownloadCancelEvent>? subscription;
try { try {
bool isCancel = false; bool isCancel = false;
subscription = DownloadEvent.listenDownloadCancel() subscription = DownloadEvent.downloadCancelStream().listen((data) {
..onData((data) { if (data.notificationId == id) {
if (data.notificationId == id) { isCancel = true;
isCancel = true; }
} });
});
int count = 0; int count = 0;
for (final f in files) { for (final f in files) {
@ -110,13 +109,13 @@ class _DownlaodHandlerAndroid extends _DownloadHandlerBase {
parentDir: parentDir, parentDir: parentDir,
shouldNotify: false, shouldNotify: false,
); );
itemSubscription = DownloadEvent.listenDownloadCancel() itemSubscription =
..onData((data) { DownloadEvent.downloadCancelStream().listen((data) {
if (data.notificationId == id) { if (data.notificationId == id) {
_log.info("[downloadFiles] Cancel requested"); _log.info("[downloadFiles] Cancel requested");
download.cancel(); download.cancel();
} }
}); });
final result = await download(); final result = await download();
successes.add(Tuple2(f, result)); successes.add(Tuple2(f, result));
} on PermissionException catch (_) { } on PermissionException catch (_) {

View file

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

View file

@ -21,6 +21,10 @@ class NcPhotosPlugin : FlutterPlugin, ActivityAware,
K.ACTION_SHOW_IMAGE_PROCESSOR_RESULT K.ACTION_SHOW_IMAGE_PROCESSOR_RESULT
const val EXTRA_IMAGE_RESULT_URI = K.EXTRA_IMAGE_RESULT_URI 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" private const val TAG = "NcPhotosPlugin"
} }