Use Fragment transaction extensions.

This commit is contained in:
Isira Seneviratne 2023-01-20 09:47:53 +05:30
parent c05734d0fe
commit a90d22f111
6 changed files with 33 additions and 40 deletions

View File

@ -10,6 +10,8 @@ import android.os.Looper
import androidx.appcompat.app.AppCompatActivity import androidx.appcompat.app.AppCompatActivity
import androidx.core.os.bundleOf import androidx.core.os.bundleOf
import androidx.core.os.postDelayed import androidx.core.os.postDelayed
import androidx.fragment.app.commitNow
import androidx.fragment.app.replace
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.constants.IntentData import com.github.libretube.constants.IntentData
import com.github.libretube.constants.PreferenceKeys import com.github.libretube.constants.PreferenceKeys
@ -90,17 +92,9 @@ object NavigationHelper {
) )
val activity = context as AppCompatActivity val activity = context as AppCompatActivity
activity.supportFragmentManager.beginTransaction() activity.supportFragmentManager.commitNow {
.remove(PlayerFragment()) replace<PlayerFragment>(R.id.container, args = bundle)
.commit() }
activity.supportFragmentManager.beginTransaction()
.replace(
R.id.container,
PlayerFragment().apply {
arguments = bundle
}
)
.commitNow()
} }
fun navigatePlaylist( fun navigatePlaylist(

View File

@ -4,6 +4,7 @@ import android.content.Intent
import android.os.Bundle import android.os.Bundle
import androidx.activity.addCallback import androidx.activity.addCallback
import androidx.fragment.app.commit import androidx.fragment.app.commit
import androidx.fragment.app.replace
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.databinding.ActivityNointernetBinding import com.github.libretube.databinding.ActivityNointernetBinding
import com.github.libretube.helpers.NavigationHelper import com.github.libretube.helpers.NavigationHelper
@ -33,10 +34,10 @@ class NoInternetActivity : BaseActivity() {
} }
binding.downloads.setOnClickListener { binding.downloads.setOnClickListener {
supportFragmentManager.beginTransaction() supportFragmentManager.commit {
.replace(R.id.noInternet_container, DownloadsFragment()) replace<DownloadsFragment>(R.id.noInternet_container)
.addToBackStack(null) addToBackStack(null)
.commit() }
} }
setContentView(binding.root) setContentView(binding.root)

View File

@ -24,10 +24,9 @@ class SettingsActivity : BaseActivity() {
} }
if (savedInstanceState == null) { if (savedInstanceState == null) {
supportFragmentManager supportFragmentManager.commit {
.beginTransaction() replace<MainSettings>(R.id.settings)
.replace(R.id.settings, MainSettings()) }
.commit()
} }
// new way of dealing with back presses instead of onBackPressed() // new way of dealing with back presses instead of onBackPressed()

View File

@ -1,17 +1,19 @@
package com.github.libretube.ui.adapters package com.github.libretube.ui.adapters
import android.annotation.SuppressLint import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.ViewGroup.MarginLayoutParams import android.view.ViewGroup.MarginLayoutParams
import android.widget.Toast import android.widget.Toast
import androidx.appcompat.content.res.AppCompatResources import androidx.appcompat.content.res.AppCompatResources
import androidx.core.os.bundleOf
import androidx.core.text.parseAsHtml import androidx.core.text.parseAsHtml
import androidx.core.view.updateLayoutParams import androidx.core.view.updateLayoutParams
import androidx.core.view.updatePadding import androidx.core.view.updatePadding
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.commit
import androidx.fragment.app.replace
import androidx.recyclerview.widget.RecyclerView import androidx.recyclerview.widget.RecyclerView
import com.github.libretube.R import com.github.libretube.R
import com.github.libretube.api.JsonHelper 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.fragments.CommentsRepliesFragment
import com.github.libretube.ui.viewholders.CommentsViewHolder import com.github.libretube.ui.viewholders.CommentsViewHolder
import com.github.libretube.util.TextUtils import com.github.libretube.util.TextUtils
import kotlinx.serialization.encodeToString
class CommentsAdapter( class CommentsAdapter(
private val fragment: Fragment?, private val fragment: Fragment?,
@ -94,21 +97,15 @@ class CommentsAdapter(
} }
if (!isRepliesAdapter && comment.repliesPage != null) { 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 { root.setOnClickListener {
fragment!!.parentFragmentManager val args = bundleOf(
.beginTransaction() IntentData.videoId to videoId,
.replace(R.id.commentFragContainer, repliesFragment) IntentData.comment to JsonHelper.json.encodeToString(comment)
.addToBackStack(null) )
.commit() fragment!!.parentFragmentManager.commit {
replace<CommentsRepliesFragment>(R.id.commentFragContainer, args = args)
addToBackStack(null)
}
} }
} }

View File

@ -35,6 +35,7 @@ import androidx.core.os.postDelayed
import androidx.core.text.parseAsHtml import androidx.core.text.parseAsHtml
import androidx.core.view.isVisible import androidx.core.view.isVisible
import androidx.fragment.app.activityViewModels import androidx.fragment.app.activityViewModels
import androidx.fragment.app.commit
import androidx.lifecycle.Lifecycle import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.LinearLayoutManager
@ -1510,9 +1511,9 @@ class PlayerFragment : BaseFragment(), OnlinePlayerOptions {
private fun killPlayerFragment() { private fun killPlayerFragment() {
viewModel.isFullscreen.value = false viewModel.isFullscreen.value = false
binding.playerMotionLayout.transitionToEnd() binding.playerMotionLayout.transitionToEnd()
mainActivity.supportFragmentManager.beginTransaction() mainActivity.supportFragmentManager.commit {
.remove(this) remove(this@PlayerFragment)
.commit() }
onDestroy() onDestroy()
} }

View File

@ -4,6 +4,7 @@ import android.os.Bundle
import android.widget.Toast import android.widget.Toast
import androidx.annotation.StringRes import androidx.annotation.StringRes
import androidx.fragment.app.Fragment import androidx.fragment.app.Fragment
import androidx.fragment.app.commitNow
import androidx.preference.Preference import androidx.preference.Preference
import com.github.libretube.BuildConfig import com.github.libretube.BuildConfig
import com.github.libretube.R import com.github.libretube.R
@ -124,9 +125,9 @@ class MainSettings : BasePreferenceFragment() {
} }
private fun navigateToSettingsFragment(newFragment: Fragment): Boolean { private fun navigateToSettingsFragment(newFragment: Fragment): Boolean {
parentFragmentManager.beginTransaction() parentFragmentManager.commitNow {
.replace(R.id.settings, newFragment) replace(R.id.settings, newFragment)
.commitNow() }
return true return true
} }
} }