筆記上傳後,總是要下載下來的,下載跟上傳的流程一樣,只是差在下載不用丟資料出去
一樣在 Fragment 的onViewCreated
建立點擊事件
binding.download.setOnClickListener {
viewModel.downloadHackmd()
}
Get 跟 Post 一樣是走副執行緒,所以直接用共常式執行,利用改變 _httpStatus 來決定畫面要顯示什麼
private var _httpStatus = MutableLiveData<String?>()
val httpStatus: LiveData<String?>
get() = _httpStatus
fun downloadHackmd() {
viewModelScope.launch {
HttpManager.INSTANCE.get(
BaseConstants.NOTES,
token,
getApplication(),
object : HttpResponseListener {
override fun onFailure(status: String, responseBody: String) {
_hackmdDownloadStatus.getValue(status)
}
override fun onSuccess(responseBody: String) {
//這邊寫入下載的資料
}
})
}
}
viewModel.httpStatus.observe(viewLifecycleOwner) { status ->
status?.let {
//這邊顯示上傳成功的畫面
}
}