homeVM
This commit is contained in:
@@ -2,8 +2,11 @@ package `in`.sminnovations.attendanceapp.screens.home.viewModel
|
||||
|
||||
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.Uri
|
||||
import android.widget.Toast
|
||||
import androidx.lifecycle.LiveData
|
||||
import androidx.lifecycle.ViewModel
|
||||
import androidx.lifecycle.asFlow
|
||||
@@ -12,7 +15,10 @@ import com.google.firebase.Timestamp
|
||||
import com.google.firebase.firestore.DocumentSnapshot
|
||||
import com.google.firebase.firestore.FirebaseFirestore
|
||||
import dagger.hilt.android.lifecycle.HiltViewModel
|
||||
import `in`.sminnovations.attendanceapp.main.AppUpdater
|
||||
import `in`.sminnovations.attendanceapp.main.NetworkMonitor
|
||||
import `in`.sminnovations.attendanceapp.main.UpdateChecker
|
||||
import `in`.sminnovations.attendanceapp.main.UpdateInfo
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.flow.StateFlow
|
||||
import kotlinx.coroutines.flow.asStateFlow
|
||||
@@ -36,8 +42,64 @@ class HomeViewModel @Inject constructor(
|
||||
private val _uiState = MutableStateFlow(AttendanceUiState())
|
||||
val uiState: StateFlow<AttendanceUiState> = _uiState.asStateFlow()
|
||||
|
||||
init {
|
||||
private val _updateInfo = MutableStateFlow<UpdateInfo?>(null)
|
||||
val updateInfo = _updateInfo.asStateFlow()
|
||||
|
||||
private val _isUpdateDialogVisible = MutableStateFlow(false)
|
||||
val isUpdateDialogVisible = _isUpdateDialogVisible.asStateFlow()
|
||||
|
||||
private val _downloadProgress = MutableStateFlow<Boolean>(false)
|
||||
val downloadProgress = _downloadProgress.asStateFlow()
|
||||
|
||||
private val appUpdater = AppUpdater(applicationContext)
|
||||
fun checkForUpdates(context: Context) {
|
||||
viewModelScope.launch {
|
||||
UpdateChecker.checkForUpdate(context)
|
||||
.onSuccess { updateInfo ->
|
||||
if (updateInfo.isNew) {
|
||||
_updateInfo.value = updateInfo
|
||||
_isUpdateDialogVisible.value = true
|
||||
}
|
||||
}
|
||||
.onFailure { error ->
|
||||
// Handle error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun installUpdate(context: Context, url: String) {
|
||||
|
||||
appUpdater.downloadAndInstall(url)
|
||||
viewModelScope.launch {
|
||||
appUpdater.updateState.collect { state ->
|
||||
when (state) {
|
||||
is AppUpdater.UpdaterState.Downloading -> {
|
||||
// Update progress UI (state.progress is between 0 and 1)
|
||||
_downloadProgress.value = true
|
||||
}
|
||||
is AppUpdater.UpdaterState.Success -> {
|
||||
// APK downloaded and installation started
|
||||
// You might want to show a message
|
||||
_downloadProgress.value = false
|
||||
}
|
||||
is AppUpdater.UpdaterState.Error -> {
|
||||
// Handle error (show message, etc.)
|
||||
|
||||
}
|
||||
AppUpdater.UpdaterState.Idle -> {
|
||||
// Initial state
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun dismissUpdate() {
|
||||
_isUpdateDialogVisible.value = false
|
||||
_updateInfo.value = null
|
||||
_downloadProgress.value = false
|
||||
}
|
||||
init {
|
||||
networkMonitor.startMonitoring()
|
||||
networkMonitor.isConnected.asFlow()
|
||||
.onEach { isConnected ->
|
||||
@@ -54,15 +116,12 @@ class HomeViewModel @Inject constructor(
|
||||
|
||||
}
|
||||
|
||||
fun stopMonitoring() {
|
||||
networkMonitor.stopMonitoring()
|
||||
}
|
||||
|
||||
fun reStartMonitoring() {
|
||||
networkMonitor.stopMonitoring()
|
||||
networkMonitor.startMonitoring()
|
||||
}
|
||||
|
||||
|
||||
fun startScanning() {
|
||||
viewModelScope.launch {
|
||||
_uiState.update { currentState ->
|
||||
@@ -252,20 +311,12 @@ class HomeViewModel @Inject constructor(
|
||||
}
|
||||
|
||||
|
||||
fun isDeviceConnectedToInternet(context: Context): Boolean {
|
||||
val connectivityManager =
|
||||
context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
val network = connectivityManager.activeNetwork ?: return false
|
||||
val networkCapabilities =
|
||||
connectivityManager.getNetworkCapabilities(network) ?: return false
|
||||
return when {
|
||||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_WIFI) -> true
|
||||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR) -> true
|
||||
networkCapabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET) -> true
|
||||
else -> false
|
||||
}
|
||||
fun isAppDisabled(): Boolean {
|
||||
val a = firebaseFirestore.collection("deviceState").document("state")
|
||||
println(a)
|
||||
return true
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
networkMonitor.stopMonitoring()
|
||||
@@ -273,14 +324,3 @@ class HomeViewModel @Inject constructor(
|
||||
|
||||
}
|
||||
|
||||
data class AttendanceUiState(
|
||||
val isScanning: Boolean = false,
|
||||
val qrCodeValue: String? = null,
|
||||
val userName: String? = null,
|
||||
val isCheckedIn: Boolean = false,
|
||||
val isCheckedOut: Boolean = false,
|
||||
val checkedOutTime: String = "",
|
||||
val errorMessage: String? = null,
|
||||
val isLoading: Boolean = false,
|
||||
val isConnected : Boolean =true
|
||||
)
|
||||
Reference in New Issue
Block a user