fix: can not show download result

This commit is contained in:
AnranYus
2024-06-15 03:00:42 +08:00
parent 5baa502363
commit f66fa1131a
2 changed files with 12 additions and 11 deletions
@@ -106,20 +106,15 @@ class PostActivity : ComponentActivity() {
val result =
downloadBinder.downloadImage(viewModel.uiState.value.selectPost?.fileUrl ?: "")
.await()
launch(Dispatchers.Main) {
if (result.isSuccess) {
Toast.makeText(
this@PostActivity, "Download successful", Toast.LENGTH_SHORT
).show()
} else {
Toast.makeText(
this@PostActivity, "Download fail", Toast.LENGTH_SHORT
).show()
}
}
viewModel.setDownloadResult(result)
}
}
viewModel.downloadResult.observe(this){
val message = it.getOrThrow()
Toast.makeText(this,message.toString(),Toast.LENGTH_SHORT).show()
}
val serviceIntent = Intent(this, DownloadService::class.java)
bindService(serviceIntent, connection, BIND_AUTO_CREATE)
}
@@ -10,6 +10,7 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch
import java.io.Serializable
class PostViewModel:ViewModel() {
private val _uiState = MutableStateFlow(PostUiState())
@@ -19,6 +20,7 @@ class PostViewModel:ViewModel() {
PostRepository()
}
val downloadAction = MutableLiveData<Unit>()
val downloadResult = MutableLiveData<Result<Serializable>>()
init {
initData()
@@ -53,5 +55,9 @@ class PostViewModel:ViewModel() {
}
}
fun setDownloadResult(result: Result<Serializable>){
downloadResult.postValue(result)
}
}