@@ -20,13 +20,14 @@ import kotlinx.coroutines.withContext
20
20
import javax.inject.Inject
21
21
22
22
@HiltViewModel
23
- class DownloadViewModel @Inject constructor(): ViewModel() {
23
+ class DownloadViewModel @Inject constructor() : ViewModel() {
24
24
25
25
private val _viewState = MutableStateFlow (DownloadViewState ())
26
26
val viewState = _viewState .asStateFlow()
27
+ private var isDownloading = false
27
28
28
29
data class DownloadViewState (
29
- val isDownloading : Boolean = false ,
30
+ val showVideoCard : Boolean = false ,
30
31
val progress : Float = 0f ,
31
32
val url : String = " " ,
32
33
val videoTitle : String = " " ,
@@ -42,50 +43,54 @@ class DownloadViewModel @Inject constructor(): ViewModel() {
42
43
43
44
44
45
fun startDownloadVideo () {
46
+ if (isDownloading) {
47
+ TextUtil .makeToast(context.getString(R .string.task_running))
48
+ return
49
+ }
50
+ isDownloading = true
45
51
viewModelScope.launch(Dispatchers .IO ) {
46
- with (_viewState .value.url ) {
47
- if (isNullOrBlank ()) {
52
+ with (_viewState ) {
53
+ if (value.url.isBlank ()) {
48
54
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
88
79
}
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
89
94
}
90
95
}
91
96
}
@@ -95,6 +100,7 @@ class DownloadViewModel @Inject constructor(): ViewModel() {
95
100
_viewState .update { it.copy(progress = 0f , isDownloadError = true , errorMessage = s) }
96
101
TextUtil .makeToast(s)
97
102
}
103
+ isDownloading = false
98
104
}
99
105
100
106
fun openVideoFile () {
0 commit comments