Skip to content

added release state display to info app #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions app/src/main/kotlin/app/grapheneos/info/InfoApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ fun InfoApp() {
) {
ReleaseNotesScreen(
releaseNotesUiState.value.entries.toSortedMap().toList().asReversed(),
releaseNotesUiState.value.releaseStates.toSortedMap().toList(),
{ useCaches, onFinishedUpdating ->
releaseNotesViewModel.updateReleaseNotes(
useCaches = useCaches,
Expand All @@ -268,6 +269,15 @@ fun InfoApp() {
onFinishedUpdating = onFinishedUpdating,
)
},
{ useCaches, onFinishedUpdating ->
releaseNotesViewModel.updateReleaseStates(
useCaches = useCaches,
showSnackbarError = {
snackbarHostState.showSnackbar(it)
},
onFinishedUpdating = onFinishedUpdating,
)
},
releaseNotesLazyListState,
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ import kotlinx.coroutines.launch
@Composable
fun ReleaseNotesScreen(
entries: List<Pair<String, String>>,
releaseStates: List<Pair<String, String>>,
updateReleaseNotes: (useCaches: Boolean, finishedUpdating: () -> Unit) -> Unit,
updateReleaseStates: (useCaches: Boolean, finishedUpdating: () -> Unit) -> Unit,
lazyListState: LazyListState,
) {
val lifecycleOwner = LocalLifecycleOwner.current
Expand All @@ -49,6 +51,7 @@ fun ReleaseNotesScreen(
if (event == Lifecycle.Event.ON_START) {
refreshCoroutineScope.launch {
updateReleaseNotes(true) {}
updateReleaseStates(true) {}
}
}
}
Expand Down Expand Up @@ -105,6 +108,9 @@ fun ReleaseNotesScreen(
state = lazyListState,
verticalArrangement = Arrangement.Top
) {
item {
ReleaseState(releaseStates)
}
items(
items = entries,
key = { it.first }) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@ class ReleaseNotesUiState(savedStateHandle: SavedStateHandle) {
}
/** Unsorted release notes, use .toSortedMap().toList().asReversible() to get them in the proper order. */
val entries: MutableMap<String, String> = mutableStateMapOf()

/** Unsorted release states, use .toSortedMap().toList().asReversible() to get them in the proper order. */
val releaseStates: MutableMap<String, String> = mutableStateMapOf("stable" to "-", "beta" to "-", "alpha" to "-")
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class ReleaseNotesViewModel(
countAsInitialScroll = false,
onFinishedUpdating = {},
)
updateReleaseStates(
useCaches = true,
showSnackbarError = {},
onFinishedUpdating = {},
)
}

fun updateReleaseNotes(
Expand Down Expand Up @@ -121,6 +126,8 @@ class ReleaseNotesViewModel(
} finally {
connection.disconnect()
}


} catch (e: IOException) {
val errorMessage =
application.getString(R.string.update_release_notes_failed_to_create_httpsurlconnection_snackbar_message)
Expand All @@ -133,4 +140,79 @@ class ReleaseNotesViewModel(
}
}
}

fun updateReleaseStates(
useCaches: Boolean,
showSnackbarError: suspend (message: String) -> Unit,
onFinishedUpdating: () -> Unit = {},
) {
var board = android.os.Build.DEVICE
val releasePhases = arrayOf("stable", "beta", "alpha")
for (releasePhase in releasePhases) {
viewModelScope.launch(Dispatchers.IO) {

try {
val url = URL("https://releases.grapheneos.org/$board-$releasePhase")
val connection = url.openConnection() as HttpsURLConnection

connection.apply {
connectTimeout = 10_000
readTimeout = 30_000
}

try {

connection.useCaches = useCaches

connection.connect()

val responseText = String(connection.inputStream.readBytes())

Log.e(TAG, responseText);

withContext(Dispatchers.Main) {
_uiState.value.releaseStates[releasePhase] = responseText.split(" ")[0]
}

connection.disconnect()

} catch (e: SocketTimeoutException) {
val errorMessage =
application.getString(R.string.update_release_states_socket_timeout_exception_snackbar_message)
Log.e(TAG, errorMessage, e)
viewModelScope.launch {
showSnackbarError("$errorMessage: $e")
}
} catch (e: IOException) {
val errorMessage =
application.getString(R.string.update_release_states_io_exception_snackbar_message)
Log.e(TAG, errorMessage, e)
viewModelScope.launch {
showSnackbarError("$errorMessage: $e")
}
} catch (e: UnknownServiceException) {
val errorMessage =
application.getString(R.string.update_release_states_unknown_service_exception_snackbar_message)
Log.e(TAG, errorMessage, e)
viewModelScope.launch {
showSnackbarError("$errorMessage: $e")
}
} finally {
connection.disconnect()
}


} catch (e: IOException) {
val errorMessage =
application.getString(R.string.update_release_states_failed_to_create_httpsurlconnection_snackbar_message)
Log.e(TAG, errorMessage, e)
viewModelScope.launch {
showSnackbarError("$errorMessage: $e")
}
} finally {
onFinishedUpdating()
}
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package app.grapheneos.info.ui.releasenotes

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.ElevatedCard
import androidx.compose.material3.MaterialTheme.typography
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import app.grapheneos.info.R

@Composable
fun ReleaseState(releaseStates: List<Pair<String, String>>) {

Row(
modifier = Modifier
.fillMaxSize(), horizontalArrangement = Arrangement.SpaceEvenly) {
Column(
) {
ElevatedCard() {
Text(
stringResource(R.string.stable),
style = typography.titleMedium,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 5.dp).align(Alignment.CenterHorizontally)
)
Text(
text = releaseStates.find { it.first == "stable" }?.second.toString(),
style = typography.bodyMedium,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 5.dp).align(Alignment.CenterHorizontally)
)
}
}
Column(
) {
ElevatedCard() {
Text(
stringResource(R.string.beta),
style = typography.titleMedium,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 5.dp).align(Alignment.CenterHorizontally)
)
Text(
text = releaseStates.find { it.first == "beta" }?.second.toString(),
style = typography.bodyMedium,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 5.dp).align(Alignment.CenterHorizontally)
)
}
}
Column(
) {
ElevatedCard() {
Text(
stringResource(R.string.alpha),
style = typography.titleMedium,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 5.dp).align(Alignment.CenterHorizontally)
)
Text(
text = releaseStates.find { it.first == "alpha" }?.second.toString(),
style = typography.bodyMedium,
modifier = Modifier.padding(horizontal = 16.dp, vertical = 5.dp).align(Alignment.CenterHorizontally)
)
}
}
}
}
9 changes: 9 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,13 @@
<string name="account_info_item_copy_term">Copy %1$s</string>
<string name="browser_link_illegal_argument_exception_snackbar_error">Unable to open link. Make sure a browser is installed and enabled on your device.</string>
<string name="release_notes_top_bar_info_button_content_description">Info about the releases</string>
<string name="stable">Stable</string>
<string name="beta">Beta</string>
<string name="alpha">Alpha</string>
<string name="update_release_states_socket_timeout_exception_snackbar_message">Socket Timeout Exception</string>
<string name="update_release_states_io_exception_snackbar_message">Failed to retrieve latest release states</string>
<string name="update_release_states_unknown_service_exception_snackbar_message">Unknown Service Exception</string>
<string name="update_release_states_failed_to_create_httpsurlconnection_snackbar_message">Failed to create
HttpsURLConnection
</string>
</resources>