Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LoadTasksWithRawQuery not working #954

Open
arijit121 opened this issue May 18, 2024 · 0 comments
Open

LoadTasksWithRawQuery not working #954

arijit121 opened this issue May 18, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@arijit121
Copy link

LoadTasksWithRawQuery not giving proper value

class DownloadHandler {
  Future<void> download({required String url}) async {
    try {
      if (kIsWeb) {
        await JsProvider().downloadFile(url: url, name: url.split("/").last);
      } else {
        PopUpItems().toastMessage("Downloading ...", Colors.blueAccent);
        _bindBackgroundIsolate();
        await FlutterDownloader.registerCallback(downloadCallback, step: 1);
        final taskId = await FlutterDownloader.enqueue(
            url: url,
            savedDir: await downloadPath(),
            showNotification: true,
            // show download progress in status bar (for Android)
            openFileFromNotification: true,
            // click on notification to open downloaded file (for Android),
            saveInPublicStorage: true);

        AppLog.i(taskId, tag: "TaskId");
      }
    } catch (e, stacktrace) {
      AppLog.e(e.toString(), error: e, stackTrace: stacktrace);
    }
  }

  Future<String> downloadPath() async {
    try {
      String directoryPath;
      if (Platform.isIOS) {
        directoryPath = (await getApplicationDocumentsDirectory()).path ?? "";
      } else {
        directoryPath = "/storage/emulated/0/Download";

        bool dirDownloadExists = await Directory(directoryPath).exists();
        if (dirDownloadExists) {
          directoryPath = "/storage/emulated/0/Download";
        } else {
          directoryPath = "/storage/emulated/0/Downloads";
        }
      }
      bool dirDownloadExists = await Directory(directoryPath).exists();
      if (!dirDownloadExists) {
        await Directory(directoryPath).create();
      }
      return directoryPath;
    } catch (e, stacktrace) {
      AppLog.e(e.toString(), error: e, stackTrace: stacktrace);
    }
    return "";
  }

  @pragma('vm:entry-point')
  static void downloadCallback(
    String id,
    int status,
    int progress,
  ) async {
    AppLog.i('task ($id) is in status ($status) and process ($progress)',
        tag: 'Callback on background isolate');
    IsolateNameServer.lookupPortByName('downloader_send_port')
        ?.send([id, status, progress]);
  }

  void _bindBackgroundIsolate() {
    List<TaskInfo>? _tasks;
    ReceivePort _port = ReceivePort();
    final isSuccess = IsolateNameServer.registerPortWithName(
      _port.sendPort,
      'downloader_send_port',
    );
    if (!isSuccess) {
      _unbindBackgroundIsolate();
      _bindBackgroundIsolate();
      return;
    }
    _port.listen((dynamic data) async {
      final taskId = (data as List<dynamic>)[0] as String;
      final status = DownloadTaskStatus.fromInt(data[1] as int);
      final progress = data[2] as int;
      // var v = await FlutterDownloader.loadTasks();
      AppLog.i('task ($taskId) is in status ($status) and process ($progress)',
          tag: 'Callback on UI isolate:');

      if (progress == 100) {
        String query = "SELECT * FROM task WHERE task_id='$taskId'";
        List<DownloadTask>? tasks =
            await FlutterDownloader.loadTasksWithRawQuery(query: query);

        await Permission.storage.request();
        await Permission.manageExternalStorage.request();
        await Permission.accessMediaLocation.request();
        await OpenFilex.open(
            "${tasks?.first.savedDir ?? ""}/${tasks?.first.filename ?? ""}");
      }
    });
  }

  void _unbindBackgroundIsolate() {
    IsolateNameServer.removePortNameMapping('downloader_send_port');
  }
}

class TaskInfo {
  TaskInfo({this.name, this.link});

  final String? name;
  final String? link;

  String? taskId;
  int? progress = 0;
  DownloadTaskStatus? status = DownloadTaskStatus.undefined;
}

Expected behavior

Give proper value.

Device:- IOS, Flutter version 3.19.6, plugin version 1.11.6

@arijit121 arijit121 added the bug Something isn't working label May 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant