Skip to content

Commit 594cac1

Browse files
author
junkfood
committed
bugFix
1 parent 326867f commit 594cac1

File tree

5 files changed

+55
-54
lines changed

5 files changed

+55
-54
lines changed

app/src/main/java/com/junkfood/seal/database/VideoInfoDao.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ interface VideoInfoDao {
2222

2323
@Query("DELETE FROM DownloadedVideoInfo WHERE videoTitle = :title")
2424
suspend fun deleteByTitle(title: String)
25+
26+
@Query("DELETE FROM DownloadedVideoInfo WHERE videoPath = :path")
27+
suspend fun deleteByPath(path: String)
2528
}

app/src/main/java/com/junkfood/seal/ui/page/download/DownloadPage.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ fun DownloadPage(
8888
{
8989
with(viewState.value) {
9090
Column {
91-
AnimatedVisibility(visible = isDownloading) {
91+
AnimatedVisibility(visible = showVideoCard) {
9292
VideoCard(
9393
videoTitle,
9494
videoAuthor,

app/src/main/java/com/junkfood/seal/ui/page/download/DownloadViewModel.kt

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ import kotlinx.coroutines.withContext
2020
import javax.inject.Inject
2121

2222
@HiltViewModel
23-
class DownloadViewModel @Inject constructor(): ViewModel() {
23+
class DownloadViewModel @Inject constructor() : ViewModel() {
2424

2525
private val _viewState = MutableStateFlow(DownloadViewState())
2626
val viewState = _viewState.asStateFlow()
27+
private var isDownloading = false
2728

2829
data class DownloadViewState(
29-
val isDownloading: Boolean = false,
30+
val showVideoCard: Boolean = false,
3031
val progress: Float = 0f,
3132
val url: String = "",
3233
val videoTitle: String = "",
@@ -42,50 +43,54 @@ class DownloadViewModel @Inject constructor(): ViewModel() {
4243

4344

4445
fun startDownloadVideo() {
46+
if (isDownloading) {
47+
TextUtil.makeToast(context.getString(R.string.task_running))
48+
return
49+
}
50+
isDownloading = true
4551
viewModelScope.launch(Dispatchers.IO) {
46-
with(_viewState.value.url) {
47-
if (isNullOrBlank()) {
52+
with(_viewState) {
53+
if (value.url.isBlank()) {
4854
showErrorMessage(context.getString(R.string.url_empty))
49-
} else {
50-
_viewState.update { it.copy(isDownloadError = false) }
51-
val videoInfo = DownloadUtil.fetchVideoInfo(this@with)
52-
if (videoInfo == null) {
53-
showErrorMessage(context.getString(R.string.fetch_info_error_msg))
54-
return@launch
55-
}
56-
_viewState.update {
57-
it.copy(
58-
progress = 0f,
59-
isDownloading = true,
60-
videoTitle = videoInfo.title,
61-
videoAuthor = videoInfo.uploader,
62-
videoThumbnailUrl = TextUtil.urlHttpToHttps(videoInfo.thumbnail)
63-
)
64-
}
65-
downloadResultTemp = DownloadUtil.downloadVideo(this@with, videoInfo)
66-
{ fl: Float, _: Long, _: String -> _viewState.update { it.copy(progress = fl) } }
67-
//isDownloading.postValue(false)
68-
if (downloadResultTemp.resultCode == DownloadUtil.ResultCode.EXCEPTION) {
69-
showErrorMessage(context.getString(R.string.download_error_msg))
70-
} else {
71-
DatabaseUtil.insertInfo(
72-
DownloadedVideoInfo(
73-
0,
74-
videoInfo.title.toString(),
75-
videoInfo.uploader.toString(),
76-
viewState.value.url,
77-
videoInfo.thumbnail.toString(),
78-
downloadResultTemp.filePath.toString()
79-
)
80-
)
81-
withContext(Dispatchers.Main) {
82-
_viewState.update { it.copy(progress = 100f) }
83-
if (PreferenceUtil.getValue("open_when_finish")) openFile(
84-
downloadResultTemp
85-
)
86-
}
87-
}
55+
return@launch
56+
}
57+
update { it.copy(isDownloadError = false) }
58+
val videoInfo = DownloadUtil.fetchVideoInfo(value.url)
59+
if (videoInfo == null) {
60+
showErrorMessage(context.getString(R.string.fetch_info_error_msg))
61+
return@launch
62+
}
63+
update {
64+
it.copy(
65+
progress = 0f,
66+
showVideoCard = true,
67+
videoTitle = videoInfo.title,
68+
videoAuthor = videoInfo.uploader,
69+
videoThumbnailUrl = TextUtil.urlHttpToHttps(videoInfo.thumbnail)
70+
)
71+
}
72+
73+
downloadResultTemp = DownloadUtil.downloadVideo(value.url, videoInfo)
74+
{ fl: Float, _: Long, _: String -> _viewState.update { it.copy(progress = fl) } }
75+
76+
if (downloadResultTemp.resultCode == DownloadUtil.ResultCode.EXCEPTION) {
77+
showErrorMessage(context.getString(R.string.download_error_msg))
78+
return@launch
8879
}
80+
DatabaseUtil.insertInfo(
81+
DownloadedVideoInfo(
82+
0,
83+
videoInfo.title.toString(),
84+
videoInfo.uploader.toString(),
85+
viewState.value.url,
86+
videoInfo.thumbnail.toString(),
87+
downloadResultTemp.filePath.toString()
88+
)
89+
)
90+
_viewState.update { it.copy(progress = 100f) }
91+
if (PreferenceUtil.getValue("open_when_finish"))
92+
openFile(downloadResultTemp)
93+
isDownloading = false
8994
}
9095
}
9196
}
@@ -95,6 +100,7 @@ class DownloadViewModel @Inject constructor(): ViewModel() {
95100
_viewState.update { it.copy(progress = 0f, isDownloadError = true, errorMessage = s) }
96101
TextUtil.makeToast(s)
97102
}
103+
isDownloading = false
98104
}
99105

100106
fun openVideoFile() {

app/src/main/java/com/junkfood/seal/util/DatabaseUtil.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ object DatabaseUtil {
1818

1919
suspend fun insertInfo(vararg infoList: DownloadedVideoInfo) {
2020
for (info in infoList) {
21-
dao.deleteByTitle(info.videoTitle)
21+
dao.deleteByPath(info.videoPath)
2222
dao.insertAll(info)
2323
}
2424
}

app/src/main/java/com/junkfood/seal/util/DownloadUtil.kt

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ object DownloadUtil {
1515
class Result(val resultCode: ResultCode, val filePath: String?) {
1616
companion object {
1717
fun failure(): Result {
18-
WIP = 0
1918
return Result(ResultCode.EXCEPTION, null)
2019
}
2120

2221
fun success(title: String, ext: String): Result {
23-
WIP = 0
2422
with(if (ext == "mp3") ResultCode.FINISH_AUDIO else ResultCode.FINISH_VIDEO) {
2523
return Result(this, "${BaseApplication.downloadDir}/$title.$ext")
2624
}
@@ -33,14 +31,9 @@ object DownloadUtil {
3331
}
3432

3533
private const val TAG = "DownloadUtil"
36-
private var WIP = 0
3734

3835
suspend fun fetchVideoInfo(url: String): VideoInfo? {
39-
if (WIP == 1) {
40-
toast(context.getString(R.string.task_running))
41-
WIP = 0
42-
return null
43-
} else WIP = 1
36+
4437
val videoInfo: VideoInfo
4538
try {
4639
toast(context.getString(R.string.fetching_info))
@@ -52,7 +45,6 @@ object DownloadUtil {
5245
}
5346
} catch (e: Exception) {
5447
FileUtil.createLogFileOnDevice(e)
55-
WIP = 0
5648
return null
5749
}
5850
return videoInfo

0 commit comments

Comments
 (0)