Skip to content

Commit

Permalink
feat(settings/about): add build information
Browse files Browse the repository at this point in the history
  • Loading branch information
aliernfrog committed Sep 12, 2024
1 parent 482cc08 commit 58eca90
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 12 deletions.
37 changes: 37 additions & 0 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import org.apache.commons.io.output.ByteArrayOutputStream

plugins {
id("com.android.application")
id("org.jetbrains.kotlin.android")
Expand Down Expand Up @@ -81,6 +83,41 @@ android.defaultConfig.buildConfigField("String[]", "LANGUAGES", "new String[]{${
languages.joinToString(",") { "\"$it\"" }
}}")

// Utilities to get git environment information
// Source: https://github.com/vendetta-mod/VendettaManager/blob/main/app/build.gradle.kts
fun getCurrentBranch() = exec("git", "symbolic-ref", "--short", "HEAD")
?: exec("git", "describe", "--tags", "--exact-match")
fun getLatestCommit() = exec("git", "rev-parse", "--short", "HEAD")
fun hasLocalChanges(): Boolean {
val branch = getCurrentBranch()
val uncommittedChanges = exec("git", "status", "-s")?.isNotEmpty() ?: false
val unpushedChanges = exec("git", "log", "origin/$branch..HEAD")?.isNotBlank() ?: false
return uncommittedChanges || unpushedChanges
}

android.defaultConfig.run {
buildConfigField("String", "GIT_BRANCH", "\"${getCurrentBranch()}\"")
buildConfigField("String", "GIT_COMMIT", "\"${getLatestCommit()}\"")
buildConfigField("boolean", "GIT_LOCAL_CHANGES", "${hasLocalChanges()}")
}

fun exec(vararg command: String) = try {
val stdout = ByteArrayOutputStream()
val errout = ByteArrayOutputStream()
exec {
commandLine = command.toList()
standardOutput = stdout
errorOutput = errout
isIgnoreExitValue = true
}

if (errout.size() > 0) throw Error(errout.toString(Charsets.UTF_8))
stdout.toString(Charsets.UTF_8).trim()
} catch (_: Throwable) {
null
}


dependencies {
implementation("androidx.core:core-ktx:1.13.1")
implementation("androidx.core:core-splashscreen:1.0.1")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,6 @@ fun AboutPage(
val appIcon = remember {
context.packageManager.getApplicationIcon(context.packageName).toBitmap().asImageBitmap()
}
val appVersion = remember {
"${mainViewModel.applicationVersionName} (${mainViewModel.applicationVersionCode})"
}

SettingsPageContainer(
title = stringResource(R.string.settings_about),
Expand Down Expand Up @@ -111,7 +108,7 @@ fun AboutPage(
style = MaterialTheme.typography.titleLarge
)
Text(
text = appVersion,
text = mainViewModel.applicationVersionLabel,
style = MaterialTheme.typography.bodyMedium,
modifier = Modifier.clickable {
settingsViewModel.onAboutClick()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
Expand Down Expand Up @@ -92,9 +91,6 @@ private fun SettingsRootPage(
onNavigateRequest: (SettingsPage) -> Unit
) {
val scope = rememberCoroutineScope()
val appVersion = remember {
"${mainViewModel.applicationVersionName} (${mainViewModel.applicationVersionCode})"
}

AppScaffold(
topBar = { scrollBehavior ->
Expand Down Expand Up @@ -126,7 +122,8 @@ private fun SettingsRootPage(
.forEach { page ->
ButtonRow(
title = stringResource(page.title),
description = if (page == SettingsPage.ABOUT) appVersion else stringResource(page.description),
description = if (page == SettingsPage.ABOUT) mainViewModel.applicationVersionLabel
else stringResource(page.description),
painter = rememberVectorPainter(page.icon)
) {
onNavigateRequest(page)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import androidx.core.app.LocaleManagerCompat
import androidx.core.os.LocaleListCompat
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.aliernfrog.pftool.BuildConfig
import com.aliernfrog.pftool.R
import com.aliernfrog.pftool.SettingsConstant
import com.aliernfrog.pftool.TAG
Expand Down Expand Up @@ -64,9 +65,19 @@ class MainViewModel(
lateinit var scope: CoroutineScope
val updateSheetState = SheetState(skipPartiallyExpanded = false, Density(context))

val applicationVersionName = "v${GeneralUtil.getAppVersionName(context)}"
val applicationVersionCode = GeneralUtil.getAppVersionCode(context)
private val applicationVersionName = "v${GeneralUtil.getAppVersionName(context)}"
private val applicationVersionCode = GeneralUtil.getAppVersionCode(context)
private val applicationIsPreRelease = applicationVersionName.contains("-alpha")
val applicationVersionLabel = "$applicationVersionName (${
BuildConfig.GIT_COMMIT.ifBlank { applicationVersionCode.toString() }
}${
if (BuildConfig.GIT_LOCAL_CHANGES) "*" else ""
}${
BuildConfig.GIT_BRANCH.let {
if (it == applicationVersionName) ""
else " - ${it.ifBlank { "local" }}"
}
})"

private val defaultLanguage = GeneralUtil.getLanguageFromCode("en-US")!!
val deviceLanguage = LocaleManagerCompat.getSystemLocales(context)[0]?.toLanguage() ?: defaultLanguage
Expand Down Expand Up @@ -96,7 +107,7 @@ class MainViewModel(

val debugInfo: String
get() = arrayOf(
"PF Tool $applicationVersionName ($applicationVersionCode)",
"PF Tool $applicationVersionLabel",
"Android API ${Build.VERSION.SDK_INT}",
"Storage access type ${prefs.storageAccessType}",
SettingsConstant.experimentalPrefOptions.joinToString("\n") {
Expand Down

0 comments on commit 58eca90

Please sign in to comment.