From d63db4ab40c71b07368f7778d8fb0f23392352e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20G=C3=B6ransson?= Date: Mon, 11 Sep 2023 15:59:09 +0200 Subject: [PATCH 1/4] Perform AS Code Cleanup --- .../compose/util/RememberPrevious.kt | 2 +- .../dataproxy/MullvadProblemReport.kt | 8 +-- .../mullvadvpn/ui/BlockingController.kt | 2 +- .../ui/CollapsibleTitleController.kt | 66 +++++++++---------- .../ui/CustomTransformationMethod.kt | 4 +- .../mullvadvpn/ui/fragment/LoginFragment.kt | 2 +- .../ui/fragment/OutOfTimeFragment.kt | 14 ++-- .../ui/fragment/ProblemReportFragment.kt | 2 +- .../fragment/RedeemVoucherDialogFragment.kt | 4 +- .../mullvadvpn/ui/widget/AccountInput.kt | 24 +++---- .../mullvadvpn/ui/widget/BackButton.kt | 4 +- .../mullvad/mullvadvpn/ui/widget/Button.kt | 8 +-- .../util/SegmentedInputFormatter.kt | 7 +- .../net/mullvad/mullvadvpn/lib/ipc/Event.kt | 2 +- .../net/mullvad/mullvadvpn/lib/ipc/Request.kt | 2 +- .../mullvad/mullvadvpn/model/Constraint.kt | 6 +- .../mullvadvpn/model/CustomTunnelEndpoint.kt | 2 +- .../net/mullvad/mullvadvpn/model/Device.kt | 4 +- .../net/mullvad/talpid/TalpidVpnService.kt | 10 +-- .../mullvadvpn/service/MullvadVpnService.kt | 3 +- .../AccountExpiryNotification.kt | 4 +- .../notifications/TunnelStateNotification.kt | 4 +- 22 files changed, 91 insertions(+), 93 deletions(-) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/util/RememberPrevious.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/util/RememberPrevious.kt index ef47f6147234..6782e0ab5519 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/util/RememberPrevious.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/compose/util/RememberPrevious.kt @@ -31,7 +31,7 @@ fun rememberPrevious( private fun rememberRef(): MutableState { // for some reason it always recreated the value with vararg keys, // leaving out the keys as a parameter for remember for now - return remember() { + return remember { object : MutableState { override var value: T? = null diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/MullvadProblemReport.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/MullvadProblemReport.kt index 7104deb686d8..27b1eb694db5 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/MullvadProblemReport.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/dataproxy/MullvadProblemReport.kt @@ -15,13 +15,13 @@ const val PROBLEM_REPORT_FILE = "problem_report.txt" class MullvadProblemReport { private sealed class Command { - class Collect() : Command() + data object Collect : Command() class Load(val logs: CompletableDeferred) : Command() class Send(val result: CompletableDeferred) : Command() - class Delete() : Command() + data object Delete : Command() } val logDirectory = CompletableDeferred() @@ -44,7 +44,7 @@ class MullvadProblemReport { } fun collect() { - commandChannel.trySendBlocking(Command.Collect()) + commandChannel.trySendBlocking(Command.Collect) } suspend fun load(): String { @@ -64,7 +64,7 @@ class MullvadProblemReport { } fun deleteReportFile() { - commandChannel.trySendBlocking(Command.Delete()) + commandChannel.trySendBlocking(Command.Delete) } private fun spawnActor() = diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/BlockingController.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/BlockingController.kt index 4dd53fa937bb..3305418a483b 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/BlockingController.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/BlockingController.kt @@ -10,7 +10,7 @@ class BlockingController(val blockableView: BlockableView) { var innerJob: Job? = null fun action() { - if (!(job?.isActive ?: false)) { + if (job?.isActive != true) { job = GlobalScope.launch(Dispatchers.Main) { blockableView.setEnabled(false) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/CollapsibleTitleController.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/CollapsibleTitleController.kt index 658fd3fed3ee..1c37945602fa 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/CollapsibleTitleController.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/CollapsibleTitleController.kt @@ -46,16 +46,15 @@ class CollapsibleTitleController(val parentView: View, scrollAreaId: Int = R.id. private val xOffsetInterpolation = LinearInterpolation() private val yOffsetInterpolation = LinearInterpolation() - private val collapsedTitleLayoutListener: LayoutListener = - LayoutListener() { collapsedTitle -> - val (x, y) = calculateViewCoordinates(collapsedTitle) + private val collapsedTitleLayoutListener: LayoutListener = LayoutListener { collapsedTitle -> + val (x, y) = calculateViewCoordinates(collapsedTitle) - collapsedTitleHeight = collapsedTitle.height.toFloat() + collapsedTitleHeight = collapsedTitle.height.toFloat() - scaleInterpolation.end = collapsedTitleHeight / maxOf(1.0f, titleHeight) - xOffsetInterpolation.end = x - yOffsetInterpolation.end = y - } + scaleInterpolation.end = collapsedTitleHeight / maxOf(1.0f, titleHeight) + xOffsetInterpolation.end = x + yOffsetInterpolation.end = y + } private val collapsedTitleView = parentView.findViewById(R.id.collapsed_title).apply { @@ -63,37 +62,35 @@ class CollapsibleTitleController(val parentView: View, scrollAreaId: Int = R.id. visibility = View.INVISIBLE } - private val expandedTitleLayoutListener: LayoutListener = - LayoutListener() { expandedTitle -> - val (x, y) = calculateViewCoordinates(expandedTitle) + private val expandedTitleLayoutListener: LayoutListener = LayoutListener { expandedTitle -> + val (x, y) = calculateViewCoordinates(expandedTitle) - val expandedTitleMarginTop = - when (val layoutParams = expandedTitle.layoutParams) { - is MarginLayoutParams -> layoutParams.topMargin - else -> 0 - } + val expandedTitleMarginTop = + when (val layoutParams = expandedTitle.layoutParams) { + is MarginLayoutParams -> layoutParams.topMargin + else -> 0 + } - expandedTitleHeight = expandedTitle.height.toFloat() + expandedTitleHeight = expandedTitle.height.toFloat() - scaleInterpolation.start = expandedTitleHeight / maxOf(1.0f, titleHeight) - xOffsetInterpolation.start = x - yOffsetInterpolation.start = y + scaleInterpolation.start = expandedTitleHeight / maxOf(1.0f, titleHeight) + xOffsetInterpolation.start = x + yOffsetInterpolation.start = y - scrollInterpolation.end = expandedTitleHeight + expandedTitleMarginTop - } + scrollInterpolation.end = expandedTitleHeight + expandedTitleMarginTop + } - private val titleLayoutListener: LayoutListener = - LayoutListener() { title -> - val (x, y) = calculateViewCoordinates(title) + private val titleLayoutListener: LayoutListener = LayoutListener { title -> + val (x, y) = calculateViewCoordinates(title) - titleWidth = title.width.toFloat() - titleHeight = title.height.toFloat() + titleWidth = title.width.toFloat() + titleHeight = title.height.toFloat() - scaleInterpolation.start = expandedTitleHeight / maxOf(1.0f, titleHeight) - scaleInterpolation.end = collapsedTitleHeight / maxOf(1.0f, titleHeight) - xOffsetInterpolation.reference = x - yOffsetInterpolation.reference = y - } + scaleInterpolation.start = expandedTitleHeight / maxOf(1.0f, titleHeight) + scaleInterpolation.end = collapsedTitleHeight / maxOf(1.0f, titleHeight) + xOffsetInterpolation.reference = x + yOffsetInterpolation.reference = y + } private val titleView = parentView.findViewById(R.id.title).apply { @@ -104,8 +101,9 @@ class CollapsibleTitleController(val parentView: View, scrollAreaId: Int = R.id. pivotY = 0.0f } - private val scrollAreaLayoutListener: LayoutListener = - LayoutListener() { scrollOffset = scrollArea.verticalScrollOffset.toFloat() } + private val scrollAreaLayoutListener: LayoutListener = LayoutListener { + scrollOffset = scrollArea.verticalScrollOffset.toFloat() + } private val scrollArea = parentView.findViewById(scrollAreaId).let { view -> diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/CustomTransformationMethod.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/CustomTransformationMethod.kt index 083258c62d35..451c51fa3f0f 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/CustomTransformationMethod.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/CustomTransformationMethod.kt @@ -10,7 +10,7 @@ private const val DOT_CHAR = '\u2022' private const val EMPTY_STRING = "" private const val SPACE_CHAR = ' ' -class GroupedTransformationMethod() : TransformationMethod { +class GroupedTransformationMethod : TransformationMethod { override fun getTransformation(source: CharSequence?, view: View?): CharSequence { return source?.groupWithSpaces() ?: EMPTY_STRING } @@ -26,7 +26,7 @@ class GroupedTransformationMethod() : TransformationMethod { } } -class GroupedPasswordTransformationMethod() : PasswordTransformationMethod() { +class GroupedPasswordTransformationMethod : PasswordTransformationMethod() { override fun getTransformation(source: CharSequence?, view: View?): CharSequence { return if (source != null && view != null) { super.getTransformation(source, view) diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/LoginFragment.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/LoginFragment.kt index c02ed0b65269..7e298e3f7392 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/LoginFragment.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/LoginFragment.kt @@ -272,7 +272,7 @@ class LoginFragment : BaseFragment(), NavigationBarPainter { private fun loginFailure(description: String? = "") { title.setText(R.string.login_fail_title) - subtitle.setText(description) + subtitle.text = description loggingInStatus.visibility = View.GONE loginFailStatus.visibility = View.VISIBLE diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/OutOfTimeFragment.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/OutOfTimeFragment.kt index 9b5eb395ad97..1a59f19ff760 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/OutOfTimeFragment.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/OutOfTimeFragment.kt @@ -96,11 +96,11 @@ class OutOfTimeFragment : BaseFragment() { newAccount = false setOnClickAction("openAccountPageInBrowser", jobTracker) { - setEnabled(false) + isEnabled = false serviceConnectionManager.authTokenCache()?.fetchAuthToken()?.let { token -> context.openAccountPageInBrowser(token) } - setEnabled(true) + isEnabled = true } isEnabled = true @@ -170,10 +170,10 @@ class OutOfTimeFragment : BaseFragment() { disconnectButton.apply { if (showButton) { - setEnabled(true) + isEnabled = true visibility = View.VISIBLE } else { - setEnabled(false) + isEnabled = false visibility = View.GONE } } @@ -182,17 +182,17 @@ class OutOfTimeFragment : BaseFragment() { private fun updateBuyButtons() { val currentState = tunnelState val hasConnectivity = currentState is TunnelState.Disconnected - sitePaymentButton.setEnabled(hasConnectivity) + sitePaymentButton.isEnabled = hasConnectivity val isOffline = currentState is TunnelState.Error && currentState.errorState.cause is ErrorStateCause.IsOffline - redeemButton.setEnabled(!isOffline) + redeemButton.isEnabled = !isOffline } private fun checkExpiry(maybeExpiry: DateTime?) { maybeExpiry?.let { expiry -> - if (expiry.isAfterNow()) { + if (expiry.isAfterNow) { jobTracker.newUiJob("advanceToConnectScreen") { advanceToConnectScreen() } } } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/ProblemReportFragment.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/ProblemReportFragment.kt index 4d5ca3c9b59a..b30b5713fbba 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/ProblemReportFragment.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/ProblemReportFragment.kt @@ -280,7 +280,7 @@ class ProblemReportFragment : BaseFragment() { } private fun setSendButtonEnabled(enabled: Boolean) { - sendButton.setEnabled(enabled) + sendButton.isEnabled = enabled sendButton.alpha = if (enabled) 1.0F else 0.5F } diff --git a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/RedeemVoucherDialogFragment.kt b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/RedeemVoucherDialogFragment.kt index 59318a8268b9..46472ea6cc1e 100644 --- a/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/RedeemVoucherDialogFragment.kt +++ b/android/app/src/main/kotlin/net/mullvad/mullvadvpn/ui/fragment/RedeemVoucherDialogFragment.kt @@ -88,7 +88,7 @@ class RedeemVoucherDialogFragment : DialogFragment() { redeemButton = view.findViewById