PageViewModel: clearer message if no connection

This commit is contained in:
dece 2022-01-09 22:19:33 +01:00
parent bf28d30e4c
commit afd0b42c83
3 changed files with 15 additions and 1 deletions

View file

@ -2,6 +2,7 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="dev.lowrespalmtree.comet"> package="dev.lowrespalmtree.comet">
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<application <application

View file

@ -140,7 +140,9 @@ class MainActivity : AppCompatActivity(), ContentAdapter.ContentAdapterListen {
is PageViewModel.FailureEvent -> { is PageViewModel.FailureEvent -> {
var message = event.details var message = event.details
if (!event.serverDetails.isNullOrEmpty()) if (!event.serverDetails.isNullOrEmpty())
message += "\n\n" + "Server details: ${event.serverDetails}" message += "\n\nServer details: ${event.serverDetails}"
if (!isConnectedToNetwork(this))
message += "\n\nInternet may be inaccessible…"
alert(message, title = event.short) alert(message, title = event.short)
updateState(PageViewModel.State.IDLE) updateState(PageViewModel.State.IDLE)
} }

View file

@ -0,0 +1,11 @@
package dev.lowrespalmtree.comet
import android.content.Context
import android.net.ConnectivityManager
import android.net.NetworkCapabilities
fun isConnectedToNetwork(context: Context): Boolean {
val connManager = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
val caps = connManager.getNetworkCapabilities(connManager.activeNetwork) ?: return false
return caps.hasCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED)
}