@@ -25,6 +25,7 @@ import androidx.activity.result.contract.ActivityResultContracts
2525import androidx.appcompat.app.AlertDialog
2626import androidx.core.content.ContextCompat
2727import androidx.core.graphics.drawable.toDrawable
28+ import androidx.lifecycle.lifecycleScope
2829import com.google.android.material.bottomsheet.BottomSheetBehavior
2930import com.google.android.material.bottomsheet.BottomSheetDialog
3031import com.google.android.material.bottomsheet.BottomSheetDialogFragment
@@ -262,7 +263,7 @@ class AddResourceFragment : BottomSheetDialogFragment() {
262263 startActivity(Intent (activity, AddResourceActivity ::class .java).putExtra(" resource_local_url" , path))
263264 } else {
264265 val userModel = userProfileDbHandler.userModel ? : return
265- showAlert(requireContext(), path, myPersonalRepository, userModel.id, userModel.name) {
266+ showAlert(requireContext(), path, myPersonalRepository, userModel.id, userModel.name, viewLifecycleOwner.lifecycleScope ) {
266267 dismiss()
267268 }
268269 }
@@ -278,29 +279,41 @@ class AddResourceFragment : BottomSheetDialogFragment() {
278279 repository : MyPersonalRepository ,
279280 userId : String? ,
280281 userName : String? ,
281- onDismiss : (() -> Unit )? = null
282+ scope : CoroutineScope ,
283+ onDismiss : () -> Unit
282284 ) {
283285 val v = LayoutInflater .from(context).inflate(R .layout.alert_my_personal, null )
284286 val etTitle = v.findViewById<EditText >(R .id.et_title)
285287 val etDesc = v.findViewById<EditText >(R .id.et_description)
286- AlertDialog .Builder (context, R .style.AlertDialogTheme )
288+ val dialog = AlertDialog .Builder (context, R .style.AlertDialogTheme )
287289 .setTitle(R .string.enter_resource_detail)
288290 .setView(v)
289- .setPositiveButton(" Save" ) { _: DialogInterface ? , _: Int ->
291+ .setPositiveButton(R .string.save, null )
292+ .setNegativeButton(R .string.dismiss, null )
293+ .create()
294+
295+ dialog.setOnShowListener {
296+ val positiveButton = dialog.getButton(AlertDialog .BUTTON_POSITIVE )
297+ positiveButton.setOnClickListener {
290298 val title = etTitle.text.toString().trim { it <= ' ' }
291299 if (title.isEmpty()) {
292300 Utilities .toast(context, context.getString(R .string.title_is_required))
293- return @setPositiveButton
301+ return @setOnClickListener
294302 }
295303 val desc = etDesc.text.toString().trim { it <= ' ' }
296- CoroutineScope (Dispatchers .IO ).launch {
304+ positiveButton.isEnabled = false
305+ scope.launch(Dispatchers .IO ) {
297306 repository.savePersonalResource(title, userId, userName, path, desc)
298307 withContext(Dispatchers .Main ) {
299308 Utilities .toast(context, context.getString(R .string.resource_saved_to_my_personal))
300- onDismiss?.invoke()
309+ positiveButton.isEnabled = true
310+ dialog.dismiss()
311+ onDismiss.invoke()
301312 }
302313 }
303- }.setNegativeButton(R .string.dismiss, null ).show()
314+ }
315+ }
316+ dialog.show()
304317 }
305318 }
306319}
0 commit comments