Skip to content

Commit

Permalink
- Fix for #141 (#168)
Browse files Browse the repository at this point in the history
- also fix back navigation back breaking UI from trading close trade buttons
  • Loading branch information
rodvar authored Jan 20, 2025
1 parent 84a46f5 commit 170755e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ abstract class BasePresenter(private val rootPresenter: MainPresenter?): ViewPre
rootPresenter?.registerChild(child = this)
}

protected fun enableInteractive(enable: Boolean) {
protected fun enableInteractive(enable: Boolean = true) {
uiScope.launch {
if (enable) {
delay(250L)
Expand Down Expand Up @@ -180,13 +180,16 @@ abstract class BasePresenter(private val rootPresenter: MainPresenter?): ViewPre
}

override fun goBack(): Boolean {
enableInteractive(false)
var wentBack = false
uiScope.launch(Dispatchers.Main) {
try {
log.i { "goBack default implementation" }
wentBack = rootNavigator.popBackStack()
} catch (e: Exception) {
log.e(e) { "Failed to navigate back" }
} finally {
enableInteractive()
}
}
return wentBack;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package network.bisq.mobile.presentation.ui.uicases.offerbook

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.*
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
Expand All @@ -20,12 +19,23 @@ class OfferbookMarketPresenter(
private val offersServiceFacade: OffersServiceFacade,
) : BasePresenter(mainPresenter) {

companion object {
const val FILTER_REFRESH_FREQUENCY = 3000L
}

//todo
//var marketListItemWithNumOffers: List<MarketListItem> = offerbookServiceFacade.getSortedOfferbookMarketItems()

private val jobScope = CoroutineScope(SupervisorJob())
private var updateJob: Job? = null

private var mainCurrencies = OffersServiceFacade.mainCurrencies

private var _sortBy = MutableStateFlow(MarketSortBy.MostOffers)
// flag to force market update trigger when needed
private val _forceTrigger = MutableStateFlow(false)
var forceTrigger: StateFlow<Boolean> = _forceTrigger

private val _sortBy = MutableStateFlow(MarketSortBy.MostOffers)
var sortBy: StateFlow<MarketSortBy> = _sortBy
fun setSortBy(newValue: MarketSortBy) {
_sortBy.value = newValue
Expand All @@ -46,8 +56,9 @@ class OfferbookMarketPresenter(
val marketListItemWithNumOffers: StateFlow<List<MarketListItem>> = combine(
_filter,
_searchText,
_sortBy
) { filter: MarketFilter, searchText: String, sortBy: MarketSortBy ->
_sortBy,
_forceTrigger
) { filter: MarketFilter, searchText: String, sortBy: MarketSortBy, forceTrigger: Boolean ->
computeMarketList(filter, searchText, sortBy)
}.stateIn(
CoroutineScope(Dispatchers.Main),
Expand Down Expand Up @@ -99,8 +110,31 @@ class OfferbookMarketPresenter(
}

override fun onViewAttached() {
startUpdatingMarketPrices()
}

override fun onViewUnattaching() {
stopUpdatingMarketPrices()
}

private fun startUpdatingMarketPrices() {
// Launch a coroutine that updates the market prices every 3 seconds
updateJob = jobScope.launch {
while (updateJob != null) { // Ensure the loop stops when the coroutine is cancelled
updateMarketPrices()
delay(FILTER_REFRESH_FREQUENCY) // Wait for 3 seconds before the next update
}
}
}

private fun stopUpdatingMarketPrices() {
// Cancel the job to stop the background updates
updateJob?.cancel()
updateJob = null
}

private fun updateMarketPrices() {
// fake trigger a refresh
_forceTrigger.value = !_forceTrigger.value
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ class TradeDetailsHeaderPresenter(

private fun closeWorkflow() {
// Do not navigate, close button on the same screen does it
// navigateBack()
navigateBack()
}

private fun reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,6 @@ class TradeFlowPresenter(
tradeStateChanged(tradeState)
}
}
presenterScope.launch {
// When a trade gets closed we set selectedTrade to null
//todo maybe we should not rely on selectedTrade but us a more dedicated method
tradesServiceFacade.selectedTrade.collect() { selectedTrade ->
if (selectedTrade == null) {
navigateBack()
}
}
}
}

override fun onViewUnattaching() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ class BuyerState4Presenter(
})
}

fun closeWorkflow() {
// doing a shark navigateBack causes white broken UI screen
navigateToTab(Routes.TabOpenTradeList)
private fun closeWorkflow() {
navigateBack()
}

fun onExportTradeDate() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ class SellerState4Presenter(
}

fun closeWorkflow() {
// doing a shark navigateBack causes white broken UI screen
navigateToTab(Routes.TabOpenTradeList)
navigateBack()
}

fun onExportTradeDate() {
Expand Down

0 comments on commit 170755e

Please sign in to comment.