-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathRootFragment.kt
102 lines (90 loc) · 3.66 KB
/
RootFragment.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package dev.enro.example
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import com.google.android.material.bottomnavigation.BottomNavigationView
import dagger.hilt.android.AndroidEntryPoint
import dev.enro.annotations.NavigationDestination
import dev.enro.core.NavigationKey
import dev.enro.core.container.EmptyBehavior
import dev.enro.core.container.acceptKey
import dev.enro.core.container.acceptNone
import dev.enro.core.containerManager
import dev.enro.core.fragment.container.FragmentNavigationContainer
import dev.enro.core.fragment.container.navigationContainer
import dev.enro.core.fragment.container.setVisibilityAnimated
import dev.enro.example.databinding.FragmentRootBinding
import dev.enro.example.destinations.compose.ExampleComposable
import dev.enro.example.destinations.fragment.ExampleFragment
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.parcelize.Parcelize
@Parcelize
class RootFragment : NavigationKey.SupportsPush
@AndroidEntryPoint
@NavigationDestination(RootFragment::class)
class RootFragmentDestination : Fragment() {
private val homeContainer by navigationContainer(
containerId = R.id.homeContainer,
root = { Home() },
filter = acceptKey {
it is Home || it is ExampleFragment || it is ExampleComposable
},
emptyBehavior = EmptyBehavior.CloseParent
)
private val featuresContainer by navigationContainer(
containerId = R.id.featuresContainer,
root = { Features() },
emptyBehavior = EmptyBehavior.Action {
requireView().findViewById<BottomNavigationView>(R.id.bottomNavigation).selectedItemId = R.id.home
true
}
)
private val backstackContainer by navigationContainer(
containerId = R.id.profileContainer,
root = { Backstacks() },
filter = acceptNone(),
emptyBehavior = EmptyBehavior.Action {
requireView().findViewById<BottomNavigationView>(R.id.bottomNavigation).selectedItemId = R.id.home
true
}
)
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val binding = FragmentRootBinding.inflate(layoutInflater)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
val binding = FragmentRootBinding.bind(view)
binding.bottomNavigation.bindContainers(
R.id.home to homeContainer,
R.id.features to featuresContainer,
R.id.backstack to backstackContainer,
)
if(savedInstanceState == null) {
binding.bottomNavigation.selectedItemId = R.id.home
}
}
private fun BottomNavigationView.bindContainers(
vararg containers: Pair<Int, FragmentNavigationContainer>
) {
containerManager.activeContainerFlow
.onEach { _ ->
val activeContainer = containers.firstOrNull { it.second.isActive }
?: containers.firstOrNull { it.first == selectedItemId}
containers.forEach {
it.second.setVisibilityAnimated(it.second == activeContainer?.second)
}
selectedItemId = activeContainer?.first ?: return@onEach
}
.launchIn(lifecycleScope)
setOnItemSelectedListener { item ->
containers.firstOrNull { it.first == item.itemId }
?.second
?.setActive()
return@setOnItemSelectedListener true
}
}
}