AppUpdator.kt

This commit is contained in:
sathwikcs
2025-02-03 16:57:59 +05:30
parent cccbecf934
commit fa75748e2c

View File

@@ -1,16 +1,13 @@
package `in`.sminnovations.attendanceapp.main package `in`.sminnovations.attendanceapp.utils
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.core.content.FileProvider import androidx.core.content.FileProvider
import com.google.firebase.firestore.ktx.firestore
import com.google.firebase.ktx.Firebase
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import kotlinx.coroutines.tasks.await
import kotlinx.coroutines.withContext import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient import okhttp3.OkHttpClient
import okhttp3.Request import okhttp3.Request
@@ -30,81 +27,6 @@ data class UpdateInfo(
val releaseNote : String = "" val releaseNote : String = ""
) )
object UpdateChecker {
private const val COLLECTION_NAME = "app_updates"
private const val DOCUMENT_NAME = "latest_version"
suspend fun checkForUpdate(
context: Context
): Result<UpdateInfo> = runCatching {
val currentVersion = getCurrentVersion(context)
val updateInfo = getLatestVersion()
if (isNewerVersion(currentVersion, updateInfo.version)) {
updateInfo.copy(isNew = true)
} else {
updateInfo.copy(isNew = false)
}
}
private fun getCurrentVersion(context: Context): String {
return try {
context.packageManager.getPackageInfo(context.packageName, 0).versionName
} catch (e: Exception) {
throw UpdateCheckException("Failed to get current app version", e)
}
}
private suspend fun getLatestVersion(): UpdateInfo {
return try {
val document = Firebase.firestore
.collection(COLLECTION_NAME)
.document(DOCUMENT_NAME)
.get()
.await()
if (!document.exists()) {
throw UpdateCheckException("Update document does not exist")
}
UpdateInfo(
version = document.getString("version") ?: throw UpdateCheckException("Version field missing"),
url = document.getString("url") ?: throw UpdateCheckException("URL field missing"),
releaseNote = document.getString("ReleaseNote") ?: throw UpdateCheckException("ReleaseNote field missing")
)
} catch (e: Exception) {
throw UpdateCheckException("Failed to fetch update info", e)
}
}
private fun isNewerVersion(currentVersion: String, latestVersion: String): Boolean {
return try {
val current = currentVersion.split(".").map { it.toIntOrNull() ?: 0 }
val latest = latestVersion.split(".").map { it.toIntOrNull() ?: 0 }
for (i in 0 until maxOf(current.size, latest.size)) {
val currentNum = current.getOrNull(i) ?: 0
val latestNum = latest.getOrNull(i) ?: 0
when {
latestNum > currentNum -> return true
latestNum < currentNum -> return false
}
}
false
} catch (e: Exception) {
false
}
}
}
class UpdateCheckException(
message: String,
cause: Throwable? = null
) : Exception(message, cause){
}
class AppUpdater(private val context: Context) { class AppUpdater(private val context: Context) {
sealed class UpdaterState { sealed class UpdaterState {