From 3a9a02b562cbde4a1df5392ab6aa0dac823e0a15 Mon Sep 17 00:00:00 2001 From: xwaxes01 Date: Thu, 7 Oct 2021 11:08:01 +0300 Subject: [PATCH] feat: Bug fixes and improvements --- README.md | 40 +++++++++++++++++-- .../murerwadownloader/FirstFragment.kt | 22 ++++++++-- filedownloader/build.gradle | 2 +- .../filedownloader/DownloadInterface.kt | 23 ++++++++++- .../murerwa/filedownloader/FileDownloader.kt | 31 +++++++++----- 5 files changed, 99 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 69f63b1..e618109 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,13 @@ In your app-level `build.gradle` file, add the following dependency } ## Usage -To use Murerwa Downloader, create an instance of the `FileDownloader` class in the Fragment you want to perform downloads. +To use Murerwa Downloader in a Fragment or Activity, implement the `DownloadInterface` in your Fragment/Activity + + class MyFragment : Fragment, DownloadInterface { + ... + } + +To initiate a download, create an instance of the `FileDownloader` class in the Fragment/Activity you want to perform downloads. val fileDownloader = FileDownloader( downloadLink = url, @@ -28,7 +34,7 @@ To use Murerwa Downloader, create an instance of the `FileDownloader` class in t downloadInterface = this ) -To start the download, simply call the download function of the class. +Start the download, by simply calling the download function of the class. fileDownloader.downloadFile() @@ -38,12 +44,40 @@ To observe the download progress of the library, implement to `DownloadInterface ... } -Then implement the `onDownloadProgressChanged` function and perform any UI/UX tasks + +## User Feedback +The `DownloadInterface` of the Murerwa Downloader library has four functions you can override as shown below. + +### 1. OnDownloadProgressChanged +To keep track of download progress changes, implement the `onDownloadProgressChanged` function and perform any UI/UX tasks override fun onDownloadProgressChanged(newProgress: Int) { // Maybe show the user the download progress } +### 2. OnErrorOccurred +To display error messages if the download fails, override the `onErrorOccurred` function. + + override fun onErrorOccurred(error: String) { + // Display an error message or log error + } + +### 3. OnDownloadStarted +The library issues a callback when download starts. To show a message, simply override the `onDownloadStarted` function + + override fun onDownloadStarted() { + // Do something + } + +### 4. OnDownloadCompleted +The library also issues a callback when download completes. To show a message, simply override the `onDownloadCompleted` function + + override fun onDownloadCompleted() { + // Do something + } + +If you do not override any of these functions, a default message is logged to your console. + ## Contribution If your would like to contribute to the project, you could complete any of the following tasks; 1. Allow ability to request storage permissions from inside the library diff --git a/app/src/main/java/com/murerwa/murerwadownloader/FirstFragment.kt b/app/src/main/java/com/murerwa/murerwadownloader/FirstFragment.kt index 9213017..836f4a7 100644 --- a/app/src/main/java/com/murerwa/murerwadownloader/FirstFragment.kt +++ b/app/src/main/java/com/murerwa/murerwadownloader/FirstFragment.kt @@ -1,8 +1,10 @@ package com.murerwa.murerwadownloader import android.os.Bundle +import android.util.Log import androidx.fragment.app.Fragment import android.view.View +import android.widget.Toast import com.murerwa.filedownloader.DownloadInterface import com.murerwa.filedownloader.FileDownloader import com.murerwa.murerwadownloader.databinding.FragmentFirstBinding @@ -13,10 +15,8 @@ class FirstFragment : Fragment(R.layout.fragment_first), DownloadInterface { private val binding get() = _binding!! -// private val url = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0f/A._Schwarzenegger.jpg/1200px-A._Schwarzenegger.jpg" -// private val url = "https://parcelle.kuzasystems.com/downloads/releases/1.0.1.15.apk" private val url = "http://www.ecomesty.co.ke/kytabu/the-time-machine-by-h.-g.-wells.epub" -// private val url = "https://murerwa.com/murerwa_cv.pdf" +// private val url = "" override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) @@ -40,10 +40,26 @@ class FirstFragment : Fragment(R.layout.fragment_first), DownloadInterface { } override fun onDownloadProgressChanged(newProgress: Int) { + super.onDownloadProgressChanged(newProgress) binding.progressBar.progress = newProgress binding.textView.text = "$newProgress%" } + override fun onDownloadStarted() { + super.onDownloadStarted() + Toast.makeText(context, "Downloading started", Toast.LENGTH_SHORT).show() + } + + override fun onDownloadCompleted() { + super.onDownloadCompleted() + Toast.makeText(context, "Download completed", Toast.LENGTH_SHORT).show() + } + + override fun onErrorOccurred(error: String) { + super.onErrorOccurred(error) + Toast.makeText(context, "Sorry. The download could not be completed", Toast.LENGTH_SHORT).show() + } + override fun onDestroyView() { super.onDestroyView() _binding = null diff --git a/filedownloader/build.gradle b/filedownloader/build.gradle index 95ced0a..5dc072f 100644 --- a/filedownloader/build.gradle +++ b/filedownloader/build.gradle @@ -54,7 +54,7 @@ afterEvaluate { groupId = 'com.github.xwaxes01' artifactId = 'filedownloader' - version = '0.0.5' + version = '0.0.6' } } } diff --git a/filedownloader/src/main/java/com/murerwa/filedownloader/DownloadInterface.kt b/filedownloader/src/main/java/com/murerwa/filedownloader/DownloadInterface.kt index 6480748..cef369d 100644 --- a/filedownloader/src/main/java/com/murerwa/filedownloader/DownloadInterface.kt +++ b/filedownloader/src/main/java/com/murerwa/filedownloader/DownloadInterface.kt @@ -1,5 +1,26 @@ package com.murerwa.filedownloader +import android.util.Log + +const val TAG = "MURERWA_DOWNLOADER" + interface DownloadInterface { - fun onDownloadProgressChanged(newProgress: Int) + fun onDownloadProgressChanged(newProgress: Int) { + Log.d(TAG, "DownloadProgress - $newProgress") + } + + fun onErrorOccurred(error: String) { + // Default implementation + Log.d(TAG, "Error - $error") + } + + fun onDownloadStarted() { + // Default implementation + Log.d(TAG, "Message - File Download started") + } + + fun onDownloadCompleted() { + // Default implementation + Log.d(TAG, "Success - File Download completed") + } } \ No newline at end of file diff --git a/filedownloader/src/main/java/com/murerwa/filedownloader/FileDownloader.kt b/filedownloader/src/main/java/com/murerwa/filedownloader/FileDownloader.kt index f7870b1..f627367 100644 --- a/filedownloader/src/main/java/com/murerwa/filedownloader/FileDownloader.kt +++ b/filedownloader/src/main/java/com/murerwa/filedownloader/FileDownloader.kt @@ -10,6 +10,7 @@ import kotlinx.coroutines.withContext import java.io.File import java.io.FileOutputStream import java.io.IOException +import java.lang.Exception import java.net.HttpURLConnection import java.net.URL @@ -24,10 +25,6 @@ class FileDownloader( fun downloadFile() { GlobalScope.launch(Dispatchers.IO) { try { - withContext(Dispatchers.Main) { - Toast.makeText(context, "Downloading file", Toast.LENGTH_LONG).show() - } - // Create directory if does not exist filePath.mkdirs() @@ -38,11 +35,11 @@ class FileDownloader( connection.doInput = true connection.connect() - Log.d("FILE PATH", "PATH: $filePath") +// Log.d("FILE PATH", "PATH: $filePath") val fileLength = connection.contentLength - Log.d("FILE LENGTH", fileLength.toString()) +// Log.d("FILE LENGTH", fileLength.toString()) val outputFile = File(filePath, fileName) val fos = FileOutputStream(outputFile) @@ -54,8 +51,14 @@ class FileDownloader( } if (inputStream == connection.errorStream) { - Log.d("ERROR","It looks like the passed file does not exist") + withContext(Dispatchers.Main) { + downloadInterface.onErrorOccurred("It looks like the passed link does not exist") + } } else { + withContext(Dispatchers.Main) { + downloadInterface.onDownloadStarted() + } + val buffer = ByteArray(4096) var len1: Int var total = 0 @@ -73,15 +76,21 @@ class FileDownloader( fos.close() inputStream.close() - Log.d("OutputStream", connection.toString()) +// Log.d("OutputStream", connection.toString()) withContext(Dispatchers.Main) { - Toast.makeText(context, "Download completed", Toast.LENGTH_LONG).show() + downloadInterface.onDownloadCompleted() } } - } catch (e: IOException) { - e.printStackTrace() + } catch (ioException: IOException) { + withContext(Dispatchers.Main) { + downloadInterface.onErrorOccurred(ioException.stackTraceToString()) + } + } catch (exception: Exception) { + withContext(Dispatchers.Main) { + downloadInterface.onErrorOccurred(exception.stackTraceToString()) + } } } }