@@ -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