Skip to content

Commit

Permalink
feat: Update checked, with support for stable and snapshot builds
Browse files Browse the repository at this point in the history
feat: Show launchy version in top bar
  • Loading branch information
0ffz committed May 31, 2024
1 parent f8382b5 commit b80cdad
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 3 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
group=com.mineinabyss
version=2.0.0-beta.1
version=2.0.0-beta.2
idofrontVersion=0.24.3
3 changes: 3 additions & 0 deletions src/main/kotlin/com/mineinabyss/launchy/data/Constants.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ import androidx.compose.ui.unit.dp
object Constants {
val SETTINGS_HORIZONTAL_PADDING = 10.dp
val SETTINGS_PRIMARY_BUTTON_WIDTH = 140.dp

val APP_VERSION = System.getProperty("jpackage.app-version") ?: null
val GITHUB_REPO = "MineInAbyss/launchy"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.mineinabyss.launchy.logic

import com.mineinabyss.launchy.data.Constants
import com.mineinabyss.launchy.data.Formats
import io.ktor.client.request.*
import io.ktor.client.statement.*
import io.ktor.http.*
import kotlinx.serialization.Serializable

sealed interface AppUpdateState {
data object Unknown : AppUpdateState
data object NoUpdate : AppUpdateState
data class UpdateAvailable(val release: GithubRelease) : AppUpdateState
}

object GithubUpdateChecker {
suspend fun checkForUpdates(): AppUpdateState = runCatching {
val currVersion = Constants.APP_VERSION ?: return AppUpdateState.Unknown
val currTag = "v$currVersion"
val current = Downloader.httpClient.get {
url("https://api.github.com/repos/${Constants.GITHUB_REPO}/releases/tags/$currTag")
}
val currentRelease = if (current.status != HttpStatusCode.OK) null
else Formats.json.decodeFromString<GithubRelease>(current.bodyAsText())

val latest = if (currentRelease?.prerelease == true) getLatestPrerelease()
else getLatestStableRelease()
println("Current $currentRelease")
println("Latest $latest")
return if (latest.tag_name == currTag) AppUpdateState.NoUpdate else AppUpdateState.UpdateAvailable(latest)
}.getOrDefault(AppUpdateState.Unknown)

suspend fun getLatestPrerelease(): GithubRelease {
val response = Downloader.httpClient.get {
url("https://api.github.com/repos/${Constants.GITHUB_REPO}/releases")
}

val releases = Formats.json.decodeFromString<List<GithubRelease>>(response.bodyAsText())
return releases.maxBy { it.published_at }
}

suspend fun getLatestStableRelease(): GithubRelease {
val response = Downloader.httpClient.get {
url("https://api.github.com/repos/${Constants.GITHUB_REPO}/releases/latest")
}
return Formats.json.decodeFromString<GithubRelease>(response.bodyAsText())
}
}


@Serializable
data class GithubRelease(
val published_at: String,
val tag_name: String,
val html_url: String,
val prerelease: Boolean,
)
3 changes: 2 additions & 1 deletion src/main/kotlin/com/mineinabyss/launchy/ui/TopBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.WindowPlacement
import com.mineinabyss.launchy.LocalLaunchyState
import com.mineinabyss.launchy.data.Constants
import com.mineinabyss.launchy.ui.elements.BetterWindowDraggableArea
import com.mineinabyss.launchy.ui.state.TopBarState

Expand Down Expand Up @@ -128,7 +129,7 @@ fun LaunchyTitle() {
tint = MaterialTheme.colorScheme.primary
)
Text(
"Launchy",
"Launchy - ${Constants.APP_VERSION ?: "dev"}",
fontWeight = FontWeight.Medium,
color = MaterialTheme.colorScheme.primary
)
Expand Down
25 changes: 24 additions & 1 deletion src/main/kotlin/com/mineinabyss/launchy/ui/screens/Screens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,18 @@ import androidx.compose.animation.core.animateDpAsState
import androidx.compose.foundation.layout.*
import androidx.compose.material.LinearProgressIndicator
import androidx.compose.material.LocalTextStyle
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.rounded.Update
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import com.mineinabyss.launchy.LocalLaunchyState
import com.mineinabyss.launchy.logic.AppUpdateState
import com.mineinabyss.launchy.logic.DesktopHelpers
import com.mineinabyss.launchy.logic.GithubUpdateChecker
import com.mineinabyss.launchy.state.InProgressTask
import com.mineinabyss.launchy.state.modpack.GameInstanceState
import com.mineinabyss.launchy.ui.AppTopBar
Expand All @@ -31,6 +36,7 @@ import com.mineinabyss.launchy.ui.state.TopBar
var screen: Screen by mutableStateOf(Screen.Default)

var dialog: Dialog by mutableStateOf(Dialog.None)
var updateAvailable: AppUpdateState by mutableStateOf(AppUpdateState.Unknown)

private val ModpackStateProvider = compositionLocalOf<GameInstanceState> { error("No local modpack provided") }

Expand All @@ -41,7 +47,20 @@ val LocalGameInstanceState: GameInstanceState

@Composable
fun Screens() = Scaffold(
snackbarHost = { SnackbarHost(snackbarHostState) }
snackbarHost = { SnackbarHost(snackbarHostState) },
floatingActionButton = {
val update = updateAvailable
Row {
AnimatedVisibility(update is AppUpdateState.UpdateAvailable) {
if (update !is AppUpdateState.UpdateAvailable) return@AnimatedVisibility
ExtendedFloatingActionButton(
text = { Text("Update available") },
icon = { Icon(Icons.Rounded.Update, "") },
onClick = { DesktopHelpers.browse(update.release.html_url) },
)
}
}
}
) {
val state = LocalLaunchyState
val packState = state.instanceState
Expand All @@ -66,6 +85,10 @@ fun Screens() = Scaffold(
LaunchedEffect(isDefault, state.ui.preferHue) {
if (isDefault) currentHue = state.ui.preferHue
}
LaunchedEffect(Unit) {
updateAvailable = GithubUpdateChecker.checkForUpdates()
}


AppTopBar(
state = TopBar,
Expand Down

0 comments on commit b80cdad

Please sign in to comment.