Wear: add dependency injection

This commit is contained in:
Milos Kozak 2021-12-09 22:42:43 +01:00
parent ca8568c204
commit d08b018edf
8 changed files with 84 additions and 20 deletions

View file

@ -12,17 +12,13 @@ buildscript {
} }
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'
apply plugin: 'kotlin-allopen' apply plugin: 'kotlin-allopen'
apply plugin: 'com.hiya.jacoco-android' apply plugin: 'com.hiya.jacoco-android'
jacoco { apply from: "${project.rootDir}/gradle/android_dependencies.gradle"
toolVersion = "0.8.7" apply from: "${project.rootDir}/gradle/jacoco_global.gradle"
}
tasks.withType(Test) {
jacoco.includeNoLocationClasses = true
jacoco.excludes = ['jdk.internal.*']
}
ext { ext {
wearableVersion = "2.8.1" wearableVersion = "2.8.1"
@ -159,6 +155,17 @@ dependencies {
testImplementation "org.hamcrest:hamcrest-all:1.3" testImplementation "org.hamcrest:hamcrest-all:1.3"
implementation "androidx.core:core-ktx:$coreVersion" implementation "androidx.core:core-ktx:$coreVersion"
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
// for old fashioned support-app version
implementation "com.google.dagger:dagger:$dagger_version"
implementation "com.google.dagger:dagger-android:$dagger_version"
implementation "com.google.dagger:dagger-android-support:$dagger_version"
annotationProcessor "com.google.dagger:dagger-compiler:$dagger_version"
annotationProcessor "com.google.dagger:dagger-android-processor:$dagger_version"
kapt "com.google.dagger:dagger-android-processor:$dagger_version"
kapt "com.google.dagger:dagger-compiler:$dagger_version"
} }
repositories { repositories {
mavenCentral() mavenCentral()

View file

@ -9,12 +9,16 @@ import android.preference.PreferenceManager;
import androidx.localbroadcastmanager.content.LocalBroadcastManager; import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import dagger.android.AndroidInjector;
import dagger.android.DaggerApplication;
import info.nightscout.androidaps.di.DaggerAppComponent;
/** /**
* Created for xDrip+ by Emma Black on 3/21/15. * Created for xDrip+ by Emma Black on 3/21/15.
* Adapted for AAPS by dlvoy 2019-11-06. * Adapted for AAPS by dlvoy 2019-11-06.
*/ */
public class Aaps extends Application implements SharedPreferences.OnSharedPreferenceChangeListener { public class Aaps extends DaggerApplication implements SharedPreferences.OnSharedPreferenceChangeListener {
@SuppressLint("StaticFieldLeak") @SuppressLint("StaticFieldLeak")
private static Context context; private static Context context;
@ -30,6 +34,14 @@ public class Aaps extends Application implements SharedPreferences.OnSharedPrefe
super.onCreate(); super.onCreate();
} }
@Override
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
return DaggerAppComponent
.builder()
.application(this)
.build();
}
private void updatePrefs(SharedPreferences sharedPrefs) { private void updatePrefs(SharedPreferences sharedPrefs) {
unicodeComplications = sharedPrefs.getBoolean("complication_unicode", true); unicodeComplications = sharedPrefs.getBoolean("complication_unicode", true);
complicationTapAction = sharedPrefs.getString("complication_tap_action", "default"); complicationTapAction = sharedPrefs.getString("complication_tap_action", "default");

View file

@ -9,10 +9,6 @@ import info.nightscout.androidaps.data.RawDisplayData;
import info.nightscout.androidaps.interaction.utils.DisplayFormat; import info.nightscout.androidaps.interaction.utils.DisplayFormat;
import info.nightscout.androidaps.interaction.utils.SmallestDoubleString; import info.nightscout.androidaps.interaction.utils.SmallestDoubleString;
import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MAX_FIELD_LEN_SHORT;
import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MIN_FIELD_LEN_COB;
import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MIN_FIELD_LEN_IOB;
/* /*
* Created by dlvoy on 2019-11-12 * Created by dlvoy on 2019-11-12
*/ */
@ -25,8 +21,8 @@ public class BrCobIobComplication extends BaseComplicationProviderService {
ComplicationData complicationData = null; ComplicationData complicationData = null;
if (dataType == ComplicationData.TYPE_SHORT_TEXT) { if (dataType == ComplicationData.TYPE_SHORT_TEXT) {
final String cob = new SmallestDoubleString(raw.sCOB2, SmallestDoubleString.Units.USE).minimise(MIN_FIELD_LEN_COB); final String cob = new SmallestDoubleString(raw.sCOB2, SmallestDoubleString.Units.USE).minimise(DisplayFormat.MIN_FIELD_LEN_COB);
final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(Math.max(MIN_FIELD_LEN_IOB, (MAX_FIELD_LEN_SHORT -1) - cob.length())); final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(Math.max(DisplayFormat.MIN_FIELD_LEN_IOB, (DisplayFormat.MAX_FIELD_LEN_SHORT -1) - cob.length()));
final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)
.setShortText(ComplicationText.plainText(DisplayFormat.basalRateSymbol()+raw.sBasalRate)) .setShortText(ComplicationText.plainText(DisplayFormat.basalRateSymbol()+raw.sBasalRate))

View file

@ -6,10 +6,9 @@ import android.support.wearable.complications.ComplicationText;
import android.util.Log; import android.util.Log;
import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.data.RawDisplayData;
import info.nightscout.androidaps.interaction.utils.DisplayFormat;
import info.nightscout.androidaps.interaction.utils.SmallestDoubleString; import info.nightscout.androidaps.interaction.utils.SmallestDoubleString;
import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MAX_FIELD_LEN_SHORT;
/* /*
* Created by dlvoy on 2019-11-12 * Created by dlvoy on 2019-11-12
*/ */
@ -23,7 +22,7 @@ public class CobIobComplication extends BaseComplicationProviderService {
if (dataType == ComplicationData.TYPE_SHORT_TEXT) { if (dataType == ComplicationData.TYPE_SHORT_TEXT) {
final String cob = raw.sCOB2; final String cob = raw.sCOB2;
final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(MAX_FIELD_LEN_SHORT); final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(DisplayFormat.MAX_FIELD_LEN_SHORT);
final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)
.setShortText(ComplicationText.plainText(cob)) .setShortText(ComplicationText.plainText(cob))

View file

@ -8,10 +8,9 @@ import android.util.Log;
import info.nightscout.androidaps.R; import info.nightscout.androidaps.R;
import info.nightscout.androidaps.data.RawDisplayData; import info.nightscout.androidaps.data.RawDisplayData;
import info.nightscout.androidaps.interaction.utils.DisplayFormat;
import info.nightscout.androidaps.interaction.utils.SmallestDoubleString; import info.nightscout.androidaps.interaction.utils.SmallestDoubleString;
import static info.nightscout.androidaps.interaction.utils.DisplayFormat.MAX_FIELD_LEN_SHORT;
/* /*
* Created by dlvoy on 2019-11-12 * Created by dlvoy on 2019-11-12
*/ */
@ -24,7 +23,7 @@ public class IobIconComplication extends BaseComplicationProviderService {
ComplicationData complicationData = null; ComplicationData complicationData = null;
if (dataType == ComplicationData.TYPE_SHORT_TEXT) { if (dataType == ComplicationData.TYPE_SHORT_TEXT) {
final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(MAX_FIELD_LEN_SHORT); final String iob = new SmallestDoubleString(raw.sIOB1, SmallestDoubleString.Units.USE).minimise(DisplayFormat.MAX_FIELD_LEN_SHORT);
final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT) final ComplicationData.Builder builder = new ComplicationData.Builder(ComplicationData.TYPE_SHORT_TEXT)
.setShortText(ComplicationText.plainText(iob)) .setShortText(ComplicationText.plainText(iob))

View file

@ -0,0 +1,27 @@
package info.nightscout.androidaps.di
import dagger.BindsInstance
import dagger.Component
import dagger.android.AndroidInjectionModule
import dagger.android.AndroidInjector
import info.nightscout.androidaps.Aaps
import javax.inject.Singleton
@Singleton
@Component(
modules = [
AndroidInjectionModule::class,
WearModule::class
]
)
interface AppComponent : AndroidInjector<Aaps> {
@Component.Builder
interface Builder {
@BindsInstance
fun application(aaps: Aaps): Builder
fun build(): AppComponent
}
}

View file

@ -0,0 +1,22 @@
package info.nightscout.androidaps.di
import android.content.Context
import dagger.Binds
import dagger.Module
import dagger.android.HasAndroidInjector
import info.nightscout.androidaps.Aaps
@Suppress("unused")
@Module(includes = [
WearModule.AppBindings::class
])
open class WearModule {
@Module
interface AppBindings {
@Binds fun bindContext(aaps: Aaps): Context
@Binds fun bindInjector(aaps: Aaps): HasAndroidInjector
}
}

View file

@ -15,12 +15,14 @@ import java.util.Map;
import java.util.Set; import java.util.Set;
import info.nightscout.androidaps.Aaps; import info.nightscout.androidaps.Aaps;
import javax.inject.Singleton;
/** /**
* Created by andy on 3/5/19. * Created by andy on 3/5/19.
* Adapted by dlvoy on 2019-11-06 using code from jamorham JoH class * Adapted by dlvoy on 2019-11-06 using code from jamorham JoH class
*/ */
@Singleton
public class WearUtil { public class WearUtil {
private final static boolean debug_wakelocks = false; private final static boolean debug_wakelocks = false;