Added IRouter to move navigation out of UI code

This commit is contained in:
dankl 2020-01-08 18:13:32 +01:00 committed by dankito
parent eed03d348e
commit 3f4553281d
3 changed files with 73 additions and 27 deletions

View File

@ -0,0 +1,16 @@
package net.dankito.banking.ui
import net.dankito.banking.ui.model.Account
import net.dankito.banking.ui.model.tan.EnterTanGeneratorAtcResult
import net.dankito.banking.ui.model.tan.EnterTanResult
import net.dankito.banking.ui.model.tan.TanChallenge
import net.dankito.banking.ui.model.tan.TanGeneratorTanMedium
interface IRouter {
fun getTanFromUserOffUiThread(account: Account, tanChallenge: TanChallenge): EnterTanResult
fun getAtcFromUserOffUiThread(tanMedium: TanGeneratorTanMedium): EnterTanGeneratorAtcResult
}

View File

@ -33,7 +33,8 @@ class MainActivity : AppCompatActivity() {
private lateinit var floatingActionMenuButton: MainActivityFloatingActionMenuButton
val presenter = MainWindowPresenter(fints4javaBankingClientCreator(), Base64ServiceAndroid(), object : BankingClientCallback {
val presenter = MainWindowPresenter(fints4javaBankingClientCreator(),
Base64ServiceAndroid(), object : BankingClientCallback {
override fun enterTan(account: Account, tanChallenge: TanChallenge): EnterTanResult {
return getTanFromUserOffUiThread(account, tanChallenge)
@ -45,6 +46,8 @@ class MainActivity : AppCompatActivity() {
})
private val router = RouterAndroid(this, presenter)
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
@ -108,35 +111,11 @@ class MainActivity : AppCompatActivity() {
private fun getTanFromUserOffUiThread(account: Account, tanChallenge: TanChallenge): EnterTanResult {
val enteredTan = AtomicReference<EnterTanResult>(null)
val tanEnteredLatch = CountDownLatch(1)
runOnUiThread {
EnterTanDialog().show(account, tanChallenge, presenter, this@MainActivity, false) {
enteredTan.set(it)
tanEnteredLatch.countDown()
}
}
try { tanEnteredLatch.await() } catch (ignored: Exception) { }
return enteredTan.get()
return router.getTanFromUserOffUiThread(account, tanChallenge)
}
private fun getAtcFromUserOffUiThread(tanMedium: TanGeneratorTanMedium): EnterTanGeneratorAtcResult {
val result = AtomicReference<EnterTanGeneratorAtcResult>(null)
val tanEnteredLatch = CountDownLatch(1)
runOnUiThread {
EnterAtcDialog().show(tanMedium, this@MainActivity, false) { enteredResult ->
result.set(enteredResult)
tanEnteredLatch.countDown()
}
}
try { tanEnteredLatch.await() } catch (ignored: Exception) { }
return result.get()
return router.getAtcFromUserOffUiThread(tanMedium)
}
}

View File

@ -0,0 +1,51 @@
package net.dankito.banking.fints4java.android
import android.support.v7.app.AppCompatActivity
import net.dankito.banking.fints4java.android.ui.MainWindowPresenter
import net.dankito.banking.fints4java.android.ui.dialogs.EnterAtcDialog
import net.dankito.banking.fints4java.android.ui.dialogs.EnterTanDialog
import net.dankito.banking.ui.IRouter
import net.dankito.banking.ui.model.Account
import net.dankito.banking.ui.model.tan.EnterTanGeneratorAtcResult
import net.dankito.banking.ui.model.tan.EnterTanResult
import net.dankito.banking.ui.model.tan.TanChallenge
import net.dankito.banking.ui.model.tan.TanGeneratorTanMedium
import java.util.concurrent.CountDownLatch
import java.util.concurrent.atomic.AtomicReference
open class RouterAndroid(protected val activity: AppCompatActivity, protected val presenter: MainWindowPresenter) : IRouter {
override fun getTanFromUserOffUiThread(account: Account, tanChallenge: TanChallenge): EnterTanResult {
val enteredTan = AtomicReference<EnterTanResult>(null)
val tanEnteredLatch = CountDownLatch(1)
activity.runOnUiThread {
EnterTanDialog().show(account, tanChallenge, presenter, activity, false) {
enteredTan.set(it)
tanEnteredLatch.countDown()
}
}
try { tanEnteredLatch.await() } catch (ignored: Exception) { }
return enteredTan.get()
}
override fun getAtcFromUserOffUiThread(tanMedium: TanGeneratorTanMedium): EnterTanGeneratorAtcResult {
val result = AtomicReference<EnterTanGeneratorAtcResult>(null)
val tanEnteredLatch = CountDownLatch(1)
activity.runOnUiThread {
EnterAtcDialog().show(tanMedium, activity, false) { enteredResult ->
result.set(enteredResult)
tanEnteredLatch.countDown()
}
}
try { tanEnteredLatch.await() } catch (ignored: Exception) { }
return result.get()
}
}