Refactor: Centralize build flavor checks

Introduces `BuildFlavorChecker` to centralize the logic for determining if the build is the "offline" flavor. This new checker replaces direct `BuildConfig.FLAVOR` comparisons throughout the codebase, removing redundant `@Suppress("KotlinConstantConditions")` annotations.
This commit is contained in:
greenart7c3
2025-12-18 07:47:55 -03:00
parent 5523c32c50
commit ecbb89e872
14 changed files with 51 additions and 67 deletions
@@ -412,12 +412,7 @@ class Amber :
}
val wasActive = client.isActive()
@Suppress("KotlinConstantConditions")
// TODO: You can filter inside each update filter for only
// localhost relays and keep these alive even on the offline
// mode
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
// these update the relay list in the filters and send them to the
// relay, reconnecting if needed
if (isAppInForeground) {
@@ -0,0 +1,9 @@
package com.greenart7c3.nostrsigner
object BuildFlavorChecker {
@Suppress("SimplifyBooleanWithConstants")
fun isOfflineFlavor(): Boolean {
@Suppress("KotlinConstantConditions")
return BuildConfig.FLAVOR == "offline"
}
}
@@ -268,8 +268,7 @@ class MainActivity : AppCompatActivity() {
override fun onResume() {
Amber.instance.setMainActivity(this)
mainViewModel.showBunkerRequests()
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
val connectivityManager =
(getSystemService(ConnectivityManager::class.java) as ConnectivityManager)
connectivityManager.getNetworkCapabilities(connectivityManager.activeNetwork)?.let {
@@ -6,14 +6,13 @@ import android.content.Intent
import android.os.Build
import android.util.Log
import com.greenart7c3.nostrsigner.Amber
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
class BootReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR == "offline") return
if (BuildFlavorChecker.isOfflineFlavor()) return
Log.d(Amber.TAG, "Received intent: ${intent.action}")
when (intent.action) {
@@ -8,7 +8,7 @@ import android.net.NetworkCapabilities
import android.os.IBinder
import android.util.Log
import com.greenart7c3.nostrsigner.Amber
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.database.LogEntity
import java.util.Timer
@@ -30,8 +30,7 @@ class ConnectivityService : Service() {
override fun onAvailable(network: Network) {
super.onAvailable(network)
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR == "offline") return
if (BuildFlavorChecker.isOfflineFlavor()) return
if (Amber.instance.settings.killSwitch.value) return
if (lastNetwork != null && lastNetwork != network) {
@@ -52,8 +51,7 @@ class ConnectivityService : Service() {
) {
super.onCapabilitiesChanged(network, networkCapabilities)
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR == "offline") return
if (BuildFlavorChecker.isOfflineFlavor()) return
if (Amber.instance.settings.killSwitch.value) return
scope.launch(Dispatchers.IO) {
@@ -70,8 +68,7 @@ class ConnectivityService : Service() {
override fun onLost(network: Network) {
super.onLost(network)
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR == "offline") return
if (BuildFlavorChecker.isOfflineFlavor()) return
lastNetwork = null
@@ -96,16 +93,14 @@ class ConnectivityService : Service() {
while (Amber.instance.isStartingAppState.value) {
delay(1000)
}
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline" && !Amber.instance.settings.killSwitch.value) {
if (!BuildFlavorChecker.isOfflineFlavor() && !Amber.instance.settings.killSwitch.value) {
Amber.instance.client.connect()
Amber.instance.applicationIOScope.launch {
Amber.instance.checkForNewRelaysAndUpdateAllFilters()
}
}
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
val connectivityManager =
(getSystemService(ConnectivityManager::class.java) as ConnectivityManager)
connectivityManager.registerDefaultNetworkCallback(networkCallback)
@@ -117,10 +112,6 @@ class ConnectivityService : Service() {
timer.schedule(
object : TimerTask() {
override fun run() {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR == "offline") {
return
}
if (Amber.instance.settings.killSwitch.value) return
scope.launch {
@@ -137,11 +128,17 @@ class ConnectivityService : Service() {
time = System.currentTimeMillis(),
),
)
Amber.instance.notificationSubscription.updateFilter()
if (!BuildFlavorChecker.isOfflineFlavor()) {
Amber.instance.notificationSubscription.updateFilter()
}
}
}
}
if (BuildFlavorChecker.isOfflineFlavor()) {
return
}
scope.launch {
if (!Amber.instance.client.isActive()) {
Amber.instance.client.connect()
@@ -160,8 +157,7 @@ class ConnectivityService : Service() {
override fun onDestroy() {
isStarted = false
timer.cancel()
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
try {
Log.d(Amber.TAG, "unregisterNetworkCallback")
val connectivityManager =
@@ -47,7 +47,7 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.net.toUri
import com.greenart7c3.nostrsigner.Amber
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.models.Account
import com.vitorpamplona.quartz.nip01Core.relay.client.NostrClient
@@ -77,8 +77,7 @@ fun DisplayCrashMessages(
}
stackTrace.value?.let { stack ->
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR == "offline") {
if (BuildFlavorChecker.isOfflineFlavor()) {
CrashReportAlertDialog(
text = stringResource(R.string.copy_crash_report_to_clipboard),
onDismiss = { stackTrace.value = null },
@@ -25,7 +25,7 @@ import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.TextFieldValue
import androidx.compose.ui.unit.dp
import com.greenart7c3.nostrsigner.Amber
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.models.Account
@@ -104,8 +104,7 @@ fun DefaultProfileRelaysScreen(
)
LocalPreferences.saveSettingsToEncryptedStorage(Amber.instance.settings)
scope.launch(Dispatchers.IO) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
Amber.instance.checkForNewRelaysAndUpdateAllFilters()
isLoading.value = false
} else {
@@ -143,8 +142,7 @@ fun DefaultProfileRelaysScreen(
)
LocalPreferences.saveSettingsToEncryptedStorage(Amber.instance.settings)
scope.launch(Dispatchers.IO) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
Amber.instance.checkForNewRelaysAndUpdateAllFilters()
isLoading.value = false
} else {
@@ -173,8 +171,7 @@ fun DefaultProfileRelaysScreen(
)
LocalPreferences.saveSettingsToEncryptedStorage(Amber.instance.settings)
scope.launch(Dispatchers.IO) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
Amber.instance.checkForNewRelaysAndUpdateAllFilters()
isLoading.value = false
} else {
@@ -55,7 +55,7 @@ import androidx.navigation.compose.composable
import androidx.navigation.compose.currentBackStackEntryAsState
import androidx.navigation.navArgument
import com.greenart7c3.nostrsigner.Amber
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.models.Account
@@ -109,8 +109,7 @@ private fun askNotificationPermission(
@SuppressLint("BatteryLife")
fun requestIgnoreBatteryOptimizations(context: Context) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR == "offline") return
if (BuildFlavorChecker.isOfflineFlavor()) return
if (Amber.instance.client.relayStatusFlow().value.connected.isEmpty()) return
val packageName = context.packageName
val pm = context.getSystemService(PowerManager::class.java)
@@ -161,8 +160,7 @@ fun MainScreen(
var showDialog by remember { mutableStateOf(false) }
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
LaunchedEffect(Unit) {
launch(Dispatchers.IO) {
askNotificationPermission(
@@ -51,6 +51,7 @@ import androidx.compose.ui.unit.sp
import androidx.navigation.NavController
import com.greenart7c3.nostrsigner.Amber
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.models.Account
@@ -171,8 +172,7 @@ fun SettingsScreen(
)
}
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
Box(
Modifier
.padding(vertical = 8.dp),
@@ -75,7 +75,7 @@ import androidx.compose.ui.window.DialogProperties
import androidx.navigation.NavHostController
import coil3.compose.SubcomposeAsyncImage
import com.greenart7c3.nostrsigner.Amber
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.models.Account
@@ -241,8 +241,7 @@ fun AccountBackupScreen(
) {
localAccount?.let { localAccount ->
val profileUrl = localAccount.picture.value
@Suppress("KotlinConstantConditions")
if (profileUrl.isNotBlank() && BuildConfig.FLAVOR != "offline") {
if (profileUrl.isNotBlank() && !BuildFlavorChecker.isOfflineFlavor()) {
SubcomposeAsyncImage(
profileUrl,
"profile picture",
@@ -46,7 +46,7 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.compose.collectAsLazyPagingItems
import com.greenart7c3.nostrsigner.Amber
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.LocalPreferences
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.checkNotInMainThread
@@ -122,8 +122,7 @@ fun DefaultRelaysScreen(
)
LocalPreferences.saveSettingsToEncryptedStorage(Amber.instance.settings)
scope.launch(Dispatchers.IO) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
Amber.instance.checkForNewRelaysAndUpdateAllFilters()
delay(2000)
Amber.instance.client.reconnect()
@@ -171,8 +170,7 @@ fun DefaultRelaysScreen(
)
LocalPreferences.saveSettingsToEncryptedStorage(Amber.instance.settings)
scope.launch(Dispatchers.IO) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
Amber.instance.checkForNewRelaysAndUpdateAllFilters()
isLoading.value = false
} else {
@@ -209,8 +207,7 @@ fun DefaultRelaysScreen(
)
LocalPreferences.saveSettingsToEncryptedStorage(Amber.instance.settings)
scope.launch(Dispatchers.IO) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
Amber.instance.checkForNewRelaysAndUpdateAllFilters()
isLoading.value = false
} else {
@@ -239,8 +236,7 @@ fun DefaultRelaysScreen(
)
LocalPreferences.saveSettingsToEncryptedStorage(Amber.instance.settings)
scope.launch(Dispatchers.IO) {
@Suppress("KotlinConstantConditions")
if (BuildConfig.FLAVOR != "offline") {
if (!BuildFlavorChecker.isOfflineFlavor()) {
Amber.instance.checkForNewRelaysAndUpdateAllFilters()
isLoading.value = false
} else {
@@ -17,7 +17,7 @@ import androidx.compose.ui.res.vectorResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavBackStackEntry
import androidx.navigation.NavHostController
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.ui.navigation.Route
@@ -26,8 +26,7 @@ fun AmberFloatingButton(
navController: NavHostController,
navBackStackEntry: NavBackStackEntry?,
) {
@Suppress("KotlinConstantConditions")
if (navBackStackEntry?.destination?.route == Route.Applications.route && BuildConfig.FLAVOR != "offline") {
if (navBackStackEntry?.destination?.route == Route.Applications.route && !BuildFlavorChecker.isOfflineFlavor()) {
NewBunkerFloatingButton(
onClick = {
navController.navigate(Route.NewApplication.route)
@@ -22,7 +22,7 @@ import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp
import coil3.compose.SubcomposeAsyncImage
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.models.Account
import com.greenart7c3.nostrsigner.ui.CenterCircularProgressIndicator
import com.greenart7c3.nostrsigner.ui.navigation.Route
@@ -54,8 +54,7 @@ fun AmberNavigationBar(
},
icon = {
if (it.route == Route.Accounts.route) {
@Suppress("KotlinConstantConditions")
if (!profileUrl.isNullOrBlank() && BuildConfig.FLAVOR != "offline") {
if (!profileUrl.isNullOrBlank() && !BuildFlavorChecker.isOfflineFlavor()) {
SubcomposeAsyncImage(
profileUrl,
it.route,
@@ -39,7 +39,7 @@ import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.window.core.layout.WindowSizeClass.Companion.WIDTH_DP_MEDIUM_LOWER_BOUND
import coil3.compose.AsyncImage
import com.greenart7c3.nostrsigner.BuildConfig
import com.greenart7c3.nostrsigner.BuildFlavorChecker
import com.greenart7c3.nostrsigner.R
import com.greenart7c3.nostrsigner.models.Account
import com.greenart7c3.nostrsigner.models.Permission
@@ -58,8 +58,7 @@ fun ProfilePicture(account: Account) {
verticalArrangement = Arrangement.Center,
) {
val profileUrl by account.picture.collectAsStateWithLifecycle()
@Suppress("KotlinConstantConditions")
if (profileUrl.isNotBlank() && BuildConfig.FLAVOR != "offline") {
if (profileUrl.isNotBlank() && !BuildFlavorChecker.isOfflineFlavor()) {
AsyncImage(
profileUrl,
Route.Accounts.route,