@@ -19,22 +19,19 @@ package com.itsaky.androidide.fragments
1919
2020import android.os.Bundle
2121import android.view.View
22- import android.view.ViewTreeObserver.OnGlobalLayoutListener
22+ import android.content.res.Configuration
2323import androidx.core.view.ViewCompat
2424import androidx.core.view.WindowInsetsCompat
2525import androidx.core.view.updatePadding
2626import androidx.fragment.app.viewModels
27- import com.google.android.flexbox.FlexDirection
28- import com.google.android.flexbox.FlexboxLayoutManager
29- import com.google.android.flexbox.JustifyContent
27+ import androidx.recyclerview.widget.GridLayoutManager
3028import com.itsaky.androidide.R
3129import com.itsaky.androidide.adapters.TemplateListAdapter
3230import com.itsaky.androidide.databinding.FragmentTemplateListBinding
3331import com.itsaky.androidide.idetooltips.TooltipManager
3432import com.itsaky.androidide.idetooltips.TooltipTag.EXIT_TO_MAIN
3533import com.itsaky.androidide.templates.ITemplateProvider
3634import com.itsaky.androidide.templates.ProjectTemplate
37- import com.itsaky.androidide.utils.FlexboxUtils
3835import com.itsaky.androidide.viewmodel.MainViewModel
3936import org.slf4j.LoggerFactory
4037
@@ -49,9 +46,6 @@ class TemplateListFragment :
4946 FragmentTemplateListBinding ::bind,
5047 ) {
5148 private var adapter: TemplateListAdapter ? = null
52- private var layoutManager: FlexboxLayoutManager ? = null
53-
54- private lateinit var globalLayoutListener: OnGlobalLayoutListener
5549
5650 private val viewModel by viewModels<MainViewModel >(ownerProducer = { requireActivity() })
5751
@@ -65,41 +59,31 @@ class TemplateListFragment :
6559 ) {
6660 super .onViewCreated(view, savedInstanceState)
6761
68- ViewCompat .setOnApplyWindowInsetsListener(binding.root) { v, windowInsets ->
69- val insets = windowInsets.getInsets(WindowInsetsCompat .Type .systemBars())
70- v.updatePadding(bottom = insets.bottom)
71- windowInsets
72- }
73-
74- layoutManager = FlexboxLayoutManager (requireContext(), FlexDirection .ROW )
75- layoutManager!! .justifyContent = JustifyContent .SPACE_EVENLY
76-
77- binding.list.layoutManager = layoutManager
78-
79- // This makes sure that the items are evenly distributed in the list
80- // and the last row is always aligned to the start
81- globalLayoutListener =
82- FlexboxUtils .createGlobalLayoutListenerToDistributeFlexboxItemsEvenly(
83- { adapter },
84- { layoutManager },
85- ) { adapter, diff ->
86- adapter.fillDiff(diff)
87- }
62+ ViewCompat .setOnApplyWindowInsetsListener(binding.root) { v, windowInsets ->
63+ val insets = windowInsets.getInsets(WindowInsetsCompat .Type .systemBars())
64+ v.updatePadding(bottom = insets.bottom)
65+ windowInsets
66+ }
67+
68+ val screenWidthDp = resources.configuration.screenWidthDp
69+ val minItemWidthDp = 160
70+ val initialSpans = (screenWidthDp / minItemWidthDp).coerceIn(1 , 4 )
8871
89- binding.list.viewTreeObserver.addOnGlobalLayoutListener(globalLayoutListener)
72+ val gridLayoutManager = GridLayoutManager (requireContext(), initialSpans)
73+ binding.list.layoutManager = gridLayoutManager
9074
9175 binding.exitButton.setOnClickListener {
9276 viewModel.setScreen(MainViewModel .SCREEN_MAIN )
9377 }
9478
95- binding.exitButton.setOnLongClickListener {
96- TooltipManager .showIdeCategoryTooltip(
97- context = requireContext(),
98- anchorView = binding.root,
99- tag = EXIT_TO_MAIN ,
100- )
101- true
102- }
79+ binding.exitButton.setOnLongClickListener {
80+ TooltipManager .showIdeCategoryTooltip(
81+ context = requireContext(),
82+ anchorView = binding.root,
83+ tag = EXIT_TO_MAIN ,
84+ )
85+ true
86+ }
10387
10488 viewModel.currentScreen.observe(viewLifecycleOwner) { current ->
10589 if (current == MainViewModel .SCREEN_TEMPLATE_DETAILS ) {
@@ -110,8 +94,16 @@ class TemplateListFragment :
11094 }
11195 }
11296
97+ override fun onConfigurationChanged (newConfig : Configuration ) {
98+ super .onConfigurationChanged(newConfig)
99+
100+ val minItemWidthDp = 160
101+ val optimalSpans = (newConfig.screenWidthDp / minItemWidthDp).coerceIn(1 , 4 )
102+
103+ (binding.list.layoutManager as ? GridLayoutManager )?.spanCount = optimalSpans
104+ }
105+
113106 override fun onDestroyView () {
114- binding.list.viewTreeObserver.removeOnGlobalLayoutListener(globalLayoutListener)
115107 super .onDestroyView()
116108 }
117109
@@ -138,16 +130,14 @@ class TemplateListFragment :
138130 },
139131 onLongClick = { template, itemView ->
140132 template.tooltipTag?.let { tag ->
141- TooltipManager .showIdeCategoryTooltip(
142- context = requireContext(),
143- anchorView = itemView,
144- tag = tag
145- )
146- }
147- },
133+ TooltipManager .showIdeCategoryTooltip(
134+ context = requireContext(),
135+ anchorView = itemView,
136+ tag = tag
137+ )
138+ }
139+ },
148140 )
149-
150141 binding.list.adapter = adapter
151142 }
152-
153143}
0 commit comments