Skip to content

Commit 98c2620

Browse files
committed
fix: backup tags on activity edit
1 parent d4a288c commit 98c2620

File tree

3 files changed

+36
-34
lines changed

3 files changed

+36
-34
lines changed

app/src/main/java/to/bitkit/ui/screens/wallets/send/SendCoinSelectionViewModel.kt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import org.lightningdevkit.ldknode.SpendableUtxo
1414
import to.bitkit.di.BgDispatcher
1515
import to.bitkit.env.Env
1616
import to.bitkit.ext.rawId
17+
import to.bitkit.repositories.ActivityRepo
1718
import to.bitkit.repositories.LightningRepo
18-
import to.bitkit.services.CoreService
1919
import to.bitkit.ui.shared.toast.ToastEventBus
2020
import to.bitkit.utils.Logger
2121
import javax.inject.Inject
@@ -24,7 +24,7 @@ import javax.inject.Inject
2424
class SendCoinSelectionViewModel @Inject constructor(
2525
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
2626
private val lightningRepo: LightningRepo,
27-
private val coreService: CoreService,
27+
private val activityRepo: ActivityRepo,
2828
) : ViewModel() {
2929

3030
private val _uiState = MutableStateFlow(CoinSelectionUiState())
@@ -74,17 +74,19 @@ class SendCoinSelectionViewModel @Inject constructor(
7474
if (_tagsByTxId.value.containsKey(txId)) return
7575

7676
viewModelScope.launch(bgDispatcher) {
77-
runCatching {
78-
// find activity by txId
79-
onchainActivities.firstOrNull { (it as? Onchain)?.v1?.txId == txId }?.let { activity ->
80-
// get tags by activity id
81-
coreService.activity.tags(forActivityId = activity.rawId())
82-
.takeIf { it.isNotEmpty() }
83-
?.let { tags ->
77+
// find activity by txId
78+
onchainActivities.firstOrNull { (it as? Onchain)?.v1?.txId == txId }?.let { activity ->
79+
// get tags by activity id
80+
activityRepo.getActivityTags(activity.rawId())
81+
.onSuccess { tags ->
82+
if (tags.isNotEmpty()) {
8483
// add map entry linking tags to utxo.outpoint.txid
8584
_tagsByTxId.update { currentMap -> currentMap + (txId to tags) }
8685
}
87-
}
86+
}
87+
.onFailure { e ->
88+
Logger.error("Failed to load tags for utxo $txId", e)
89+
}
8890
}
8991
}
9092
}

app/src/main/java/to/bitkit/viewmodels/ActivityDetailViewModel.kt

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import kotlinx.coroutines.withContext
1414
import to.bitkit.data.SettingsStore
1515
import to.bitkit.di.BgDispatcher
1616
import to.bitkit.ext.rawId
17+
import to.bitkit.repositories.ActivityRepo
1718
import to.bitkit.repositories.BlocktankRepo
18-
import to.bitkit.services.CoreService
1919
import to.bitkit.utils.AddressChecker
2020
import to.bitkit.utils.Logger
2121
import to.bitkit.utils.TxDetails
@@ -25,7 +25,7 @@ import javax.inject.Inject
2525
class ActivityDetailViewModel @Inject constructor(
2626
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
2727
private val addressChecker: AddressChecker,
28-
private val coreService: CoreService,
28+
private val activityRepo: ActivityRepo,
2929
private val settingsStore: SettingsStore,
3030
private val blocktankRepo: BlocktankRepo,
3131
) : ViewModel() {
@@ -48,40 +48,41 @@ class ActivityDetailViewModel @Inject constructor(
4848
fun loadTags() {
4949
val id = activity?.rawId() ?: return
5050
viewModelScope.launch(bgDispatcher) {
51-
try {
52-
val activityTags = coreService.activity.tags(forActivityId = id)
53-
_tags.value = activityTags
54-
} catch (e: Exception) {
55-
Logger.error("Failed to load tags for activity $id", e, TAG)
56-
_tags.value = emptyList()
57-
}
51+
activityRepo.getActivityTags(id)
52+
.onSuccess { activityTags ->
53+
_tags.value = activityTags
54+
}
55+
.onFailure { e ->
56+
Logger.error("Failed to load tags for activity $id", e, TAG)
57+
_tags.value = emptyList()
58+
}
5859
}
5960
}
6061

6162
fun removeTag(tag: String) {
6263
val id = activity?.rawId() ?: return
6364
viewModelScope.launch(bgDispatcher) {
64-
try {
65-
coreService.activity.dropTags(fromActivityId = id, tags = listOf(tag))
66-
loadTags()
67-
} catch (e: Exception) {
68-
Logger.error("Failed to remove tag $tag from activity $id", e, TAG)
69-
}
65+
activityRepo.removeTagsFromActivity(id, listOf(tag))
66+
.onSuccess {
67+
loadTags()
68+
}
69+
.onFailure { e ->
70+
Logger.error("Failed to remove tag $tag from activity $id", e, TAG)
71+
}
7072
}
7173
}
7274

7375
fun addTag(tag: String) {
7476
val id = activity?.rawId() ?: return
7577
viewModelScope.launch(bgDispatcher) {
76-
try {
77-
val result = coreService.activity.appendTags(toActivityId = id, tags = listOf(tag))
78-
if (result.isSuccess) {
78+
activityRepo.addTagsToActivity(id, listOf(tag))
79+
.onSuccess {
7980
settingsStore.addLastUsedTag(tag)
8081
loadTags()
8182
}
82-
} catch (e: Exception) {
83-
Logger.error("Failed to add tag $tag to activity $id", e, TAG)
84-
}
83+
.onFailure { e ->
84+
Logger.error("Failed to add tag $tag to activity $id", e, TAG)
85+
}
8586
}
8687
}
8788

app/src/test/java/to/bitkit/repositories/ActivityDetailViewModelTest.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import org.mockito.kotlin.doReturn
88
import org.mockito.kotlin.mock
99
import org.mockito.kotlin.whenever
1010
import to.bitkit.data.SettingsStore
11-
import to.bitkit.services.CoreService
1211
import to.bitkit.test.BaseUnitTest
1312
import to.bitkit.utils.AddressChecker
1413
import to.bitkit.viewmodels.ActivityDetailViewModel
@@ -17,7 +16,7 @@ import kotlin.test.assertNull
1716

1817
class ActivityDetailViewModelTest : BaseUnitTest() {
1918

20-
private val coreService = mock<CoreService>()
19+
private val activityRepo = mock<ActivityRepo>()
2120
private val blocktankRepo = mock<BlocktankRepo>()
2221
private val settingsStore = mock<SettingsStore>()
2322
private val addressChecker = mock<AddressChecker>()
@@ -30,7 +29,7 @@ class ActivityDetailViewModelTest : BaseUnitTest() {
3029

3130
sut = ActivityDetailViewModel(
3231
bgDispatcher = testDispatcher,
33-
coreService = coreService,
32+
activityRepo = activityRepo,
3433
blocktankRepo = blocktankRepo,
3534
settingsStore = settingsStore,
3635
addressChecker = addressChecker,

0 commit comments

Comments
 (0)