From b50d509088d71bfb9664cbcaf4dc22271d87c92c Mon Sep 17 00:00:00 2001 From: Yash-Garg Date: Thu, 13 Oct 2022 17:50:13 +0530 Subject: [PATCH] fix: use bundle to pass torrent name instead of constructor arg --- .../dev/yashgarg/qbit/ui/dialogs/RenameTorrentDialog.kt | 8 +++++--- .../yashgarg/qbit/ui/torrent/TorrentDetailsFragment.kt | 7 +++++-- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/app/src/main/kotlin/dev/yashgarg/qbit/ui/dialogs/RenameTorrentDialog.kt b/app/src/main/kotlin/dev/yashgarg/qbit/ui/dialogs/RenameTorrentDialog.kt index cd302a4d..7c33fe3e 100644 --- a/app/src/main/kotlin/dev/yashgarg/qbit/ui/dialogs/RenameTorrentDialog.kt +++ b/app/src/main/kotlin/dev/yashgarg/qbit/ui/dialogs/RenameTorrentDialog.kt @@ -11,7 +11,7 @@ import com.google.android.material.textfield.TextInputEditText import com.google.android.material.textfield.TextInputLayout import dev.yashgarg.qbit.R -class RenameTorrentDialog(private val title: String) : DialogFragment() { +class RenameTorrentDialog : DialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { super.onCreateDialog(savedInstanceState) val alertDialogBuilder = MaterialAlertDialogBuilder(requireContext()) @@ -24,6 +24,7 @@ class RenameTorrentDialog(private val title: String) : DialogFragment() { setPositiveButton("Rename", null) } + val title = arguments?.getString(TORRENT_NAME_KEY) val dialog = alertDialogBuilder.create() dialog.window?.setSoftInputMode(5) @@ -31,7 +32,7 @@ class RenameTorrentDialog(private val title: String) : DialogFragment() { val nameTil = dialog.findViewById(R.id.torrentName_til) val nameTiet = dialog.findViewById(R.id.torrentName_tiet) nameTiet?.setText(title) - nameTiet?.setSelection(title.length) + nameTiet?.setSelection(title?.length ?: 0) dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener { if (!nameTiet?.text.isNullOrEmpty()) { @@ -50,8 +51,9 @@ class RenameTorrentDialog(private val title: String) : DialogFragment() { } companion object { - fun newInstance(title: String): RenameTorrentDialog = RenameTorrentDialog(title) + fun newInstance(): RenameTorrentDialog = RenameTorrentDialog() const val TAG = "RenameTorrentDialogFragment" + const val TORRENT_NAME_KEY = "torrent_name" const val RENAME_TORRENT_KEY = "rename_torrent" const val RENAME_KEY = "rename_fragment" } diff --git a/app/src/main/kotlin/dev/yashgarg/qbit/ui/torrent/TorrentDetailsFragment.kt b/app/src/main/kotlin/dev/yashgarg/qbit/ui/torrent/TorrentDetailsFragment.kt index ab82faad..c6d23a5d 100644 --- a/app/src/main/kotlin/dev/yashgarg/qbit/ui/torrent/TorrentDetailsFragment.kt +++ b/app/src/main/kotlin/dev/yashgarg/qbit/ui/torrent/TorrentDetailsFragment.kt @@ -3,6 +3,7 @@ package dev.yashgarg.qbit.ui.torrent import android.os.Bundle import android.view.View import android.widget.Toast +import androidx.core.os.bundleOf import androidx.fragment.app.Fragment import androidx.fragment.app.viewModels import androidx.lifecycle.flowWithLifecycle @@ -118,8 +119,10 @@ class TorrentDetailsFragment : Fragment(R.layout.torrent_details_fragment) { add("Rename") .setIcon(R.drawable.twotone_drive_file_rename_outline_24) .setOnMenuItemClickListener { - RenameTorrentDialog.newInstance(torrent.name) - .show(childFragmentManager, RenameTorrentDialog.TAG) + val dialog = RenameTorrentDialog.newInstance() + dialog.arguments = + bundleOf(RenameTorrentDialog.TORRENT_NAME_KEY to torrent.name) + dialog.show(childFragmentManager, RenameTorrentDialog.TAG) true } }