mirror of
https://github.com/Myzel394/NumberHub.git
synced 2025-06-18 16:25:27 +02:00
parent
1846485ce9
commit
d7db2780c8
@ -74,7 +74,8 @@ internal fun Project.configureKotlinAndroid(
|
||||
"-opt-in=androidx.compose.animation.ExperimentalAnimationApi",
|
||||
"-opt-in=androidx.compose.foundation.ExperimentalFoundationApi",
|
||||
"-opt-in=androidx.compose.ui.unit.ExperimentalUnitApi",
|
||||
"-opt-in=androidx.lifecycle.compose.ExperimentalLifecycleComposeApi"
|
||||
"-opt-in=androidx.lifecycle.compose.ExperimentalLifecycleComposeApi",
|
||||
"-opt-in=kotlinx.coroutines.ExperimentalCoroutinesApi",
|
||||
)
|
||||
jvmTarget = JavaVersion.VERSION_11.toString()
|
||||
}
|
||||
|
@ -17,6 +17,7 @@
|
||||
~ along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
-->
|
||||
|
||||
<manifest>
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android">
|
||||
|
||||
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
|
||||
</manifest>
|
@ -45,6 +45,7 @@ import androidx.compose.material3.MaterialTheme
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.material3.TopAppBarDefaults
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
@ -75,6 +76,7 @@ import com.sadellie.unitto.core.ui.common.textfield.ExpressionTextField
|
||||
import com.sadellie.unitto.core.ui.common.textfield.FormatterSymbols
|
||||
import com.sadellie.unitto.core.ui.common.textfield.UnformattedTextField
|
||||
import com.sadellie.unitto.data.common.format
|
||||
import com.sadellie.unitto.data.model.UnitGroup
|
||||
import com.sadellie.unitto.data.model.unit.AbstractUnit
|
||||
import com.sadellie.unitto.feature.converter.components.DefaultKeyboard
|
||||
import com.sadellie.unitto.feature.converter.components.NumberBaseKeyboard
|
||||
@ -282,6 +284,15 @@ private fun Default(
|
||||
)
|
||||
}
|
||||
|
||||
val connection by connectivityState()
|
||||
|
||||
LaunchedEffect(connection) {
|
||||
if ((connection == ConnectionState.Available) and (uiState.result == ConverterResult.Error)) {
|
||||
val unitFrom = uiState.unitFrom
|
||||
if (unitFrom.group == UnitGroup.CURRENCY) refreshCurrencyRates(unitFrom)
|
||||
}
|
||||
}
|
||||
|
||||
PortraitLandscape(
|
||||
modifier = modifier.fillMaxSize(),
|
||||
content1 = { contentModifier ->
|
||||
|
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Unitto is a unit converter for Android
|
||||
* Copyright (c) 2023 Elshan Agaev
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package com.sadellie.unitto.feature.converter
|
||||
|
||||
import android.content.Context
|
||||
import android.net.ConnectivityManager
|
||||
import android.net.Network
|
||||
import android.net.NetworkCapabilities
|
||||
import android.net.NetworkRequest
|
||||
import androidx.compose.runtime.Composable
|
||||
import androidx.compose.runtime.State
|
||||
import androidx.compose.ui.platform.LocalContext
|
||||
import androidx.lifecycle.compose.collectAsStateWithLifecycle
|
||||
import kotlinx.coroutines.channels.awaitClose
|
||||
import kotlinx.coroutines.flow.callbackFlow
|
||||
|
||||
// https://github.com/PatilShreyas/NotyKT/pull/210/files#diff-88d0c098edd51dfdd06d871b22de23efe0935234fe80b2b54592583262fbe846
|
||||
|
||||
internal sealed class ConnectionState {
|
||||
data object Available : ConnectionState()
|
||||
data object Unavailable : ConnectionState()
|
||||
}
|
||||
|
||||
private fun Context.observeConnectivityAsFlow() = callbackFlow {
|
||||
val connectivityManager = getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
|
||||
|
||||
val callback = object : ConnectivityManager.NetworkCallback() {
|
||||
override fun onAvailable(network: Network) { trySend(ConnectionState.Available) }
|
||||
override fun onLost(network: Network) { trySend(ConnectionState.Unavailable) }
|
||||
}
|
||||
|
||||
val networkRequest = NetworkRequest.Builder()
|
||||
.addCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
|
||||
.build()
|
||||
|
||||
connectivityManager.registerNetworkCallback(networkRequest, callback)
|
||||
|
||||
awaitClose {
|
||||
connectivityManager.unregisterNetworkCallback(callback)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
internal fun connectivityState(): State<ConnectionState> {
|
||||
val context = LocalContext.current
|
||||
|
||||
return context
|
||||
.observeConnectivityAsFlow()
|
||||
.collectAsStateWithLifecycle(ConnectionState.Unavailable)
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user