From 4d3825d624fc50ca6f6d73fdff0bf085009f4dce Mon Sep 17 00:00:00 2001 From: Joaquim Ley Date: Tue, 4 Dec 2018 17:27:39 +0100 Subject: [PATCH] Small improvements in Framework abstraction Prep work for #87 --- .../data-sharedpreferences/build.gradle | 3 +-- .../FrameworkLocalStorageImpl.kt | 2 +- .../FrameworkLocalStorageTest.kt | 2 +- transport-eta-android/presentation/build.gradle | 2 +- .../com/joaquimley/transporteta/sms/SmsSender.kt | 15 +++++++++++++++ .../transporteta/sms/SmsControllerImpl.kt | 6 +++--- .../java/com/joaquimley/SmsControllerTest.kt | 4 +++- transport-eta-android/ui-mobile/build.gradle | 2 +- .../ui/di/module/SmsControllerModule.kt | 16 ++++++++++------ 9 files changed, 36 insertions(+), 16 deletions(-) create mode 100644 transport-eta-android/sms/src/main/java/com/joaquimley/transporteta/sms/SmsSender.kt diff --git a/transport-eta-android/data-sharedpreferences/build.gradle b/transport-eta-android/data-sharedpreferences/build.gradle index 2d9a9b7..1dca9ac 100644 --- a/transport-eta-android/data-sharedpreferences/build.gradle +++ b/transport-eta-android/data-sharedpreferences/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.android.library' -apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android' +apply plugin: 'kotlin-kapt' apply plugin: 'kotlinx-serialization' apply plugin: 'kotlin-android-extensions' @@ -9,7 +9,6 @@ android { defaultConfig { minSdkVersion build_versions.min_sdk targetSdkVersion build_versions.target_sdk - testInstrumentationRunner "com.joaquimley.transporteta.ui.test.TestRunner" } testOptions { diff --git a/transport-eta-android/data-sharedpreferences/src/main/java/com/joaquimley/transporteta/sharedpreferences/FrameworkLocalStorageImpl.kt b/transport-eta-android/data-sharedpreferences/src/main/java/com/joaquimley/transporteta/sharedpreferences/FrameworkLocalStorageImpl.kt index 30870ab..ef91ae7 100644 --- a/transport-eta-android/data-sharedpreferences/src/main/java/com/joaquimley/transporteta/sharedpreferences/FrameworkLocalStorageImpl.kt +++ b/transport-eta-android/data-sharedpreferences/src/main/java/com/joaquimley/transporteta/sharedpreferences/FrameworkLocalStorageImpl.kt @@ -107,7 +107,7 @@ class FrameworkLocalStorageImpl @Inject constructor(private val sharedPreference } private fun getSlot(): Slot? { - // TODO Improve this, also check for updates + // TODO Improve this return when { getFromSharedPrefs(Slot.SAVE_SLOT_ONE) == null -> Slot.SAVE_SLOT_ONE getFromSharedPrefs(Slot.SAVE_SLOT_TWO) == null -> Slot.SAVE_SLOT_TWO diff --git a/transport-eta-android/data-sharedpreferences/src/test/java/com/joaquimley/transporteta/sharedpreferences/FrameworkLocalStorageTest.kt b/transport-eta-android/data-sharedpreferences/src/test/java/com/joaquimley/transporteta/sharedpreferences/FrameworkLocalStorageTest.kt index 19c3544..0ded95d 100644 --- a/transport-eta-android/data-sharedpreferences/src/test/java/com/joaquimley/transporteta/sharedpreferences/FrameworkLocalStorageTest.kt +++ b/transport-eta-android/data-sharedpreferences/src/test/java/com/joaquimley/transporteta/sharedpreferences/FrameworkLocalStorageTest.kt @@ -46,7 +46,7 @@ class FrameworkLocalStorageTest { robot.stubMapperFromStringToModel(modelStringThree) // Act - // Nothing <->xo + // Nothing <-> // Assert verify(mockSharedPreferences, times(1)).getString(FrameworkLocalStorageImpl.Slot.SAVE_SLOT_ONE.name, null) verify(mockSharedPreferences, times(1)).getString(FrameworkLocalStorageImpl.Slot.SAVE_SLOT_TWO.name, null) diff --git a/transport-eta-android/presentation/build.gradle b/transport-eta-android/presentation/build.gradle index 84c7f2b..589078f 100644 --- a/transport-eta-android/presentation/build.gradle +++ b/transport-eta-android/presentation/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.android.library' -apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android' +apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions' android { diff --git a/transport-eta-android/sms/src/main/java/com/joaquimley/transporteta/sms/SmsSender.kt b/transport-eta-android/sms/src/main/java/com/joaquimley/transporteta/sms/SmsSender.kt new file mode 100644 index 0000000..1c07eea --- /dev/null +++ b/transport-eta-android/sms/src/main/java/com/joaquimley/transporteta/sms/SmsSender.kt @@ -0,0 +1,15 @@ +package com.joaquimley.transporteta.sms + +import android.telephony.SmsManager +import javax.inject.Inject +import javax.inject.Singleton + +@Singleton +class SmsSender @Inject constructor(private val androidSmsManager: SmsManager) { + + // TODO Improve with intents: https://github.com/JoaquimLey/transport-eta/issues/87 + fun send(serviceNumber: String, busStopCode: Int) { + androidSmsManager.sendTextMessage(serviceNumber, null, "C $busStopCode", null, null) + + } +}gs \ No newline at end of file diff --git a/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt b/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt index 94c186c..2e7d8b3 100644 --- a/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt +++ b/transport-eta-android/sms/src/prod/java/com/joaquimley/transporteta/sms/SmsControllerImpl.kt @@ -1,6 +1,5 @@ package com.joaquimley.transporteta.sms -import android.telephony.SmsManager import android.util.Log import com.joaquimley.transporteta.sms.model.SmsModel import io.reactivex.Single @@ -12,7 +11,8 @@ import javax.inject.Singleton @Singleton -class SmsControllerImpl @Inject constructor(private val smsBroadcastReceiver: SmsBroadcastReceiver) : SmsController { +class SmsControllerImpl @Inject constructor(private val smsBroadcastReceiver: SmsBroadcastReceiver, + private val smsSender: SmsSender) : SmsController { // TODO build a queue for requests @@ -44,7 +44,7 @@ class SmsControllerImpl @Inject constructor(private val smsBroadcastReceiver: Sm } this.busStopCode = busStopCode - SmsManager.getDefault().sendTextMessage(smsBroadcastReceiver.serviceNumber, null, "C $busStopCode", null, null) + smsSender.send(smsBroadcastReceiver.serviceNumber, busStopCode) return Single.create { emitter -> smsRequestDisposable = smsPublishSubject.subscribe({ sms -> emitter.onSuccess(sms) diff --git a/transport-eta-android/sms/src/test/java/com/joaquimley/SmsControllerTest.kt b/transport-eta-android/sms/src/test/java/com/joaquimley/SmsControllerTest.kt index 467eacc..a67702c 100644 --- a/transport-eta-android/sms/src/test/java/com/joaquimley/SmsControllerTest.kt +++ b/transport-eta-android/sms/src/test/java/com/joaquimley/SmsControllerTest.kt @@ -3,6 +3,7 @@ package com.joaquimley import com.joaquimley.transporteta.sms.SmsBroadcastReceiver import com.joaquimley.transporteta.sms.SmsController import com.joaquimley.transporteta.sms.SmsControllerImpl +import com.joaquimley.transporteta.sms.SmsSender import com.nhaarman.mockitokotlin2.atLeastOnce import com.nhaarman.mockitokotlin2.mock import com.nhaarman.mockitokotlin2.verify @@ -16,13 +17,14 @@ class SmsControllerTest { private val robot = Robot() private val mockSmsBroadcastReceiver = mock() + private val mockSmsManager = mock() private lateinit var smsController: SmsController @Before fun setup() { robot.stubSmsBroadcastReceiverSuccess() - smsController = SmsControllerImpl(mockSmsBroadcastReceiver) + smsController = SmsControllerImpl(mockSmsBroadcastReceiver, mockSmsManager) } @After diff --git a/transport-eta-android/ui-mobile/build.gradle b/transport-eta-android/ui-mobile/build.gradle index edbee11..a0f99a1 100644 --- a/transport-eta-android/ui-mobile/build.gradle +++ b/transport-eta-android/ui-mobile/build.gradle @@ -1,6 +1,6 @@ apply plugin: 'com.android.application' -apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android' +apply plugin: 'kotlin-kapt' apply plugin: 'kotlin-android-extensions' apply plugin: 'androidx.navigation.safeargs' diff --git a/transport-eta-android/ui-mobile/src/main/java/com/joaquimley/transporteta/ui/di/module/SmsControllerModule.kt b/transport-eta-android/ui-mobile/src/main/java/com/joaquimley/transporteta/ui/di/module/SmsControllerModule.kt index 6cce776..cec56ca 100644 --- a/transport-eta-android/ui-mobile/src/main/java/com/joaquimley/transporteta/ui/di/module/SmsControllerModule.kt +++ b/transport-eta-android/ui-mobile/src/main/java/com/joaquimley/transporteta/ui/di/module/SmsControllerModule.kt @@ -1,9 +1,7 @@ package com.joaquimley.transporteta.ui.di.module -import com.joaquimley.transporteta.sms.SmsBroadcastReceiver -import com.joaquimley.transporteta.sms.SmsBroadcastReceiverImpl -import com.joaquimley.transporteta.sms.SmsController -import com.joaquimley.transporteta.sms.SmsControllerImpl +import android.telephony.SmsManager +import com.joaquimley.transporteta.sms.* import com.joaquimley.transporteta.ui.di.qualifier.SmsServiceInfo import com.joaquimley.transporteta.ui.di.scope.PerApplication import dagger.Module @@ -40,7 +38,13 @@ class SmsControllerModule { @Provides @PerApplication - internal fun provideSmsController(smsBroadcastReceiver: SmsBroadcastReceiver): SmsController { - return SmsControllerImpl(smsBroadcastReceiver) + internal fun provideSmsSender(): com.joaquimley.transporteta.sms.SmsSender { + return SmsSender(SmsManager.getDefault()) + } + + @Provides + @PerApplication + internal fun provideSmsController(smsBroadcastReceiver: SmsBroadcastReceiver, smsSender: SmsSender): SmsController { + return SmsControllerImpl(smsBroadcastReceiver, smsSender) } }