@@ -23,35 +23,43 @@ package com.owncloud.android.ui.dialog
23
23
import android.app.Dialog
24
24
import android.os.Build
25
25
import android.os.Bundle
26
+ import android.os.Parcelable
26
27
import android.view.View
27
28
import androidx.annotation.RequiresApi
28
29
import androidx.appcompat.app.AlertDialog
30
+ import androidx.core.os.bundleOf
29
31
import androidx.fragment.app.DialogFragment
30
32
import com.google.android.material.dialog.MaterialAlertDialogBuilder
31
33
import com.nextcloud.client.di.Injectable
32
34
import com.owncloud.android.R
33
35
import com.owncloud.android.databinding.StoragePermissionDialogBinding
34
- import com.owncloud.android.ui.dialog.StoragePermissionDialogFragment.Listener
35
36
import com.owncloud.android.utils.theme.ViewThemeUtils
37
+ import kotlinx.parcelize.Parcelize
36
38
import javax.inject.Inject
37
39
38
40
/* *
39
41
* Dialog that shows permission options in SDK >= 30
40
42
*
41
43
* Allows choosing "full access" (MANAGE_ALL_FILES) or "read-only media" (READ_EXTERNAL_STORAGE)
42
- *
43
- * @param listener a [Listener] for button clicks. The dialog will auto-dismiss after the callback is called.
44
- * @param permissionRequired Whether the permission is absolutely required by the calling component.
45
- * This changes the texts to a more strict version.
46
44
*/
47
45
@RequiresApi(Build .VERSION_CODES .R )
48
- class StoragePermissionDialogFragment ( val listener : Listener , val permissionRequired : Boolean = false ) :
46
+ class StoragePermissionDialogFragment :
49
47
DialogFragment (), Injectable {
48
+
50
49
private lateinit var binding: StoragePermissionDialogBinding
51
50
51
+ private var permissionRequired = false
52
+
52
53
@Inject
53
54
lateinit var viewThemeUtils: ViewThemeUtils
54
55
56
+ override fun onCreate (savedInstanceState : Bundle ? ) {
57
+ super .onCreate(savedInstanceState)
58
+ arguments?.let {
59
+ permissionRequired = it.getBoolean(ARG_PERMISSION_REQUIRED , false )
60
+ }
61
+ }
62
+
55
63
override fun onStart () {
56
64
super .onStart()
57
65
dialog?.let {
@@ -75,12 +83,12 @@ class StoragePermissionDialogFragment(val listener: Listener, val permissionRequ
75
83
// Setup layout
76
84
viewThemeUtils.material.colorMaterialButtonPrimaryFilled(binding.btnFullAccess)
77
85
binding.btnFullAccess.setOnClickListener {
78
- listener.onClickFullAccess( )
86
+ setResult( Result . FULL_ACCESS )
79
87
dismiss()
80
88
}
81
89
viewThemeUtils.platform.colorTextButtons(binding.btnReadOnly)
82
90
binding.btnReadOnly.setOnClickListener {
83
- listener.onClickMediaReadOnly( )
91
+ setResult( Result . MEDIA_READ_ONLY )
84
92
dismiss()
85
93
}
86
94
@@ -94,7 +102,7 @@ class StoragePermissionDialogFragment(val listener: Listener, val permissionRequ
94
102
.setTitle(titleResource)
95
103
.setView(view)
96
104
.setNegativeButton(R .string.common_cancel) { _, _ ->
97
- listener.onCancel( )
105
+ setResult( Result . CANCEL )
98
106
dismiss()
99
107
}
100
108
@@ -103,9 +111,28 @@ class StoragePermissionDialogFragment(val listener: Listener, val permissionRequ
103
111
return builder.create()
104
112
}
105
113
106
- interface Listener {
107
- fun onCancel ()
108
- fun onClickFullAccess ()
109
- fun onClickMediaReadOnly ()
114
+ private fun setResult (result : Result ) {
115
+ parentFragmentManager.setFragmentResult(REQUEST_KEY , bundleOf(RESULT_KEY to result))
116
+ }
117
+
118
+ @Parcelize
119
+ enum class Result : Parcelable {
120
+ CANCEL , FULL_ACCESS , MEDIA_READ_ONLY
121
+ }
122
+
123
+ companion object {
124
+ private const val ARG_PERMISSION_REQUIRED = " ARG_PERMISSION_REQUIRED"
125
+ const val REQUEST_KEY = " REQUEST_KEY_STORAGE_PERMISSION"
126
+ const val RESULT_KEY = " RESULT"
127
+
128
+ /* *
129
+ * @param permissionRequired Whether the permission is absolutely required by the calling component.
130
+ * This changes the texts to a more strict version.
131
+ */
132
+ fun newInstance (permissionRequired : Boolean ): StoragePermissionDialogFragment {
133
+ return StoragePermissionDialogFragment ().apply {
134
+ arguments = bundleOf(ARG_PERMISSION_REQUIRED to permissionRequired)
135
+ }
136
+ }
110
137
}
111
138
}
0 commit comments