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.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
activity.supportFragmentManager.commitNow {
replace<PlayerFragment>(R.id.container, args = bundle)
}
)
.commitNow()
}
fun navigatePlaylist(

View File

@ -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<DownloadsFragment>(R.id.noInternet_container)
addToBackStack(null)
}
}
setContentView(binding.root)

View File

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

View File

@ -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<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.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()
}

View File

@ -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
}
}