From a90d22f111cea64609994f24bb3a7e5956eae71a Mon Sep 17 00:00:00 2001 From: Isira Seneviratne Date: Fri, 20 Jan 2023 09:47:53 +0530 Subject: [PATCH] Use Fragment transaction extensions. --- .../libretube/helpers/NavigationHelper.kt | 16 ++++------- .../ui/activities/NoInternetActivity.kt | 9 ++++--- .../ui/activities/SettingsActivity.kt | 7 +++-- .../libretube/ui/adapters/CommentsAdapter.kt | 27 +++++++++---------- .../libretube/ui/fragments/PlayerFragment.kt | 7 ++--- .../libretube/ui/preferences/MainSettings.kt | 7 ++--- 6 files changed, 33 insertions(+), 40 deletions(-) diff --git a/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt b/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt index d8449d216..794ef2e3a 100644 --- a/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt +++ b/app/src/main/java/com/github/libretube/helpers/NavigationHelper.kt @@ -10,6 +10,8 @@ import android.os.Looper import androidx.appcompat.app.AppCompatActivity import androidx.core.os.bundleOf import androidx.core.os.postDelayed +import androidx.fragment.app.commitNow +import androidx.fragment.app.replace import com.github.libretube.R import com.github.libretube.constants.IntentData import com.github.libretube.constants.PreferenceKeys @@ -90,17 +92,9 @@ object NavigationHelper { ) val activity = context as AppCompatActivity - activity.supportFragmentManager.beginTransaction() - .remove(PlayerFragment()) - .commit() - activity.supportFragmentManager.beginTransaction() - .replace( - R.id.container, - PlayerFragment().apply { - arguments = bundle - } - ) - .commitNow() + activity.supportFragmentManager.commitNow { + replace(R.id.container, args = bundle) + } } fun navigatePlaylist( diff --git a/app/src/main/java/com/github/libretube/ui/activities/NoInternetActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/NoInternetActivity.kt index 5b3ce4be1..83a4580d5 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/NoInternetActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/NoInternetActivity.kt @@ -4,6 +4,7 @@ import android.content.Intent import android.os.Bundle import androidx.activity.addCallback import androidx.fragment.app.commit +import androidx.fragment.app.replace import com.github.libretube.R import com.github.libretube.databinding.ActivityNointernetBinding import com.github.libretube.helpers.NavigationHelper @@ -33,10 +34,10 @@ class NoInternetActivity : BaseActivity() { } binding.downloads.setOnClickListener { - supportFragmentManager.beginTransaction() - .replace(R.id.noInternet_container, DownloadsFragment()) - .addToBackStack(null) - .commit() + supportFragmentManager.commit { + replace(R.id.noInternet_container) + addToBackStack(null) + } } setContentView(binding.root) diff --git a/app/src/main/java/com/github/libretube/ui/activities/SettingsActivity.kt b/app/src/main/java/com/github/libretube/ui/activities/SettingsActivity.kt index 68d4605f4..d8eaa2e8f 100644 --- a/app/src/main/java/com/github/libretube/ui/activities/SettingsActivity.kt +++ b/app/src/main/java/com/github/libretube/ui/activities/SettingsActivity.kt @@ -24,10 +24,9 @@ class SettingsActivity : BaseActivity() { } if (savedInstanceState == null) { - supportFragmentManager - .beginTransaction() - .replace(R.id.settings, MainSettings()) - .commit() + supportFragmentManager.commit { + replace(R.id.settings) + } } // new way of dealing with back presses instead of onBackPressed() diff --git a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt index c06319679..15301c038 100644 --- a/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt +++ b/app/src/main/java/com/github/libretube/ui/adapters/CommentsAdapter.kt @@ -1,17 +1,19 @@ package com.github.libretube.ui.adapters import android.annotation.SuppressLint -import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.view.ViewGroup.MarginLayoutParams import android.widget.Toast import androidx.appcompat.content.res.AppCompatResources +import androidx.core.os.bundleOf import androidx.core.text.parseAsHtml import androidx.core.view.updateLayoutParams import androidx.core.view.updatePadding import androidx.fragment.app.Fragment +import androidx.fragment.app.commit +import androidx.fragment.app.replace import androidx.recyclerview.widget.RecyclerView import com.github.libretube.R import com.github.libretube.api.JsonHelper @@ -26,6 +28,7 @@ import com.github.libretube.helpers.ThemeHelper import com.github.libretube.ui.fragments.CommentsRepliesFragment import com.github.libretube.ui.viewholders.CommentsViewHolder import com.github.libretube.util.TextUtils +import kotlinx.serialization.encodeToString class CommentsAdapter( private val fragment: Fragment?, @@ -94,21 +97,15 @@ class CommentsAdapter( } if (!isRepliesAdapter && comment.repliesPage != null) { - val repliesFragment = CommentsRepliesFragment().apply { - arguments = Bundle().apply { - putString(IntentData.videoId, videoId) - putString( - IntentData.comment, - JsonHelper.json.encodeToString(Comment.serializer(), comment) - ) - } - } root.setOnClickListener { - fragment!!.parentFragmentManager - .beginTransaction() - .replace(R.id.commentFragContainer, repliesFragment) - .addToBackStack(null) - .commit() + val args = bundleOf( + IntentData.videoId to videoId, + IntentData.comment to JsonHelper.json.encodeToString(comment) + ) + fragment!!.parentFragmentManager.commit { + replace(R.id.commentFragContainer, args = args) + addToBackStack(null) + } } } diff --git a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt index 33514024a..e79c92976 100644 --- a/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt +++ b/app/src/main/java/com/github/libretube/ui/fragments/PlayerFragment.kt @@ -35,6 +35,7 @@ import androidx.core.os.postDelayed import androidx.core.text.parseAsHtml import androidx.core.view.isVisible import androidx.fragment.app.activityViewModels +import androidx.fragment.app.commit import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager @@ -1510,9 +1511,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions { private fun killPlayerFragment() { viewModel.isFullscreen.value = false binding.playerMotionLayout.transitionToEnd() - mainActivity.supportFragmentManager.beginTransaction() - .remove(this) - .commit() + mainActivity.supportFragmentManager.commit { + remove(this@PlayerFragment) + } onDestroy() } diff --git a/app/src/main/java/com/github/libretube/ui/preferences/MainSettings.kt b/app/src/main/java/com/github/libretube/ui/preferences/MainSettings.kt index a9ff2410c..12bdfee16 100644 --- a/app/src/main/java/com/github/libretube/ui/preferences/MainSettings.kt +++ b/app/src/main/java/com/github/libretube/ui/preferences/MainSettings.kt @@ -4,6 +4,7 @@ import android.os.Bundle import android.widget.Toast import androidx.annotation.StringRes import androidx.fragment.app.Fragment +import androidx.fragment.app.commitNow import androidx.preference.Preference import com.github.libretube.BuildConfig import com.github.libretube.R @@ -124,9 +125,9 @@ class MainSettings : BasePreferenceFragment() { } private fun navigateToSettingsFragment(newFragment: Fragment): Boolean { - parentFragmentManager.beginTransaction() - .replace(R.id.settings, newFragment) - .commitNow() + parentFragmentManager.commitNow { + replace(R.id.settings, newFragment) + } return true } }