Use new fragment result API
This commit is contained in:
@@ -7,8 +7,10 @@ import android.os.Bundle
|
||||
import android.os.Parcelable
|
||||
import androidx.appcompat.app.AlertDialog
|
||||
import androidx.appcompat.app.AppCompatDialogFragment
|
||||
import androidx.core.os.bundleOf
|
||||
import androidx.fragment.app.Fragment
|
||||
import be.mygod.vpnhotspot.util.showAllowingStateLoss
|
||||
import androidx.fragment.app.setFragmentResult
|
||||
import androidx.fragment.app.setFragmentResultListener
|
||||
import kotlinx.android.parcel.Parcelize
|
||||
|
||||
/**
|
||||
@@ -17,33 +19,44 @@ import kotlinx.android.parcel.Parcelize
|
||||
abstract class AlertDialogFragment<Arg : Parcelable, Ret : Parcelable> :
|
||||
AppCompatDialogFragment(), DialogInterface.OnClickListener {
|
||||
companion object {
|
||||
private const val KEY_RESULT = "result"
|
||||
private const val KEY_ARG = "arg"
|
||||
private const val KEY_RET = "ret"
|
||||
fun <T : Parcelable> getRet(data: Intent) = data.getParcelableExtra<T>(KEY_RET)!!
|
||||
private const val KEY_WHICH = "which"
|
||||
|
||||
fun <Ret : Parcelable> setResultListener(fragment: Fragment, requestKey: String,
|
||||
listener: (Int, Ret?) -> Unit) {
|
||||
fragment.setFragmentResultListener(requestKey) { _, bundle ->
|
||||
listener(bundle.getInt(KEY_WHICH, Activity.RESULT_CANCELED), bundle.getParcelable(KEY_RET))
|
||||
}
|
||||
}
|
||||
inline fun <reified T : AlertDialogFragment<*, Ret>, Ret : Parcelable> setResultListener(
|
||||
fragment: Fragment, noinline listener: (Int, Ret?) -> Unit) =
|
||||
setResultListener(fragment, T::class.java.name, listener)
|
||||
}
|
||||
protected abstract fun AlertDialog.Builder.prepare(listener: DialogInterface.OnClickListener)
|
||||
|
||||
private val resultKey get() = requireArguments().getString(KEY_RESULT)
|
||||
protected val arg by lazy { requireArguments().getParcelable<Arg>(KEY_ARG)!! }
|
||||
protected open val ret: Ret? get() = null
|
||||
fun withArg(arg: Arg) = apply { arguments = Bundle().apply { putParcelable(KEY_ARG, arg) } }
|
||||
|
||||
private fun args() = arguments ?: Bundle().also { arguments = it }
|
||||
fun arg(arg: Arg) = args().putParcelable(KEY_ARG, arg)
|
||||
fun key(resultKey: String = javaClass.name) = args().putString(KEY_RESULT, resultKey)
|
||||
|
||||
override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog =
|
||||
AlertDialog.Builder(requireContext()).also { it.prepare(this) }.create()
|
||||
|
||||
override fun onClick(dialog: DialogInterface?, which: Int) {
|
||||
targetFragment?.onActivityResult(targetRequestCode, which, ret?.let {
|
||||
Intent().replaceExtras(Bundle().apply { putParcelable(KEY_RET, it) })
|
||||
setFragmentResult(resultKey ?: return, Bundle().apply {
|
||||
putInt(KEY_WHICH, which)
|
||||
putParcelable(KEY_RET, ret ?: return@apply)
|
||||
})
|
||||
}
|
||||
|
||||
override fun onDismiss(dialog: DialogInterface) {
|
||||
super.onDismiss(dialog)
|
||||
targetFragment?.onActivityResult(targetRequestCode, Activity.RESULT_CANCELED, null)
|
||||
}
|
||||
|
||||
fun show(target: Fragment, requestCode: Int = 0, tag: String = javaClass.simpleName) {
|
||||
setTargetFragment(target, requestCode)
|
||||
showAllowingStateLoss(target.parentFragmentManager, tag)
|
||||
setFragmentResult(resultKey ?: return, bundleOf(KEY_WHICH to Activity.RESULT_CANCELED))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user