Skip to content

Commit ab213b1

Browse files
committed
fix: change version comparing
1 parent 3e45336 commit ab213b1

3 files changed

Lines changed: 29 additions & 5 deletions

File tree

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ on:
55
branches:
66
- '*'
77
tags-ignore:
8-
- 'release-*'
8+
- 'v[0-9]+.[0-9]+.[0-9]+'
99
pull_request:
1010
branches:
1111
- main

app/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ android {
1515
minSdk = 29
1616
targetSdk = 35
1717
versionCode = 2
18-
versionName = "0.0.4"
18+
versionName = "0.1.0"
1919

2020
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
2121
}

app/src/main/java/com/zekecode/akira_financialtracker/ui/viewmodels/MainViewModel.kt

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,39 @@ class MainViewModel @Inject constructor(
6464
}
6565
Log.d("MainViewModel", "Latest release response is: ${latestRelease.toString()}")
6666
latestRelease.let {
67-
val latestVersionCode = it.tagName.removePrefix("v").toIntOrNull() ?: 0
68-
val currentVersionCode = userRepository.getAppVersionCode()
67+
Log.d("MainViewModel", "Tag name is: ${it.tagName}")
68+
val latestVersion = it.tagName.removePrefix("v")
69+
Log.d("MainViewModel", "Latest version is: $latestVersion")
70+
val currentVersion = userRepository.getAppVersionName()
6971

70-
if (latestVersionCode > currentVersionCode) {
72+
if (currentVersion.isNullOrEmpty()) {
73+
Log.d("MainViewModel", "Can't retrieve current version...")
74+
_isUpdateAvailable.postValue(false)
75+
return@let
76+
}
77+
78+
if (isNewerVersion(latestVersion, currentVersion)) {
7179
_isUpdateAvailable.postValue(true)
7280
} else {
7381
_isUpdateAvailable.postValue(false)
7482
}
7583
}
7684
}
7785
}
86+
87+
private fun isNewerVersion(latestVersion: String, currentVersion: String): Boolean {
88+
val latestParts = latestVersion.split(".")
89+
val currentParts = currentVersion.split(".")
90+
91+
for (i in latestParts.indices) {
92+
val latestPart = latestParts.getOrNull(i)?.toIntOrNull() ?: 0
93+
val currentPart = currentParts.getOrNull(i)?.toIntOrNull() ?: 0
94+
if (latestPart > currentPart) {
95+
return true
96+
} else if (latestPart < currentPart) {
97+
return false
98+
}
99+
}
100+
return false
101+
}
78102
}

0 commit comments

Comments
 (0)