Added Dagger singleton

Attempting to fix casting exceptions in threaded sync adapter and NPE
in locale
gtask_note_sync
Alex Baker 8 years ago
parent b57fff2cdd
commit 874a0c3c5f

@ -11,8 +11,8 @@ public abstract class InjectingAbstractThreadedSyncAdapter extends AbstractThrea
} }
private void inject(Context context) { private void inject(Context context) {
inject(((InjectingApplication) context.getApplicationContext()) inject(Dagger.get(context)
.getComponent() .getApplicationComponent()
.plus(new SyncAdapterModule())); .plus(new SyncAdapterModule()));
} }

@ -0,0 +1,45 @@
package org.tasks.injection;
import android.content.Context;
import org.tasks.locale.Locale;
import timber.log.Timber;
class Dagger {
private static final Object lock = new Object();
private static Dagger instance;
public static Dagger get(Context context) {
if (instance == null) {
synchronized (lock) {
if (instance == null) {
instance = new Dagger(context);
}
}
}
return instance;
}
private ApplicationComponent applicationComponent;
private Dagger(Context context) {
Context localeContext = context.getApplicationContext();
try {
localeContext = Locale.getInstance(localeContext)
.createConfigurationContext(localeContext);
} catch (Exception e) {
Timber.e(e.getMessage(), e);
}
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(localeContext))
.build();
}
ApplicationComponent getApplicationComponent() {
return applicationComponent;
}
}

@ -15,9 +15,7 @@ public abstract class InjectingApplication extends BaseApplication {
Context context = Locale.getInstance(this).createConfigurationContext(getApplicationContext()); Context context = Locale.getInstance(this).createConfigurationContext(getApplicationContext());
applicationComponent = DaggerApplicationComponent.builder() applicationComponent = Dagger.get(context).getApplicationComponent();
.applicationModule(new ApplicationModule(context))
.build();
inject(applicationComponent); inject(applicationComponent);
} }

@ -28,12 +28,17 @@ public class Locale {
public static Locale getInstance(Context context) { public static Locale getInstance(Context context) {
if (INSTANCE == null) { if (INSTANCE == null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); synchronized (DEFAULT) {
String language = prefs.getString(context.getString(R.string.p_language), null); if (INSTANCE == null) {
int directionOverride = Integer.parseInt(prefs.getString(context.getString(R.string.p_layout_direction), "-1")); Context applicationContext = context.getApplicationContext();
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(applicationContext);
String language = prefs.getString(applicationContext.getString(R.string.p_language), null);
int directionOverride = Integer.parseInt(prefs.getString(applicationContext.getString(R.string.p_layout_direction), "-1"));
INSTANCE = new Locale(DEFAULT.getLocale(), language, directionOverride); INSTANCE = new Locale(DEFAULT.getLocale(), language, directionOverride);
java.util.Locale.setDefault(INSTANCE.getLocale()); java.util.Locale.setDefault(INSTANCE.getLocale());
} }
}
}
return getInstance(); return getInstance();
} }

Loading…
Cancel
Save