From 3f4553281d49a72905f417d717a7e442bff72ff9 Mon Sep 17 00:00:00 2001 From: dankl Date: Wed, 8 Jan 2020 18:13:32 +0100 Subject: [PATCH] Added IRouter to move navigation out of UI code --- .../java/net/dankito/banking/ui/IRouter.kt | 16 ++++++ .../fints4java/android/MainActivity.kt | 33 +++--------- .../fints4java/android/RouterAndroid.kt | 51 +++++++++++++++++++ 3 files changed, 73 insertions(+), 27 deletions(-) create mode 100644 BankingUiCommon/src/main/java/net/dankito/banking/ui/IRouter.kt create mode 100644 fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/RouterAndroid.kt diff --git a/BankingUiCommon/src/main/java/net/dankito/banking/ui/IRouter.kt b/BankingUiCommon/src/main/java/net/dankito/banking/ui/IRouter.kt new file mode 100644 index 00000000..ebf70a7b --- /dev/null +++ b/BankingUiCommon/src/main/java/net/dankito/banking/ui/IRouter.kt @@ -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 + +} \ No newline at end of file diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt index aa27a1cc..fb6ddcf0 100644 --- a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/MainActivity.kt @@ -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(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(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) } } diff --git a/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/RouterAndroid.kt b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/RouterAndroid.kt new file mode 100644 index 00000000..ffcf1b70 --- /dev/null +++ b/fints4javaAndroidApp/src/main/java/net/dankito/banking/fints4java/android/RouterAndroid.kt @@ -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(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(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() + } + +} \ No newline at end of file