From 556c49fbfdbdc011f2303d83c26becdbe1db9d27 Mon Sep 17 00:00:00 2001 From: GG Bond Date: Mon, 27 Nov 2023 00:19:42 +0800 Subject: [PATCH] Check meteredNetworkWaring --- .../kotlin/com/hippo/ehviewer/Settings.kt | 2 +- .../com/hippo/ehviewer/ui/MainActivity.kt | 52 +++++++------------ .../hippo/ehviewer/ui/settings/EhScreen.kt | 2 +- 3 files changed, 22 insertions(+), 34 deletions(-) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt b/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt index 7fb0c4fd1..6de45f644 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/Settings.kt @@ -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 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) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt index bd3903df2..542780f29 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/MainActivity.kt @@ -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(extraBufferCapacity = 1) private var intentFlow = MutableSharedFlow(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)) diff --git a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt index e5eb5d00d..9851ce18c 100644 --- a/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt +++ b/app/src/main/kotlin/com/hippo/ehviewer/ui/settings/EhScreen.kt @@ -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(