|
| 1 | +package com.eatssu.android.presentation.event |
| 2 | + |
| 3 | +import android.view.View |
| 4 | +import androidx.compose.ui.platform.ComposeView |
| 5 | +import androidx.compose.ui.platform.ViewCompositionStrategy |
| 6 | +import androidx.core.view.doOnLayout |
| 7 | +import com.eatssu.design_system.theme.EatssuTheme |
| 8 | +import com.google.android.material.bottomnavigation.BottomNavigationView |
| 9 | +import dagger.hilt.android.scopes.ActivityScoped |
| 10 | +import javax.inject.Inject |
| 11 | + |
| 12 | +@ActivityScoped |
| 13 | +class AnyoneButMeEventTooltipController @Inject constructor() { |
| 14 | + private companion object { |
| 15 | + const val BOTTOM_NAVIGATION_ITEM_COUNT = 4 |
| 16 | + const val ANYONE_BUT_ME_MENU_INDEX = 2 |
| 17 | + } |
| 18 | + |
| 19 | + private lateinit var tooltipComposeView: ComposeView |
| 20 | + private lateinit var bottomNavigationView: BottomNavigationView |
| 21 | + |
| 22 | + private val bottomNavigationLayoutChangeListener = View.OnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> |
| 23 | + updateTooltipPosition() |
| 24 | + } |
| 25 | + private val tooltipLayoutChangeListener = View.OnLayoutChangeListener { _, _, _, _, _, _, _, _, _ -> |
| 26 | + updateTooltipPosition() |
| 27 | + } |
| 28 | + |
| 29 | + fun bind( |
| 30 | + tooltipComposeView: ComposeView, |
| 31 | + bottomNavigationView: BottomNavigationView, |
| 32 | + ) { |
| 33 | + clearPreviousBindings() |
| 34 | + |
| 35 | + this.tooltipComposeView = tooltipComposeView |
| 36 | + this.bottomNavigationView = bottomNavigationView |
| 37 | + |
| 38 | + setupContent() |
| 39 | + observeLayout() |
| 40 | + } |
| 41 | + |
| 42 | + private fun clearPreviousBindings() { |
| 43 | + if (::bottomNavigationView.isInitialized) { |
| 44 | + bottomNavigationView.removeOnLayoutChangeListener(bottomNavigationLayoutChangeListener) |
| 45 | + } |
| 46 | + |
| 47 | + if (::tooltipComposeView.isInitialized) { |
| 48 | + tooltipComposeView.removeOnLayoutChangeListener(tooltipLayoutChangeListener) |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private fun setupContent() { |
| 53 | + tooltipComposeView.apply { |
| 54 | + isClickable = false |
| 55 | + isFocusable = false |
| 56 | + importantForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_NO |
| 57 | + setViewCompositionStrategy( |
| 58 | + ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed |
| 59 | + ) |
| 60 | + setContent { |
| 61 | + EatssuTheme { |
| 62 | + AnyoneButMeEventTooltip() |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private fun observeLayout() { |
| 69 | + bottomNavigationView.addOnLayoutChangeListener(bottomNavigationLayoutChangeListener) |
| 70 | + tooltipComposeView.addOnLayoutChangeListener(tooltipLayoutChangeListener) |
| 71 | + |
| 72 | + bottomNavigationView.doOnLayout { updateTooltipPosition() } |
| 73 | + tooltipComposeView.doOnLayout { updateTooltipPosition() } |
| 74 | + tooltipComposeView.post { updateTooltipPosition() } |
| 75 | + } |
| 76 | + |
| 77 | + private fun updateTooltipPosition() { |
| 78 | + if (!::tooltipComposeView.isInitialized || !::bottomNavigationView.isInitialized) return |
| 79 | + if (tooltipComposeView.width == 0 || tooltipComposeView.height == 0) return |
| 80 | + |
| 81 | + val itemWidth = bottomNavigationView.width / BOTTOM_NAVIGATION_ITEM_COUNT.toFloat() |
| 82 | + val itemCenterX = itemWidth * ANYONE_BUT_ME_MENU_INDEX + (itemWidth / 2f) |
| 83 | + |
| 84 | + tooltipComposeView.x = |
| 85 | + bottomNavigationView.x + |
| 86 | + itemCenterX - |
| 87 | + (tooltipComposeView.width / 2f) |
| 88 | + tooltipComposeView.y = |
| 89 | + bottomNavigationView.y - tooltipComposeView.height.toFloat() |
| 90 | + tooltipComposeView.visibility = View.VISIBLE |
| 91 | + } |
| 92 | +} |
0 commit comments