Мне нужно перенести логику открытия контейнера в первый раз из Fragment во ViewModel и управлять этим через viewState.  /**

[Fragment] контейнера для баллов
*/
@AndroidEntryPoint
class BonusPointsContainerFragment : MvvmBaseFragment(R.layout.fragment_bonus_points_container) { private val viewBinding by viewBinding(FragmentBonusPointsContainerBinding::bind) private val screenArgs by screenArgs(BonusPointsContainerArgs) private val viewModel by viewModels() private val pageAdapter by lifecycleAwareLazy {
BonusPointsTabAdapter(this)
} override val analyticsName: String? = null private var isFirstOpen = true private val pageChangeListener = object : ViewPager2.OnPageChangeCallback() {
override fun onPageSelected(position: Int) {
Log.d("TESTING", "onPageSelected")
if (isFirstOpen) {
val tabPosition = when (screenArgs.tabId) {
BALANCE_TAB -> 0
INFO_TAB -> 1
else -> throw IllegalArgumentException("invalid tabId ${screenArgs.tabId}")
}
viewModel.updateCurrentTab(screenArgs.tabId, tabPosition)
} else {
val tab = when (position) {
0 -> BALANCE_TAB
1 -> INFO_TAB
else -> throw IllegalArgumentException("invalid tabId ${screenArgs.tabId}")
}
viewModel.updateCurrentTab(tab, position)
}
}
} private val pageList: MutableList = mutableListOf(
BonusPointsTabPage(BonusPointsScreens.BonusPointsHistoryScreen()),
BonusPointsTabPage(BonusPointsScreens.InfoScreen()),
) override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
actionBarManager.setTitle(R.string.bonus_points_screen_title)
actionBarManager.showBackButton()

with(viewBinding) {
tabsContainer.registerOnPageChangeCallback(pageChangeListener)
tabsContainer.adapter = pageAdapter
pageAdapter.pages = pageList

tabsButtonContainer.setOnButtonCheckedListener { buttonId ->
Log.d("TESTING", "setOnButtonCheckedListener")
when (buttonId) {
BALANCE_TAB -> {
viewModel.updateCurrentTab(BALANCE_TAB, 0)
}

INFO_TAB -> {
viewModel.updateCurrentTab(INFO_TAB, 1)
}
}
}
}
with(viewLifecycleOwner) {
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
launch { viewModel.viewState.collect(::onViewStateChange) }
}
}
}
} override fun onDestroyView() {
super.onDestroyView()
viewBinding.tabsContainer.unregisterOnPageChangeCallback(pageChangeListener)
} private fun onViewStateChange(state: BonusPointsViewState) {
with(viewBinding) {
Log.d("TESTING", "onViewStateChange.tabButtonId = ${state.tabButtonId}")
tabsButtonContainer.check(state.tabButtonId)
if (isFirstOpen) {
tabsContainer.setCurrentItem(state.currentPosition, false)
isFirstOpen = false
} else {
tabsContainer.setCurrentItem(state.currentPosition, true)
}
}
} companion object {
val BALANCE_TAB = R.id.balance_tab
val INFO_TAB = R.id.info_tab
}
}

/**

[ViewModel] для контейнера баллов
*/
@HiltViewModel
class BonusPointsContainerViewModel @Inject constructor() : ViewModel() { private val _viewState = MutableStateFlow(BonusPointsViewState(tabButtonId = R.id.balance_tab, currentPosition = 0)) /** Состояние экрана /
val viewState: Flow = _viewState.asStateFlow()
.stateIn(viewModelScope, SharingStarted.Lazily, _viewState.value)
/*
Обновить текущий таб
@param tabButtonId идентификатор кнопки таба
@param currentPosition позиция таба
*/
fun updateCurrentTab(tabButtonId: Int, currentPosition: Int) {
val state = BonusPointsViewState(tabButtonId, currentPosition)
_viewState.update { state }
}
}