@@ -4,7 +4,6 @@ import android.annotation.SuppressLint
44import android.app.Dialog
55import android.content.Context
66import android.content.DialogInterface
7- import android.os.Build
87import android.os.Bundle
98import android.text.TextUtils
109import android.view.KeyEvent
@@ -13,26 +12,34 @@ import android.widget.TextView
1312import androidx.fragment.app.DialogFragment
1413import androidx.fragment.app.FragmentManager
1514import com.bhm.network.R
15+ import com.bhm.network.base.HttpLoadingDialog.Companion.KEY_DIALOG_CANCELABLE
16+ import com.bhm.network.base.HttpLoadingDialog.Companion.KEY_DIALOG_DIALOG_DISMISS_INTERRUPT_REQUEST
17+ import com.bhm.network.base.HttpLoadingDialog.Companion.KEY_DIALOG_LOADING_TITLE
18+ import com.bhm.network.base.HttpLoadingDialog.Companion.KEY_JOB_ID
1619import com.bhm.network.core.HttpOptions
1720import com.bhm.network.core.JobManager
18- import java.util.*
21+ import java.util.Objects
1922
2023open class HttpLoadingFragment : DialogFragment () {
2124
2225 private var textView: TextView ? = null
2326
2427 private var cancelDialogEvent: (() -> Unit )? = null
2528
26- var builder: HttpOptions ? = null
29+ var dialogCancelable: Boolean = false
30+
31+ var isDialogDismissInterruptRequest: Boolean = false
32+
33+ var jobKey: String = " "
34+
35+ var loadingTitle: String = " "
2736
2837 override fun onAttach (context : Context ) {
2938 super .onAttach(context)
30- builder = if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
31- arguments?.getSerializable(" httpOptions" , HttpOptions ::class .java)
32- } else {
33- @Suppress(" DEPRECATION" )
34- arguments?.getSerializable(" httpOptions" ) as HttpOptions ?
35- }
39+ dialogCancelable = arguments?.getBoolean(KEY_DIALOG_CANCELABLE )? : false
40+ isDialogDismissInterruptRequest = arguments?.getBoolean(KEY_DIALOG_DIALOG_DISMISS_INTERRUPT_REQUEST )? : false
41+ jobKey = arguments?.getString(KEY_JOB_ID )? : " "
42+ loadingTitle = arguments?.getString(KEY_DIALOG_LOADING_TITLE )? : " "
3643 }
3744
3845 override fun show (manager : FragmentManager , tag : String? ) {
@@ -88,14 +95,14 @@ open class HttpLoadingFragment : DialogFragment() {
8895 if (activity != null ) {
8996 dialog.setOwnerActivity(requireActivity())
9097 dialog.setCanceledOnTouchOutside(false ) // 这个值最好设置成false,点击其他区域关闭loading,体验效果不佳
91- dialog.setCancelable(builder?.isCancelable ? : false )
98+ dialog.setCancelable(dialogCancelable )
9299 dialog.setOnKeyListener(DialogInterface .OnKeyListener { _, i, keyEvent ->
93100 if (i == KeyEvent .KEYCODE_BACK && dialog.isShowing
94101 && keyEvent.action == KeyEvent .ACTION_UP
95102 ) {
96- if (builder?.isCancelable == true ) {
97- if (builder?. isDialogDismissInterruptRequest == true ) {
98- JobManager .get().removeJob(builder?. jobKey? : " " )
103+ if (dialogCancelable ) {
104+ if (isDialogDismissInterruptRequest) {
105+ JobManager .get().removeJob(jobKey)
99106 }
100107 cancelDialogEvent?.invoke()
101108 if (cancelDialogEvent == null ) {
@@ -106,7 +113,7 @@ open class HttpLoadingFragment : DialogFragment() {
106113 if (System .currentTimeMillis() - onBackPressed > 1000 ) {
107114 onBackPressed = System .currentTimeMillis()
108115 } else {
109- JobManager .get().removeJob(builder?. jobKey? : " " )
116+ JobManager .get().removeJob(jobKey)
110117 cancelDialogEvent?.invoke()
111118 if (cancelDialogEvent == null ) {
112119 dismiss()
@@ -132,8 +139,8 @@ open class HttpLoadingFragment : DialogFragment() {
132139 ) // 设置布局
133140 textView = it.findViewById(R .id.dialog_text_loading)
134141 }
135- if (! TextUtils .isEmpty(builder?. loadingTitle)) {
136- textView?.text = builder?. loadingTitle
142+ if (! TextUtils .isEmpty(loadingTitle)) {
143+ textView?.text = loadingTitle
137144 }
138145 return dialog
139146 }
0 commit comments