Skip to content

Commit

Permalink
Fix long method lints
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawa committed Mar 7, 2024
1 parent 8e993df commit 060ed9c
Show file tree
Hide file tree
Showing 6 changed files with 558 additions and 441 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@ import android.net.Uri
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.tween
import androidx.compose.foundation.ScrollState
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.defaultMinSize
import androidx.compose.foundation.layout.fillMaxHeight
Expand Down Expand Up @@ -158,7 +161,6 @@ fun Connect(navigator: DestinationsNavigator) {
)
}

@Suppress("LongMethod")
@Composable
fun ConnectScreen(
state: ConnectUiState,
Expand All @@ -175,15 +177,6 @@ fun ConnectScreen(
) {

val scrollState = rememberScrollState()
var lastConnectionActionTimestamp by remember { mutableLongStateOf(0L) }

fun handleThrottledAction(action: () -> Unit) {
val currentTime = System.currentTimeMillis()
if ((currentTime - lastConnectionActionTimestamp) > CONNECT_BUTTON_THROTTLE_MILLIS) {
lastConnectionActionTimestamp = currentTime
action.invoke()
}
}

ScaffoldWithTopBarAndDeviceName(
topBarColor = state.tunnelUiState.topBarColor(),
Expand All @@ -195,45 +188,11 @@ fun ConnectScreen(
) {
var progressIndicatorBias by remember { mutableFloatStateOf(0f) }

// Distance to marker when secure/unsecure
val baseZoom =
animateFloatAsState(
targetValue =
if (state.tunnelRealState is TunnelState.Connected) SECURE_ZOOM
else UNSECURE_ZOOM,
animationSpec = tween(SECURE_ZOOM_ANIMATION_MILLIS),
label = "baseZoom"
)

val markers =
state.tunnelRealState.toMarker(state.location)?.let { listOf(it) } ?: emptyList()

AnimatedMap(
modifier = Modifier.padding(top = it.calculateTopPadding()),
cameraLocation = state.location?.toLatLong() ?: fallbackLatLong,
cameraBaseZoom = baseZoom.value,
cameraVerticalBias = progressIndicatorBias,
markers = markers,
globeColors =
GlobeColors(
landColor = MaterialTheme.colorScheme.primary,
oceanColor = MaterialTheme.colorScheme.secondary,
)
)

Column(
verticalArrangement = Arrangement.Bottom,
horizontalAlignment = Alignment.Start,
modifier =
Modifier.animateContentSize()
.padding(top = it.calculateTopPadding())
.fillMaxHeight()
.drawVerticalScrollbar(
scrollState,
color = MaterialTheme.colorScheme.onPrimary.copy(alpha = AlphaScrollbar)
)
.verticalScroll(scrollState)
.testTag(SCROLLABLE_COLUMN_TEST_TAG)
MapColumn(
state,
it,
progressIndicatorBias,
scrollState,
) {
Spacer(modifier = Modifier.defaultMinSize(minHeight = Dimens.mediumPadding).weight(1f))
MullvadCircularProgressIndicatorLarge(
Expand All @@ -260,66 +219,19 @@ fun ConnectScreen(
}
)
Spacer(modifier = Modifier.defaultMinSize(minHeight = Dimens.mediumPadding).weight(1f))
ConnectionStatusText(
state = state.tunnelRealState,
modifier = Modifier.padding(horizontal = Dimens.sideMargin)
)
Text(
text = state.location?.country ?: "",
style = MaterialTheme.typography.headlineLarge,
color = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.padding(horizontal = Dimens.sideMargin)
)
Text(
text = state.location?.city ?: "",
style = MaterialTheme.typography.headlineLarge,
color = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.padding(horizontal = Dimens.sideMargin)
)
var expanded by rememberSaveable { mutableStateOf(false) }
LocationInfo(
onToggleTunnelInfo = { expanded = !expanded },
isVisible = state.showLocationInfo,
isExpanded = expanded,
location = state.location,
inAddress = state.inAddress,
outAddress = state.outAddress,
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = Dimens.sideMargin)
.testTag(LOCATION_INFO_TEST_TAG)
)
Spacer(modifier = Modifier.height(Dimens.buttonSpacing))
SwitchLocationButton(
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = Dimens.sideMargin)
.testTag(SELECT_LOCATION_BUTTON_TEST_TAG),
onClick = onSwitchLocationClick,
showChevron = state.showLocation,
text =
if (state.showLocation && state.selectedRelayItem != null) {
state.selectedRelayItem.locationName
} else {
stringResource(id = R.string.switch_location)
}
)

ConnectionInfo(state = state)

Spacer(modifier = Modifier.height(Dimens.buttonSpacing))
ConnectionButton(
state = state.tunnelUiState,
modifier =
Modifier.padding(horizontal = Dimens.sideMargin)
.padding(bottom = Dimens.screenVerticalMargin)
.testTag(CONNECT_BUTTON_TEST_TAG),
disconnectClick = onDisconnectClick,
reconnectClick = { handleThrottledAction(onReconnectClick) },
cancelClick = onCancelClick,
connectClick = { handleThrottledAction(onConnectClick) },
reconnectButtonTestTag = RECONNECT_BUTTON_TEST_TAG

ButtonPanel(
state,
onSwitchLocationClick,
onDisconnectClick,
onReconnectClick,
onCancelClick,
onConnectClick,
)
// We need to manually add this padding so we align size with the map
// component and marker with the progress indicator.
Spacer(modifier = Modifier.height(it.calculateBottomPadding()))
}

NotificationBanner(
Expand All @@ -333,6 +245,141 @@ fun ConnectScreen(
}
}

@Composable
private fun MapColumn(
state: ConnectUiState,
it: PaddingValues,
progressIndicatorBias: Float,
scrollState: ScrollState,
content: @Composable ColumnScope.() -> Unit
) {

// Distance to marker when secure/unsecure
val baseZoom =
animateFloatAsState(
targetValue =
if (state.tunnelRealState is TunnelState.Connected) SECURE_ZOOM else UNSECURE_ZOOM,
animationSpec = tween(SECURE_ZOOM_ANIMATION_MILLIS),
label = "baseZoom"
)

val markers = state.tunnelRealState.toMarker(state.location)?.let { listOf(it) } ?: emptyList()

AnimatedMap(
modifier = Modifier.padding(top = it.calculateTopPadding()),
cameraLocation = state.location?.toLatLong() ?: fallbackLatLong,
cameraBaseZoom = baseZoom.value,
cameraVerticalBias = progressIndicatorBias,
markers = markers,
globeColors =
GlobeColors(
landColor = MaterialTheme.colorScheme.primary,
oceanColor = MaterialTheme.colorScheme.secondary,
)
)

Column(
verticalArrangement = Arrangement.Bottom,
horizontalAlignment = Alignment.Start,
modifier =
Modifier.animateContentSize()
.padding(top = it.calculateTopPadding())
.fillMaxHeight()
.drawVerticalScrollbar(
scrollState,
color = MaterialTheme.colorScheme.onPrimary.copy(alpha = AlphaScrollbar)
)
.verticalScroll(scrollState)
.testTag(SCROLLABLE_COLUMN_TEST_TAG)
) {
content()
// We need to manually add this padding so we align size with the map
// component and marker with the progress indicator.
Spacer(modifier = Modifier.height(it.calculateBottomPadding()))
}
}

@Composable
private fun ConnectionInfo(state: ConnectUiState) {
ConnectionStatusText(
state = state.tunnelRealState,
modifier = Modifier.padding(horizontal = Dimens.sideMargin)
)
Text(
text = state.location?.country ?: "",
style = MaterialTheme.typography.headlineLarge,
color = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.padding(horizontal = Dimens.sideMargin)
)
Text(
text = state.location?.city ?: "",
style = MaterialTheme.typography.headlineLarge,
color = MaterialTheme.colorScheme.onPrimary,
modifier = Modifier.padding(horizontal = Dimens.sideMargin)
)
var expanded by rememberSaveable { mutableStateOf(false) }
LocationInfo(
onToggleTunnelInfo = { expanded = !expanded },
isVisible = state.showLocationInfo,
isExpanded = expanded,
location = state.location,
inAddress = state.inAddress,
outAddress = state.outAddress,
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = Dimens.sideMargin)
.testTag(LOCATION_INFO_TEST_TAG)
)
}

@Composable
private fun ButtonPanel(
state: ConnectUiState,
onSwitchLocationClick: () -> Unit,
onDisconnectClick: () -> Unit,
onReconnectClick: () -> Unit,
onCancelClick: () -> Unit,
onConnectClick: () -> Unit,
) {
var lastConnectionActionTimestamp by remember { mutableLongStateOf(0L) }

fun handleThrottledAction(action: () -> Unit) {
val currentTime = System.currentTimeMillis()
if ((currentTime - lastConnectionActionTimestamp) > CONNECT_BUTTON_THROTTLE_MILLIS) {
lastConnectionActionTimestamp = currentTime
action.invoke()
}
}

SwitchLocationButton(
modifier =
Modifier.fillMaxWidth()
.padding(horizontal = Dimens.sideMargin)
.testTag(SELECT_LOCATION_BUTTON_TEST_TAG),
onClick = onSwitchLocationClick,
showChevron = state.showLocation,
text =
if (state.showLocation && state.selectedRelayItem != null) {
state.selectedRelayItem.locationName
} else {
stringResource(id = R.string.switch_location)
}
)
Spacer(modifier = Modifier.height(Dimens.buttonSpacing))
ConnectionButton(
state = state.tunnelUiState,
modifier =
Modifier.padding(horizontal = Dimens.sideMargin)
.padding(bottom = Dimens.screenVerticalMargin)
.testTag(CONNECT_BUTTON_TEST_TAG),
disconnectClick = onDisconnectClick,
reconnectClick = { handleThrottledAction(onReconnectClick) },
cancelClick = onCancelClick,
connectClick = { handleThrottledAction(onConnectClick) },
reconnectButtonTestTag = RECONNECT_BUTTON_TEST_TAG
)
}

@Composable
fun TunnelState.toMarker(location: GeoIpLocation?): Marker? {
if (location == null) return null
Expand Down
Loading

0 comments on commit 060ed9c

Please sign in to comment.