Commit 3b0ebae4 authored by Sarath's avatar Sarath
Browse files

Improved whirlpool swipe refresh loading

parent 1d6e1c9f
package com.samourai.wallet.whirlpool
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import androidx.lifecycle.*
import com.samourai.wallet.api.APIFactory
import com.samourai.wallet.home.BalanceActivity
import com.samourai.wallet.service.JobRefreshService
import com.samourai.whirlpool.client.wallet.AndroidWhirlpoolWalletService
import com.samourai.whirlpool.client.wallet.beans.MixableStatus
import com.samourai.whirlpool.client.wallet.beans.WhirlpoolAccount
......@@ -12,10 +16,7 @@ import com.samourai.whirlpool.client.wallet.beans.WhirlpoolUtxoStatus
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.schedulers.Schedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.*
import java.util.*
import com.samourai.whirlpool.client.wallet.AndroidWhirlpoolWalletService.ConnectionStates as Connection
......@@ -26,6 +27,7 @@ class WhirlPoolHomeViewModel : ViewModel() {
private val whirlpoolLoading = MutableLiveData(true)
private val refreshStatus = MutableLiveData(false)
private val compositeDisposable = CompositeDisposable()
private val wallet = AndroidWhirlpoolWalletService.getInstance();
......@@ -42,6 +44,7 @@ class WhirlPoolHomeViewModel : ViewModel() {
val mixingBalance: LiveData<Long> get() = mixingBalanceLive
val totalBalance: LiveData<Long> get() = totalBalanceLive
val onboardStatus: LiveData<Boolean> get() = whirlpoolOnboarded
val listRefreshStatus: LiveData<Boolean> get() = refreshStatus
init {
val disposable = wallet.listenConnectionStatus()
......@@ -158,4 +161,44 @@ class WhirlPoolHomeViewModel : ViewModel() {
loadUtxos()
loadBalances()
}
val broadCastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
intent?.let {
if (BalanceActivity.DISPLAY_INTENT == intent.action) {
setRefresh(false)
}
}
}
}
fun refreshList(context:Context) {
val wallet = AndroidWhirlpoolWalletService.getInstance().whirlpoolWalletOrNull;
if (wallet != null) {
setRefresh(true)
viewModelScope.launch(Dispatchers.IO) {
try {
withContext(Dispatchers.Main){
val intent = Intent(context, JobRefreshService::class.java)
intent.putExtra("notifTx", false)
intent.putExtra("dragged", true)
intent.putExtra("launch", false)
JobRefreshService.enqueueWork(context, intent)
}
wallet.refreshUtxos()
refresh()
}catch (e:Exception){
throw CancellationException()
}
}
}
}
fun setRefresh(b: Boolean) {
refreshStatus.postValue(b)
}
}
\ No newline at end of file
......@@ -89,7 +89,7 @@ class MixListAdapter : RecyclerView.Adapter<MixListAdapter.ViewHolder>() {
fun updateList(utxos: List<WhirlpoolUtxo>) {
scope.launch {
scope.launch (Dispatchers.Default){
try {
val sorted = utxos
.sortedBy { it.utxoState.status != WhirlpoolUtxoStatus.READY }
......
......@@ -11,6 +11,7 @@ import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import androidx.lifecycle.Observer
import androidx.lifecycle.viewModelScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
......@@ -69,11 +70,14 @@ class MixListFragment : Fragment() {
if (isRemixList) viewModel.remixLive else viewModel.mixingLive
mixList.observe(viewLifecycleOwner, { list ->
if(viewModel.listRefreshStatus.value != false) {
return@observe
}
val onBoardingActive = viewModel.onboardStatus.value ?: showOnboard
if (onBoardingActive) {
return@observe
}
if (list.isEmpty()) {
if (list.isEmpty() && !binding.mixSwipeContainer.isRefreshing) {
showAddMoreCoinView(!isRemixList)
} else {
if (binding.mixListContainer.visibility != View.VISIBLE && !showOnboard) {
......@@ -112,6 +116,7 @@ class MixListFragment : Fragment() {
if (it.utxoState != null && it.utxoState.status != null) {
if (it.utxoState.status == WhirlpoolUtxoStatus.MIX_STARTED) {
wallet.mixStop(it)
wallet.mixQueue(it)
}else{
wallet.mix(it)
}
......@@ -196,17 +201,11 @@ class MixListFragment : Fragment() {
}
private fun setUpList() {
viewModel.listRefreshStatus.observe(viewLifecycleOwner, {
binding.mixSwipeContainer.isRefreshing = it
})
binding.mixSwipeContainer.setOnRefreshListener {
val wallet = AndroidWhirlpoolWalletService.getInstance().whirlpoolWalletOrNull;
if (wallet != null) {
binding.mixSwipeContainer.isRefreshing = true;
viewModel.viewModelScope.launch(Dispatchers.IO) {
wallet.refreshUtxos()
withContext(Dispatchers.Main) {
binding.mixSwipeContainer.isRefreshing = false;
}
}
}
viewModel.refreshList(requireContext())
}
val drawable = ContextCompat.getDrawable(requireContext(), R.drawable.divider_grey)
binding.mixListRecyclerView.apply {
......
......@@ -64,7 +64,9 @@ class TransactionsFragment : Fragment() {
viewModel.setAccount(WhirlpoolAccount.POSTMIX.accountIndex);
val filter = IntentFilter(BalanceActivity.DISPLAY_INTENT)
LocalBroadcastManager.getInstance(requireContext())
.registerReceiver(broadCastReceiver, filter)
.registerReceiver(whirlPoolHomeViewModel.broadCastReceiver, filter)
LocalBroadcastManager.getInstance(requireContext())
.registerReceiver(broadCastReceiver, filter)
viewModel.loadOfflineData()
whirlPoolHomeViewModel.onboardStatus.observe(viewLifecycleOwner, { showOnBoarding ->
if (showOnBoarding) {
......@@ -147,6 +149,7 @@ class TransactionsFragment : Fragment() {
})
}
swipeRefreshLayout = SwipeRefreshLayout(container.context)
.apply {
layoutParams = FrameLayout.LayoutParams(
......@@ -156,13 +159,13 @@ class TransactionsFragment : Fragment() {
whirlPoolHomeViewModel.refresh()
this.addView(recyclerView)
setOnRefreshListener {
val intent = Intent(requireContext(), JobRefreshService::class.java)
intent.putExtra("notifTx", false)
intent.putExtra("dragged", true)
intent.putExtra("launch", false)
JobRefreshService.enqueueWork(requireContext(), intent)
whirlPoolHomeViewModel.refreshList(requireContext())
}
}
whirlPoolHomeViewModel.listRefreshStatus.observe(viewLifecycleOwner, {
swipeRefreshLayout?.isRefreshing = it
})
containerLayout.addView(swipeRefreshLayout)
return containerLayout
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment