Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,23 @@ import to.bitkit.models.WidgetType

/**
* Tracks the widget size chosen in a preview sheet's size carousel. Before the user picks a size it
* reflects the persisted size (or the type default for a not-yet-saved widget); once the user swipes
* the carousel the draft takes over. [current] is read by the widget's save action.
* reflects the persisted size (or small for a not-yet-saved widget, matching iOS); once the user
* swipes the carousel the draft takes over. [current] is read by the widget's save action.
*/
class WidgetSizeDraft(
scope: CoroutineScope,
private val type: WidgetType,
widgetsDataFlow: StateFlow<WidgetsData>,
subscriptionTimeoutMs: Long = SUBSCRIPTION_TIMEOUT,
) {
private val default = WidgetSize.default(type)

private val savedSize: StateFlow<WidgetSize> = widgetsDataFlow
.map { data -> data.widgets.firstOrNull { it.type == type }?.size ?: default }
.stateIn(scope, SharingStarted.WhileSubscribed(subscriptionTimeoutMs), default)
.map { data -> data.widgets.firstOrNull { it.type == type }?.size ?: WidgetSize.SMALL }
.stateIn(scope, SharingStarted.WhileSubscribed(subscriptionTimeoutMs), WidgetSize.SMALL)

private val _draft = MutableStateFlow<WidgetSize?>(null)

val size: StateFlow<WidgetSize> = combine(_draft, savedSize) { draft, saved -> draft ?: saved }
.stateIn(scope, SharingStarted.WhileSubscribed(subscriptionTimeoutMs), default)
.stateIn(scope, SharingStarted.WhileSubscribed(subscriptionTimeoutMs), WidgetSize.SMALL)

val current: WidgetSize get() = size.value

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fun PriceCardSmall(
horizontalArrangement = Arrangement.SpaceBetween,
modifier = Modifier
.fillMaxWidth()
.testTag("price_card_small_pair_row_${widgetData.pair.displayName}")
.testTag("PriceWidgetRow-${widgetData.pair.displayName}")
) {
Caption13Up(
text = widgetData.pair.displayName,
Expand Down
5 changes: 0 additions & 5 deletions app/src/main/java/to/bitkit/ui/sheets/WidgetsSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,10 @@ private fun WidgetsSheetContent(
val galleryViewModelStoreOwner = rememberSheetViewModelStoreOwner()
val galleryScrollState = rememberScrollState()
val navBackStackEntry by navController.currentBackStackEntryAsState()
val isGalleryRoute = navBackStackEntry?.destination?.hasRoute<WidgetsRoute.Gallery>() == true
val widgetFlowKey = navBackStackEntry?.destination?.widgetFlowKey()
?: startRoute.widgetFlowKey().takeIf { navBackStackEntry == null }
val widgetViewModelStoreOwner = rememberWidgetFlowViewModelStoreOwner(widgetFlowKey)

LaunchedEffect(isGalleryRoute) {
galleryScrollState.scrollTo(0)
}

Column(
modifier = Modifier
.fillMaxWidth()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package to.bitkit.ui.screens.widgets

import kotlinx.coroutines.ExperimentalCoroutinesApi
Comment thread
github-advanced-security[bot] marked this conversation as resolved.
Fixed
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch
import kotlinx.coroutines.test.advanceUntilIdle
import org.junit.Before
import to.bitkit.data.WidgetsData
import to.bitkit.models.WidgetSize
import to.bitkit.models.WidgetType
import to.bitkit.models.WidgetWithPosition
import to.bitkit.test.BaseUnitTest
import kotlin.test.Test
import kotlin.test.assertEquals

@OptIn(ExperimentalCoroutinesApi::class)
class WidgetSizeDraftTest : BaseUnitTest() {

private val widgetsData = MutableStateFlow(WidgetsData(widgets = emptyList()))

@Before
fun setUp() {
widgetsData.value = WidgetsData(widgets = emptyList())
}

@Test
fun `unsaved widget defaults to small for wide types`() = test {
val draft = WidgetSizeDraft(backgroundScope, WidgetType.PRICE, widgetsData)
val collector = backgroundScope.launch { draft.size.collect {} }

advanceUntilIdle()

assertEquals(WidgetSize.SMALL, draft.size.first())
collector.cancel()
}

@Test
fun `saved widget reflects persisted size`() = test {
widgetsData.value = WidgetsData(
widgets = listOf(
WidgetWithPosition(type = WidgetType.PRICE, position = 0, size = WidgetSize.WIDE),
),
)
val draft = WidgetSizeDraft(backgroundScope, WidgetType.PRICE, widgetsData)
val collector = backgroundScope.launch { draft.size.collect {} }

advanceUntilIdle()

assertEquals(WidgetSize.WIDE, draft.size.first())
collector.cancel()
}

@Test
fun `user draft overrides saved size`() = test {
widgetsData.value = WidgetsData(
widgets = listOf(
WidgetWithPosition(type = WidgetType.FACTS, position = 0, size = WidgetSize.SMALL),
),
)
val draft = WidgetSizeDraft(backgroundScope, WidgetType.FACTS, widgetsData)
val collector = backgroundScope.launch { draft.size.collect {} }

advanceUntilIdle()
draft.set(WidgetSize.WIDE)

advanceUntilIdle()

assertEquals(WidgetSize.WIDE, draft.size.first())
collector.cancel()
}
}
1 change: 1 addition & 0 deletions changelog.d/next/990.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
New widgets now open on compact size in the preview carousel, matching iOS, and the add-widgets list keeps its scroll position when navigating back.
Loading