Skip to content

Commit

Permalink
feat: Bug fixes and improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
kkmurerwa committed Oct 7, 2021
1 parent 12230d0 commit 3a9a02b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 19 deletions.
40 changes: 37 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()

Expand All @@ -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
22 changes: 19 additions & 3 deletions app/src/main/java/com/murerwa/murerwadownloader/FirstFragment.kt
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion filedownloader/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ afterEvaluate {

groupId = 'com.github.xwaxes01'
artifactId = 'filedownloader'
version = '0.0.5'
version = '0.0.6'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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()

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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())
}
}
}
}
Expand Down

0 comments on commit 3a9a02b

Please sign in to comment.