77package org.readium.r2.testapp.catalogs
88
99import android.os.Bundle
10- import android.view.*
11- import android.widget.Button
12- import android.widget.ImageView
13- import android.widget.LinearLayout
14- import android.widget.TextView
15- import androidx.appcompat.content.res.AppCompatResources
10+ import android.view.LayoutInflater
11+ import android.view.Menu
12+ import android.view.View
13+ import android.view.ViewGroup
1614import androidx.core.os.bundleOf
17- import androidx.core.view.setPadding
1815import androidx.fragment.app.Fragment
1916import androidx.fragment.app.viewModels
2017import androidx.navigation.Navigation
2118import androidx.recyclerview.widget.LinearLayoutManager
22- import androidx.recyclerview.widget.RecyclerView
2319import com.google.android.material.snackbar.Snackbar
2420import org.readium.r2.shared.opds.Facet
2521import org.readium.r2.testapp.MainActivity
@@ -34,15 +30,16 @@ import org.readium.r2.testapp.opds.GridAutoFitLayoutManager
3430class CatalogFragment : Fragment () {
3531
3632 private val catalogViewModel: CatalogViewModel by viewModels()
37- private lateinit var catalogListAdapter: CatalogListAdapter
33+ private lateinit var publicationAdapter: PublicationAdapter
34+ private lateinit var groupAdapter: GroupAdapter
35+ private lateinit var navigationAdapter: NavigationAdapter
3836 private lateinit var catalog: Catalog
3937 private var showFacetMenu = false
4038 private lateinit var facets: MutableList <Facet >
4139
4240 private var _binding : FragmentCatalogBinding ? = null
4341 private val binding get() = _binding !!
4442
45- // FIXME the entire way this fragment is built feels like a hack. Need a cleaner UI
4643 override fun onCreateView (
4744 inflater : LayoutInflater , container : ViewGroup ? ,
4845 savedInstanceState : Bundle ?
@@ -56,19 +53,36 @@ class CatalogFragment : Fragment() {
5653
5754 override fun onViewCreated (view : View , savedInstanceState : Bundle ? ) {
5855 super .onViewCreated(view, savedInstanceState)
59- catalogListAdapter = CatalogListAdapter ()
56+ publicationAdapter = PublicationAdapter ()
57+ navigationAdapter = NavigationAdapter (catalog.type)
58+ groupAdapter = GroupAdapter (catalog.type)
6059 setHasOptionsMenu(true )
6160
62- binding.catalogDetailList.apply {
61+ binding.catalogNavigationList.apply {
62+ layoutManager = LinearLayoutManager (requireContext())
63+ adapter = navigationAdapter
64+ addItemDecoration(
65+ CatalogFeedListFragment .VerticalSpaceItemDecoration (
66+ 10
67+ )
68+ )
69+ }
70+
71+ binding.catalogPublicationsList.apply {
6372 layoutManager = GridAutoFitLayoutManager (requireContext(), 120 )
64- adapter = catalogListAdapter
73+ adapter = publicationAdapter
6574 addItemDecoration(
6675 BookshelfFragment .VerticalSpaceItemDecoration (
6776 10
6877 )
6978 )
7079 }
7180
81+ binding.catalogGroupList.apply {
82+ layoutManager = LinearLayoutManager (requireContext())
83+ adapter = groupAdapter
84+ }
85+
7286 (activity as MainActivity ).supportActionBar?.title = catalog.title
7387
7488 // TODO this feels hacky, I don't want to parse the file if it has not changed
@@ -85,110 +99,10 @@ class CatalogFragment : Fragment() {
8599 }
86100 requireActivity().invalidateOptionsMenu()
87101
88- result.feed!! .navigation.forEachIndexed { index, navigation ->
89- val button = Button (requireContext())
90- button.apply {
91- layoutParams = LinearLayout .LayoutParams (
92- LinearLayout .LayoutParams .MATCH_PARENT ,
93- LinearLayout .LayoutParams .WRAP_CONTENT
94- )
95- text = navigation.title
96- setOnClickListener {
97- val catalog1 = Catalog (
98- href = navigation.href,
99- title = navigation.title!! ,
100- type = catalog.type
101- )
102- val bundle = bundleOf(CATALOGFEED to catalog1)
103- Navigation .findNavController(it)
104- .navigate(R .id.action_navigation_catalog_self, bundle)
105- }
106- }
107- binding.catalogLinearLayout.addView(button, index)
108- }
109-
110- if (result.feed!! .publications.isNotEmpty()) {
111- catalogListAdapter.submitList(result.feed!! .publications)
112- }
102+ navigationAdapter.submitList(result.feed!! .navigation)
103+ publicationAdapter.submitList(result.feed!! .publications)
104+ groupAdapter.submitList(result.feed!! .groups)
113105
114- for (group in result.feed!! .groups) {
115- if (group.publications.isNotEmpty()) {
116- val linearLayout = LinearLayout (requireContext()).apply {
117- orientation = LinearLayout .HORIZONTAL
118- setPadding(10 )
119- layoutParams = LinearLayout .LayoutParams (
120- LinearLayout .LayoutParams .MATCH_PARENT ,
121- LinearLayout .LayoutParams .WRAP_CONTENT ,
122- 1f
123- )
124- weightSum = 2f
125- addView(TextView (requireContext()).apply {
126- text = group.title
127- layoutParams = LinearLayout .LayoutParams (
128- LinearLayout .LayoutParams .WRAP_CONTENT ,
129- LinearLayout .LayoutParams .WRAP_CONTENT ,
130- 1f
131- )
132- })
133- if (group.links.size > 0 ) {
134- addView(ImageView (requireContext()).apply {
135- // FIXME Have the arrow at the very end
136- setImageDrawable(AppCompatResources .getDrawable(requireContext(), R .drawable.ic_baseline_arrow_forward_24))
137- contentDescription = getString(R .string.catalog_list_more)
138- gravity = Gravity .END
139- layoutParams = LinearLayout .LayoutParams (
140- LinearLayout .LayoutParams .WRAP_CONTENT ,
141- LinearLayout .LayoutParams .WRAP_CONTENT ,
142- 1f
143- )
144- setOnClickListener {
145- val catalog1 = Catalog (
146- href = group.links.first().href,
147- title = group.title,
148- type = catalog.type
149- )
150- val bundle = bundleOf(CATALOGFEED to catalog1)
151- Navigation .findNavController(it)
152- .navigate(R .id.action_navigation_catalog_self, bundle)
153- }
154- })
155- }
156- }
157- val publicationRecyclerView = RecyclerView (requireContext()).apply {
158- layoutManager = LinearLayoutManager (requireContext())
159- (layoutManager as LinearLayoutManager ).orientation =
160- LinearLayoutManager .HORIZONTAL
161- adapter = CatalogListAdapter ().apply {
162- submitList(group.publications)
163- }
164- }
165- binding.catalogLinearLayout.addView(linearLayout)
166- binding.catalogLinearLayout.addView(publicationRecyclerView)
167- }
168- if (group.navigation.isNotEmpty()) {
169- for (navigation in group.navigation) {
170- val button = Button (requireContext())
171- button.apply {
172- layoutParams = LinearLayout .LayoutParams (
173- LinearLayout .LayoutParams .MATCH_PARENT ,
174- LinearLayout .LayoutParams .WRAP_CONTENT
175- )
176- text = navigation.title
177- setOnClickListener {
178- val catalog1 = Catalog (
179- href = navigation.href,
180- title = navigation.title!! ,
181- type = catalog.type
182- )
183- val bundle = bundleOf(CATALOGFEED to catalog1)
184- Navigation .findNavController(it)
185- .navigate(R .id.action_navigation_catalog_self, bundle)
186- }
187- }
188- binding.catalogLinearLayout.addView(button)
189- }
190- }
191- }
192106 binding.catalogProgressBar.visibility = View .GONE
193107 })
194108 }
0 commit comments