2018-12-30 17:37:43 -08:00
|
|
|
package cash.z.android.wallet.ui.presenter
|
|
|
|
|
2019-01-31 12:44:25 -08:00
|
|
|
import android.util.Log
|
2019-02-21 20:38:06 -08:00
|
|
|
import cash.z.android.wallet.ZcashWalletApplication
|
|
|
|
import cash.z.android.wallet.ui.fragment.HomeFragment
|
2019-01-29 10:08:33 -08:00
|
|
|
import cash.z.android.wallet.ui.presenter.Presenter.PresenterView
|
2019-02-07 19:18:32 -08:00
|
|
|
import cash.z.wallet.sdk.dao.WalletTransaction
|
|
|
|
import cash.z.wallet.sdk.data.ActiveSendTransaction
|
2019-02-03 17:27:54 -08:00
|
|
|
import cash.z.wallet.sdk.data.ActiveTransaction
|
2019-01-02 21:32:27 -08:00
|
|
|
import cash.z.wallet.sdk.data.Synchronizer
|
2019-02-03 17:27:54 -08:00
|
|
|
import cash.z.wallet.sdk.data.TransactionState
|
2019-02-03 23:08:26 -08:00
|
|
|
import kotlinx.coroutines.*
|
|
|
|
import kotlinx.coroutines.Dispatchers.Main
|
2018-12-30 17:37:43 -08:00
|
|
|
import kotlinx.coroutines.channels.ReceiveChannel
|
2019-02-21 20:38:06 -08:00
|
|
|
import javax.inject.Inject
|
2019-02-03 23:08:26 -08:00
|
|
|
import kotlin.coroutines.CoroutineContext
|
2018-12-30 17:37:43 -08:00
|
|
|
|
2019-02-21 20:38:06 -08:00
|
|
|
class HomePresenter @Inject constructor(
|
|
|
|
private val view: HomeFragment,
|
2019-01-02 21:32:27 -08:00
|
|
|
private val synchronizer: Synchronizer
|
2019-02-03 23:08:26 -08:00
|
|
|
) : Presenter, CoroutineScope {
|
|
|
|
|
|
|
|
private val job = Job()
|
|
|
|
override val coroutineContext: CoroutineContext get() = Dispatchers.IO + job
|
2019-01-29 10:08:33 -08:00
|
|
|
|
|
|
|
interface HomeView : PresenterView {
|
2019-01-31 12:44:25 -08:00
|
|
|
fun setTransactions(transactions: List<WalletTransaction>)
|
2019-01-29 10:08:33 -08:00
|
|
|
fun updateBalance(old: Long, new: Long)
|
2019-02-03 17:27:54 -08:00
|
|
|
fun setActiveTransactions(activeTransactionMap: Map<ActiveTransaction, TransactionState>)
|
2019-02-07 19:18:32 -08:00
|
|
|
fun onCancelledTooLate()
|
2019-01-29 10:08:33 -08:00
|
|
|
}
|
2018-12-30 17:37:43 -08:00
|
|
|
|
|
|
|
override suspend fun start() {
|
2019-01-31 12:44:25 -08:00
|
|
|
Log.e("@TWIG-t", "homePresenter starting!")
|
2019-02-16 00:47:39 -08:00
|
|
|
launchBalanceBinder(synchronizer.balance())
|
|
|
|
launchTransactionBinder(synchronizer.allTransactions())
|
2019-02-03 23:08:26 -08:00
|
|
|
launchActiveTransactionMonitor(synchronizer.activeTransactions())
|
2018-12-30 17:37:43 -08:00
|
|
|
}
|
|
|
|
|
2019-01-29 10:08:33 -08:00
|
|
|
override fun stop() {
|
2019-01-31 12:44:25 -08:00
|
|
|
Log.e("@TWIG-t", "homePresenter stopping!")
|
2019-02-03 23:08:26 -08:00
|
|
|
job.cancel()
|
2018-12-30 17:37:43 -08:00
|
|
|
}
|
|
|
|
|
2019-02-03 23:08:26 -08:00
|
|
|
private fun CoroutineScope.launchBalanceBinder(channel: ReceiveChannel<Long>) = launch {
|
2019-01-29 10:08:33 -08:00
|
|
|
var old: Long? = null
|
2019-01-31 12:44:25 -08:00
|
|
|
Log.e("@TWIG-t", "balance binder starting!")
|
|
|
|
for (new in channel) {
|
2019-02-03 23:08:26 -08:00
|
|
|
Log.e("@TWIG-t", "polled a balance item")
|
2019-01-31 12:44:25 -08:00
|
|
|
bind(old, new).also { old = new }
|
2018-12-30 17:37:43 -08:00
|
|
|
}
|
2019-01-31 12:44:25 -08:00
|
|
|
Log.e("@TWIG", "balance binder exiting!")
|
2018-12-30 17:37:43 -08:00
|
|
|
}
|
|
|
|
|
2019-02-07 19:18:32 -08:00
|
|
|
private fun CoroutineScope.launchTransactionBinder(channel: ReceiveChannel<List<WalletTransaction>>) = launch {
|
2019-01-31 12:44:25 -08:00
|
|
|
Log.e("@TWIG", "transaction binder starting!")
|
2019-02-07 19:18:32 -08:00
|
|
|
for (walletTransactionList in channel) {
|
|
|
|
Log.e("@TWIG", "received ${walletTransactionList.size} transactions for presenting")
|
|
|
|
bind(walletTransactionList)
|
2018-12-30 18:41:47 -08:00
|
|
|
}
|
2019-01-31 12:44:25 -08:00
|
|
|
Log.e("@TWIG", "transaction binder exiting!")
|
2018-12-30 18:41:47 -08:00
|
|
|
}
|
|
|
|
|
2019-02-03 23:08:26 -08:00
|
|
|
private fun CoroutineScope.launchActiveTransactionMonitor(channel: ReceiveChannel<Map<ActiveTransaction, TransactionState>>) = launch {
|
2019-02-03 17:27:54 -08:00
|
|
|
Log.e("@TWIG-v", "active transaction monitor starting!")
|
|
|
|
for (i in channel) {
|
|
|
|
bind(i)
|
|
|
|
}
|
|
|
|
Log.e("@TWIG-v", "active transaction monitor exiting!")
|
|
|
|
}
|
|
|
|
|
2019-02-03 23:08:26 -08:00
|
|
|
|
|
|
|
//
|
|
|
|
// View Callbacks on Main Thread
|
|
|
|
//
|
|
|
|
|
|
|
|
private fun bind(old: Long?, new: Long) = onMain {
|
2019-02-12 16:09:06 -08:00
|
|
|
Log.e("@TWIG-b", "binding balance of $new")
|
2019-01-31 12:44:25 -08:00
|
|
|
view.updateBalance(old ?: 0L, new)
|
2018-12-30 17:37:43 -08:00
|
|
|
}
|
2018-12-30 18:41:47 -08:00
|
|
|
|
2019-02-03 23:08:26 -08:00
|
|
|
|
|
|
|
private fun bind(transactions: List<WalletTransaction>) = onMain {
|
2019-02-12 16:09:06 -08:00
|
|
|
Log.e("@TWIG-b", "binding ${transactions.size} walletTransactions")
|
2019-02-19 10:09:53 -08:00
|
|
|
view.setTransactions(transactions.sortedByDescending {
|
|
|
|
it.timeInSeconds
|
|
|
|
})
|
2019-01-29 10:08:33 -08:00
|
|
|
}
|
|
|
|
|
2019-02-03 23:08:26 -08:00
|
|
|
private fun bind(activeTransactionMap: Map<ActiveTransaction, TransactionState>) = onMain {
|
2019-02-12 16:09:06 -08:00
|
|
|
Log.e("@TWIG-b", "binding a.t. map of size ${activeTransactionMap.size}")
|
2019-02-03 17:27:54 -08:00
|
|
|
if (activeTransactionMap.isNotEmpty()) view.setActiveTransactions(activeTransactionMap)
|
|
|
|
}
|
|
|
|
|
2019-02-07 19:18:32 -08:00
|
|
|
fun onCancelActiveTransaction(transaction: ActiveSendTransaction) {
|
|
|
|
Log.e("@TWIG", "requesting to cancel send for transaction ${transaction.internalId}")
|
|
|
|
val isTooLate = !synchronizer.cancelSend(transaction)
|
|
|
|
if (isTooLate) {
|
|
|
|
view.onCancelledTooLate()
|
|
|
|
}
|
2019-02-02 07:07:25 -08:00
|
|
|
}
|
|
|
|
|
2019-02-03 23:08:26 -08:00
|
|
|
private fun onMain(block: () -> Unit) = launch {
|
|
|
|
withContext(Main) {
|
|
|
|
Log.e("@TWIG-t", "running task on main thread - start ${coroutineContext[Job]} | ${coroutineContext[CoroutineName]}")
|
|
|
|
block()
|
|
|
|
Log.e("@TWIG-t", "running task on main thread - complete")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-12-30 17:37:43 -08:00
|
|
|
}
|
2019-01-31 12:44:25 -08:00
|
|
|
|