package com.pixelized.biblib.ui.screen.home.common.connectivity

import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.pixelized.biblib.repository.connectivity.IConnectivityRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class ConnectivityViewModel @Inject constructor(
    private val connectivityRepository: IConnectivityRepository,
) : ViewModel() {

    private val _isNetworkAvailable = mutableStateOf(connectivityRepository.isNetworkAvailable)
    val isNetworkAvailable: State<Boolean> get() = _isNetworkAvailable

    init {
        viewModelScope.launch {
            connectivityRepository.networkAvailabilityFlow.collect {
                _isNetworkAvailable.value = it
            }
        }
    }
}