Check meteredNetworkWaring

This commit is contained in:
GG Bond authored and Vladimir Dostoyevskiy committed 2023-11-27 00:37:19 +08:00
1 parent 358a5249a4
commit 556c49fbfd
3 files changed
+22 -34

No files matched your search

@@ -105,6 +105,7 @@ object Settings : DataStorePreferences(null) {
val detailSize = intPref("detail_size_2", 0)
val thumbColumns = intPref("thumb_columns", 3)
val listThumbSize = intPref("list_tile_size", 40)
val meteredNetworkWarning = boolPref("cellular_network_warning", false)
val needSignInFlow: Flow<Boolean>
var downloadScheme by stringOrNullPref("image_scheme", null)
@@ -130,7 +131,6 @@ object Settings : DataStorePreferences(null) {
var showJpnTitle by boolPref("show_jpn_title", false)
var showGalleryPages by boolPref("show_gallery_pages", true)
var showTagTranslations by boolPref(KEY_SHOW_TAG_TRANSLATIONS, false).observed { updateWhenTagTranslationChanges() }
var meteredNetworkWarning by boolPref("cellular_network_warning", false)
var appLinkVerifyTip by boolPref("app_link_verify_tip", false)
var enabledSecurity by boolPref("enable_secure", false)
var useCIUpdateChannel by boolPref("ci_update_channel", false)
@@ -85,14 +85,13 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.compose.LifecycleResumeEffect
import androidx.lifecycle.lifecycleScope
import androidx.navigation.compose.rememberNavController
import com.google.android.material.snackbar.Snackbar
import com.hippo.ehviewer.R
import com.hippo.ehviewer.Settings
import com.hippo.ehviewer.client.data.ListUrlBuilder
import com.hippo.ehviewer.client.parser.GalleryDetailUrlParser
import com.hippo.ehviewer.client.parser.GalleryPageUrlParser
import com.hippo.ehviewer.collectAsState
import com.hippo.ehviewer.download.DownloadService
import com.hippo.ehviewer.download.downloadLocation
import com.hippo.ehviewer.icons.EhIcons
@@ -123,18 +122,16 @@ import com.hippo.ehviewer.util.ExceptionUtils
import com.hippo.ehviewer.util.addTextToClipboard
import com.hippo.ehviewer.util.getParcelableExtraCompat
import com.hippo.ehviewer.util.getUrlFromClipboard
import com.hippo.ehviewer.util.isAtLeastQ
import com.ramcosta.composedestinations.DestinationsNavHost
import com.ramcosta.composedestinations.rememberNavHostEngine
import com.ramcosta.composedestinations.utils.currentDestinationAsState
import eu.kanade.tachiyomi.util.lang.launchUI
import eu.kanade.tachiyomi.util.lang.withIOContext
import java.io.File
import java.io.FileOutputStream
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import moe.tarsin.coroutines.runSuspendCatching
import splitties.systemservices.clipboardManager
@@ -196,7 +193,6 @@ class MainActivity : EhActivity() {
var drawerLocked by mutableStateOf(false)
private var tipFlow = MutableSharedFlow<String>(extraBufferCapacity = 1)
private var intentFlow = MutableSharedFlow<Intent>(extraBufferCapacity = 4)
private val isInitializedFlow = MutableStateFlow(false)
@SuppressLint("UnusedMaterial3ScaffoldPaddingParameter")
override fun onCreate(savedInstanceState: Bundle?) {
@@ -273,6 +269,24 @@ class MainActivity : EhActivity() {
snackbarState.showSnackbar(it)
}
}
val warning = stringResource(R.string.metered_network_warning)
val settings = stringResource(R.string.settings)
val checkMeteredNetwork by Settings.meteredNetworkWarning.collectAsState()
if (checkMeteredNetwork) {
LaunchedEffect(Unit) {
if (connectivityManager.isActiveNetworkMetered) {
if (isAtLeastQ) {
val ret = snackbarState.showSnackbar(warning, settings)
if (ret == SnackbarResult.ActionPerformed) {
val panelIntent = Intent(android.provider.Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
startActivity(panelIntent)
}
} else {
snackbarState.showSnackbar(warning)
}
}
}
}
val snackMessage = stringResource(R.string.clipboard_gallery_url_snack_message)
val snackAction = stringResource(R.string.clipboard_gallery_url_snack_action)
LifecycleResumeEffect {
@@ -391,12 +405,6 @@ class MainActivity : EhActivity() {
onNewIntent(intent)
}
checkDownloadLocation()
if (Settings.meteredNetworkWarning) {
lifecycleScope.launchUI {
isInitializedFlow.first { it }
checkMeteredNetwork()
}
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
if (!Settings.appLinkVerifyTip) {
try {
@@ -453,26 +461,6 @@ class MainActivity : EhActivity() {
.show()
}
private fun checkMeteredNetwork() {
if (connectivityManager.isActiveNetworkMetered) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Snackbar.make(
findViewById(R.id.content),
R.string.metered_network_warning,
Snackbar.LENGTH_LONG,
)
.setAction(R.string.settings) {
val panelIntent =
Intent(android.provider.Settings.Panel.ACTION_INTERNET_CONNECTIVITY)
startActivity(panelIntent)
}
.show()
} else {
showTip(R.string.metered_network_warning, BaseScene.LENGTH_LONG)
}
}
}
override fun onResume() {
if (Settings.needSignIn) {
startActivity(Intent(this, ConfigureActivity::class.java))
@@ -310,7 +310,7 @@ fun EhScreen(navigator: DestinationsNavigator) {
) { navigator.navigate(FilterScreenDestination) }
SwitchPreference(
title = stringResource(id = R.string.settings_eh_metered_network_warning),
value = Settings::meteredNetworkWarning,
value = Settings.meteredNetworkWarning::value,
)
if (signin) {
SwitchPreference(