That fuck shit the fascists are using
1package org.tm.archive.components
2
3import android.content.DialogInterface
4import android.os.Bundle
5import android.view.View
6import androidx.activity.OnBackPressedCallback
7import androidx.fragment.app.DialogFragment
8import androidx.fragment.app.Fragment
9import org.tm.archive.R
10import org.tm.archive.util.fragments.findListener
11
12/**
13 * Convenience class for wrapping Fragments in full-screen dialogs. Due to how fragments work, they
14 * must be public static classes. Therefore, this class should be subclassed as its own entity, rather
15 * than via `object : WrapperDialogFragment`.
16 *
17 * Example usage:
18 *
19 * ```
20 * class Dialog : WrapperDialogFragment() {
21 * override fun getWrappedFragment(): Fragment {
22 * return NavHostFragment.create(R.navigation.private_story_settings, requireArguments())
23 * }
24 * }
25 *
26 * companion object {
27 * fun createAsDialog(distributionListId: DistributionListId): DialogFragment {
28 * return Dialog().apply {
29 * arguments = PrivateStorySettingsFragmentArgs.Builder(distributionListId).build().toBundle()
30 * }
31 * }
32 * }
33 * ```
34 */
35abstract class WrapperDialogFragment : DialogFragment(R.layout.fragment_container) {
36
37 override fun onCreate(savedInstanceState: Bundle?) {
38 super.onCreate(savedInstanceState)
39
40 setStyle(STYLE_NO_FRAME, R.style.Signal_DayNight_Dialog_FullScreen)
41 }
42
43 override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
44 requireActivity().onBackPressedDispatcher.addCallback(
45 viewLifecycleOwner,
46 object : OnBackPressedCallback(true) {
47 override fun handleOnBackPressed() {
48 onHandleBackPressed()
49 }
50 }
51 )
52
53 if (savedInstanceState == null) {
54 childFragmentManager.beginTransaction()
55 .replace(R.id.fragment_container, getWrappedFragment())
56 .commitAllowingStateLoss()
57 }
58 }
59
60 open fun onHandleBackPressed() {
61 dismissAllowingStateLoss()
62 }
63
64 override fun onDismiss(dialog: DialogInterface) {
65 super.onDismiss(dialog)
66 findListener<WrapperDialogFragmentCallback>()?.onWrapperDialogFragmentDismissed()
67 }
68
69 abstract fun getWrappedFragment(): Fragment
70
71 interface WrapperDialogFragmentCallback {
72 fun onWrapperDialogFragmentDismissed()
73 }
74}