From 99caa031e87bcd905537d648b961826385ac0f43 Mon Sep 17 00:00:00 2001 From: Benoit Marty Date: Tue, 26 Sep 2023 18:07:24 +0200 Subject: [PATCH] Replace `mutableStateOf` by `mutableIntStateOf` and `mutableFloatStateOf`. Use `intValue` and `floatValue` to avoid unboxing. --- .../features/location/api/StaticMapView.kt | 4 ++-- .../waitlistscreen/WaitListPresenter.kt | 7 ++++--- .../impl/media/viewer/MediaViewerPresenter.kt | 3 ++- .../components/TimelineItemEventRow.kt | 2 +- .../impl/bugreport/BugReportPresenter.kt | 18 ++++++++++-------- .../swipe/SwipeableActionsState.kt | 15 ++++++++------- .../designsystem/theme/components/Slider.kt | 4 ++-- .../matrix/ui/components/SelectedUsersList.kt | 4 ++-- 8 files changed, 31 insertions(+), 26 deletions(-) diff --git a/features/location/api/src/main/kotlin/io/element/android/features/location/api/StaticMapView.kt b/features/location/api/src/main/kotlin/io/element/android/features/location/api/StaticMapView.kt index ead0b98228..f4443e92f4 100644 --- a/features/location/api/src/main/kotlin/io/element/android/features/location/api/StaticMapView.kt +++ b/features/location/api/src/main/kotlin/io/element/android/features/location/api/StaticMapView.kt @@ -21,7 +21,7 @@ import androidx.compose.foundation.layout.BoxWithConstraints import androidx.compose.foundation.layout.size import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment @@ -64,7 +64,7 @@ fun StaticMapView( contentAlignment = Alignment.Center ) { val context = LocalContext.current - var retryHash by remember { mutableStateOf(0) } + var retryHash by remember { mutableIntStateOf(0) } val builder = remember { StaticMapUrlBuilder(context) } val painter = rememberAsyncImagePainter( model = if (constraints.isZero) { diff --git a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/screens/waitlistscreen/WaitListPresenter.kt b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/screens/waitlistscreen/WaitListPresenter.kt index 9c07204ab2..039986343b 100644 --- a/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/screens/waitlistscreen/WaitListPresenter.kt +++ b/features/login/impl/src/main/kotlin/io/element/android/features/login/impl/screens/waitlistscreen/WaitListPresenter.kt @@ -18,6 +18,7 @@ package io.element.android.features.login.impl.screens.waitlistscreen import androidx.compose.runtime.Composable import androidx.compose.runtime.MutableState +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope @@ -58,14 +59,14 @@ class WaitListPresenter @AssistedInject constructor( mutableStateOf(Async.Uninitialized) } - val attemptNumber: MutableState = remember { mutableStateOf(0) } + val attemptNumber = remember { mutableIntStateOf(0) } fun handleEvents(event: WaitListEvents) { when (event) { WaitListEvents.AttemptLogin -> { // Do not attempt to login on first resume of the View. - attemptNumber.value++ - if (attemptNumber.value > 1) { + attemptNumber.intValue++ + if (attemptNumber.intValue > 1) { coroutineScope.loginAttempt(formState, loginAction) } } diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/viewer/MediaViewerPresenter.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/viewer/MediaViewerPresenter.kt index 13a9ff3bee..b295f68e11 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/viewer/MediaViewerPresenter.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/media/viewer/MediaViewerPresenter.kt @@ -21,6 +21,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.MutableState import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope @@ -59,7 +60,7 @@ class MediaViewerPresenter @AssistedInject constructor( @Composable override fun present(): MediaViewerState { val coroutineScope = rememberCoroutineScope() - var loadMediaTrigger by remember { mutableStateOf(0) } + var loadMediaTrigger by remember { mutableIntStateOf(0) } val mediaFile: MutableState = remember { mutableStateOf(null) } diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/TimelineItemEventRow.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/TimelineItemEventRow.kt index 8a20007151..4ab98ce953 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/TimelineItemEventRow.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/timeline/components/TimelineItemEventRow.kt @@ -147,7 +147,7 @@ fun TimelineItemEventRow( } if (canReply) { val state: SwipeableActionsState = rememberSwipeableActionsState() - val offset = state.offset.value + val offset = state.offset.floatValue val swipeThresholdPx = 40.dp.toPx() val thresholdCrossed = abs(offset) > swipeThresholdPx SwipeSensitivity(3f) { diff --git a/features/rageshake/impl/src/main/kotlin/io/element/android/features/rageshake/impl/bugreport/BugReportPresenter.kt b/features/rageshake/impl/src/main/kotlin/io/element/android/features/rageshake/impl/bugreport/BugReportPresenter.kt index 5fd95e79bd..9258909201 100644 --- a/features/rageshake/impl/src/main/kotlin/io/element/android/features/rageshake/impl/bugreport/BugReportPresenter.kt +++ b/features/rageshake/impl/src/main/kotlin/io/element/android/features/rageshake/impl/bugreport/BugReportPresenter.kt @@ -17,9 +17,11 @@ package io.element.android.features.rageshake.impl.bugreport import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableFloatState import androidx.compose.runtime.MutableState import androidx.compose.runtime.collectAsState import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable @@ -43,27 +45,27 @@ class BugReportPresenter @Inject constructor( ) : Presenter { private class BugReporterUploadListener( - private val sendingProgress: MutableState, + private val sendingProgress: MutableFloatState, private val sendingAction: MutableState> ) : BugReporterListener { override fun onUploadCancelled() { - sendingProgress.value = 0f + sendingProgress.floatValue = 0f sendingAction.value = Async.Uninitialized } override fun onUploadFailed(reason: String?) { - sendingProgress.value = 0f + sendingProgress.floatValue = 0f sendingAction.value = Async.Failure(Exception(reason)) } override fun onProgress(progress: Int) { - sendingProgress.value = progress.toFloat() / 100 + sendingProgress.floatValue = progress.toFloat() / 100 sendingAction.value = Async.Loading() } override fun onUploadSucceed(reportUrl: String?) { - sendingProgress.value = 0f + sendingProgress.floatValue = 0f sendingAction.value = Async.Success(Unit) } } @@ -80,7 +82,7 @@ class BugReportPresenter @Inject constructor( .collectAsState(initial = "") val sendingProgress = remember { - mutableStateOf(0f) + mutableFloatStateOf(0f) } val sendingAction: MutableState> = remember { mutableStateOf(Async.Uninitialized) @@ -107,7 +109,7 @@ class BugReportPresenter @Inject constructor( copy(sendScreenshot = event.sendScreenshot) } BugReportEvents.ClearError -> { - sendingProgress.value = 0f + sendingProgress.floatValue = 0f sendingAction.value = Async.Uninitialized } } @@ -115,7 +117,7 @@ class BugReportPresenter @Inject constructor( return BugReportState( hasCrashLogs = crashInfo.isNotEmpty(), - sendingProgress = sendingProgress.value, + sendingProgress = sendingProgress.floatValue, sending = sendingAction.value, formState = formState.value, screenshotUri = screenshotUri.value, diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/swipe/SwipeableActionsState.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/swipe/SwipeableActionsState.kt index 77d80cdde5..542c46fae1 100644 --- a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/swipe/SwipeableActionsState.kt +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/swipe/SwipeableActionsState.kt @@ -21,9 +21,10 @@ import androidx.compose.animation.core.tween import androidx.compose.foundation.MutatePriority import androidx.compose.foundation.gestures.DraggableState import androidx.compose.runtime.Composable +import androidx.compose.runtime.FloatState import androidx.compose.runtime.Stable -import androidx.compose.runtime.State import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue @@ -41,8 +42,8 @@ class SwipeableActionsState { /** * The current position (in pixels) of the content. */ - val offset: State get() = offsetState - private var offsetState = mutableStateOf(0f) + val offset: FloatState get() = offsetState + private var offsetState = mutableFloatStateOf(0f) /** * Whether the content is currently animating to reset its offset after it was swiped. @@ -51,21 +52,21 @@ class SwipeableActionsState { private set val draggableState = DraggableState { delta -> - val targetOffset = offsetState.value + delta + val targetOffset = offsetState.floatValue + delta val isAllowed = isResettingOnRelease || targetOffset > 0f - offsetState.value += if (isAllowed) delta else 0f + offsetState.floatValue += if (isAllowed) delta else 0f } suspend fun resetOffset() { draggableState.drag(MutatePriority.PreventUserInput) { isResettingOnRelease = true try { - Animatable(offsetState.value).animateTo( + Animatable(offsetState.floatValue).animateTo( targetValue = 0f, animationSpec = tween(durationMillis = 300), ) { - dragBy(value - offsetState.value) + dragBy(value - offsetState.floatValue) } } finally { isResettingOnRelease = false diff --git a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/Slider.kt b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/Slider.kt index 872376583d..2041d7998d 100644 --- a/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/Slider.kt +++ b/libraries/designsystem/src/main/kotlin/io/element/android/libraries/designsystem/theme/components/Slider.kt @@ -22,7 +22,7 @@ import androidx.compose.material3.SliderColors import androidx.compose.material3.SliderDefaults import androidx.compose.runtime.Composable import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.mutableFloatStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue import androidx.compose.ui.Modifier @@ -62,7 +62,7 @@ internal fun SlidersPreview() = ElementThemedPreview { ContentToPreview() } @Composable private fun ContentToPreview() { - var value by remember { mutableStateOf(0.33f) } + var value by remember { mutableFloatStateOf(0.33f) } Column { Slider(onValueChange = { value = it }, value = value, enabled = true) Slider(steps = 10, onValueChange = { value = it }, value = value, enabled = true) diff --git a/libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/components/SelectedUsersList.kt b/libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/components/SelectedUsersList.kt index 36eb445ae6..884f45de02 100644 --- a/libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/components/SelectedUsersList.kt +++ b/libraries/matrixui/src/main/kotlin/io/element/android/libraries/matrix/ui/components/SelectedUsersList.kt @@ -29,7 +29,7 @@ import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.derivedStateOf import androidx.compose.runtime.getValue -import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.mutableIntStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue @@ -55,7 +55,7 @@ fun SelectedUsersList( ) { val lazyListState = rememberLazyListState() if (autoScroll) { - var currentSize by rememberSaveable { mutableStateOf(selectedUsers.size) } + var currentSize by rememberSaveable { mutableIntStateOf(selectedUsers.size) } LaunchedEffect(selectedUsers.size) { val isItemAdded = selectedUsers.size > currentSize if (isItemAdded) {