This commit is contained in:
AdrianLxM 2019-12-21 20:17:08 +01:00
parent f2aa21ce82
commit 3d1c1140b5
4 changed files with 56 additions and 50 deletions

View file

@ -1,6 +1,5 @@
package info.nightscout.androidaps;
import android.app.Application;
import android.content.BroadcastReceiver;
import android.content.IntentFilter;
import android.content.res.Resources;
@ -22,7 +21,7 @@ import java.io.File;
import java.util.ArrayList;
import dagger.android.AndroidInjector;
import dagger.android.HasAndroidInjector;
import dagger.android.DaggerApplication;
import info.nightscout.androidaps.data.ConstraintChecker;
import info.nightscout.androidaps.db.DatabaseHelper;
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;
public class MainApp extends Application implements HasAndroidInjector {
private static Logger log = LoggerFactory.getLogger(L.CORE);
private static KeepAliveReceiver keepAliveReceiver;
public class MainApp extends DaggerApplication {
static Logger log = LoggerFactory.getLogger(L.CORE);
static KeepAliveReceiver keepAliveReceiver;
private static MainApp sInstance;
static MainApp sInstance;
public static Resources sResources;
private static FirebaseAnalytics mFirebaseAnalytics;
static FirebaseAnalytics mFirebaseAnalytics;
private static DatabaseHelper sDatabaseHelper = null;
private static ConstraintChecker sConstraintsChecker = null;
static DatabaseHelper sDatabaseHelper = null;
static ConstraintChecker sConstraintsChecker = null;
private static ArrayList<PluginBase> pluginsList = null;
static ArrayList<PluginBase> pluginsList = null;
private static DataReceiver dataReceiver = new DataReceiver();
private static NSAlarmReceiver alarmReciever = new NSAlarmReceiver();
private static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver();
private static DBAccessReceiver dbAccessReciever = new DBAccessReceiver();
private LocalBroadcastManager lbm;
static DataReceiver dataReceiver = new DataReceiver();
static NSAlarmReceiver alarmReciever = new NSAlarmReceiver();
static AckAlarmReceiver ackAlarmReciever = new AckAlarmReceiver();
static DBAccessReceiver dbAccessReciever = new DBAccessReceiver();
LocalBroadcastManager lbm;
BroadcastReceiver btReceiver;
TimeDateOrTZChangeReceiver timeDateOrTZChangeReceiver;
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;
@ -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() {
lbm = LocalBroadcastManager.getInstance(this);
lbm.registerReceiver(dataReceiver, new IntentFilter(Intents.ACTION_NEW_TREATMENT));

View file

@ -1,18 +1,29 @@
package info.nightscout.androidaps.dependencyInjection
import android.app.Application
import dagger.BindsInstance
import dagger.Component
import dagger.android.AndroidInjectionModule
import dagger.android.AndroidInjector
import info.nightscout.androidaps.MainApp
import javax.inject.Singleton
@Singleton
@Component(
modules = [
AndroidInjectionModule::class,
ActivitiesModule::class,
AppModule::class
]
modules = [
AndroidInjectionModule::class,
ActivitiesModule::class,
AppModule::class
]
)
interface AppComponent : AndroidInjector<MainApp> {
interface AppComponent {
fun inject(mainApp: MainApp)
@Component.Builder
interface Builder {
@BindsInstance
fun application(application: Application): Builder
fun build(): AppComponent
}
}

View file

@ -1,31 +1,27 @@
package info.nightscout.androidaps.dependencyInjection
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.preference.PreferenceManager
import dagger.Binds
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
}
@Module(includes = [AppModule.AppBindings::class])
class AppModule() {
@Provides
@Singleton
fun provideSharedPreferences(): SP {
return SP(PreferenceManager.getDefaultSharedPreferences(provideContext()))
fun provideSharedPreferences(context: Context): SP {
return SP(PreferenceManager.getDefaultSharedPreferences(context))
}
@Module
interface AppBindings {
@Binds
fun bindContext(application: Application): Context
}
}

View file

@ -10,7 +10,7 @@ import javax.inject.Singleton
* Created by mike on 17.02.2017.
*/
@Singleton
class SP @Inject internal constructor(private val sharedPreferences: SharedPreferences) {
class SP @Inject constructor(private val sharedPreferences: SharedPreferences) {
fun getAll(): Map<String, *> = sharedPreferences.all