Тебе нужно сравнить первоначальную версию Fragment и ViewModel и конечную и объяснить очень подробно мне, всё, что изменилось и для чего это сделано. package ru.maxi.mobile.feature.bonusPoints.ui.container

import android.os.Bundle
import android.view.View
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.viewpager2.widget.ViewPager2
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import ru.maxi.mobile.R
import ru.maxi.mobile.base.ui.MvvmBaseFragment
import ru.maxi.mobile.common.utils.android.screenArgs
import ru.maxi.mobile.databinding.FragmentBonusPointsContainerBinding
import ru.maxi.mobile.feature.bonusPoints.BonusPointsScreens
import ru.maxi.mobile.feature.bonusPoints.ui.container.model.BonusPointsTabPage
import ru.maxi.mobile.feature.bonusPoints.ui.container.model.BonusPointsViewState
import ru.maxi.mobile.uikit.utils.lifecycleAwareLazy

/**
 * [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<BonusPointsContainerViewModel>()

    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) {
            if (isFirstOpen) {
                val tabPosition = when (screenArgs.tabId) {
                    BALANCE_TAB -> 0
                    INFO_TAB -> 1
                    else -> throw IllegalArgumentException("invalid tabId ${screenArgs.tabId}")
                }
                viewBinding.tabsButtonContainer.check(screenArgs.tabId)
                viewBinding.tabsContainer.setCurrentItem(tabPosition, false)
                isFirstOpen = false
            } 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<BonusPointsTabPage> = 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 ->
                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) {
            tabsButtonContainer.check(state.tabButtonId)
            tabsContainer.setCurrentItem(state.currentPosition, true)
        }
    }

    companion object {
        val BALANCE_TAB = R.id.balance_tab
        val INFO_TAB = R.id.info_tab
    }
}
package ru.maxi.mobile.feature.bonusPoints.ui.container

import android.os.Bundle
import android.view.View
import androidx.annotation.IdRes
import androidx.fragment.app.viewModels
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.viewpager2.widget.ViewPager2
import by.kirich1409.viewbindingdelegate.viewBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import ru.maxi.mobile.R
import ru.maxi.mobile.base.ui.MvvmBaseFragment
import ru.maxi.mobile.databinding.FragmentBonusPointsContainerBinding
import ru.maxi.mobile.feature.bonusPoints.BonusPointsScreens
import ru.maxi.mobile.feature.bonusPoints.ui.container.model.BonusPointsTabPage
import ru.maxi.mobile.feature.bonusPoints.ui.container.model.BonusPointsViewState
import ru.maxi.mobile.uikit.utils.lifecycleAwareLazy

/**
 * [Fragment] контейнера для баллов
 */
@AndroidEntryPoint
class BonusPointsContainerFragment : MvvmBaseFragment(R.layout.fragment_bonus_points_container) {

    private val viewBinding by viewBinding(FragmentBonusPointsContainerBinding::bind)

    private val viewModel by viewModels<BonusPointsContainerViewModel>()

    private val pageAdapter by lifecycleAwareLazy {
        BonusPointsTabAdapter(this)
    }

    private val pageChangeListener = object : ViewPager2.OnPageChangeCallback() {
        override fun onPageSelected(position: Int) {
            BonusPointsScreens.ContainerTab.entries
                .getOrNull(position)
                ?.let(viewModel::updateCurrentTab)
        }
    }

    private val pageList: MutableList<BonusPointsTabPage> = mutableListOf(
        BonusPointsTabPage(BonusPointsScreens.BonusPointsHistoryScreen()),
        BonusPointsTabPage(BonusPointsScreens.InfoScreen()),
    )

    private var isFirstOpen = true

    override val analyticsName: String? = null

    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 ->
                BonusPointsScreens.ContainerTab.entries
                    .find { it.buttonId == buttonId }
                    ?.let(viewModel::updateCurrentTab)
            }
        }

        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) {
            tabsButtonContainer.check(state.currentTab.buttonId)
            tabsContainer.setCurrentItem(state.currentTab.ordinal, !isFirstOpen)
        }
        isFirstOpen = false
    }
}

@get:IdRes
private val BonusPointsScreens.ContainerTab.buttonId: Int
    get() = when (this) {
        BonusPointsScreens.ContainerTab.BALANCE_TAB -> R.id.balance_tab
        BonusPointsScreens.ContainerTab.INFO_TAB -> R.id.info_tab
    }
package ru.maxi.mobile.feature.bonusPoints.ui.container

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import ru.maxi.mobile.R
import ru.maxi.mobile.feature.bonusPoints.ui.container.model.BonusPointsViewState
import javax.inject.Inject

/**
 * [ViewModel] для контейнера баллов
 */
@HiltViewModel
class BonusPointsContainerViewModel @Inject constructor() : ViewModel() {

    private val _viewState = MutableStateFlow(BonusPointsViewState(tabButtonId = R.id.balance_tab, currentPosition = 0))

    /** Состояние экрана */
    val viewState: Flow<BonusPointsViewState> = _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 }
    }
}
package ru.maxi.mobile.feature.bonusPoints.ui.container

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.flow.update
import ru.maxi.mobile.common.utils.android.screenArgs
import ru.maxi.mobile.feature.bonusPoints.BonusPointsScreens
import ru.maxi.mobile.feature.bonusPoints.ui.container.model.BonusPointsViewState
import javax.inject.Inject

/**
 * [ViewModel] для контейнера баллов
 */
@HiltViewModel
class BonusPointsContainerViewModel @Inject constructor(
    savedStateHandle: SavedStateHandle,
) : ViewModel() {

    private val screenArgs by screenArgs(savedStateHandle, BonusPointsContainerArgs)

    private val _viewState = MutableStateFlow(BonusPointsViewState(screenArgs.initialTab))

    /** Состояние экрана */
    val viewState: Flow<BonusPointsViewState> = _viewState.asStateFlow()
        .stateIn(viewModelScope, SharingStarted.Lazily, _viewState.value)

    /**
     * Обновить текущий таб на [tab]
     */
    fun updateCurrentTab(tab: BonusPointsScreens.ContainerTab) {
        _viewState.update { BonusPointsViewState(tab) }
    }
}