Skip to content

Commit c429548

Browse files
committed
feat: unarchive funded accounts
1 parent 72d68f8 commit c429548

File tree

5 files changed

+31
-16
lines changed

5 files changed

+31
-16
lines changed

common/src/commonMain/kotlin/com/blockstream/common/gdk/GdkSession.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1754,7 +1754,7 @@ class GdkSession constructor(
17541754
private suspend fun initializeSessionData(wallet: GreenWallet?, initNetwork: String?, initAccount: Long?) {
17551755
// Check if active account index was archived from 1) a different client (multisig) or 2) from cached Singlesig hww session
17561756
// Expect refresh = true to be already called
1757-
updateAccounts()
1757+
updateAccounts(unarchiveFunded = true)
17581758

17591759
_activeAccountStateFlow.value = accounts.value.find {
17601760
it.networkId == initNetwork && it.pointer == initAccount && !it.hidden
@@ -2054,7 +2054,7 @@ class GdkSession constructor(
20542054
_activeAccountStateFlow.value = accounts.value.find { it.id == account.id } ?: account
20552055
}
20562056

2057-
fun removeAccount(account: Account) {
2057+
suspend fun removeAccount(account: Account) {
20582058
if (account.isLightning) {
20592059
hasLightning = false
20602060

@@ -2217,7 +2217,7 @@ class GdkSession constructor(
22172217
accountsAndBalancesMutex.withLock {
22182218

22192219
// Update accounts
2220-
updateAccounts(refresh = refresh)
2220+
updateAccounts(refresh = refresh, unarchiveFunded = true)
22212221

22222222
for (account in this@GdkSession.allAccounts.value) {
22232223
if ((updateBalancesForAccounts == null && updateBalancesForNetwork == null) || updateBalancesForAccounts?.find { account.id == it.id } != null || account.network == updateBalancesForNetwork) {
@@ -2634,14 +2634,22 @@ class GdkSession constructor(
26342634
}
26352635
}
26362636

2637-
private fun updateAccounts(refresh: Boolean = false) {
2637+
private suspend fun updateAccounts(refresh: Boolean = false, unarchiveFunded: Boolean = false) {
26382638
getAccounts(refresh).also { fetchedAccounts ->
26392639
_allAccountsStateFlow.value = fetchedAccounts
2640+
26402641
fetchedAccounts.filter { !it.hidden }.also {
26412642
_accountsStateFlow.value = it
26422643
_zeroAccounts.value = it.isEmpty() && failedNetworks.value.isEmpty()
26432644
}
26442645

2646+
if (unarchiveFunded) {
2647+
// Unarchive accounts if they are hidden and funded
2648+
fetchedAccounts.filter { it.hidden && it.isFunded(this) }.forEach {
2649+
updateAccount(it, isHidden = false)
2650+
}
2651+
}
2652+
26452653
// Update active account to get fresh data, also prevent it from being archived and active
26462654
_activeAccountStateFlow.value = accounts.value.find {
26472655
activeAccount.value?.id == it.id

common/src/commonMain/kotlin/com/blockstream/common/models/archived/ArchivedAccountsViewModel.kt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import com.blockstream.common.extensions.launchIn
99
import com.blockstream.common.extensions.previewAccountAssetBalance
1010
import com.blockstream.common.extensions.previewWallet
1111
import com.blockstream.common.gdk.data.AccountAssetBalance
12+
import com.blockstream.common.gdk.data.AccountType
1213
import com.blockstream.common.models.GreenViewModel
1314
import com.blockstream.ui.navigation.NavData
1415
import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState
@@ -35,9 +36,13 @@ class ArchivedAccountsViewModel(greenWallet: GreenWallet, navigateToRoot: Boolea
3536
ArchivedAccountsViewModelAbstract(greenWallet = greenWallet, navigateToRoot = navigateToRoot) {
3637

3738
override val archivedAccounts: StateFlow<DataState<List<AccountAssetBalance>>> =
38-
session.allAccounts.map {
39+
session.allAccounts.map { allAccounts ->
3940
DataState.Success(
40-
it.filter { it.hidden && it.hasHistory(session) }.map {
41+
allAccounts.filter {
42+
it.hidden
43+
}.filter {
44+
it.hasHistory(session) || !(it.type == AccountType.BIP49_SEGWIT_WRAPPED && it.pointer == 0L) // GDK default account
45+
}.map {
4146
AccountAssetBalance.create(accountAsset = it.accountAsset, session = session)
4247
}
4348
)

common/src/commonMain/kotlin/com/blockstream/common/models/assetaccounts/AssetAccountDetailsViewModel.kt

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import com.blockstream.green.utils.Loggable
2626
import com.blockstream.ui.events.Event
2727
import com.blockstream.ui.navigation.NavAction
2828
import com.blockstream.ui.navigation.NavData
29-
import org.jetbrains.compose.resources.getString
3029
import com.rickclephas.kmp.nativecoroutines.NativeCoroutinesState
3130
import com.rickclephas.kmp.observableviewmodel.coroutineScope
3231
import com.rickclephas.kmp.observableviewmodel.launch
@@ -38,6 +37,7 @@ import kotlinx.coroutines.flow.filter
3837
import kotlinx.coroutines.flow.launchIn
3938
import kotlinx.coroutines.flow.map
4039
import kotlinx.coroutines.flow.onEach
40+
import org.jetbrains.compose.resources.getString
4141

4242
abstract class AssetAccountDetailsViewModelAbstract(
4343
greenWallet: GreenWallet, accountAssetOrNull: AccountAsset? = null
@@ -232,7 +232,7 @@ class AssetAccountDetailsViewModel(
232232
accountAsset: AccountAsset?,
233233
watchOnly: Boolean
234234
): List<NavAction> {
235-
if (account.isLightning) {
235+
if (account.isLightning || watchOnly) {
236236
return emptyList()
237237
}
238238

@@ -244,7 +244,7 @@ class AssetAccountDetailsViewModel(
244244
onClick = {
245245
postEvent(NavigateDestinations.RenameAccount(greenWallet = greenWallet, account = account))
246246
}
247-
).takeIf { !watchOnly },
247+
),
248248

249249
NavAction(
250250
title = getString(Res.string.id_watchonly),
@@ -260,20 +260,17 @@ class AssetAccountDetailsViewModel(
260260
postEvent(NavigateDestinations.WatchOnlyCredentialsSettings(greenWallet = greenWallet, network = account.network))
261261
}
262262
}
263-
).takeIf { !watchOnly },
263+
),
264264

265265
NavAction(
266266
title = getString(Res.string.id_archive_account),
267267
icon = Res.drawable.box_arrow_down,
268268
isMenuEntry = true,
269+
enabled = !account.isFunded(session) && !account.hasUnconfirmedTransactions(session),
269270
onClick = {
270271
postEvent(Events.ArchiveAccount(account))
271272
}
272-
).takeIf {
273-
!watchOnly &&
274-
!account.isFunded(session) &&
275-
!account.hasUnconfirmedTransactions(session)
276-
}
273+
)
277274
)
278275
}
279276

compose/src/commonMain/kotlin/com/blockstream/compose/components/PopupMenu.kt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ data class MenuEntry(
3636
val title: String,
3737
val iconRes: DrawableResource? = null,
3838
val imageVector: ImageVector? = null,
39+
val enabled: Boolean = true,
3940
val onClick: () -> Unit = {}
4041
) {
4142
companion object {
@@ -46,6 +47,7 @@ data class MenuEntry(
4647
title = navAction.title ?: stringResource(navAction.titleRes!!),
4748
iconRes = navAction.icon,
4849
imageVector = navAction.imageVector,
50+
enabled = navAction.enabled,
4951
onClick = navAction.onClick
5052
)
5153
}
@@ -70,6 +72,7 @@ fun PopupMenu(modifier: Modifier = Modifier, state: PopupState, entries: List<Me
7072
entries.forEach {
7173
DropdownMenuItem(
7274
text = { Text(it.title) },
75+
enabled = it.enabled,
7376
onClick = {
7477
it.onClick.invoke()
7578
state.isContextMenuVisible.value = false
@@ -115,14 +118,15 @@ fun ActionMenu(
115118
TextButton(
116119
onClick = it.onClick,
117120
modifier = Modifier.align(Alignment.CenterVertically),
121+
enabled = it.enabled,
118122
colors = ButtonDefaults.textButtonColors(contentColor = MaterialTheme.colorScheme.onSurface)
119123
) {
120124
Text(text = it.title ?: stringResource(it.titleRes!!), style = labelMedium)
121125
}
122126
} else {
123127
IconButton(onClick = {
124128
it.onClick()
125-
}) {
129+
}, enabled = it.enabled) {
126130
it.icon?.also {
127131
Icon(
128132
painter = painterResource(it),

ui-common/src/commonMain/kotlin/com/blockstream/ui/navigation/AppBarState.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ data class NavAction(
4343
val icon: DrawableResource? = null,
4444
val imageVector: ImageVector? = null,
4545
val isMenuEntry: Boolean = false,
46+
val enabled: Boolean = true,
4647
val onClick: () -> Unit = { }
4748
)
4849

0 commit comments

Comments
 (0)