Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import org.grapheneos.tls.ModernTLSSocketFactory
import java.io.IOException
import java.net.SocketTimeoutException
import java.net.URL
import java.net.UnknownHostException
import java.net.UnknownServiceException
import javax.net.ssl.HttpsURLConnection

Expand Down Expand Up @@ -60,38 +61,49 @@ class ReleasesViewModel(

try {
connection.useCaches = useCaches

connection.connect()

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

var newEntries = "<entry>(.*?)</entry>".toRegex().findAll(responseText).map { it.groups[1]!!.value }.map { entry ->
Pair("<id>(.*?)</id>".toRegex().find(entry)?.groups?.get(1)?.value ?: entry.hashCode().toString(), entry)
}.toMap()
var newEntries = "<entry>(.*?)</entry>".toRegex()
.findAll(responseText)
.map { it.groups[1]!!.value }
.map { entry ->
Pair(
"<id>(.*?)</id>".toRegex()
.find(entry)?.groups?.get(1)?.value
?: entry.hashCode().toString(),
entry
)
}
.toMap()

var currentOsChangelogIndex = newEntries.toSortedMap().toList().asReversed().indexOfFirst { entry ->
val title = "<title>(.*?)</title>".toRegex()
.find(entry.second)?.groups?.get(1)?.value
var currentOsChangelogIndex =
newEntries.toSortedMap().toList().asReversed().indexOfFirst { entry ->
val title = "<title>(.*?)</title>".toRegex()
.find(entry.second)?.groups?.get(1)?.value

title == android.os.Build.VERSION.INCREMENTAL
}
title == android.os.Build.VERSION.INCREMENTAL
}

if (currentOsChangelogIndex == -1) {
currentOsChangelogIndex = 0
}

newEntries = newEntries.toSortedMap().toList().asReversed().filterIndexed { index, _ ->
index <= currentOsChangelogIndex + 3
}.toMap()
newEntries = newEntries.toSortedMap().toList().asReversed()
.filterIndexed { index, _ ->
index <= currentOsChangelogIndex + 3
}
.toMap()

// Only update if there are changes to the number of changelogs
if ((newEntries.count() - uiState.value.entries.size) != 0) {
withContext(Dispatchers.Main) {
_uiState.value.entries.filterKeys {
!newEntries.keys.contains(it)
}.forEach {
_uiState.value.entries.remove(it.key)
}
_uiState.value.entries
.filterKeys { !newEntries.keys.contains(it) }
.forEach {
_uiState.value.entries.remove(it.key)
}
_uiState.value.entries.putAll(newEntries)
}
}
Expand All @@ -101,35 +113,47 @@ class ReleasesViewModel(
scrollChangelogLazyListTo(currentOsChangelogIndex)
}
} catch (e: SocketTimeoutException) {
val errorMessage =
application.getString(R.string.update_changelog_socket_timeout_exception_snackbar_message)
val errorMessage = application.getString(
R.string.update_changelog_socket_timeout_exception_snackbar_message
)
Log.e(TAG, errorMessage, e)
viewModelScope.launch {
showSnackbarError("$errorMessage: $e")
showSnackbarError(errorMessage)
}
} catch (e: IOException) {
val errorMessage =
application.getString(R.string.update_changelog_io_exception_snackbar_message)
if (e is UnknownHostException || e.cause is UnknownHostException) {
application.getString(
R.string.update_changelog_no_internet_connection_snackbar_message
)
} else {
application.getString(
R.string.update_changelog_io_exception_snackbar_message
)
}

Log.e(TAG, errorMessage, e)
viewModelScope.launch {
showSnackbarError("$errorMessage: $e")
showSnackbarError(errorMessage)
}
} catch (e: UnknownServiceException) {
val errorMessage =
application.getString(R.string.update_changelog_unknown_service_exception_snackbar_message)
val errorMessage = application.getString(
R.string.update_changelog_unknown_service_exception_snackbar_message
)
Log.e(TAG, errorMessage, e)
viewModelScope.launch {
showSnackbarError("$errorMessage: $e")
showSnackbarError(errorMessage)
}
} finally {
connection.disconnect()
}
} catch (e: IOException) {
val errorMessage =
application.getString(R.string.update_changelog_failed_to_create_httpsurlconnection_snackbar_message)
val errorMessage = application.getString(
R.string.update_changelog_failed_to_create_httpsurlconnection_snackbar_message
)
Log.e(TAG, errorMessage, e)
viewModelScope.launch {
showSnackbarError("$errorMessage: $e")
showSnackbarError(errorMessage)
}
} finally {
onFinishedUpdating()
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,8 @@
<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="releases_top_bar_info_button_content_description">Info about the releases</string>
<string name="releases_see_all_button">See all release notes</string>
<string name="update_changelog_no_internet_connection_snackbar_message">
Internet connection not found
</string>

</resources>