Skip to content

Commit a08c16e

Browse files
committed
feat: fetch transaction delta where applicable in app
Signed-off-by: Brandon McAnsh <git@bmcreations.dev>
1 parent 3fd5fec commit a08c16e

4 files changed

Lines changed: 29 additions & 22 deletions

File tree

api/src/main/java/com/getcode/model/Chat.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ sealed interface Verb {
110110

111111
data class ChatMessage(
112112
val id: ID,
113-
val cursor: Cursor?,
113+
val cursor: Cursor,
114114
val dateMillis: Long,
115115
val contents: List<MessageContent>
116116
)

api/src/main/java/com/getcode/network/HistoryController.kt

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import androidx.paging.PagingConfig
66
import androidx.paging.PagingData
77
import androidx.paging.PagingSource
88
import androidx.paging.cachedIn
9+
import androidx.paging.map
910
import com.getcode.ed25519.Ed25519.KeyPair
1011
import com.getcode.manager.SessionManager
1112
import com.getcode.model.Chat
@@ -31,6 +32,7 @@ import kotlinx.coroutines.flow.asStateFlow
3132
import kotlinx.coroutines.flow.catch
3233
import kotlinx.coroutines.flow.collect
3334
import kotlinx.coroutines.flow.filterNotNull
35+
import kotlinx.coroutines.flow.lastOrNull
3436
import kotlinx.coroutines.flow.map
3537
import kotlinx.coroutines.flow.update
3638
import kotlinx.coroutines.reactive.asFlow
@@ -62,17 +64,18 @@ class HistoryController @Inject constructor(
6264
private val pagerMap = mutableMapOf<ID, PagingSource<Cursor, ChatMessage>>()
6365
private val chatFlows = mutableMapOf<ID, Flow<PagingData<ChatMessage>>>()
6466

65-
private fun chatMessagePager(chatId: ID) = Pager(
66-
PagingConfig(pageSize = 20)
67-
) {
67+
private val pagingConfig = PagingConfig(pageSize = 20)
68+
69+
private fun chatMessagePager(chatId: ID) = Pager(pagingConfig) {
6870
pagerMap[chatId] ?: ChatMessagePagingSource(client, owner()!!, chatId).also {
6971
pagerMap[chatId] = it
7072
}
7173
}
7274

73-
fun chatFlow(chatId: ID) = chatFlows[chatId] ?: chatMessagePager(chatId).flow.cachedIn(GlobalScope).also {
74-
chatFlows[chatId] = it
75-
}
75+
fun chatFlow(chatId: ID) =
76+
chatFlows[chatId] ?: chatMessagePager(chatId).flow.cachedIn(GlobalScope).also {
77+
chatFlows[chatId] = it
78+
}
7679

7780
val unreadCount = chats
7881
.filterNotNull()
@@ -95,6 +98,12 @@ class HistoryController @Inject constructor(
9598
owner = owner,
9699
afterId = latestTransaction.id.toByteArray()
97100
)
101+
102+
fetchChats()
103+
104+
pagerMap.entries.onEach { (id, pagingSource) ->
105+
pagingSource.invalidate()
106+
}
98107
}
99108

100109
@SuppressLint("CheckResult")

app/src/main/java/com/getcode/view/main/getKin/GetKinSheetViewModel.kt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import com.getcode.manager.TopBarManager
77
import com.getcode.model.KinAmount
88
import com.getcode.model.PrefsBool
99
import com.getcode.network.BalanceController
10+
import com.getcode.network.HistoryController
1011
import com.getcode.network.client.Client
1112
import com.getcode.network.client.fetchPaymentHistoryDelta
1213
import com.getcode.network.client.requestFirstKinAirdrop
@@ -29,6 +30,7 @@ import kotlinx.coroutines.flow.launchIn
2930
import kotlinx.coroutines.flow.map
3031
import kotlinx.coroutines.flow.mapNotNull
3132
import kotlinx.coroutines.flow.onEach
33+
import kotlinx.coroutines.launch
3234
import kotlinx.coroutines.reactive.asFlow
3335
import java.util.concurrent.TimeUnit
3436
import javax.inject.Inject
@@ -41,6 +43,7 @@ class GetKinSheetViewModel @Inject constructor(
4143
private val client: Client,
4244
private val networkObserver: NetworkConnectivityListener,
4345
private val resources: ResourceHelper,
46+
private val historyController: HistoryController,
4447
) : BaseViewModel2<GetKinSheetViewModel.State, GetKinSheetViewModel.Event>(
4548
initialState = State(),
4649
updateStateForEvent = updateStateForEvent
@@ -121,12 +124,8 @@ class GetKinSheetViewModel @Inject constructor(
121124
}
122125
)
123126
.flatMapLatest {
124-
Completable.concatArray(
125-
balanceController.fetchBalance(),
126-
client.fetchPaymentHistoryDelta(owner = SessionManager.getKeyPair()!!)
127-
.ignoreElement()
128-
).toFlowable<Any>().asFlow()
129-
}
127+
Completable.concatArray(balanceController.fetchBalance()).toFlowable<Any>().asFlow()
128+
}.onEach { historyController.fetchDelta() }
130129
.launchIn(viewModelScope)
131130

132131
eventFlow

app/src/main/java/com/getcode/view/main/home/HomeViewModel.kt

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import com.getcode.network.client.RemoteSendException
4545
import com.getcode.network.client.awaitEstablishRelationship
4646
import com.getcode.network.client.cancelRemoteSend
4747
import com.getcode.network.client.fetchLimits
48-
import com.getcode.network.client.fetchPaymentHistoryDelta
4948
import com.getcode.network.client.receiveRemoteSuspend
5049
import com.getcode.network.client.sendRemotely
5150
import com.getcode.network.client.sendRequestToReceiveBill
@@ -304,12 +303,12 @@ class HomeViewModel @Inject constructor(
304303
Completable.concatArray(
305304
balanceController.fetchBalance(),
306305
client.fetchLimits(isForce = true),
307-
client.fetchPaymentHistoryDelta(owner).ignoreElement()
308306
)
309307
}
310308
.subscribe({
311309
cancelSend(PresentationStyle.Pop)
312310
vibrator.vibrate()
311+
viewModelScope.launch { historyController.fetchDelta() }
313312
}, {
314313
ErrorUtils.handleError(it)
315314
cancelSend(style = PresentationStyle.Slide)
@@ -535,12 +534,13 @@ class HomeViewModel @Inject constructor(
535534
}
536535
}
537536

538-
private fun attemptPayment(payload: CodePayload, request: DeepLinkPaymentRequest? = null) = viewModelScope.launch {
539-
val (amount, p) = paymentRepository.attemptRequest(payload) ?: return@launch
540-
BottomBarManager.clear()
537+
private fun attemptPayment(payload: CodePayload, request: DeepLinkPaymentRequest? = null) =
538+
viewModelScope.launch {
539+
val (amount, p) = paymentRepository.attemptRequest(payload) ?: return@launch
540+
BottomBarManager.clear()
541541

542-
presentRequest(amount = amount, payload = p, request = request)
543-
}
542+
presentRequest(amount = amount, payload = p, request = request)
543+
}
544544

545545
fun presentRequest(
546546
amount: KinAmount,
@@ -835,10 +835,9 @@ class HomeViewModel @Inject constructor(
835835
Completable.concatArray(
836836
balanceController.fetchBalance(),
837837
client.fetchLimits(isForce = true),
838-
client.fetchPaymentHistoryDelta(organizer.ownerKeyPair).ignoreElement()
839838
)
840839
}
841-
.subscribe({ }, {
840+
.subscribe({ viewModelScope.launch { historyController.fetchDelta() } }, {
842841
scannedRendezvous.remove(payload.rendezvous.publicKey)
843842
ErrorUtils.handleError(it)
844843
})

0 commit comments

Comments
 (0)