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) {
inject(((InjectingApplication) context.getApplicationContext())
.getComponent()
inject(Dagger.get(context)
.getApplicationComponent()
.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());
applicationComponent = DaggerApplicationComponent.builder()
.applicationModule(new ApplicationModule(context))
.build();
applicationComponent = Dagger.get(context).getApplicationComponent();
inject(applicationComponent);
}

@ -28,11 +28,16 @@ public class Locale {
public static Locale getInstance(Context context) {
if (INSTANCE == null) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
String language = prefs.getString(context.getString(R.string.p_language), null);
int directionOverride = Integer.parseInt(prefs.getString(context.getString(R.string.p_layout_direction), "-1"));
INSTANCE = new Locale(DEFAULT.getLocale(), language, directionOverride);
java.util.Locale.setDefault(INSTANCE.getLocale());
synchronized (DEFAULT) {
if (INSTANCE == null) {
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);
java.util.Locale.setDefault(INSTANCE.getLocale());
}
}
}
return getInstance();

Loading…
Cancel
Save