From 42b2976d6f587d4122d36503e0b5e0c07bda4782 Mon Sep 17 00:00:00 2001 From: junkfood Date: Fri, 13 May 2022 14:50:20 +0800 Subject: [PATCH] bugFix --- .../junkfood/seal/database/VideoInfoDao.kt | 3 + .../seal/ui/page/download/DownloadPage.kt | 2 +- .../ui/page/download/DownloadViewModel.kt | 92 ++++++++++--------- .../com/junkfood/seal/util/DatabaseUtil.kt | 2 +- .../com/junkfood/seal/util/DownloadUtil.kt | 10 +- 5 files changed, 55 insertions(+), 54 deletions(-) diff --git a/app/src/main/java/com/junkfood/seal/database/VideoInfoDao.kt b/app/src/main/java/com/junkfood/seal/database/VideoInfoDao.kt index ad8d1671..373e9f46 100644 --- a/app/src/main/java/com/junkfood/seal/database/VideoInfoDao.kt +++ b/app/src/main/java/com/junkfood/seal/database/VideoInfoDao.kt @@ -22,4 +22,7 @@ interface VideoInfoDao { @Query("DELETE FROM DownloadedVideoInfo WHERE videoTitle = :title") suspend fun deleteByTitle(title: String) + + @Query("DELETE FROM DownloadedVideoInfo WHERE videoPath = :path") + suspend fun deleteByPath(path: String) } \ No newline at end of file diff --git a/app/src/main/java/com/junkfood/seal/ui/page/download/DownloadPage.kt b/app/src/main/java/com/junkfood/seal/ui/page/download/DownloadPage.kt index 1ddea57d..0de8dd8f 100644 --- a/app/src/main/java/com/junkfood/seal/ui/page/download/DownloadPage.kt +++ b/app/src/main/java/com/junkfood/seal/ui/page/download/DownloadPage.kt @@ -88,7 +88,7 @@ fun DownloadPage( { with(viewState.value) { Column { - AnimatedVisibility(visible = isDownloading) { + AnimatedVisibility(visible = showVideoCard) { VideoCard( videoTitle, videoAuthor, diff --git a/app/src/main/java/com/junkfood/seal/ui/page/download/DownloadViewModel.kt b/app/src/main/java/com/junkfood/seal/ui/page/download/DownloadViewModel.kt index 262fbea2..0bfe7a34 100644 --- a/app/src/main/java/com/junkfood/seal/ui/page/download/DownloadViewModel.kt +++ b/app/src/main/java/com/junkfood/seal/ui/page/download/DownloadViewModel.kt @@ -20,13 +20,14 @@ import kotlinx.coroutines.withContext import javax.inject.Inject @HiltViewModel -class DownloadViewModel @Inject constructor(): ViewModel() { +class DownloadViewModel @Inject constructor() : ViewModel() { private val _viewState = MutableStateFlow(DownloadViewState()) val viewState = _viewState.asStateFlow() + private var isDownloading = false data class DownloadViewState( - val isDownloading: Boolean = false, + val showVideoCard: Boolean = false, val progress: Float = 0f, val url: String = "", val videoTitle: String = "", @@ -42,50 +43,54 @@ class DownloadViewModel @Inject constructor(): ViewModel() { fun startDownloadVideo() { + if (isDownloading) { + TextUtil.makeToast(context.getString(R.string.task_running)) + return + } + isDownloading = true viewModelScope.launch(Dispatchers.IO) { - with(_viewState.value.url) { - if (isNullOrBlank()) { + with(_viewState) { + if (value.url.isBlank()) { showErrorMessage(context.getString(R.string.url_empty)) - } else { - _viewState.update { it.copy(isDownloadError = false) } - val videoInfo = DownloadUtil.fetchVideoInfo(this@with) - if (videoInfo == null) { - showErrorMessage(context.getString(R.string.fetch_info_error_msg)) - return@launch - } - _viewState.update { - it.copy( - progress = 0f, - isDownloading = true, - videoTitle = videoInfo.title, - videoAuthor = videoInfo.uploader, - videoThumbnailUrl = TextUtil.urlHttpToHttps(videoInfo.thumbnail) - ) - } - downloadResultTemp = DownloadUtil.downloadVideo(this@with, videoInfo) - { fl: Float, _: Long, _: String -> _viewState.update { it.copy(progress = fl) } } - //isDownloading.postValue(false) - if (downloadResultTemp.resultCode == DownloadUtil.ResultCode.EXCEPTION) { - showErrorMessage(context.getString(R.string.download_error_msg)) - } else { - DatabaseUtil.insertInfo( - DownloadedVideoInfo( - 0, - videoInfo.title.toString(), - videoInfo.uploader.toString(), - viewState.value.url, - videoInfo.thumbnail.toString(), - downloadResultTemp.filePath.toString() - ) - ) - withContext(Dispatchers.Main) { - _viewState.update { it.copy(progress = 100f) } - if (PreferenceUtil.getValue("open_when_finish")) openFile( - downloadResultTemp - ) - } - } + return@launch } + update { it.copy(isDownloadError = false) } + val videoInfo = DownloadUtil.fetchVideoInfo(value.url) + if (videoInfo == null) { + showErrorMessage(context.getString(R.string.fetch_info_error_msg)) + return@launch + } + update { + it.copy( + progress = 0f, + showVideoCard = true, + videoTitle = videoInfo.title, + videoAuthor = videoInfo.uploader, + videoThumbnailUrl = TextUtil.urlHttpToHttps(videoInfo.thumbnail) + ) + } + + downloadResultTemp = DownloadUtil.downloadVideo(value.url, videoInfo) + { fl: Float, _: Long, _: String -> _viewState.update { it.copy(progress = fl) } } + + if (downloadResultTemp.resultCode == DownloadUtil.ResultCode.EXCEPTION) { + showErrorMessage(context.getString(R.string.download_error_msg)) + return@launch + } + DatabaseUtil.insertInfo( + DownloadedVideoInfo( + 0, + videoInfo.title.toString(), + videoInfo.uploader.toString(), + viewState.value.url, + videoInfo.thumbnail.toString(), + downloadResultTemp.filePath.toString() + ) + ) + _viewState.update { it.copy(progress = 100f) } + if (PreferenceUtil.getValue("open_when_finish")) + openFile(downloadResultTemp) + isDownloading = false } } } @@ -95,6 +100,7 @@ class DownloadViewModel @Inject constructor(): ViewModel() { _viewState.update { it.copy(progress = 0f, isDownloadError = true, errorMessage = s) } TextUtil.makeToast(s) } + isDownloading = false } fun openVideoFile() { diff --git a/app/src/main/java/com/junkfood/seal/util/DatabaseUtil.kt b/app/src/main/java/com/junkfood/seal/util/DatabaseUtil.kt index 1551841d..bcc85e27 100644 --- a/app/src/main/java/com/junkfood/seal/util/DatabaseUtil.kt +++ b/app/src/main/java/com/junkfood/seal/util/DatabaseUtil.kt @@ -18,7 +18,7 @@ object DatabaseUtil { suspend fun insertInfo(vararg infoList: DownloadedVideoInfo) { for (info in infoList) { - dao.deleteByTitle(info.videoTitle) + dao.deleteByPath(info.videoPath) dao.insertAll(info) } } diff --git a/app/src/main/java/com/junkfood/seal/util/DownloadUtil.kt b/app/src/main/java/com/junkfood/seal/util/DownloadUtil.kt index ed16339f..19c7a1e4 100644 --- a/app/src/main/java/com/junkfood/seal/util/DownloadUtil.kt +++ b/app/src/main/java/com/junkfood/seal/util/DownloadUtil.kt @@ -15,12 +15,10 @@ object DownloadUtil { class Result(val resultCode: ResultCode, val filePath: String?) { companion object { fun failure(): Result { - WIP = 0 return Result(ResultCode.EXCEPTION, null) } fun success(title: String, ext: String): Result { - WIP = 0 with(if (ext == "mp3") ResultCode.FINISH_AUDIO else ResultCode.FINISH_VIDEO) { return Result(this, "${BaseApplication.downloadDir}/$title.$ext") } @@ -33,14 +31,9 @@ object DownloadUtil { } private const val TAG = "DownloadUtil" - private var WIP = 0 suspend fun fetchVideoInfo(url: String): VideoInfo? { - if (WIP == 1) { - toast(context.getString(R.string.task_running)) - WIP = 0 - return null - } else WIP = 1 + val videoInfo: VideoInfo try { toast(context.getString(R.string.fetching_info)) @@ -52,7 +45,6 @@ object DownloadUtil { } } catch (e: Exception) { FileUtil.createLogFileOnDevice(e) - WIP = 0 return null } return videoInfo