Skip to content

Commit

Permalink
Add support for system navigation
Browse files Browse the repository at this point in the history
  • Loading branch information
levinli303 committed Aug 23, 2024
1 parent 79f02f4 commit 9e0ad95
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1549,7 +1549,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main),
findViewById<View>(R.id.bottom_sheet_overlay).visibility = View.INVISIBLE
val fragment = supportFragmentManager.findFragmentById(R.id.bottom_sheet)
if (fragment != null) {
supportFragmentManager.beginTransaction().hide(fragment).remove(fragment)
supportFragmentManager.beginTransaction().hide(fragment).remove(fragment).setPrimaryNavigationFragment(null)
.commitAllowingStateLoss()
}
}
Expand Down Expand Up @@ -2010,6 +2010,7 @@ class MainActivity : AppCompatActivity(R.layout.activity_main),
val id = supportFragmentManager
.beginTransaction()
.add(R.id.bottom_sheet, fragment, BOTTOM_SHEET_ROOT_FRAGMENT_TAG)
.setPrimaryNavigationFragment(fragment)
.commitAllowingStateLoss()
showView(true, R.id.bottom_sheet_card, false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface BrowserRootFragment {
}

@AndroidEntryPoint
class BrowserFragment : Fragment(), Poppable, BrowserRootFragment, NavigationBarView.OnItemSelectedListener {
class BrowserFragment : Fragment(), BrowserRootFragment, NavigationBarView.OnItemSelectedListener {
private var currentPath = ""
private var selectedItemIndex = 0
private lateinit var loadingIndicator: CircularProgressIndicator
Expand Down Expand Up @@ -188,16 +188,6 @@ class BrowserFragment : Fragment(), Poppable, BrowserRootFragment, NavigationBar
navigationFragment.pushFragment(InfoFragment.newInstance(selection, true))
}

override fun canPop(): Boolean {
val navigationFragment = childFragmentManager.findFragmentById(R.id.navigation_container) as? BrowserNavigationFragment ?: return false
return navigationFragment.canPop()
}

override fun popLast() {
val navigationFragment = childFragmentManager.findFragmentById(R.id.navigation_container) as? BrowserNavigationFragment ?: return
navigationFragment.popLast()
}

private fun loadRootBrowserItems() = lifecycleScope.launch {
browserContainer.visibility = View.GONE
loadingIndicator.visibility = View.VISIBLE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ fun Fragment.replace(fragment: Fragment, containerID: Int): Int? {
trans = trans.hide(current).remove(current)
}
trans.add(containerID, fragment)
return trans.commitAllowingStateLoss()
return trans.setPrimaryNavigationFragment(fragment).commitAllowingStateLoss()
}

interface Poppable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import com.google.android.material.appbar.AppBarLayout
import space.celestia.mobilecelestia.R
import java.lang.ref.WeakReference

abstract class NavigationFragment: Fragment(), Poppable, Toolbar.OnMenuItemClickListener {
abstract class NavigationFragment: Fragment(), Toolbar.OnMenuItemClickListener {
interface NavigationBarItem: Parcelable

class BarButtonItem(val id: Int, val title: String?, val icon: Int? = null, val enabled: Boolean = true, val checked: Boolean? = null): NavigationBarItem {
Expand Down Expand Up @@ -237,6 +238,18 @@ abstract class NavigationFragment: Fragment(), Poppable, Toolbar.OnMenuItemClick
val top: SubFragment?
get() = childFragmentManager.findFragmentById(R.id.fragment_container) as? SubFragment

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

val weakSelf = WeakReference(this)
childFragmentManager.addOnBackStackChangedListener {
val self = weakSelf.get() ?: return@addOnBackStackChangedListener
val backEntryCount = self.childFragmentManager.backStackEntryCount
if (backEntryCount < self.commitIds.size - 1)
self.poppedToIndex(backEntryCount)
}
}

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
Expand Down Expand Up @@ -375,12 +388,12 @@ abstract class NavigationFragment: Fragment(), Poppable, Toolbar.OnMenuItemClick
return top?.menuItemClicked(groupId, id) ?: true
}

override fun canPop(): Boolean {
private fun canPop(): Boolean {
if (!isAdded) return false
return commitIds.size > 1
}

override fun popLast() {
private fun popLast() {
if (canPop()) {
popFragment()
}
Expand All @@ -393,6 +406,10 @@ abstract class NavigationFragment: Fragment(), Poppable, Toolbar.OnMenuItemClick

fun popFragmentToIndex(index: Int) {
childFragmentManager.popBackStackImmediate(commitIds[index + 1], FragmentManager.POP_BACK_STACK_INCLUSIVE)
poppedToIndex(index)
}

fun poppedToIndex(index: Int) {
if (index == 0) {
// no more return
toolbar.navigationIcon = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import android.net.Uri
import android.os.Bundle
import androidx.core.os.BundleCompat
import space.celestia.mobilecelestia.common.NavigationFragment
import space.celestia.mobilecelestia.common.Poppable

class CommonWebNavigationFragment: NavigationFragment() {
class CommonWebNavigationFragment: NavigationFragment(), Poppable {
private lateinit var uri: Uri

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -20,19 +21,14 @@ class CommonWebNavigationFragment: NavigationFragment() {

override fun canPop(): Boolean {
val currentTop = top
if (currentTop is CommonWebFragment && currentTop.canGoBack()) {
return true
}
return super.canPop()
return currentTop is CommonWebFragment && currentTop.canGoBack()
}

override fun popLast() {
val currentTop = top
if (currentTop is CommonWebFragment && currentTop.canGoBack()) {
currentTop.goBack()
return
}
super.popLast()
}

companion object {
Expand Down

0 comments on commit 9e0ad95

Please sign in to comment.