Skip to content

Commit 4565640

Browse files
committed
Fix navigation (wip)
1 parent c16736f commit 4565640

5 files changed

Lines changed: 76 additions & 129 deletions

File tree

app/src/main/kotlin/auth/AuthFragment.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,17 @@ import android.os.Bundle
55
import android.view.LayoutInflater
66
import android.view.View
77
import android.view.ViewGroup
8+
import androidx.core.os.bundleOf
9+
import androidx.core.view.isVisible
810
import androidx.fragment.app.Fragment
11+
import androidx.fragment.app.commit
12+
import androidx.fragment.app.replace
913
import co.appreactor.news.R
1014
import co.appreactor.news.databinding.FragmentAuthBinding
1115
import di.Di
1216
import entries.EntriesFilter
17+
import entries.EntriesFragment
18+
import navigation.Activity
1319
import navigation.NavDirections
1420
import navigation.findNavController
1521

@@ -48,19 +54,31 @@ class AuthFragment : Fragment() {
4854
useStandaloneBackend.setOnClickListener {
4955
binding.root.animate().alpha(0f).withEndAction {
5056
model.setStandaloneBackend()
51-
findNavController().navigate(
52-
R.id.newsFragment,
53-
NavDirections.AuthFragment.actionAuthFragmentToNewsFragment(EntriesFilter.NotBookmarked)
54-
)
57+
58+
parentFragmentManager.commit {
59+
setReorderingAllowed(true)
60+
replace(
61+
R.id.fragmentContainerView,
62+
EntriesFragment::class.java,
63+
bundleOf("filter" to EntriesFilter.NotBookmarked),
64+
)
65+
addToBackStack(null)
66+
}
67+
68+
(activity as Activity).binding.bottomNav.isVisible = true
5569
}
5670
}
5771

5872
useMinifluxBackend.setOnClickListener {
59-
findNavController().navigate(R.id.minifluxAuthFragment)
73+
parentFragmentManager.commit {
74+
setReorderingAllowed(true)
75+
replace<MinifluxAuthFragment>(R.id.fragmentContainerView)
76+
addToBackStack(null)
77+
}
6078
}
6179

6280
useNextcloudBackend.setOnClickListener {
63-
findNavController().navigate(R.id.nextcloudAuthFragment)
81+
TODO()
6482
}
6583
}
6684
}

app/src/main/kotlin/auth/MinifluxAuthFragment.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ class MinifluxAuthFragment : Fragment() {
3838
super.onViewCreated(view, savedInstanceState)
3939

4040
binding.apply {
41-
toolbar.setNavigationOnClickListener { findNavController().popBackStack() }
41+
toolbar.setNavigationOnClickListener { parentFragmentManager.popBackStack() }
4242

4343
password.setOnEditorActionListener { _, actionId, keyEvent ->
4444
if (actionId == EditorInfo.IME_ACTION_DONE || keyEvent?.keyCode == KeyEvent.KEYCODE_ENTER) {
Lines changed: 47 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,32 @@
11
package navigation
22

33
import android.os.Bundle
4-
import android.os.Handler
5-
import android.os.Looper
6-
import androidx.activity.OnBackPressedCallback
74
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.core.os.bundleOf
86
import androidx.core.view.ViewCompat
97
import androidx.core.view.WindowInsetsCompat
10-
import androidx.core.view.forEach
118
import androidx.core.view.isVisible
129
import androidx.core.view.updatePadding
13-
import androidx.fragment.app.FragmentManager
10+
import androidx.fragment.app.commit
11+
import androidx.fragment.app.replace
1412
import androidx.lifecycle.lifecycleScope
1513
import co.appreactor.news.R
1614
import co.appreactor.news.databinding.ActivityBinding
1715
import com.google.android.material.navigation.NavigationBarView.OnItemReselectedListener
1816
import conf.ConfRepo
1917
import di.Di
2018
import entries.EntriesFilter
21-
import kotlinx.coroutines.flow.first
19+
import entries.EntriesFragment
20+
import feeds.FeedsFragment
2221
import kotlinx.coroutines.launch
2322
import opengraph.OpenGraphImagesRepo
2423

2524
class Activity : AppCompatActivity() {
2625

2726
lateinit var binding: ActivityBinding
2827

29-
private val navController: NavController by lazy { getSharedNavController()!! }
30-
31-
private var currentDestinationId: Int = R.id.newsFragment
32-
33-
private var destinationChangedListener: ((Int, Bundle?) -> Unit)? = null
34-
35-
private val backPressedCallback = object : OnBackPressedCallback(true) {
36-
override fun handleOnBackPressed() {
37-
android.util.Log.d("Activity", "Gesture back pressed")
38-
if (!navController.popBackStack()) {
39-
isEnabled = false
40-
onBackPressedDispatcher.onBackPressed()
41-
}
42-
}
43-
}
44-
45-
@Deprecated("Deprecated in Java")
46-
override fun onBackPressed() {
47-
android.util.Log.d("Activity", "XXXX onBackPressed called")
48-
if (!navController.popBackStack()) {
49-
@Suppress("DEPRECATION")
50-
super.onBackPressed()
51-
}
52-
}
53-
5428
override fun onCreate(savedInstanceState: Bundle?) {
5529
super.onCreate(savedInstanceState)
56-
android.util.Log.d("Activity", "XXXX onCreate called - NEW INSTANCE")
5730
binding = ActivityBinding.inflate(layoutInflater)
5831
setContentView(binding.root)
5932

@@ -68,111 +41,76 @@ class Activity : AppCompatActivity() {
6841

6942
Di.get(ConfRepo::class.java).update { it.copy(syncedOnStartup = false) }
7043
lifecycleScope.launch { Di.get(OpenGraphImagesRepo::class.java).fetchEntryImages() }
71-
72-
NavController.create(supportFragmentManager, R.id.navHost)
73-
74-
onBackPressedDispatcher.addCallback(this, backPressedCallback)
7544
}
7645

7746
override fun onStart() {
7847
super.onStart()
79-
android.util.Log.d("Activity", "XXXX onStart called")
80-
81-
destinationChangedListener?.let { navController.removeOnDestinationChangedListener(it) }
8248

83-
destinationChangedListener = { destinationId, args ->
84-
android.util.Log.d("Activity", "Destination changed: $destinationId")
85-
currentDestinationId = destinationId
86-
87-
// Use the visible fragment to determine bottom nav visibility
88-
updateBottomNavForCurrentDestination()
49+
lifecycleScope.launch {
50+
val confRepo = Di.get(ConfRepo::class.java)
51+
val config = confRepo.conf.value
8952

90-
if (binding.bottomNav.isVisible) {
91-
binding.bottomNav.menu.forEach { item ->
92-
if (item.itemId == destinationId) {
93-
item.isChecked = true
94-
}
53+
if (config.backend.isNotBlank()) {
54+
supportFragmentManager.commit {
55+
replace(
56+
R.id.fragmentContainerView,
57+
EntriesFragment::class.java,
58+
bundleOf("filter" to EntriesFilter.NotBookmarked),
59+
)
9560
}
96-
}
9761

98-
when (destinationId) {
99-
R.id.newsFragment -> args?.putParcelable("filter", EntriesFilter.NotBookmarked)
100-
R.id.bookmarksFragment -> args?.putParcelable("filter", EntriesFilter.Bookmarked)
62+
binding.bottomNav.isVisible = true
63+
} else {
64+
// supportFragmentManager.commit {
65+
// setReorderingAllowed(true)
66+
// replace<AuthFragment>(R.id.fragmentContainerView)
67+
// addToBackStack(null)
68+
// }
10169
}
10270
}
10371

104-
navController.addOnDestinationChangedListener(destinationChangedListener!!)
105-
106-
if (navController.getCurrentDestinationId() == null) {
107-
navigateToStartDestination()
108-
}
109-
110-
updateBottomNavForCurrentDestination()
111-
11272
binding.bottomNav.apply {
11373
setOnItemSelectedListener { item ->
11474
when (item.itemId) {
11575
R.id.newsFragment -> {
116-
navController.navigate(R.id.newsFragment, NavDirections.AuthFragment.actionAuthFragmentToNewsFragment(EntriesFilter.NotBookmarked))
76+
supportFragmentManager.commit {
77+
replace(
78+
R.id.fragmentContainerView,
79+
EntriesFragment::class.java,
80+
bundleOf("filter" to EntriesFilter.NotBookmarked),
81+
)
82+
}
11783
true
11884
}
85+
11986
R.id.bookmarksFragment -> {
120-
navController.navigate(R.id.bookmarksFragment, NavDirections.AuthFragment.actionAuthFragmentToNewsFragment(EntriesFilter.Bookmarked))
87+
supportFragmentManager.commit {
88+
replace(
89+
R.id.fragmentContainerView,
90+
EntriesFragment::class.java,
91+
bundleOf("filter" to EntriesFilter.Bookmarked),
92+
)
93+
}
12194
true
12295
}
96+
12397
R.id.feedsFragment -> {
124-
navController.navigate(R.id.feedsFragment)
98+
supportFragmentManager.commit {
99+
replace(
100+
R.id.fragmentContainerView,
101+
FeedsFragment::class.java,
102+
bundleOf("url" to ""),
103+
)
104+
}
125105
true
126106
}
107+
127108
else -> false
128109
}
129110
}
130-
setOnItemReselectedListener(createOnItemReselectedListener())
131-
}
132-
}
133111

134-
private fun updateBottomNavForCurrentDestination() {
135-
// Check the actual visible fragment to determine bottom nav visibility
136-
val visibleFragment = supportFragmentManager.findFragmentById(R.id.navHost)
137-
138-
val showBottomNav = when (visibleFragment) {
139-
is entries.EntriesFragment -> true
140-
is feeds.FeedsFragment -> true
141-
else -> false
142-
}
143-
144-
binding.bottomNav.isVisible = showBottomNav
145-
android.util.Log.d("Activity", "Bottom nav visibility: $showBottomNav for ${visibleFragment?.javaClass?.simpleName}")
146-
}
147-
148-
private fun navigateToStartDestination() {
149-
lifecycleScope.launch {
150-
val confRepo = Di.get(ConfRepo::class.java)
151-
val config = confRepo.conf.value
152-
153-
// Only navigate to news if already configured, otherwise go to auth
154-
if (config.minifluxServerUrl.isNotBlank() || config.nextcloudServerUrl.isNotBlank() || config.initialSyncCompleted) {
155-
navController.navigate(R.id.newsFragment, NavDirections.AuthFragment.actionAuthFragmentToNewsFragment(EntriesFilter.NotBookmarked))
156-
} else {
157-
navController.navigate(R.id.authFragment)
158-
}
159-
}
160-
}
161-
162-
private fun updateBottomNavVisibility(destinationId: Int) {
163-
val showBottomNav = when (destinationId) {
164-
R.id.authFragment,
165-
R.id.minifluxAuthFragment,
166-
R.id.nextcloudAuthFragment,
167-
R.id.settingsFragment,
168-
R.id.enclosuresFragment,
169-
R.id.entryFragment,
170-
R.id.searchFragment,
171-
R.id.feedEntriesFragment,
172-
R.id.feedSettingsFragment -> false
173-
else -> true
112+
setOnItemReselectedListener(createOnItemReselectedListener())
174113
}
175-
binding.bottomNav.isVisible = showBottomNav
176114
}
177115

178116
private fun createOnItemReselectedListener(): OnItemReselectedListener {
@@ -184,14 +122,4 @@ class Activity : AppCompatActivity() {
184122
}
185123
}
186124
}
187-
188-
override fun onResume() {
189-
super.onResume()
190-
android.util.Log.d("Activity", "XXXX onResume called - updating bottom nav")
191-
192-
// Post the update to ensure the fragment transaction is complete
193-
binding.bottomNav.postDelayed({
194-
updateBottomNavForCurrentDestination()
195-
}, 100)
196-
}
197125
}

app/src/main/kotlin/navigation/NavController.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ private var sharedNavController: NavController? = null
213213

214214
fun Fragment.findNavController(): NavController {
215215
val activity = requireActivity()
216-
val containerView = activity.findViewById<android.view.View>(R.id.navHost)
216+
val containerView = activity.findViewById<android.view.View>(R.id.fragmentContainerView)
217217
val containerViewId = containerView?.id ?: throw IllegalStateException("navHost not found")
218218
val fragmentManager = activity.supportFragmentManager
219219

app/src/main/res/layout/activity.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
android:layout_height="match_parent"
77
android:orientation="vertical">
88

9-
<FrameLayout
10-
android:id="@+id/navHost"
9+
<androidx.fragment.app.FragmentContainerView
10+
android:id="@+id/fragmentContainerView"
11+
android:name="auth.AuthFragment"
1112
android:layout_width="match_parent"
1213
android:layout_height="0dp"
1314
android:layout_weight="1" />

0 commit comments

Comments
 (0)