Skip to content

Commit e8b3cdc

Browse files
refactor(kt-lib): 重构 HttpRequestViewModel 和 RetrofitFactoryFragment
- 重构 HttpRequestViewModel: -继承 ViewModel 替代 BaseViewModel - 移除 MutableLiveData,改为使用回调函数处理请求结果 - 简化错误处理逻辑 - 更新 RetrofitFactoryFragment: - 使用 ViewModelProvider 初始化 HttpRequestViewModel - 移除 httpRequestResult 的观察者,直接在回调中处理结果 - 优化数据加载和错误处理逻辑
1 parent bd83c41 commit e8b3cdc

File tree

2 files changed

+30
-45
lines changed

2 files changed

+30
-45
lines changed

app/src/main/java/com/pengxh/kt/lib/fragments/utils/RetrofitFactoryFragment.kt

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ import androidx.lifecycle.ViewModelProvider
77
import com.pengxh.kt.lib.databinding.FragmentUtilsRetrofitFactoryBinding
88
import com.pengxh.kt.lib.vm.HttpRequestViewModel
99
import com.pengxh.kt.lite.base.KotlinBaseFragment
10-
import com.pengxh.kt.lite.utils.LoadState
10+
import com.pengxh.kt.lite.extensions.show
1111
import com.pengxh.kt.lite.utils.LoadingDialog
1212

1313
class RetrofitFactoryFragment : KotlinBaseFragment<FragmentUtilsRetrofitFactoryBinding>() {
1414

15-
private lateinit var httpRequestViewModel: HttpRequestViewModel
15+
private val httpRequestViewModel by lazy { ViewModelProvider(this)[HttpRequestViewModel::class.java] }
1616

1717
override fun initViewBinding(
1818
inflater: LayoutInflater,
@@ -26,32 +26,25 @@ class RetrofitFactoryFragment : KotlinBaseFragment<FragmentUtilsRetrofitFactoryB
2626
}
2727

2828
override fun initOnCreate(savedInstanceState: Bundle?) {
29-
httpRequestViewModel = ViewModelProvider(this)[HttpRequestViewModel::class.java]
30-
httpRequestViewModel.getNewsByPage(requireContext(), "头条", 1)
31-
httpRequestViewModel.httpRequestResult.observe(this) {
32-
if (it.status == 0) {
33-
binding.textView.text = it.result.list.first().title
29+
httpRequestViewModel.getNewsByPage(
30+
"头条", 1,
31+
onLoading = {
32+
LoadingDialog.show(requireActivity(), "数据请求中,请稍后...")
33+
},
34+
onSuccess = {
35+
LoadingDialog.dismiss()
36+
binding.textView.text = it.result.list.first().content
37+
},
38+
onFailed = {
39+
LoadingDialog.dismiss()
40+
it.show(requireContext())
3441
}
35-
}
42+
)
3643
}
3744

3845

3946
override fun observeRequestState() {
40-
httpRequestViewModel.loadState.observe(this) {
41-
when (it) {
42-
LoadState.Loading -> {
43-
LoadingDialog.show(requireActivity(), "数据请求中,请稍后...")
44-
}
45-
46-
LoadState.Success -> {
47-
LoadingDialog.dismiss()
48-
}
49-
50-
else -> {
51-
LoadingDialog.dismiss()
52-
}
53-
}
54-
}
47+
5548
}
5649

5750
override fun initEvent() {

app/src/main/java/com/pengxh/kt/lib/vm/HttpRequestViewModel.kt

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
11
package com.pengxh.kt.lib.vm
22

3-
import android.content.Context
4-
import androidx.lifecycle.MutableLiveData
3+
import androidx.lifecycle.ViewModel
54
import com.google.gson.Gson
65
import com.google.gson.JsonObject
7-
import com.google.gson.reflect.TypeToken
86
import com.pengxh.kt.lib.model.NewsListModel
97
import com.pengxh.kt.lib.utils.RetrofitServiceManager
10-
import com.pengxh.kt.lite.base.BaseViewModel
118
import com.pengxh.kt.lite.extensions.launch
12-
import com.pengxh.kt.lite.extensions.show
13-
import com.pengxh.kt.lite.utils.LoadState
9+
import com.pengxh.kt.lite.extensions.unpackingResponse
1410

15-
class HttpRequestViewModel : BaseViewModel() {
11+
class HttpRequestViewModel : ViewModel() {
1612

1713
val gson by lazy { Gson() }
18-
val httpRequestResult = MutableLiveData<NewsListModel>()
1914

20-
fun getNewsByPage(context: Context, channel: String, start: Int) = launch({
21-
loadState.value = LoadState.Loading
15+
fun getNewsByPage(
16+
channel: String,
17+
start: Int,
18+
onLoading: () -> Unit,
19+
onSuccess: (NewsListModel) -> Unit,
20+
onFailed: (String) -> Unit
21+
) = launch({
22+
onLoading()
2223
val response = RetrofitServiceManager.getNewsByPage(channel, start)
2324
val header = response.getResponseHeader()
2425
when (header.first) {
25-
0 -> {
26-
httpRequestResult.value = gson.fromJson<NewsListModel>(
27-
response, object : TypeToken<NewsListModel>() {}.type
28-
)
29-
loadState.value = LoadState.Success
30-
}
31-
32-
else -> {
33-
loadState.value = LoadState.Fail
34-
header.second.show(context)
35-
}
26+
0 -> onSuccess(unpackingResponse<NewsListModel>(response))
27+
else -> onFailed(header.second)
3628
}
3729
}, {
38-
loadState.value = LoadState.Fail
30+
onFailed(it.message ?: "Unknown error")
3931
})
4032

4133
private fun String.getResponseHeader(): Pair<Int, String> {

0 commit comments

Comments
 (0)