-
-
Notifications
You must be signed in to change notification settings - Fork 469
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use custom logic for fragment back stack #3958
Merged
nielsvanvelzen
merged 1 commit into
jellyfin:master
from
nielsvanvelzen:navigation-custom-history
Sep 3, 2024
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
184 changes: 184 additions & 0 deletions
184
app/src/main/java/org/jellyfin/androidtv/ui/browsing/DestinationFragmentView.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,184 @@ | ||
package org.jellyfin.androidtv.ui.browsing | ||
|
||
import android.annotation.SuppressLint | ||
import android.content.Context | ||
import android.os.Bundle | ||
import android.os.Parcel | ||
import android.os.Parcelable | ||
import android.util.AttributeSet | ||
import android.widget.FrameLayout | ||
import androidx.core.os.BundleCompat | ||
import androidx.core.os.ParcelCompat | ||
import androidx.core.os.bundleOf | ||
import androidx.fragment.app.Fragment | ||
import androidx.fragment.app.FragmentContainerView | ||
import androidx.fragment.app.FragmentManager | ||
import androidx.fragment.app.FragmentTransaction | ||
import org.jellyfin.androidtv.R | ||
import org.jellyfin.androidtv.ui.navigation.NavigationAction | ||
import java.util.Stack | ||
|
||
private class HistoryEntry( | ||
val name: Class<out Fragment>, | ||
val arguments: Bundle = bundleOf(), | ||
|
||
var fragment: Fragment? = null, | ||
var savedState: Fragment.SavedState? = null, | ||
) : Parcelable { | ||
override fun describeContents(): Int = 0 | ||
|
||
override fun writeToParcel(dest: Parcel, flags: Int) { | ||
dest.writeString(name.name) | ||
dest.writeBundle(arguments) | ||
dest.writeParcelable(savedState, 0) | ||
} | ||
|
||
companion object CREATOR : Parcelable.Creator<HistoryEntry> { | ||
@Suppress("UNCHECKED_CAST") | ||
override fun createFromParcel(parcel: Parcel): HistoryEntry = HistoryEntry( | ||
name = Class.forName(parcel.readString()!!) as Class<out Fragment>, | ||
arguments = parcel.readBundle(this::class.java.classLoader)!!, | ||
fragment = null, | ||
savedState = ParcelCompat.readParcelable(parcel, this::class.java.classLoader, Fragment.SavedState::class.java)!!, | ||
) | ||
|
||
override fun newArray(size: Int): Array<HistoryEntry?> = arrayOfNulls(size) | ||
} | ||
} | ||
|
||
class DestinationFragmentView @JvmOverloads constructor( | ||
context: Context, | ||
attrs: AttributeSet? = null, | ||
) : FrameLayout(context, attrs) { | ||
private companion object { | ||
private const val FRAGMENT_TAG_CONTENT = "content" | ||
private const val BUNDLE_SUPER = "super" | ||
private const val BUNDLE_HISTORY = "history" | ||
} | ||
|
||
private val fragmentManager by lazy { | ||
FragmentManager.findFragmentManager(this) | ||
} | ||
|
||
private val container by lazy { | ||
FragmentContainerView(context).also { view -> | ||
view.id = R.id.container | ||
addView(view) | ||
} | ||
} | ||
|
||
private val history = Stack<HistoryEntry>() | ||
|
||
fun navigate(action: NavigationAction.NavigateFragment) { | ||
val entry = HistoryEntry(action.destination.fragment.java, action.destination.arguments) | ||
|
||
// Create the base transaction so we can mutate everything at once | ||
val transaction = fragmentManager.beginTransaction() | ||
|
||
if (action.clear) { | ||
// Clear all current fragments from the history before adding the new entry | ||
history.mapNotNull { it.fragment }.forEach { transaction.remove(it) } | ||
history.clear() | ||
history.push(entry) | ||
} else if (action.replace && action.addToBackStack && history.isNotEmpty()) { | ||
// Remove the top-most entry before replacing it with the next | ||
val currentFragment = history[history.size - 1].fragment | ||
if (currentFragment != null) transaction.remove(currentFragment) | ||
history[history.size - 1] = entry | ||
} else { | ||
// Add to the end of the history | ||
saveCurrentFragmentState() | ||
history.push(entry) | ||
} | ||
|
||
activateHistoryEntry(entry, transaction) | ||
} | ||
|
||
fun goBack(): Boolean { | ||
// Require at least 2 items (current & previous) to go back | ||
if (history.size < 2) return false | ||
|
||
// Remove current entry | ||
history.pop() | ||
|
||
// Read & set previous entry | ||
val entry = history.last() | ||
activateHistoryEntry(entry) | ||
|
||
return true | ||
} | ||
|
||
private fun saveCurrentFragmentState() { | ||
if (history.isEmpty()) return | ||
|
||
// Update the top-most history entry with state from current fragment | ||
val fragment = requireNotNull(fragmentManager.findFragmentByTag(FRAGMENT_TAG_CONTENT)) | ||
history[history.size - 1].savedState = fragmentManager.saveFragmentInstanceState(fragment) | ||
} | ||
|
||
@SuppressLint("CommitTransaction") | ||
private fun activateHistoryEntry( | ||
entry: HistoryEntry, | ||
transaction: FragmentTransaction = fragmentManager.beginTransaction(), | ||
) { | ||
var fragment = entry.fragment | ||
|
||
// Create if there is no existing fragment | ||
if (fragment == null) { | ||
fragment = fragmentManager.fragmentFactory.instantiate(context.classLoader, entry.name.name).apply { | ||
setInitialSavedState(entry.savedState) | ||
} | ||
entry.fragment = fragment | ||
} | ||
|
||
// Update arguments | ||
fragment.arguments = entry.arguments | ||
|
||
transaction.apply { | ||
// Set options | ||
setReorderingAllowed(true) | ||
setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out) | ||
|
||
// Detach current fragment | ||
fragmentManager.findFragmentByTag(FRAGMENT_TAG_CONTENT)?.let(::detach) | ||
|
||
// Attach or add next fragment | ||
if (fragment.isDetached) attach(fragment) | ||
else add(container.id, fragment, FRAGMENT_TAG_CONTENT) | ||
} | ||
|
||
// Commit | ||
if (fragmentManager.isStateSaved) transaction.commitNowAllowingStateLoss() | ||
else transaction.commitNow() | ||
} | ||
|
||
override fun onSaveInstanceState(): Parcelable { | ||
// Always retrieve current state before writing | ||
saveCurrentFragmentState() | ||
|
||
// Save state | ||
return bundleOf( | ||
BUNDLE_SUPER to super.onSaveInstanceState(), | ||
BUNDLE_HISTORY to history.toTypedArray() | ||
) | ||
} | ||
|
||
override fun onRestoreInstanceState(state: Parcelable?) { | ||
// Ignore if not a bundle | ||
if (state !is Bundle) return super.onRestoreInstanceState(state) | ||
|
||
// Call parent | ||
@Suppress("DEPRECATION") | ||
val parent = state.getParcelable<Parcelable>(BUNDLE_SUPER) | ||
if (parent != null) super.onRestoreInstanceState(parent) | ||
|
||
// Restore history | ||
@Suppress("UNCHECKED_CAST") | ||
val savedHistory = BundleCompat.getParcelableArray(state, BUNDLE_HISTORY, HistoryEntry::class.java) as Array<HistoryEntry>? | ||
if (savedHistory != null) { | ||
history.clear() | ||
history.addAll(savedHistory) | ||
if (history.isNotEmpty()) activateHistoryEntry(history.last()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Check warning
Code scanning / detekt
Line detected, which is longer than the defined maximum line length in the code style. Warning