Skip to content

Commit 2d7d9ef

Browse files
committed
fix: preload contact payments
1 parent 266f923 commit 2d7d9ef

2 files changed

Lines changed: 25 additions & 24 deletions

File tree

app/src/main/java/to/bitkit/ui/screens/contacts/AddContactViewModel.kt

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,14 @@ class AddContactViewModel @Inject constructor(
6767
}
6868
pubkyRepo.fetchContactProfile(publicKey)
6969
.onSuccess { profile ->
70-
_uiState.update { it.copy(fetchedProfile = profile, isLoading = false) }
71-
loadPaymentEndpoint(profile.publicKey)
70+
val hasEndpoint = loadPaymentEndpoint(profile.publicKey)
71+
_uiState.update {
72+
it.copy(
73+
fetchedProfile = profile,
74+
hasPublicPaymentEndpoint = hasEndpoint,
75+
isLoading = false,
76+
)
77+
}
7278
}
7379
.onFailure { error ->
7480
_uiState.update { state ->
@@ -90,16 +96,12 @@ class AddContactViewModel @Inject constructor(
9096
}
9197
}
9298

93-
private fun loadPaymentEndpoint(publicKey: String) {
94-
viewModelScope.launch {
95-
publicPaykitRepo.hasPayablePublicEndpoint(publicKey)
96-
.onSuccess { hasEndpoint ->
97-
_uiState.update { it.copy(hasPublicPaymentEndpoint = hasEndpoint) }
98-
}
99-
.onFailure {
100-
Logger.warn("Failed to load public Paykit endpoint for '$publicKey'", it, context = TAG)
101-
}
102-
}
99+
private suspend fun loadPaymentEndpoint(publicKey: String): Boolean {
100+
return publicPaykitRepo.hasPayablePublicEndpoint(publicKey)
101+
.onFailure {
102+
Logger.warn("Failed to load public Paykit endpoint for '$publicKey'", it, context = TAG)
103+
}
104+
.getOrDefault(false)
103105
}
104106

105107
fun payContact() {

app/src/main/java/to/bitkit/ui/screens/contacts/ContactDetailViewModel.kt

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,30 +53,33 @@ class ContactDetailViewModel @Inject constructor(
5353

5454
init {
5555
loadContact()
56-
loadPaymentEndpoint()
5756
observeContactUpdates()
5857
}
5958

6059
fun loadContact() {
6160
viewModelScope.launch {
61+
_uiState.update { it.copy(isLoading = true) }
6262
val cached = pubkyRepo.contacts.value.find { it.publicKey == publicKey }
6363
if (cached != null) {
64+
val hasEndpoint = loadPaymentEndpoint()
6465
_uiState.update {
6566
it.copy(
6667
profile = cached,
6768
tags = cached.tags.toImmutableList(),
69+
hasPublicPaymentEndpoint = hasEndpoint,
6870
isLoading = false,
6971
)
7072
}
7173
return@launch
7274
}
73-
_uiState.update { it.copy(isLoading = true) }
7475
pubkyRepo.fetchContactProfile(publicKey)
7576
.onSuccess { profile ->
77+
val hasEndpoint = loadPaymentEndpoint()
7678
_uiState.update {
7779
it.copy(
7880
profile = profile,
7981
tags = profile.tags.toImmutableList(),
82+
hasPublicPaymentEndpoint = hasEndpoint,
8083
isLoading = false,
8184
)
8285
}
@@ -92,16 +95,12 @@ class ContactDetailViewModel @Inject constructor(
9295
}
9396
}
9497

95-
private fun loadPaymentEndpoint() {
96-
viewModelScope.launch {
97-
publicPaykitRepo.hasPayablePublicEndpoint(publicKey)
98-
.onSuccess { hasEndpoint ->
99-
_uiState.update { it.copy(hasPublicPaymentEndpoint = hasEndpoint) }
100-
}
101-
.onFailure {
102-
Logger.warn("Failed to load public Paykit endpoint for '$publicKey'", it, context = TAG)
103-
}
104-
}
98+
private suspend fun loadPaymentEndpoint(): Boolean {
99+
return publicPaykitRepo.hasPayablePublicEndpoint(publicKey)
100+
.onFailure {
101+
Logger.warn("Failed to load public Paykit endpoint for '$publicKey'", it, context = TAG)
102+
}
103+
.getOrDefault(false)
105104
}
106105

107106
fun payContact() {

0 commit comments

Comments
 (0)