Skip to content

Commit 4913c05

Browse files
authored
Merge pull request #92 from figonzal1/feature/non-collapsible-toolbar-ads-map
fix: keep toolbar fixed on ads and map tabs
2 parents 82f4a54 + 7fd1149 commit 4913c05

1 file changed

Lines changed: 24 additions & 9 deletions

File tree

app/src/main/java/cl/figonzal/lastquakechile/core/ui/MainActivity.kt

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import androidx.coordinatorlayout.widget.CoordinatorLayout
1010
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
1111
import androidx.core.view.ViewCompat
1212
import androidx.core.view.WindowInsetsCompat
13+
import androidx.core.view.doOnLayout
1314
import androidx.core.view.isVisible
1415
import androidx.preference.PreferenceManager
1516
import cl.figonzal.lastquakechile.R
@@ -42,6 +43,11 @@ import timber.log.Timber
4243

4344
class MainActivity : AppCompatActivity() {
4445

46+
companion object {
47+
// Pestañas cuyo toolbar debe permanecer fijo (no colapsable): ads y mapa
48+
private val NON_COLLAPSIBLE_TABS = setOf(0, 2)
49+
}
50+
4551
private val sharedPrefUtil: SharedPrefUtil by inject()
4652
private val ioDispatcher: CoroutineDispatcher by inject(named("ioDispatcher"))
4753

@@ -120,7 +126,7 @@ class MainActivity : AppCompatActivity() {
120126
// TabLayoutMediator.attach() selecciona la pestaña 0 antes de que exista el listener,
121127
// así que el estado inicial hay que fijarlo aquí. Se consulta después de
122128
// handleShortcuts para no colapsar cuando la app arranca en otra pestaña.
123-
if (viewPager.currentItem == 0) appBar.setExpanded(false, false)
129+
if (viewPager.currentItem in NON_COLLAPSIBLE_TABS) appBar.setExpanded(false, false)
124130
}
125131
}
126132

@@ -169,21 +175,30 @@ class MainActivity : AppCompatActivity() {
169175
else -> hideAdBanner(false)
170176
}
171177

172-
// El anuncio ocupa la pestaña completa: con el AppBar expandido su borde
173-
// inferior (y con él el CTA) queda bajo el borde de la pantalla.
174-
if (tab.position == 0) appBar.setExpanded(false)
178+
// Ads y mapa ocupan la pestaña completa: con el AppBar expandido su borde
179+
// inferior (y con él el CTA del ad, o altura útil del mapa) queda bajo el
180+
// borde de la pantalla.
181+
if (tab.position in NON_COLLAPSIBLE_TABS) appBar.setExpanded(false)
175182
}
176183

177184
override fun onTabUnselected(tab: TabLayout.Tab) = Unit
178185
override fun onTabReselected(tab: TabLayout.Tab) = Unit
179186
})
180187

181188
// Sin esto el usuario puede volver a expandir el AppBar arrastrándolo y dejar el
182-
// CTA del anuncio fuera de pantalla: el AdFragment no tiene scroll anidado.
183-
((appBar.layoutParams as? CoordinatorLayout.LayoutParams)?.behavior as? AppBarLayout.Behavior)
184-
?.setDragCallback(object : AppBarLayout.Behavior.DragCallback() {
185-
override fun canDrag(appBarLayout: AppBarLayout) = viewPager.currentItem != 0
186-
})
189+
// CTA del anuncio, o el mapa, con menos altura útil: ninguno de los dos tiene
190+
// scroll anidado.
191+
// doOnLayout: el AppBarLayout no declara app:layout_behavior en XML, así que
192+
// CoordinatorLayout.LayoutParams.behavior se resuelve recién en el primer layout
193+
// pass (vía @DefaultBehavior). Leerlo aquí de forma síncrona (dentro de onCreate)
194+
// siempre da null y el ?.setDragCallback no hace nada.
195+
appBar.doOnLayout {
196+
((appBar.layoutParams as? CoordinatorLayout.LayoutParams)?.behavior as? AppBarLayout.Behavior)
197+
?.setDragCallback(object : AppBarLayout.Behavior.DragCallback() {
198+
override fun canDrag(appBarLayout: AppBarLayout) =
199+
viewPager.currentItem !in NON_COLLAPSIBLE_TABS
200+
})
201+
}
187202

188203
}
189204
}

0 commit comments

Comments
 (0)