some dagger code

This commit is contained in:
Milos Kozak 2019-12-13 02:12:19 +01:00
parent 9bd24ffdbe
commit f2aa21ce82
7 changed files with 219 additions and 3 deletions

View file

@ -13,6 +13,7 @@ buildscript {
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'io.fabric'
apply plugin: 'jacoco-android'
@ -208,6 +209,10 @@ android {
}
useLibrary "org.apache.http.legacy"
configurations.all {
resolutionStrategy.force 'com.google.code.findbugs:jsr305:1.3.9'
}
}
allprojects {
@ -312,6 +317,19 @@ dependencies {
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test:rules:1.3.0-alpha03'
androidTestImplementation 'com.google.code.findbugs:jsr305:3.0.2'
/* Dagger2 - We are going to use dagger.android which includes
* support for Activity and fragment injection so we need to include
* the following dependencies */
implementation 'com.google.dagger:dagger-android:2.25.2'
implementation 'com.google.dagger:dagger-android-support:2.25.2'
annotationProcessor 'com.google.dagger:dagger-compiler:2.25.2'
annotationProcessor 'com.google.dagger:dagger-android-processor:2.25.2'
kapt 'com.google.dagger:dagger-android-processor:2.25.2'
/* Dagger2 - default dependency */
kapt 'com.google.dagger:dagger-compiler:2.25.2'
}

View file

@ -37,6 +37,7 @@ import com.joanzapata.iconify.fonts.FontAwesomeModule;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import dagger.android.AndroidInjection;
import info.nightscout.androidaps.activities.HistoryBrowseActivity;
import info.nightscout.androidaps.activities.NoSplashAppCompatActivity;
import info.nightscout.androidaps.activities.PreferencesActivity;
@ -73,6 +74,7 @@ public class MainActivity extends NoSplashAppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
Iconify.with(new FontAwesomeModule());

View file

@ -21,8 +21,11 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.ArrayList;
import dagger.android.AndroidInjector;
import dagger.android.HasAndroidInjector;
import info.nightscout.androidaps.data.ConstraintChecker;
import info.nightscout.androidaps.db.DatabaseHelper;
import info.nightscout.androidaps.dependencyInjection.DaggerAppComponent;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpInterface;
@ -51,8 +54,6 @@ import info.nightscout.androidaps.plugins.general.nsclient.receivers.DBAccessRec
import info.nightscout.androidaps.plugins.general.overview.OverviewPlugin;
import info.nightscout.androidaps.plugins.general.persistentNotification.PersistentNotificationPlugin;
import info.nightscout.androidaps.plugins.general.smsCommunicator.SmsCommunicatorPlugin;
import info.nightscout.androidaps.plugins.source.RandomBgPlugin;
import info.nightscout.androidaps.utils.ActivityMonitor;
import info.nightscout.androidaps.plugins.general.wear.WearPlugin;
import info.nightscout.androidaps.plugins.general.xdripStatusline.StatuslinePlugin;
import info.nightscout.androidaps.plugins.insulin.InsulinOrefFreePeakPlugin;
@ -74,6 +75,7 @@ import info.nightscout.androidaps.plugins.sensitivity.SensitivityAAPSPlugin;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref0Plugin;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref1Plugin;
import info.nightscout.androidaps.plugins.sensitivity.SensitivityWeightedAveragePlugin;
import info.nightscout.androidaps.plugins.source.RandomBgPlugin;
import info.nightscout.androidaps.plugins.source.SourceDexcomPlugin;
import info.nightscout.androidaps.plugins.source.SourceEversensePlugin;
import info.nightscout.androidaps.plugins.source.SourceGlimpPlugin;
@ -88,6 +90,7 @@ import info.nightscout.androidaps.receivers.KeepAliveReceiver;
import info.nightscout.androidaps.receivers.NSAlarmReceiver;
import info.nightscout.androidaps.receivers.TimeDateOrTZChangeReceiver;
import info.nightscout.androidaps.services.Intents;
import info.nightscout.androidaps.utils.ActivityMonitor;
import info.nightscout.androidaps.utils.FabricPrivacy;
import info.nightscout.androidaps.utils.LocaleHelper;
import io.fabric.sdk.android.Fabric;
@ -95,7 +98,7 @@ import io.fabric.sdk.android.Fabric;
import static info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion;
public class MainApp extends Application {
public class MainApp extends Application implements HasAndroidInjector {
private static Logger log = LoggerFactory.getLogger(L.CORE);
private static KeepAliveReceiver keepAliveReceiver;
@ -120,9 +123,19 @@ public class MainApp extends Application {
public static boolean devBranch;
public static boolean engineeringMode;
AndroidInjector<Object> activityInjector;
@Override
public AndroidInjector<Object> androidInjector() {
return activityInjector;
}
@Override
public void onCreate() {
super.onCreate();
// Dagger injection
DaggerAppComponent.create().inject(this);
log.debug("onCreate");
sInstance = this;
sResources = getResources();
@ -442,4 +455,5 @@ public class MainApp extends Application {
float scale = sResources.getDisplayMetrics().density;
return (int) (dp * scale + 0.5f);
}
}

View file

@ -0,0 +1,11 @@
package info.nightscout.androidaps.dependencyInjection
import dagger.Module
import dagger.android.ContributesAndroidInjector
import info.nightscout.androidaps.MainActivity
@Module
abstract class ActivitiesModule {
@ContributesAndroidInjector
abstract fun contributeMainActivity(): MainActivity
}

View file

@ -0,0 +1,18 @@
package info.nightscout.androidaps.dependencyInjection
import dagger.Component
import dagger.android.AndroidInjectionModule
import info.nightscout.androidaps.MainApp
@Component(
modules = [
AndroidInjectionModule::class,
ActivitiesModule::class,
AppModule::class
]
)
interface AppComponent {
fun inject(mainApp: MainApp)
}

View file

@ -0,0 +1,31 @@
package info.nightscout.androidaps.dependencyInjection
import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import dagger.Module
import dagger.Provides
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.utils.sharedPreferences.SP
import javax.inject.Singleton
@Module
class AppModule(private val application: MainApp) {
@Provides
@Singleton
fun provideApplication(): MainApp {
return application
}
@Provides
fun provideContext(): Context {
return application.applicationContext
}
@Provides
@Singleton
fun provideSharedPreferences(): SP {
return SP(PreferenceManager.getDefaultSharedPreferences(provideContext()))
}
}

View file

@ -0,0 +1,122 @@
package info.nightscout.androidaps.utils.sharedPreferences
import android.content.SharedPreferences
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.utils.SafeParse
import javax.inject.Inject
import javax.inject.Singleton
/**
* Created by mike on 17.02.2017.
*/
@Singleton
class SP @Inject internal constructor(private val sharedPreferences: SharedPreferences) {
fun getAll(): Map<String, *> = sharedPreferences.all
fun clear() = sharedPreferences.edit().clear().apply()
fun contains(key: String): Boolean = sharedPreferences.contains(key)
fun contains(resourceId: Int): Boolean = sharedPreferences.contains(MainApp.gs(resourceId))
fun remove(resourceID: Int) =
sharedPreferences.edit().remove(MainApp.gs(resourceID)).apply()
fun remove(key: String) =
sharedPreferences.edit().remove(key).apply()
fun getString(resourceID: Int, defaultValue: String): String =
sharedPreferences.getString(MainApp.gs(resourceID), defaultValue)
fun getString(key: String, defaultValue: String): String =
sharedPreferences.getString(key, defaultValue)
fun getBoolean(resourceID: Int, defaultValue: Boolean): Boolean {
return try {
sharedPreferences.getBoolean(MainApp.gs(resourceID), defaultValue)
} catch (e: Exception) {
defaultValue
}
}
fun getBoolean(key: String, defaultValue: Boolean): Boolean {
return try {
sharedPreferences.getBoolean(key, defaultValue)
} catch (e: Exception) {
defaultValue
}
}
fun getDouble(resourceID: Int, defaultValue: Double): Double =
SafeParse.stringToDouble(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString()))
fun getDouble(key: String, defaultValue: Double): Double =
SafeParse.stringToDouble(sharedPreferences.getString(key, defaultValue.toString()))
fun getInt(resourceID: Int, defaultValue: Int): Int {
return try {
sharedPreferences.getInt(MainApp.gs(resourceID), defaultValue)
} catch (e: Exception) {
SafeParse.stringToInt(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString()))
}
}
fun getInt(key: String, defaultValue: Int): Int {
return try {
sharedPreferences.getInt(key, defaultValue)
} catch (e: Exception) {
SafeParse.stringToInt(sharedPreferences.getString(key, defaultValue.toString()))
}
}
fun getLong(resourceID: Int, defaultValue: Long): Long {
return try {
sharedPreferences.getLong(MainApp.gs(resourceID), defaultValue)
} catch (e: Exception) {
SafeParse.stringToLong(sharedPreferences.getString(MainApp.gs(resourceID), defaultValue.toString()))
}
}
fun getLong(key: String, defaultValue: Long): Long {
return try {
sharedPreferences.getLong(key, defaultValue)
} catch (e: Exception) {
SafeParse.stringToLong(sharedPreferences.getString(key, defaultValue.toString()))
}
}
fun putBoolean(key: String, value: Boolean) = sharedPreferences.edit().putBoolean(key, value).apply()
fun putBoolean(resourceID: Int, value: Boolean) =
sharedPreferences.edit().putBoolean(MainApp.gs(resourceID), value).apply()
fun putDouble(key: String, value: Double) =
sharedPreferences.edit().putString(key, java.lang.Double.toString(value)).apply()
fun putLong(key: String, value: Long) =
sharedPreferences.edit().putLong(key, value).apply()
fun putLong(resourceID: Int, value: Long) =
sharedPreferences.edit().putLong(MainApp.gs(resourceID), value).apply()
fun putInt(key: String, value: Int) =
sharedPreferences.edit().putInt(key, value).apply()
fun putInt(resourceID: Int, value: Int) =
sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply()
fun incInt(resourceID: Int) {
val value = getInt(resourceID, 0) + 1
sharedPreferences.edit().putInt(MainApp.gs(resourceID), value).apply()
}
fun putString(resourceID: Int, value: String) =
sharedPreferences.edit().putString(MainApp.gs(resourceID), value).apply()
fun putString(key: String, value: String) =
sharedPreferences.edit().putString(key, value).apply()
}