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,8 +14,8 @@ import org.lightningdevkit.ldknode.SpendableUtxo
import to.bitkit.di.BgDispatcher
import to.bitkit.env.Env
import to.bitkit.ext.rawId
import to.bitkit.repositories.ActivityRepo
import to.bitkit.repositories.LightningRepo
import to.bitkit.services.CoreService
import to.bitkit.ui.shared.toast.ToastEventBus
import to.bitkit.utils.Logger
import javax.inject.Inject
Expand All @@ -24,7 +24,7 @@ import javax.inject.Inject
class SendCoinSelectionViewModel @Inject constructor(
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
private val lightningRepo: LightningRepo,
private val coreService: CoreService,
private val activityRepo: ActivityRepo,
) : ViewModel() {

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

viewModelScope.launch(bgDispatcher) {
runCatching {
// find activity by txId
onchainActivities.firstOrNull { (it as? Onchain)?.v1?.txId == txId }?.let { activity ->
// get tags by activity id
coreService.activity.tags(forActivityId = activity.rawId())
.takeIf { it.isNotEmpty() }
?.let { tags ->
// find activity by txId
onchainActivities.firstOrNull { (it as? Onchain)?.v1?.txId == txId }?.let { activity ->
// get tags by activity id
activityRepo.getActivityTags(activity.rawId())
.onSuccess { tags ->
if (tags.isNotEmpty()) {
// add map entry linking tags to utxo.outpoint.txid
_tagsByTxId.update { currentMap -> currentMap + (txId to tags) }
}
}
}
.onFailure { e ->
Logger.error("Failed to load tags for utxo $txId", e)
}
}
}
}
Expand Down
43 changes: 22 additions & 21 deletions app/src/main/java/to/bitkit/viewmodels/ActivityDetailViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import kotlinx.coroutines.withContext
import to.bitkit.data.SettingsStore
import to.bitkit.di.BgDispatcher
import to.bitkit.ext.rawId
import to.bitkit.repositories.ActivityRepo
import to.bitkit.repositories.BlocktankRepo
import to.bitkit.services.CoreService
import to.bitkit.utils.AddressChecker
import to.bitkit.utils.Logger
import to.bitkit.utils.TxDetails
Expand All @@ -25,7 +25,7 @@ import javax.inject.Inject
class ActivityDetailViewModel @Inject constructor(
@BgDispatcher private val bgDispatcher: CoroutineDispatcher,
private val addressChecker: AddressChecker,
private val coreService: CoreService,
private val activityRepo: ActivityRepo,
private val settingsStore: SettingsStore,
private val blocktankRepo: BlocktankRepo,
) : ViewModel() {
Expand All @@ -48,40 +48,41 @@ class ActivityDetailViewModel @Inject constructor(
fun loadTags() {
val id = activity?.rawId() ?: return
viewModelScope.launch(bgDispatcher) {
try {
val activityTags = coreService.activity.tags(forActivityId = id)
_tags.value = activityTags
} catch (e: Exception) {
Logger.error("Failed to load tags for activity $id", e, TAG)
_tags.value = emptyList()
}
activityRepo.getActivityTags(id)
.onSuccess { activityTags ->
_tags.value = activityTags
}
.onFailure { e ->
Logger.error("Failed to load tags for activity $id", e, TAG)
_tags.value = emptyList()
}
}
}

fun removeTag(tag: String) {
val id = activity?.rawId() ?: return
viewModelScope.launch(bgDispatcher) {
try {
coreService.activity.dropTags(fromActivityId = id, tags = listOf(tag))
loadTags()
} catch (e: Exception) {
Logger.error("Failed to remove tag $tag from activity $id", e, TAG)
}
activityRepo.removeTagsFromActivity(id, listOf(tag))
.onSuccess {
loadTags()
}
.onFailure { e ->
Logger.error("Failed to remove tag $tag from activity $id", e, TAG)
}
}
}

fun addTag(tag: String) {
val id = activity?.rawId() ?: return
viewModelScope.launch(bgDispatcher) {
try {
val result = coreService.activity.appendTags(toActivityId = id, tags = listOf(tag))
if (result.isSuccess) {
activityRepo.addTagsToActivity(id, listOf(tag))
.onSuccess {
settingsStore.addLastUsedTag(tag)
loadTags()
}
} catch (e: Exception) {
Logger.error("Failed to add tag $tag to activity $id", e, TAG)
}
.onFailure { e ->
Logger.error("Failed to add tag $tag to activity $id", e, TAG)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.mockito.kotlin.doReturn
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import to.bitkit.data.SettingsStore
import to.bitkit.services.CoreService
import to.bitkit.test.BaseUnitTest
import to.bitkit.utils.AddressChecker
import to.bitkit.viewmodels.ActivityDetailViewModel
Expand All @@ -17,7 +16,7 @@ import kotlin.test.assertNull

class ActivityDetailViewModelTest : BaseUnitTest() {

private val coreService = mock<CoreService>()
private val activityRepo = mock<ActivityRepo>()
private val blocktankRepo = mock<BlocktankRepo>()
private val settingsStore = mock<SettingsStore>()
private val addressChecker = mock<AddressChecker>()
Expand All @@ -30,7 +29,7 @@ class ActivityDetailViewModelTest : BaseUnitTest() {

sut = ActivityDetailViewModel(
bgDispatcher = testDispatcher,
coreService = coreService,
activityRepo = activityRepo,
blocktankRepo = blocktankRepo,
settingsStore = settingsStore,
addressChecker = addressChecker,
Expand Down
Loading