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 @@ -10,7 +10,7 @@ import timber.log.Timber

class RefreshPerpsPositionsJob(
private val walletId: String? = null
) : BaseJob(Params(PRIORITY_BACKGROUND).singleInstanceBy(GROUP).requireNetwork().persist()) {
) : BaseJob(Params(PRIORITY_BACKGROUND).singleInstanceBy(GROUP).requireNetwork()) {
companion object {
private const val serialVersionUID = 1L
const val GROUP = "RefreshPerpsPositionsJob"
Expand Down
29 changes: 19 additions & 10 deletions app/src/main/java/one/mixin/android/ui/wallet/WalletFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -464,31 +464,40 @@ class WalletFragment : BaseFragment(R.layout.fragment_wallet) {
}

fun update() {
val destination = selectedWalletDestination
when (destination) {
when (val destination = selectedWalletDestination) {
is WalletDestination.Privacy -> {
privacyWalletFragment.update()
}

is WalletDestination.Classic -> {
destination.walletId
jobManager.addJobInBackground(RefreshSingleWalletJob(destination.walletId))
}

is WalletDestination.Import -> {
destination.walletId
jobManager.addJobInBackground(RefreshSingleWalletJob(destination.walletId))
}

is WalletDestination.Watch -> {
destination.walletId
jobManager.addJobInBackground(RefreshSingleWalletJob(destination.walletId))
}

else -> {
null
}
}?.let { wallet ->
jobManager.addJobInBackground(RefreshSingleWalletJob(wallet))
else -> Unit
}
}

override fun onHiddenChanged(hidden: Boolean) {
super.onHiddenChanged(hidden)
if (hidden) {
privacyWalletFragment.stopUpdate()
} else {
update()
}
}

override fun onResume() {
super.onResume()
jobManager.addJobInBackground(RefreshSafeAccountsJob())
if (privacyWalletFragment.isVisible) privacyWalletFragment.update()
if (classicWalletFragment.isVisible) classicWalletFragment.update()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ import com.uber.autodispose.autoDispose
import dagger.hilt.android.AndroidEntryPoint
import io.reactivex.android.schedulers.AndroidSchedulers
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.isActive
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import one.mixin.android.Constants
Expand All @@ -48,6 +51,7 @@ import one.mixin.android.extension.openUrl
import one.mixin.android.extension.toast
import one.mixin.android.extension.putBoolean
import one.mixin.android.job.MixinJobManager
import one.mixin.android.job.RefreshPerpsPositionsJob
import one.mixin.android.job.RefreshSnapshotsJob
import one.mixin.android.job.RefreshTokensJob
import one.mixin.android.job.SyncOutputJob
Expand Down Expand Up @@ -113,6 +117,7 @@ import kotlin.math.abs
class WalletHomePrivacyFragment : BaseFragment(R.layout.fragment_privacy_wallet), HeaderAdapter.OnItemListener {
companion object {
const val TAG = "WalletHomePrivacyFragment"
private const val PERPS_POSITION_REFRESH_INTERVAL_MS = 3_000L

fun newInstance(): WalletHomePrivacyFragment = WalletHomePrivacyFragment()
}
Expand All @@ -131,6 +136,7 @@ class WalletHomePrivacyFragment : BaseFragment(R.layout.fragment_privacy_wallet)
private var positions: List<PerpsPositionItem> = emptyList()
private var topMovers: List<PerpsMarket> = emptyList()
private var pendingDisplays: List<PendingDisplay> = emptyList()
private var perpsPositionsRefreshJob: Job? = null
private val assetsAdapter by lazy { WalletAssetAdapter(false) }
private val perpetualViewModel by viewModels<PerpetualViewModel>()
private var walletHomeDataState = WalletHomeDataState.EMPTY
Expand Down Expand Up @@ -669,6 +675,7 @@ class WalletHomePrivacyFragment : BaseFragment(R.layout.fragment_privacy_wallet)
override fun onResume() {
super.onResume()
_walletId.value = Session.getAccountId().orEmpty()
startPerpsPositionsRefresh()
jobManager.addJobInBackground(RefreshTokensJob())
jobManager.addJobInBackground(RefreshSnapshotsJob())
jobManager.addJobInBackground(SyncOutputJob())
Expand All @@ -680,15 +687,48 @@ class WalletHomePrivacyFragment : BaseFragment(R.layout.fragment_privacy_wallet)
}

override fun onHiddenChanged(hidden: Boolean) {
super.onHiddenChanged(hidden)
if (!hidden) {
_walletId.value = Session.getAccountId().orEmpty()
startPerpsPositionsRefresh()
jobManager.addJobInBackground(RefreshTokensJob())
jobManager.addJobInBackground(RefreshSnapshotsJob())
jobManager.addJobInBackground(SyncOutputJob())
refreshAllPendingDeposit()
} else {
stopPerpsPositionsRefresh()
}
}

fun update() {
_walletId.value = Session.getAccountId().orEmpty()
startPerpsPositionsRefresh()
}

fun stopUpdate() {
stopPerpsPositionsRefresh()
}

private fun startPerpsPositionsRefresh() {
val walletId = Session.getAccountId().orEmpty()
if (walletId.isBlank() || !isResumed || isHidden) {
stopPerpsPositionsRefresh()
return
}
if (perpsPositionsRefreshJob?.isActive == true) return
perpsPositionsRefreshJob = lifecycleScope.launch {
while (isActive) {
jobManager.addJobInBackground(RefreshPerpsPositionsJob(walletId))
delay(PERPS_POSITION_REFRESH_INTERVAL_MS)
}
}
}
Comment on lines +712 to +725

private fun stopPerpsPositionsRefresh() {
perpsPositionsRefreshJob?.cancel()
perpsPositionsRefreshJob = null
}

private fun refreshAllPendingDeposit() =
lifecycleScope.launch {
handleMixinResponse(
Expand Down Expand Up @@ -730,7 +770,13 @@ class WalletHomePrivacyFragment : BaseFragment(R.layout.fragment_privacy_wallet)
snackBar?.dismiss()
}

override fun onPause() {
stopPerpsPositionsRefresh()
super.onPause()
}

override fun onDestroyView() {
stopPerpsPositionsRefresh()
assetsAdapter.headerView = null
assetsAdapter.onItemListener = null
_binding = null
Expand Down