homeVm
This commit is contained in:
@@ -24,6 +24,7 @@ import kotlinx.coroutines.tasks.await
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
import java.util.concurrent.TimeUnit
|
||||
import javax.inject.Inject
|
||||
|
||||
@HiltViewModel
|
||||
@@ -48,37 +49,38 @@ class HomeViewModel @Inject constructor(
|
||||
private val appUpdater = AppUpdater(applicationContext)
|
||||
fun checkForUpdates(context: Context) {
|
||||
viewModelScope.launch {
|
||||
UpdateChecker.checkForUpdate(context)
|
||||
.onSuccess { updateInfo ->
|
||||
UpdateChecker.checkForUpdate(context).onSuccess { updateInfo ->
|
||||
if (updateInfo.isNew) {
|
||||
_updateInfo.value = updateInfo
|
||||
_isUpdateDialogVisible.value = true
|
||||
}
|
||||
}
|
||||
.onFailure { _ ->
|
||||
}.onFailure { _ ->
|
||||
// Handle error
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun installUpdate( url: String) {
|
||||
fun installUpdate(url: String) {
|
||||
appUpdater.downloadAndInstall(url)
|
||||
viewModelScope.launch {
|
||||
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
|
||||
}
|
||||
@@ -92,15 +94,14 @@ class HomeViewModel @Inject constructor(
|
||||
_updateInfo.value = null
|
||||
_downloadProgress.value = false
|
||||
}
|
||||
|
||||
init {
|
||||
networkMonitor.startMonitoring()
|
||||
networkMonitor.isConnected.asFlow()
|
||||
.onEach { isConnected ->
|
||||
networkMonitor.isConnected.asFlow().onEach { isConnected ->
|
||||
_uiState.update { currentState ->
|
||||
currentState.copy(isConnected = isConnected)
|
||||
}
|
||||
}
|
||||
.launchIn(viewModelScope)
|
||||
}.launchIn(viewModelScope)
|
||||
if (uiState.value.isConnected) {
|
||||
viewModelScope.launch {
|
||||
startScanning()
|
||||
@@ -185,7 +186,7 @@ class HomeViewModel @Inject constructor(
|
||||
val attendanceDoc = getAttendanceDocument(userId, currentDate)
|
||||
|
||||
if (attendanceDoc?.exists() == true) {
|
||||
handleExistingAttendance( attendanceDoc)
|
||||
handleExistingAttendance(attendanceDoc)
|
||||
} else {
|
||||
handleNewAttendance(userId, currentDate)
|
||||
}
|
||||
@@ -210,11 +211,13 @@ class HomeViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun handleExistingAttendance( document: DocumentSnapshot) {
|
||||
private suspend fun handleExistingAttendance(document: DocumentSnapshot) {
|
||||
val checkOut = document.getString("checkOut")
|
||||
val checkIn = document.getString("checkIn")
|
||||
|
||||
if (isTimeDifferenceGreaterThanN(inputTime = checkIn!!,n=5)){
|
||||
when {
|
||||
checkOut == "NA" -> processCheckOut( document)
|
||||
checkOut == "NA" -> processCheckOut(document)
|
||||
checkOut != null -> {
|
||||
_uiState.update {
|
||||
it.copy(
|
||||
@@ -222,9 +225,11 @@ class HomeViewModel @Inject constructor(
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
else -> handleError("Invalid attendance record")
|
||||
}
|
||||
handleError("Wait 5 minutes before logging off")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private suspend fun handleNewAttendance(userId: String, currentDate: String) {
|
||||
@@ -248,7 +253,7 @@ class HomeViewModel @Inject constructor(
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun processCheckOut( document: DocumentSnapshot) {
|
||||
private suspend fun processCheckOut(document: DocumentSnapshot) {
|
||||
try {
|
||||
val checkOutData = hashMapOf(
|
||||
"checkOut" to formatTimestamp(Timestamp.now()), "DayCompleted" to true
|
||||
@@ -298,11 +303,27 @@ class HomeViewModel @Inject constructor(
|
||||
|
||||
private fun formatTimestamp(timestamp: Timestamp): String {
|
||||
return SimpleDateFormat(
|
||||
"dd MMMM yyyy hh:mm:ss a",
|
||||
Locale.getDefault()
|
||||
"dd MMMM yyyy hh:mm:ss a", Locale.getDefault()
|
||||
).format(timestamp.toDate())
|
||||
}
|
||||
|
||||
private fun isJustCheckedIn(checkInTime: String): Boolean {
|
||||
var finalValue = false
|
||||
val currentTime = SimpleDateFormat("dd MMMM yyyy hh:mm:ss a", Locale.getDefault()).format(Timestamp.now().toDate()).toList()
|
||||
val checkInTimeList = checkInTime.toList()
|
||||
for (i in checkInTimeList.indices){
|
||||
for (j in currentTime.indices){
|
||||
val a = checkInTimeList[i].digitToInt()
|
||||
val b = currentTime[j].digitToInt()
|
||||
println(a)
|
||||
if (a>= b){
|
||||
!finalValue
|
||||
}
|
||||
}
|
||||
}
|
||||
return true
|
||||
|
||||
}
|
||||
|
||||
// fun isAppDisabled(): Boolean {
|
||||
// val a = firebaseFirestore.collection("deviceState").document("state")
|
||||
@@ -310,6 +331,22 @@ class HomeViewModel @Inject constructor(
|
||||
// return true
|
||||
// }
|
||||
|
||||
fun isTimeDifferenceGreaterThanN(inputTime: String, n: Int): Boolean {
|
||||
val dateFormat = SimpleDateFormat("dd MMMM yyyy hh:mm:ss a", Locale.ENGLISH)
|
||||
val givenDate: Date? = dateFormat.parse(inputTime)
|
||||
|
||||
if (givenDate == null) {
|
||||
println("Invalid date format")
|
||||
return false
|
||||
}
|
||||
|
||||
val currentDate = Date()
|
||||
val timeDifferenceMillis = givenDate.time - currentDate.time
|
||||
val timeDifferenceMinutes = TimeUnit.MILLISECONDS.toMinutes(timeDifferenceMillis)
|
||||
|
||||
return timeDifferenceMinutes > n
|
||||
}
|
||||
|
||||
override fun onCleared() {
|
||||
super.onCleared()
|
||||
networkMonitor.stopMonitoring()
|
||||
|
||||
Reference in New Issue
Block a user