Skip to content

Commit 6cc5af4

Browse files
committed
Fix save request code for dialogfragment
1 parent b59221c commit 6cc5af4

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

core/src/main/java/com/omega_r/base/components/OmegaBottomSheetDialogFragment.kt

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import java.io.Serializable
2626

2727
private const val KEY_SAVE_RESULT = "omegaSaveResult"
2828
private const val KEY_SAVE_DATA = "omegaSaveData"
29+
private const val KEY_SAVE_REQUEST_CODE = "omegaRequestCode"
2930

3031
abstract class OmegaBottomSheetDialogFragment : MvpBottomSheetDialogFragment(), OmegaComponent {
3132

@@ -39,13 +40,16 @@ abstract class OmegaBottomSheetDialogFragment : MvpBottomSheetDialogFragment(),
3940

4041
private var result: Boolean = false
4142
private var data: Serializable? = null
43+
private var requestCode: Int = 0
44+
4245

4346
override fun <T : View> findViewById(id: Int): T? = view?.findViewById(id)
4447

4548
override fun onCreate(savedInstanceState: Bundle?) {
4649
super.onCreate(savedInstanceState)
4750
result = savedInstanceState?.getBoolean(KEY_SAVE_RESULT, result) ?: result
4851
data = savedInstanceState?.getSerializable(KEY_SAVE_DATA) ?: data
52+
requestCode = savedInstanceState?.getInt(KEY_SAVE_REQUEST_CODE, targetRequestCode) ?: targetRequestCode
4953

5054
setHasOptionsMenu(this::class.findAnnotation<OmegaMenu>() != null)
5155

@@ -225,12 +229,9 @@ abstract class OmegaBottomSheetDialogFragment : MvpBottomSheetDialogFragment(),
225229

226230
override fun onDismiss(dialog: DialogInterface) {
227231
super.onDismiss(dialog)
228-
if (isResumed) {
229-
val requestCode = targetRequestCode
230-
if (requestCode != 0) {
231-
val omegaComponent = (parentFragment as? OmegaComponent) ?: (activity as? OmegaComponent)
232-
omegaComponent?.presenter?.onLaunchResult(requestCode, result, data)
233-
}
232+
if (isResumed && requestCode != 0) {
233+
val omegaComponent = (parentFragment as? OmegaComponent) ?: (activity as? OmegaComponent)
234+
omegaComponent?.presenter?.onLaunchResult(requestCode, result, data)
234235
}
235236
}
236237

core/src/main/java/com/omega_r/base/components/OmegaDialogFragment.kt

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@ import java.io.Serializable
2828
* Created by Anton Knyazev on 04.04.2019.
2929
*/
3030

31-
private const val KEY_SAVE_RESULT = "omegaSaveResult"
32-
private const val KEY_SAVE_DATA = "omegaSaveData"
31+
private const val KEY_SAVE_RESULT = "omegaSaveResult"
32+
private const val KEY_SAVE_DATA = "omegaSaveData"
33+
private const val KEY_SAVE_REQUEST_CODE = "omegaRequestCode"
3334

3435
abstract class OmegaDialogFragment : MvpAppCompatDialogFragment(), OmegaComponent {
3536

@@ -43,13 +44,15 @@ abstract class OmegaDialogFragment : MvpAppCompatDialogFragment(), OmegaComponen
4344

4445
private var result: Boolean = false
4546
private var data: Serializable? = null
47+
private var requestCode: Int = 0
4648

4749
override fun <T : View> findViewById(id: Int): T? = view?.findViewById(id)
4850

4951
override fun onCreate(savedInstanceState: Bundle?) {
5052
super.onCreate(savedInstanceState)
5153
result = savedInstanceState?.getBoolean(KEY_SAVE_RESULT, result) ?: result
5254
data = savedInstanceState?.getSerializable(KEY_SAVE_DATA) ?: data
55+
requestCode = savedInstanceState?.getInt(KEY_SAVE_REQUEST_CODE, targetRequestCode) ?: targetRequestCode
5356

5457
setHasOptionsMenu(this::class.findAnnotation<OmegaMenu>() != null)
5558
this::class.findAnnotation<OmegaClickViews>()?.let {
@@ -86,6 +89,7 @@ abstract class OmegaDialogFragment : MvpAppCompatDialogFragment(), OmegaComponen
8689
detachChildPresenter()
8790
outState.putBoolean(KEY_SAVE_RESULT, result)
8891
outState.putSerializable(KEY_SAVE_RESULT, data)
92+
outState.putInt(KEY_SAVE_REQUEST_CODE, requestCode)
8993
}
9094

9195
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
@@ -256,12 +260,9 @@ abstract class OmegaDialogFragment : MvpAppCompatDialogFragment(), OmegaComponen
256260

257261
override fun onDismiss(dialog: DialogInterface) {
258262
super.onDismiss(dialog)
259-
if (isResumed) {
260-
val requestCode = targetRequestCode
261-
if (requestCode != 0) {
262-
val omegaComponent = (parentFragment as? OmegaComponent) ?: (activity as? OmegaComponent)
263-
omegaComponent?.presenter?.onLaunchResult(requestCode, result, data)
264-
}
263+
if (isResumed && requestCode != 0) {
264+
val omegaComponent = (parentFragment as? OmegaComponent) ?: (activity as? OmegaComponent)
265+
omegaComponent?.presenter?.onLaunchResult(requestCode, result, data)
265266
}
266267
}
267268

@@ -287,7 +288,7 @@ abstract class OmegaDialogFragment : MvpAppCompatDialogFragment(), OmegaComponen
287288

288289
final override fun <T : View> bind(@IdRes res: Int, initBlock: T.() -> Unit) = super.bind(res, initBlock)
289290

290-
final override fun <T : View> bind(@IdRes vararg ids: Int, initBlock: T.() -> Unit)=
291+
final override fun <T : View> bind(@IdRes vararg ids: Int, initBlock: T.() -> Unit) =
291292
super.bind(ids = *ids, initBlock = initBlock)
292293

293294
final override fun <T : RecyclerView> bind(res: Int, adapter: RecyclerView.Adapter<*>, initBlock: T.() -> Unit) =

0 commit comments

Comments
 (0)