capacitor-plugin-downloader
TypeScript icon, indicating that this package has built-in type declarations

2.1.7 • Public • Published

Capacitor Downloader

🔥 IMPORTANT: This plugin based on Capacitor Downloader.

The author made a awesome plugin but the reason is he doesn't maintain this great plugin. I decide to clone this for personal purposes.

  • [x] Upgrade iOS dependencies
  • [x] Upgrade Android dependencies
  • [x] Integrate Android notification progress when downloading
  • [ ] Android resume, cancel, pause

npm npm Build Status

Installation

  • npm i capacitor-plugin-downloader

Android

Add import co.fitcom.capacitor.Downloader.DownloaderPlugin; and add(DownloaderPlugin.class); in the app's MainActivity.java like this:

import co.fitcom.capacitor.Downloader.DownloaderPlugin;

public class MainActivity extends BridgeActivity {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    this.init(savedInstanceState, new ArrayList<Class<? extends Plugin>>() {{
      // Additional plugins you've installed go here
      add(DownloaderPlugin.class);
    }});
  }
}

API

Method Default Android iOS Type Description
initialize() X X Promise<void> Initialize plugin in native.
createDownload(options: DownloadOptions) X X Promise<IdResponse> Creates a download task it returns the id of the task
start(options:Options, progress?: Function) X X Promise<IdResponse> Starts a download task.
resume(options:Options) X Promise<IdResponse> Resumes a download task.
cancel(options:Options) X Promise<IdResponse> Cancels a download task.
pause(options:Options) X Promise<IdResponse> Pauses a download task.

Usage

import { Downloader, DownloadEventData, ProgressEventData } from 'capacitor-downloader';
const downloader = new Downloader();
const { id } = await downloader.createDownload({
  url:
    'https://wallpaperscraft.com/image/hulk_wolverine_x_men_marvel_comics_art_99032_3840x2400.jpg'
});
downloader.start({ id });
downloader.addListener('progressUpdate', (progressData: ProgressEventData) => {
    console.log(`Progress ID : ${progressData.id}%`);
    console.log(`Current Size : ${progressData.currentSize}%`);
    console.log(`Total Size : ${progressData.totalSize}%`);
    console.log(`Download Speed in bytes : ${progressData.speed}%`);
})

downloader.addListener('downloadedSucceed', (data: DownloadedSucceedEvent) => {
    console.log(`Progress ID : ${progressData.id}%`);
    console.log(`Absolute Path : ${progressData.path}%`);
})

downloader.addListener('downloadedFailed', (data: DownloadedFailedEvent) => {
    console.log(`Progress ID : ${progressData.id}%`);
    console.log(`Error Code : ${progressData.code}%`);
    console.log(`Error Message : ${progressData.message}%`);
})

API Interface

export interface IDownloader {
  addListener<T extends Listeners>(
    event: T,
    callback: ListenerCallbacks[T]
  ): PluginListenerHandle;
  initialize(): Promise<void>;
  createDownload(options: DownloadOptions): Promise<IdResponse>;
  start(id: string): Promise<IdResponse>;
  pause(id: string): Promise<IdResponse>; // only with iOS
  resume(id: string): Promise<IdResponse>; // only with iOS
  cancel(id: string): Promise<IdResponse>; // only with iOS
}

export type Listeners =
  | 'progressUpdate'
  | 'downloadedSucceed'
  | 'downloadedFailed';
export type ListenerCallbacks = {
  progressUpdate: (event: ProgressEvent) => void;
  downloadedSucceed: (event: DownloadedSucceedEvent) => void;
  downloadedFailed: (event: DownloadedFailedEvent) => void;
};

export interface DownloadedFailedEvent {
  id: string;
  code: DownloadErrorCodes;
  message: DownloadErrorMessages;
}

export interface DownloadedSucceedEvent {
  id: string;
  path: string;
}
export interface ProgressEvent {
  id: string;
  currentSize: number;
  totalSize: number;
  speed?: number; // only with iOS
}

export interface IdResponse {
  id: string;
}

export enum StatusCode {
  PENDING = 'pending',
  PAUSED = 'paused',
  DOWNLOADING = 'downloading',
  COMPLETED = 'completed',
  ERROR = 'error',
}
export type DownloadErrorCodes = 1;
export type DownloadErrorMessages = {
  1: 'File not found';
};

export interface DownloadOptions {
  url: string;
  fileName: string;
}

Readme

Keywords

Package Sidebar

Install

npm i capacitor-plugin-downloader

Weekly Downloads

0

Version

2.1.7

License

MIT

Unpacked Size

910 kB

Total Files

198

Last publish

Collaborators

  • thanhhoa214