Skip to content

Commit

Permalink
Emit filepath directly after download finishes (#131)
Browse files Browse the repository at this point in the history
Signed-off-by: starry-shivam <[email protected]>
  • Loading branch information
starry-shivam authored Mar 21, 2024
1 parent 1f35d7d commit 0adbbe3
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ import com.starry.myne.utils.book.BookUtils
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.io.File
import javax.inject.Inject

data class BookDetailScreenState(
Expand Down Expand Up @@ -92,20 +91,19 @@ class BookDetailViewModel @Inject constructor(
) {
bookDownloader.downloadBook(book = book,
downloadProgressListener = downloadProgressListener,
onDownloadSuccess = { fileName ->
val filepath = bookDownloader.getFilePathForBook(fileName)
insertIntoDB(book = book, filepath = filepath)
onDownloadSuccess = { filePath ->
insertIntoDB(book = book, filePath = filePath)
state = state.copy(bookLibraryItem = libraryDao.getItemById(book.id))
}
)
}

private fun insertIntoDB(book: Book, filepath: String) {
private fun insertIntoDB(book: Book, filePath: String) {
val libraryItem = LibraryItem(
bookId = book.id,
title = book.title,
authors = BookUtils.getAuthorsAsString(book.authors),
filePath = filepath,
filePath = filePath,
createdAt = System.currentTimeMillis()
)
libraryDao.insert(libraryItem)
Expand Down
18 changes: 5 additions & 13 deletions app/src/main/java/com/starry/myne/utils/book/BookDownloader.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class BookDownloader(private val context: Context) {
@SuppressLint("Range")
fun downloadBook(
book: Book, downloadProgressListener: (progress: Float, status: Int) -> Unit,
onDownloadSuccess: (fileName: String) -> Unit
onDownloadSuccess: (filePath: String) -> Unit
) {
val filename = getFilenameForBook(book)
val uri = Uri.parse(book.formats.applicationepubzip)
Expand Down Expand Up @@ -104,7 +104,10 @@ class BookDownloader(private val context: Context) {
DownloadManager.STATUS_SUCCESSFUL -> {
isDownloadFinished = true
progress = 1f
onDownloadSuccess(filename)
val externalFilesDir =
context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
val filePath = File(externalFilesDir, filename).canonicalPath
onDownloadSuccess(filePath)
}

DownloadManager.STATUS_PAUSED, DownloadManager.STATUS_PENDING -> {}
Expand Down Expand Up @@ -137,17 +140,6 @@ class BookDownloader(private val context: Context) {
}
}


/**
* Returns file path for the given book's file name.
* @param fileName name of the file for which file path is required.
* @return [String] file path for the given file name.
*/
fun getFilePathForBook(fileName: String): String {
val externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS)
return File(externalFilesDir, fileName).canonicalPath
}

/**
* Returns true if book with the given id is currently being downloaded
* false otherwise.
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
buildscript {
ext {
kotlin_version = '1.9.22'
gradle_version = '8.3.0'
gradle_version = '8.3.1'
hilt_version = '2.49'
room_version = '2.6.1'
}
Expand Down

0 comments on commit 0adbbe3

Please sign in to comment.