Implemented currentOrNextActivity() so that caller gets an activity in either case

This commit is contained in:
dankito 2020-05-04 17:26:42 +02:00
parent 8fcdb0f593
commit e202587572
2 changed files with 27 additions and 19 deletions

View File

@ -21,51 +21,49 @@ import java.util.concurrent.atomic.AtomicReference
open class RouterAndroid(protected val activityTracker: CurrentActivityTracker) : IRouter { open class RouterAndroid(protected val activityTracker: CurrentActivityTracker) : IRouter {
override fun showAddAccountDialog(presenter: BankingPresenter) { override fun showAddAccountDialog(presenter: BankingPresenter) {
activityTracker.currentActivity?.let { activity -> activityTracker.currentOrNextActivity { activity ->
AddAccountDialog().show(activity) AddAccountDialog().show(activity)
} }
} }
override fun getTanFromUserFromNonUiThread(account: Account, tanChallenge: TanChallenge, presenter: BankingPresenter): EnterTanResult { override fun getTanFromUserFromNonUiThread(account: Account, tanChallenge: TanChallenge, presenter: BankingPresenter): EnterTanResult {
return activityTracker.currentActivity?.let { activity ->
val enteredTan = AtomicReference<EnterTanResult>(null) val enteredTan = AtomicReference<EnterTanResult>(null)
val tanEnteredLatch = CountDownLatch(1) val tanEnteredLatch = CountDownLatch(1)
activityTracker.currentOrNextActivity { activity ->
activity.runOnUiThread { activity.runOnUiThread {
EnterTanDialog().show(account, tanChallenge, activity, false) { EnterTanDialog().show(account, tanChallenge, activity, false) {
enteredTan.set(it) enteredTan.set(it)
tanEnteredLatch.countDown() tanEnteredLatch.countDown()
} }
} }
}
try { tanEnteredLatch.await() } catch (ignored: Exception) { } try { tanEnteredLatch.await() } catch (ignored: Exception) { }
enteredTan.get() return enteredTan.get()
}
?: EnterTanResult.userDidNotEnterTan()
} }
override fun getAtcFromUserFromNonUiThread(tanMedium: TanGeneratorTanMedium): EnterTanGeneratorAtcResult { override fun getAtcFromUserFromNonUiThread(tanMedium: TanGeneratorTanMedium): EnterTanGeneratorAtcResult {
return activityTracker.currentActivity?.let { activity ->
val result = AtomicReference<EnterTanGeneratorAtcResult>(null) val result = AtomicReference<EnterTanGeneratorAtcResult>(null)
val tanEnteredLatch = CountDownLatch(1) val tanEnteredLatch = CountDownLatch(1)
activityTracker.currentOrNextActivity { activity ->
activity.runOnUiThread { activity.runOnUiThread {
EnterAtcDialog().show(tanMedium, activity, false) { enteredResult -> EnterAtcDialog().show(tanMedium, activity, false) { enteredResult ->
result.set(enteredResult) result.set(enteredResult)
tanEnteredLatch.countDown() tanEnteredLatch.countDown()
} }
} }
}
try { tanEnteredLatch.await() } catch (ignored: Exception) { } try { tanEnteredLatch.await() } catch (ignored: Exception) { }
result.get() return result.get()
}
?: EnterTanGeneratorAtcResult.userDidNotEnterTan()
} }
override fun showTransferMoneyDialog(presenter: BankingPresenter, preselectedBankAccount: BankAccount?, preselectedValues: TransferMoneyData?) { override fun showTransferMoneyDialog(presenter: BankingPresenter, preselectedBankAccount: BankAccount?, preselectedValues: TransferMoneyData?) {
activityTracker.currentActivity?.let { activity -> activityTracker.currentOrNextActivity { activity ->
TransferMoneyDialog().show(activity, preselectedBankAccount, preselectedValues) TransferMoneyDialog().show(activity, preselectedBankAccount, preselectedValues)
} }
} }

View File

@ -8,6 +8,9 @@ import kotlin.concurrent.schedule
open class CurrentActivityTracker { open class CurrentActivityTracker {
protected val nextActivitySetListeners = CopyOnWriteArrayList<(BaseActivity) -> Unit>()
var currentActivity: BaseActivity? = null var currentActivity: BaseActivity? = null
set(value) { set(value) {
field = value // TODO: check field != value field = value // TODO: check field != value
@ -18,7 +21,14 @@ open class CurrentActivityTracker {
} }
protected val nextActivitySetListeners = CopyOnWriteArrayList<(BaseActivity) -> Unit>() open fun currentOrNextActivity(activity: (BaseActivity) -> Unit) {
currentActivity?.let {
activity(it)
}
?: addNextActivitySetListener {
activity(it)
}
}
open fun addNextActivitySetListener(listener: (BaseActivity) -> Unit) { open fun addNextActivitySetListener(listener: (BaseActivity) -> Unit) {
synchronized(nextActivitySetListeners) { synchronized(nextActivitySetListeners) {