Skip to content

Commit

Permalink
SemVer for version checking.
Browse files Browse the repository at this point in the history
  • Loading branch information
zegkljan committed Apr 2, 2022
1 parent 3d1b6fa commit 06807ed
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 59 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,9 @@ dependencies {
implementation "io.ktor:ktor-client-core:$ktor_version"
implementation "io.ktor:ktor-client-android:$ktor_version"

// Semver
implementation "net.swiftzer.semver:semver:1.1.2"

// Unit testing
testImplementation 'androidx.test.ext:junit:1.1.3'
testImplementation 'androidx.test:rules:1.4.0'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
import androidx.navigation.Navigation
import cz.zegkljan.videoreferee.R
import cz.zegkljan.videoreferee.utils.LAST_IGNORED_VERSION_KEY
import io.ktor.client.*
import io.ktor.client.call.*
import io.ktor.client.engine.android.*
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.coroutines.launch
import net.swiftzer.semver.SemVer
import org.json.JSONArray
import org.json.JSONTokener

Expand Down Expand Up @@ -66,12 +68,12 @@ class VersionCheckFragment : Fragment() {

val prefs = activity.getPreferences(Context.MODE_PRIVATE)
val lastVersionStr = prefs.getString(LAST_IGNORED_VERSION_KEY, null)
val lastIgnoredVersion: Version? = if (lastVersionStr == null) {
val lastIgnoredVersion: SemVer? = if (lastVersionStr == null) {
null
} else {
Version.fromVersionString(lastVersionStr)
SemVer.parse(lastVersionStr)
}
val version = Version.fromVersionString(info.versionName)
val version = SemVer.parse(info.versionName)

lifecycleScope.launch {
val latest = getLatestRelease()
Expand Down Expand Up @@ -148,63 +150,9 @@ class VersionCheckFragment : Fragment() {

companion object {
const val GIT_RELEASES = "https://api.github.com/repos/zegkljan/videoreferee/releases"
/**
* A preferences key under which the last version that should be ignored when checking for new versions is stored.
* Only versions newer than the one stored under this key is offered to the user.
*/
const val LAST_IGNORED_VERSION_KEY = "last-ignored-version"

data class Version(val major: Int, val minor: Int, val patch: Int) : Comparable<Version> {
override fun compareTo(other: Version): Int {
if (major < other.major) {
return -1
} else if (major > other.major) {
return 1
}
if (minor < other.minor) {
return -1
} else if (minor > other.minor) {
return 1
}
if (patch < other.patch) {
return -1
} else if (patch > other.patch) {
return 1
}
return 0
}

override fun toString(): String {
return "$major.$minor.$patch"
}

companion object {
fun fromVersionString(versionString: String): Version {
val parts = versionString.split(".")
val major: Int
if (parts.isNotEmpty()) {
major = parts[0].toInt()
} else {
throw IllegalArgumentException("empty version")
}
val minor: Int = if (parts.size >= 2) {
parts[1].toInt()
} else {
0
}
val patch: Int = if (parts.size >= 3) {
parts[2].toInt()
} else {
0
}

return Version(major, minor, patch)
}
}
}

data class Release(val version: Version, val url: String) : Comparable<Release> {
constructor(versionString: String, url: String) : this(Version.fromVersionString(versionString), url)
data class Release(val version: SemVer, val url: String) : Comparable<Release> {
constructor(versionString: String, url: String) : this(SemVer.parse(versionString), url)

override fun compareTo(other: Release): Int {
return version.compareTo(other.version)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
package cz.zegkljan.videoreferee.utils

/**
* A preferences key under which the last version that should be ignored when checking for new versions is stored.
* Only versions newer than the one stored under this key is offered to the user.
*/
const val LAST_IGNORED_VERSION_KEY = "last-ignored-version"

const val CAMERA_SELECTOR_PREFS_VERSION = 2
const val CAMERA_SELECTOR_PREFS_VERSION_KEY = "camera-selector-prefs-version"
const val CAMERA_ID_KEY = "camera-id"
Expand Down

0 comments on commit 06807ed

Please sign in to comment.