Skip to content

Commit b93967d

Browse files
authored
Merge pull request #990 from synonymdev/fix/widget-preview-defaults
fix: align widget preview defaults with ios
2 parents c66670d + 558acc1 commit b93967d

5 files changed

Lines changed: 79 additions & 13 deletions

File tree

app/src/main/java/to/bitkit/ui/screens/widgets/WidgetSizeDraft.kt

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,23 @@ import to.bitkit.models.WidgetType
1414

1515
/**
1616
* Tracks the widget size chosen in a preview sheet's size carousel. Before the user picks a size it
17-
* reflects the persisted size (or the type default for a not-yet-saved widget); once the user swipes
18-
* the carousel the draft takes over. [current] is read by the widget's save action.
17+
* reflects the persisted size (or small for a not-yet-saved widget, matching iOS); once the user
18+
* swipes the carousel the draft takes over. [current] is read by the widget's save action.
1919
*/
2020
class WidgetSizeDraft(
2121
scope: CoroutineScope,
2222
private val type: WidgetType,
2323
widgetsDataFlow: StateFlow<WidgetsData>,
2424
subscriptionTimeoutMs: Long = SUBSCRIPTION_TIMEOUT,
2525
) {
26-
private val default = WidgetSize.default(type)
27-
2826
private val savedSize: StateFlow<WidgetSize> = widgetsDataFlow
29-
.map { data -> data.widgets.firstOrNull { it.type == type }?.size ?: default }
30-
.stateIn(scope, SharingStarted.WhileSubscribed(subscriptionTimeoutMs), default)
27+
.map { data -> data.widgets.firstOrNull { it.type == type }?.size ?: WidgetSize.SMALL }
28+
.stateIn(scope, SharingStarted.WhileSubscribed(subscriptionTimeoutMs), WidgetSize.SMALL)
3129

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

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

3735
val current: WidgetSize get() = size.value
3836

app/src/main/java/to/bitkit/ui/screens/widgets/price/PriceCard.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fun PriceCardSmall(
138138
horizontalArrangement = Arrangement.SpaceBetween,
139139
modifier = Modifier
140140
.fillMaxWidth()
141-
.testTag("price_card_small_pair_row_${widgetData.pair.displayName}")
141+
.testTag("PriceWidgetRow-${widgetData.pair.displayName}")
142142
) {
143143
Caption13Up(
144144
text = widgetData.pair.displayName,

app/src/main/java/to/bitkit/ui/sheets/WidgetsSheet.kt

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,10 @@ private fun WidgetsSheetContent(
101101
val galleryViewModelStoreOwner = rememberSheetViewModelStoreOwner()
102102
val galleryScrollState = rememberScrollState()
103103
val navBackStackEntry by navController.currentBackStackEntryAsState()
104-
val isGalleryRoute = navBackStackEntry?.destination?.hasRoute<WidgetsRoute.Gallery>() == true
105104
val widgetFlowKey = navBackStackEntry?.destination?.widgetFlowKey()
106105
?: startRoute.widgetFlowKey().takeIf { navBackStackEntry == null }
107106
val widgetViewModelStoreOwner = rememberWidgetFlowViewModelStoreOwner(widgetFlowKey)
108107

109-
LaunchedEffect(isGalleryRoute) {
110-
galleryScrollState.scrollTo(0)
111-
}
112-
113108
Column(
114109
modifier = Modifier
115110
.fillMaxWidth()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package to.bitkit.ui.screens.widgets
2+
3+
import kotlinx.coroutines.ExperimentalCoroutinesApi
4+
import kotlinx.coroutines.flow.MutableStateFlow
5+
import kotlinx.coroutines.flow.first
6+
import kotlinx.coroutines.launch
7+
import kotlinx.coroutines.test.advanceUntilIdle
8+
import org.junit.Before
9+
import to.bitkit.data.WidgetsData
10+
import to.bitkit.models.WidgetSize
11+
import to.bitkit.models.WidgetType
12+
import to.bitkit.models.WidgetWithPosition
13+
import to.bitkit.test.BaseUnitTest
14+
import kotlin.test.Test
15+
import kotlin.test.assertEquals
16+
17+
@OptIn(ExperimentalCoroutinesApi::class)
18+
class WidgetSizeDraftTest : BaseUnitTest() {
19+
20+
private val widgetsData = MutableStateFlow(WidgetsData(widgets = emptyList()))
21+
22+
@Before
23+
fun setUp() {
24+
widgetsData.value = WidgetsData(widgets = emptyList())
25+
}
26+
27+
@Test
28+
fun `unsaved widget defaults to small for wide types`() = test {
29+
val draft = WidgetSizeDraft(backgroundScope, WidgetType.PRICE, widgetsData)
30+
val collector = backgroundScope.launch { draft.size.collect {} }
31+
32+
advanceUntilIdle()
33+
34+
assertEquals(WidgetSize.SMALL, draft.size.first())
35+
collector.cancel()
36+
}
37+
38+
@Test
39+
fun `saved widget reflects persisted size`() = test {
40+
widgetsData.value = WidgetsData(
41+
widgets = listOf(
42+
WidgetWithPosition(type = WidgetType.PRICE, position = 0, size = WidgetSize.WIDE),
43+
),
44+
)
45+
val draft = WidgetSizeDraft(backgroundScope, WidgetType.PRICE, widgetsData)
46+
val collector = backgroundScope.launch { draft.size.collect {} }
47+
48+
advanceUntilIdle()
49+
50+
assertEquals(WidgetSize.WIDE, draft.size.first())
51+
collector.cancel()
52+
}
53+
54+
@Test
55+
fun `user draft overrides saved size`() = test {
56+
widgetsData.value = WidgetsData(
57+
widgets = listOf(
58+
WidgetWithPosition(type = WidgetType.FACTS, position = 0, size = WidgetSize.SMALL),
59+
),
60+
)
61+
val draft = WidgetSizeDraft(backgroundScope, WidgetType.FACTS, widgetsData)
62+
val collector = backgroundScope.launch { draft.size.collect {} }
63+
64+
advanceUntilIdle()
65+
draft.set(WidgetSize.WIDE)
66+
67+
advanceUntilIdle()
68+
69+
assertEquals(WidgetSize.WIDE, draft.size.first())
70+
collector.cancel()
71+
}
72+
}

changelog.d/next/990.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.

0 commit comments

Comments
 (0)