ImageDownloader

Welcoming ImageDownloader to our code base.

Q: What is it?
A: A class to download/retrieve image from an URL. Supported by TvlkDownloader created by @junius

Q: Why do you make it?
A: For now, it's job is to download image voucher for new products (experience and connectivity) cc @reza

Q: What’s the difference with TvlkDownloader?
A: It generates the Intent callback required by TvlkDownloader. The intent will show the image in ImageViewerActivity once user taps on the notification (after download finished).
It also provides a default message for download title & message to be shown in notification bar. You can choose not to use the default one, of course.

Q: How to use it?
A:

new ImageDownloader(context)
    .setDefaultMessages(”voucher”)
    .onExistingImage(imageFile -> {
        // do what you want to do with the image
        // here's to show it with ImageViewerActivity
        Henson.with(context).goToImageViewerActivity().url(Uri.fromFile(imageFile).toString()).build();
    }
    .getImage(folderPath, fileName, url);

url and fileNamewill acts as a primary key. Before downloading, it will first check for any downliad with given url and a file within folderPath with the name of fileName. Thus, it is recommended if you named the file with identifier (e.g bookingId, for example voucher-831231.png)

But remember, in Android 6.0 you need to handle runtime permissions. So meet PermissionUtil (https://github.com/kayvannj/PermissionUtil).

mPermissionObject = PermissionUtil
    .with(context)
    .request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
    .onAllGranted(() -> downloadImage())
    .onAnyDenied(() -> // show to user we can’t download if you don’t allow us to store the image
    .ask(requestCode);

And in Activity’s onRequestPermissionResult() add this:

if (mPermissionObject != null) {
    mPermissionObject.onRequestPermissionResult()
}

Q: Why do not you just include all the permission checking in the ImageDownloader class?
A: I really want to do that, but can not at the moment since permissionObject need to receive callback inside the activity’s onRequestPermissionResult()