From 1881676538a022cdd83e63fe3321c1ab73bc6faa Mon Sep 17 00:00:00 2001 From: parneet-guraya Date: Thu, 29 Feb 2024 17:08:29 +0530 Subject: [PATCH] use default constructor for fragment Signed-off-by: parneet-guraya --- .../com/nextcloud/talk/chat/ChatActivity.kt | 16 ++++---- .../dialog/FileAttachmentPreviewFragment.kt | 38 ++++++++++++++----- 2 files changed, 37 insertions(+), 17 deletions(-) diff --git a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt index 6233b144f..92dd22c1c 100644 --- a/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt +++ b/app/src/main/java/com/nextcloud/talk/chat/ChatActivity.kt @@ -2976,11 +2976,13 @@ class ChatActivity : filenamesWithLineBreaks.append(filename).append("\n") } - val newFragment: DialogFragment = FileAttachmentPreviewFragment.newInstance( + val newFragment = FileAttachmentPreviewFragment.newInstance( filenamesWithLineBreaks.toString(), - filesToUpload, - this::uploadFiles + filesToUpload ) + newFragment.setListener { files, caption -> + uploadFiles(files, caption) + } newFragment.show(supportFragmentManager, FileAttachmentPreviewFragment.TAG) } catch (e: IllegalStateException) { context.resources?.getString(R.string.nc_upload_failed)?.let { @@ -3050,11 +3052,11 @@ class ChatActivity : filenamesWithLineBreaks.append(filename).append("\n") } - val newFragment: DialogFragment = FileAttachmentPreviewFragment.newInstance( + val newFragment = FileAttachmentPreviewFragment.newInstance( filenamesWithLineBreaks.toString(), - filesToUpload, - this::uploadFiles + filesToUpload ) + newFragment.setListener { files, caption -> uploadFiles(files, caption) } newFragment.show(supportFragmentManager, FileAttachmentPreviewFragment.TAG) } else { UploadAndShareFilesWorker.requestStoragePermission(this) @@ -4383,7 +4385,7 @@ class ChatActivity : val lon = data["longitude"]!! metaData = "{\"type\":\"geo-location\",\"id\":\"geo:$lat,$lon\",\"latitude\":\"$lat\"," + - "\"longitude\":\"$lon\",\"name\":\"$name\"}" + "\"longitude\":\"$lon\",\"name\":\"$name\"}" } when (type) { diff --git a/app/src/main/java/com/nextcloud/talk/ui/dialog/FileAttachmentPreviewFragment.kt b/app/src/main/java/com/nextcloud/talk/ui/dialog/FileAttachmentPreviewFragment.kt index 01ce6dd79..e01db5379 100644 --- a/app/src/main/java/com/nextcloud/talk/ui/dialog/FileAttachmentPreviewFragment.kt +++ b/app/src/main/java/com/nextcloud/talk/ui/dialog/FileAttachmentPreviewFragment.kt @@ -36,14 +36,10 @@ import com.nextcloud.talk.utils.permissions.PlatformPermissionUtil import javax.inject.Inject @AutoInjector(NextcloudTalkApplication::class) -class FileAttachmentPreviewFragment( - filenames: String, - filesToUpload: MutableList, - functionToCall: (files: MutableList, caption: String) -> Unit -) : DialogFragment() { - private val files = filenames - private val filesList = filesToUpload - private val uploadFiles = functionToCall +class FileAttachmentPreviewFragment : DialogFragment() { + private lateinit var files: String + private lateinit var filesList: ArrayList + private var uploadFiles: (files: MutableList, caption: String) -> Unit = { _, _ -> } lateinit var binding: DialogFileAttachmentPreviewBinding @Inject @@ -51,7 +47,18 @@ class FileAttachmentPreviewFragment( @Inject lateinit var viewThemeUtils: ViewThemeUtils + + fun setListener(uploadFiles: (files: MutableList, caption: String) -> Unit) { + this.uploadFiles = uploadFiles + } + override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { + + arguments?.let { + files = it.getString(FILE_NAMES_ARG, "") + filesList = it.getStringArrayList(FILES_TO_UPLOAD_ARG)!! + } + binding = DialogFileAttachmentPreviewBinding.inflate(LayoutInflater.from(context)) return MaterialAlertDialogBuilder(requireContext()).setView(binding.root).create() } @@ -88,12 +95,23 @@ class FileAttachmentPreviewFragment( } companion object { + + private const val FILE_NAMES_ARG = "FILE_NAMES_ARG" + private const val FILES_TO_UPLOAD_ARG = "FILES_TO_UPLOAD_ARG" + @JvmStatic fun newInstance( filenames: String, filesToUpload: MutableList, - functionToCall: (files: MutableList, caption: String) -> Unit - ) = FileAttachmentPreviewFragment(filenames, filesToUpload, functionToCall) + ): FileAttachmentPreviewFragment { + val fileAttachmentFragment = FileAttachmentPreviewFragment() + val args = Bundle() + args.putString(FILE_NAMES_ARG, filenames) + args.putStringArrayList(FILES_TO_UPLOAD_ARG, ArrayList(filesToUpload)) + fileAttachmentFragment.arguments = args + return fileAttachmentFragment + } + val TAG: String = FilterConversationFragment::class.java.simpleName } }