Skip to content

Commit e850cef

Browse files
committed
BaseVMDialogFragment增加lifecycle
1 parent aba3089 commit e850cef

File tree

7 files changed

+195
-65
lines changed

7 files changed

+195
-65
lines changed

CHANGE_lOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# CHANGE LOG
2+
### 1.5.4
3+
4+
+ BaseVMDialogFragment增加lifecycle
25

36
### 1.5.3
47

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.catchpig.kmvvm.dialog
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import com.catchpig.kmvvm.databinding.DialogConfirmBinding
6+
import com.catchpig.kmvvm.dialog.viewmodel.ConfirmViewModel
7+
import com.catchpig.mvvm.base.dialog.BaseVMDialogFragment
8+
9+
class ConfirmVMDialogFragment : BaseVMDialogFragment<DialogConfirmBinding, ConfirmViewModel>() {
10+
companion object {
11+
fun newInstance(): ConfirmVMDialogFragment {
12+
return ConfirmVMDialogFragment()
13+
}
14+
}
15+
16+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
17+
super.onViewCreated(view, savedInstanceState)
18+
requireDialog().run {
19+
setCanceledOnTouchOutside(true)
20+
setCancelable(true)
21+
}
22+
}
23+
24+
override fun initParam() {
25+
}
26+
27+
override fun initView() {
28+
bodyBinding.confirm.setOnClickListener {
29+
dismiss()
30+
}
31+
}
32+
33+
override fun initFlow() {
34+
}
35+
36+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.catchpig.kmvvm.dialog.viewmodel
2+
3+
import com.catchpig.mvvm.base.viewmodel.BaseViewModel
4+
5+
class ConfirmViewModel : BaseViewModel() {
6+
}

app/src/main/java/com/catchpig/kmvvm/index/IndexFragment.kt

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import com.catchpig.kmvvm.R
66
import com.catchpig.kmvvm.apk.view.InstallApkActivity
77
import com.catchpig.kmvvm.child.ChildActivity
88
import com.catchpig.kmvvm.databinding.FragmentIndexBinding
9-
import com.catchpig.kmvvm.dialog.ConfirmDialogFragment
9+
import com.catchpig.kmvvm.dialog.ConfirmVMDialogFragment
1010
import com.catchpig.kmvvm.exception.HttpServerException
1111
import com.catchpig.kmvvm.fullscreen.FullScreenActivity
1212
import com.catchpig.kmvvm.recycle.RecycleActivity
@@ -54,7 +54,7 @@ class IndexFragment : BaseVMFragment<FragmentIndexBinding, IndexViewModel>(), Vi
5454
}
5555
}
5656

57-
private fun loadBanners(){
57+
private fun loadBanners() {
5858
viewModel.queryBanners().lifecycleLoadingDialog(this, true) {
5959
val images = mutableListOf<String>()
6060
this.forEach {
@@ -72,28 +72,37 @@ class IndexFragment : BaseVMFragment<FragmentIndexBinding, IndexViewModel>(), Vi
7272
R.id.open_title -> {
7373
startKtActivity<ChildActivity>()
7474
}
75+
7576
R.id.transparent -> {
7677
startKtActivity<TransparentActivity>()
7778
}
79+
7880
R.id.full_screen -> {
7981
startKtActivity<FullScreenActivity>()
8082
}
83+
8184
R.id.recycle -> {
8285
startKtActivity<RecycleActivity>()
8386
}
87+
8488
R.id.installApk -> {
8589
startKtActivity<InstallApkActivity>()
8690
}
91+
8792
R.id.handler_error -> {
8893
viewModel.handlerError().lifecycleLoadingDialog(this, errorCallback = {
8994
if (it is HttpServerException) {
9095
snackBar(it.message!!)
9196
}
9297
}) {}
9398
}
99+
94100
R.id.open_dialog -> {
95-
ConfirmDialogFragment.newInstance().show(childFragmentManager, "ConfirmDialogFragment")
101+
// ConfirmDialogFragment.newInstance().show(childFragmentManager, "ConfirmDialogFragment")
102+
ConfirmVMDialogFragment.newInstance()
103+
.show(childFragmentManager, "ConfirmVMDialogFragment")
96104
}
105+
97106
else -> {
98107
}
99108
}

app/src/main/res/layout/dialog_confirm.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
33
xmlns:app="http://schemas.android.com/apk/res-auto"
4-
android:layout_width="1920dp"
5-
android:layout_height="700dp"
4+
android:layout_width="600dp"
5+
android:layout_height="300dp"
66
android:background="@color/c_fff">
77

88
<androidx.appcompat.widget.AppCompatTextView

app/src/main/res/layout/fragment_index.xml

Lines changed: 69 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -19,67 +19,80 @@
1919
android:layout_width="100dp"
2020
android:layout_height="50dp" />
2121

22-
<Button
23-
android:id="@+id/snackbar"
24-
android:layout_width="wrap_content"
25-
android:layout_height="wrap_content"
26-
android:layout_marginTop="10dp"
27-
android:clickable="true"
28-
android:text="SnackBar" />
22+
<androidx.core.widget.NestedScrollView
23+
android:layout_width="match_parent"
24+
android:layout_height="match_parent">
25+
26+
<LinearLayout
27+
android:layout_width="match_parent"
28+
android:layout_height="match_parent"
29+
android:gravity="center"
30+
android:orientation="vertical">
31+
32+
<Button
33+
android:id="@+id/snackbar"
34+
android:layout_width="wrap_content"
35+
android:layout_height="wrap_content"
36+
android:layout_marginTop="10dp"
37+
android:clickable="true"
38+
android:text="SnackBar" />
39+
40+
<Button
41+
android:id="@+id/open_title"
42+
android:layout_width="wrap_content"
43+
android:layout_height="wrap_content"
44+
android:layout_marginTop="10dp"
45+
android:clickable="true"
46+
android:text="有标题栏" />
2947

30-
<Button
31-
android:id="@+id/open_title"
32-
android:layout_width="wrap_content"
33-
android:layout_height="wrap_content"
34-
android:layout_marginTop="10dp"
35-
android:clickable="true"
36-
android:text="有标题栏" />
48+
<Button
49+
android:id="@+id/transparent"
50+
android:layout_width="wrap_content"
51+
android:layout_height="wrap_content"
52+
android:layout_marginTop="10dp"
53+
android:clickable="true"
54+
android:text="状态栏透明" />
3755

38-
<Button
39-
android:id="@+id/transparent"
40-
android:layout_width="wrap_content"
41-
android:layout_height="wrap_content"
42-
android:layout_marginTop="10dp"
43-
android:clickable="true"
44-
android:text="状态栏透明" />
56+
<Button
57+
android:id="@+id/full_screen"
58+
android:layout_width="wrap_content"
59+
android:layout_height="wrap_content"
60+
android:layout_marginTop="10dp"
61+
android:clickable="true"
62+
android:text="全屏显示" />
4563

46-
<Button
47-
android:id="@+id/full_screen"
48-
android:layout_width="wrap_content"
49-
android:layout_height="wrap_content"
50-
android:layout_marginTop="10dp"
51-
android:clickable="true"
52-
android:text="全屏显示" />
64+
<Button
65+
android:id="@+id/recycle"
66+
android:layout_width="wrap_content"
67+
android:layout_height="wrap_content"
68+
android:layout_marginTop="10dp"
69+
android:clickable="true"
70+
android:text="Recycle列表显示" />
5371

54-
<Button
55-
android:id="@+id/recycle"
56-
android:layout_width="wrap_content"
57-
android:layout_height="wrap_content"
58-
android:layout_marginTop="10dp"
59-
android:clickable="true"
60-
android:text="Recycle列表显示" />
72+
<Button
73+
android:id="@+id/installApk"
74+
android:layout_width="wrap_content"
75+
android:layout_height="wrap_content"
76+
android:layout_marginTop="10dp"
77+
android:clickable="true"
78+
android:text="下载并安装apk" />
6179

62-
<Button
63-
android:id="@+id/installApk"
64-
android:layout_width="wrap_content"
65-
android:layout_height="wrap_content"
66-
android:layout_marginTop="10dp"
67-
android:clickable="true"
68-
android:text="下载并安装apk" />
80+
<Button
81+
android:id="@+id/handler_error"
82+
android:layout_width="wrap_content"
83+
android:layout_height="wrap_content"
84+
android:layout_marginTop="10dp"
85+
android:clickable="true"
86+
android:text="异常处理" />
6987

70-
<Button
71-
android:id="@+id/handler_error"
72-
android:layout_width="wrap_content"
73-
android:layout_height="wrap_content"
74-
android:layout_marginTop="10dp"
75-
android:clickable="true"
76-
android:text="异常处理" />
88+
<Button
89+
android:id="@+id/open_dialog"
90+
android:layout_width="wrap_content"
91+
android:layout_height="wrap_content"
92+
android:layout_marginTop="10dp"
93+
android:clickable="true"
94+
android:text="打开弹窗" />
95+
</LinearLayout>
96+
</androidx.core.widget.NestedScrollView>
7797

78-
<Button
79-
android:id="@+id/open_dialog"
80-
android:layout_width="wrap_content"
81-
android:layout_height="wrap_content"
82-
android:layout_marginTop="10dp"
83-
android:clickable="true"
84-
android:text="打开弹窗" />
8598
</LinearLayout>

mvvm/src/main/java/com/catchpig/mvvm/base/dialog/BaseVMDialogFragment.kt

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@ package com.catchpig.mvvm.base.dialog
33
import android.os.Bundle
44
import android.view.View
55
import androidx.annotation.CallSuper
6+
import androidx.annotation.StringRes
7+
import androidx.lifecycle.Lifecycle
68
import androidx.lifecycle.ViewModelProvider
9+
import androidx.lifecycle.lifecycleScope
10+
import androidx.lifecycle.repeatOnLifecycle
711
import androidx.viewbinding.ViewBinding
12+
import com.catchpig.mvvm.base.view.BaseVMView
813
import com.catchpig.mvvm.base.viewmodel.BaseViewModel
14+
import com.catchpig.mvvm.ksp.KotlinMvvmCompiler
15+
import kotlinx.coroutines.CoroutineScope
16+
import kotlinx.coroutines.Job
17+
import kotlinx.coroutines.launch
918
import java.lang.reflect.ParameterizedType
19+
import kotlin.coroutines.CoroutineContext
1020

1121
abstract class BaseVMDialogFragment<VB : ViewBinding, VM : BaseViewModel> :
12-
BaseDialogFragment<VB>() {
22+
BaseDialogFragment<VB>(), BaseVMView {
1323
protected val viewModel: VM by lazy {
1424
val type = javaClass.genericSuperclass
1525
val modelClass: Class<VM> = (type as ParameterizedType).actualTypeArguments[1] as Class<VM>
1626
ViewModelProvider(this, ViewModelProvider.NewInstanceFactory())[modelClass]
1727
}
28+
private var failedBinding: ViewBinding? = null
1829

1930
@CallSuper
2031
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
@@ -25,9 +36,61 @@ abstract class BaseVMDialogFragment<VB : ViewBinding, VM : BaseViewModel> :
2536
initFlow()
2637
}
2738

28-
abstract fun initParam()
39+
open fun bodyBinding(block: VB.() -> Unit) {
40+
bodyBinding.run(block)
41+
}
42+
43+
override fun getFailedBinding(): ViewBinding? {
44+
if (failedBinding == null) {
45+
failedBinding = KotlinMvvmCompiler.globalConfig().getFailedBinding(layoutInflater, this)
46+
}
47+
return failedBinding
48+
}
49+
50+
override fun launcherOnLifecycle(
51+
context: CoroutineContext,
52+
block: suspend CoroutineScope.() -> Unit
53+
): Job {
54+
return lifecycleScope.launch(context) {
55+
block()
56+
}
57+
}
58+
59+
override fun repeatLauncherOnLifecycle(
60+
context: CoroutineContext,
61+
state: Lifecycle.State,
62+
block: suspend CoroutineScope.() -> Unit
63+
): Job {
64+
return lifecycleScope.launch(context) {
65+
repeatOnLifecycle(state) {
66+
block()
67+
}
68+
}
69+
}
70+
71+
override fun showFailedView() {
72+
73+
}
74+
75+
override fun removeFailedView() {
76+
77+
}
78+
79+
override fun snackBar(text: CharSequence, gravity: Int) {
80+
}
81+
82+
override fun snackBar(@StringRes textRes: Int, gravity: Int) {
83+
}
84+
85+
override fun loadingDialog() {
2986

30-
abstract fun initView()
87+
}
88+
89+
override fun loadingView() {
90+
91+
}
92+
93+
override fun hideLoading() {
3194

32-
abstract fun initFlow()
95+
}
3396
}

0 commit comments

Comments
 (0)