dagger2!
This commit is contained in:
parent
f2aa21ce82
commit
3d1c1140b5
4 changed files with 56 additions and 50 deletions
|
@ -1,6 +1,5 @@
|
||||||
package info.nightscout.androidaps;
|
package info.nightscout.androidaps;
|
||||||
|
|
||||||
import android.app.Application;
|
|
||||||
import android.content.BroadcastReceiver;
|
import android.content.BroadcastReceiver;
|
||||||
import android.content.IntentFilter;
|
import android.content.IntentFilter;
|
||||||
import android.content.res.Resources;
|
import android.content.res.Resources;
|
||||||
|
@ -22,7 +21,7 @@ import java.io.File;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
|
||||||
import dagger.android.AndroidInjector;
|
import dagger.android.AndroidInjector;
|
||||||
import dagger.android.HasAndroidInjector;
|
import dagger.android.DaggerApplication;
|
||||||
import info.nightscout.androidaps.data.ConstraintChecker;
|
import info.nightscout.androidaps.data.ConstraintChecker;
|
||||||
import info.nightscout.androidaps.db.DatabaseHelper;
|
import info.nightscout.androidaps.db.DatabaseHelper;
|
||||||
import info.nightscout.androidaps.dependencyInjection.DaggerAppComponent;
|
import info.nightscout.androidaps.dependencyInjection.DaggerAppComponent;
|
||||||
|
@ -98,43 +97,34 @@ import io.fabric.sdk.android.Fabric;
|
||||||
import static info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion;
|
import static info.nightscout.androidaps.plugins.constraints.versionChecker.VersionCheckerUtilsKt.triggerCheckVersion;
|
||||||
|
|
||||||
|
|
||||||
public class MainApp extends Application implements HasAndroidInjector {
|
public class MainApp extends DaggerApplication {
|
||||||
private static Logger log = LoggerFactory.getLogger(L.CORE);
|
static Logger log = LoggerFactory.getLogger(L.CORE);
|
||||||
private static KeepAliveReceiver keepAliveReceiver;
|
static KeepAliveReceiver keepAliveReceiver;
|
||||||
|
|
||||||
private static MainApp sInstance;
|
static MainApp sInstance;
|
||||||
public static Resources sResources;
|
public static Resources sResources;
|
||||||
|
|
||||||
private static FirebaseAnalytics mFirebaseAnalytics;
|
static FirebaseAnalytics mFirebaseAnalytics;
|
||||||
|
|
||||||
private static DatabaseHelper sDatabaseHelper = null;
|
static DatabaseHelper sDatabaseHelper = null;
|
||||||
private static ConstraintChecker sConstraintsChecker = null;
|
static ConstraintChecker sConstraintsChecker = null;
|
||||||
|
|
||||||
private static ArrayList<PluginBase> pluginsList = null;
|
static ArrayList<PluginBase> pluginsList = null;
|
||||||
|
|
||||||
private static DataReceiver dataReceiver = new DataReceiver();
|
static DataReceiver dataReceiver = new DataReceiver();
|
||||||
private static NSAlarmReceiver alarmReciever = new NSAlarmReceiver();
|
static NSAlarmReceiver alarmReciever = new NSAlarmReceiver();
|
||||||
private static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver();
|
static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver();
|
||||||
private static DBAccessReceiver dbAccessReciever = new DBAccessReceiver();
|
static DBAccessReceiver dbAccessReciever = new DBAccessReceiver();
|
||||||
private LocalBroadcastManager lbm;
|
LocalBroadcastManager lbm;
|
||||||
BroadcastReceiver btReceiver;
|
BroadcastReceiver btReceiver;
|
||||||
TimeDateOrTZChangeReceiver timeDateOrTZChangeReceiver;
|
TimeDateOrTZChangeReceiver timeDateOrTZChangeReceiver;
|
||||||
|
|
||||||
public static boolean devBranch;
|
public static boolean devBranch;
|
||||||
public static boolean engineeringMode;
|
public static boolean engineeringMode;
|
||||||
|
|
||||||
AndroidInjector<Object> activityInjector;
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public AndroidInjector<Object> androidInjector() {
|
|
||||||
return activityInjector;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate() {
|
public void onCreate() {
|
||||||
super.onCreate();
|
super.onCreate();
|
||||||
// Dagger injection
|
|
||||||
DaggerAppComponent.create().inject(this);
|
|
||||||
|
|
||||||
log.debug("onCreate");
|
log.debug("onCreate");
|
||||||
sInstance = this;
|
sInstance = this;
|
||||||
|
@ -258,6 +248,15 @@ public class MainApp extends Application implements HasAndroidInjector {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected AndroidInjector<? extends DaggerApplication> applicationInjector() {
|
||||||
|
|
||||||
|
return DaggerAppComponent
|
||||||
|
.builder()
|
||||||
|
.application(this)
|
||||||
|
.build();
|
||||||
|
}
|
||||||
|
|
||||||
private void registerLocalBroadcastReceiver() {
|
private void registerLocalBroadcastReceiver() {
|
||||||
lbm = LocalBroadcastManager.getInstance(this);
|
lbm = LocalBroadcastManager.getInstance(this);
|
||||||
lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_TREATMENT));
|
lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_TREATMENT));
|
||||||
|
|
|
@ -1,18 +1,29 @@
|
||||||
package info.nightscout.androidaps.dependencyInjection
|
package info.nightscout.androidaps.dependencyInjection
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
|
import dagger.BindsInstance
|
||||||
import dagger.Component
|
import dagger.Component
|
||||||
import dagger.android.AndroidInjectionModule
|
import dagger.android.AndroidInjectionModule
|
||||||
|
import dagger.android.AndroidInjector
|
||||||
import info.nightscout.androidaps.MainApp
|
import info.nightscout.androidaps.MainApp
|
||||||
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Singleton
|
||||||
@Component(
|
@Component(
|
||||||
modules = [
|
modules = [
|
||||||
AndroidInjectionModule::class,
|
AndroidInjectionModule::class,
|
||||||
ActivitiesModule::class,
|
ActivitiesModule::class,
|
||||||
AppModule::class
|
AppModule::class
|
||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
interface AppComponent : AndroidInjector<MainApp> {
|
||||||
|
|
||||||
interface AppComponent {
|
@Component.Builder
|
||||||
fun inject(mainApp: MainApp)
|
interface Builder {
|
||||||
|
|
||||||
|
@BindsInstance
|
||||||
|
fun application(application: Application): Builder
|
||||||
|
|
||||||
|
fun build(): AppComponent
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -1,31 +1,27 @@
|
||||||
package info.nightscout.androidaps.dependencyInjection
|
package info.nightscout.androidaps.dependencyInjection
|
||||||
|
|
||||||
|
import android.app.Application
|
||||||
import android.content.Context
|
import android.content.Context
|
||||||
import android.content.SharedPreferences
|
|
||||||
import android.preference.PreferenceManager
|
import android.preference.PreferenceManager
|
||||||
|
import dagger.Binds
|
||||||
import dagger.Module
|
import dagger.Module
|
||||||
import dagger.Provides
|
import dagger.Provides
|
||||||
import info.nightscout.androidaps.MainApp
|
|
||||||
import info.nightscout.androidaps.utils.sharedPreferences.SP
|
import info.nightscout.androidaps.utils.sharedPreferences.SP
|
||||||
import javax.inject.Singleton
|
import javax.inject.Singleton
|
||||||
|
|
||||||
|
@Module(includes = [AppModule.AppBindings::class])
|
||||||
@Module
|
class AppModule() {
|
||||||
class AppModule(private val application: MainApp) {
|
|
||||||
@Provides
|
|
||||||
@Singleton
|
|
||||||
fun provideApplication(): MainApp {
|
|
||||||
return application
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
|
||||||
fun provideContext(): Context {
|
|
||||||
return application.applicationContext
|
|
||||||
}
|
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
@Singleton
|
||||||
fun provideSharedPreferences(): SP {
|
fun provideSharedPreferences(context: Context): SP {
|
||||||
return SP(PreferenceManager.getDefaultSharedPreferences(provideContext()))
|
return SP(PreferenceManager.getDefaultSharedPreferences(context))
|
||||||
|
}
|
||||||
|
|
||||||
|
@Module
|
||||||
|
interface AppBindings {
|
||||||
|
|
||||||
|
@Binds
|
||||||
|
fun bindContext(application: Application): Context
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,7 @@ import javax.inject.Singleton
|
||||||
* Created by mike on 17.02.2017.
|
* Created by mike on 17.02.2017.
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
class SP @Inject internal constructor(private val sharedPreferences: SharedPreferences) {
|
class SP @Inject constructor(private val sharedPreferences: SharedPreferences) {
|
||||||
|
|
||||||
fun getAll(): Map<String, *> = sharedPreferences.all
|
fun getAll(): Map<String, *> = sharedPreferences.all
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue