Skip to content

Commit

Permalink
Allowing Named shared preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
dlemures committed Jul 18, 2016
1 parent 8f35d9f commit 8ce965c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 5 deletions.
Expand Up @@ -55,14 +55,18 @@

public class SmoothieApplicationModule extends Module {
public SmoothieApplicationModule(Application application) {
this(application, null);
}

public SmoothieApplicationModule(Application application, String preferencesName) {
bind(Application.class).toInstance(application);
bind(AccountManager.class).toProviderInstance(new AccountManagerProvider(application));
bind(AssetManager.class).toProviderInstance(new AssetManagerProvider(application));
bind(ContentResolver.class).toProviderInstance(new ContentResolverProvider(application));
bind(Handler.class).toProviderInstance(new HandlerProvider());
bind(PackageManager.class).toProviderInstance(new PackageManagerProvider(application));
bind(Resources.class).toProviderInstance(new ResourcesProvider(application));
bind(SharedPreferences.class).toProviderInstance(new SharedPreferencesProvider(application));
bind(SharedPreferences.class).toProviderInstance(new SharedPreferencesProvider(application, preferencesName));
bindSystemServices(application);
bindPackageInfo(application);
}
Expand Down
@@ -1,21 +1,31 @@
package toothpick.smoothie.provider;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import javax.inject.Inject;
import javax.inject.Provider;

public class SharedPreferencesProvider implements Provider<SharedPreferences> {
Application application;
String preferencesName;

@Inject
public SharedPreferencesProvider(Application application) {
this(application, null);
}

public SharedPreferencesProvider(Application application, String preferencesName) {
this.application = application;
this.preferencesName = preferencesName;
}

@Override
public SharedPreferences get() {
if (preferencesName != null) {
return application.getSharedPreferences(preferencesName, Context.MODE_PRIVATE);
}
return PreferenceManager.getDefaultSharedPreferences(application);
}
}
@@ -1,14 +1,14 @@
package toothpick.smoothie.module;

import android.accounts.AccountManager;
import android.app.Activity;
import android.app.ActivityManager;
import android.app.AlarmManager;
import android.app.Application;
import android.app.DownloadManager;
import android.app.KeyguardManager;
import android.app.NotificationManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.AssetManager;
import android.content.res.Resources;
Expand All @@ -19,12 +19,12 @@
import android.os.Handler;
import android.os.PowerManager;
import android.os.Vibrator;
import android.preference.PreferenceManager;
import android.telephony.TelephonyManager;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import toothpick.Scope;
Expand All @@ -40,7 +40,6 @@ public class SmoothieApplicationModuleTest {
@Test
public void testModule_shouldReturnApplicationBindings() throws Exception {
//GIVEN
Activity activity = Robolectric.buildActivity(Activity.class).create().get();
Application application = RuntimeEnvironment.application;
Scope appScope = Toothpick.openScope(application);
appScope.installModules(new SmoothieApplicationModule(application));
Expand All @@ -67,7 +66,6 @@ public void testModule_shouldReturnApplicationBindings() throws Exception {
@Test
public void testModule_shouldReturnSystemServices() throws Exception {
//GIVEN
Activity activity = Robolectric.buildActivity(Activity.class).create().get();
Application application = RuntimeEnvironment.application;
Scope appScope = Toothpick.openScope(application);
appScope.installModules(new SmoothieApplicationModule(application));
Expand Down Expand Up @@ -108,4 +106,43 @@ public void testModule_shouldReturnSystemServices() throws Exception {
assertThat(audioManager, notNullValue());
assertThat(downloadManager, notNullValue());
}

@Test
public void testModule_shouldReturnDefaultSharedPreferences() throws Exception {
//GIVEN
Application application = RuntimeEnvironment.application;

String itemKey = "isValid";
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(application);
sharedPreferences.edit().putBoolean(itemKey, true).commit();

Scope appScope = Toothpick.openScope(application);
appScope.installModules(new SmoothieApplicationModule(application));

//WHEN
SharedPreferences sharedPreferencesFromScope = appScope.getInstance(SharedPreferences.class);

//THEN
assertThat(sharedPreferencesFromScope.getBoolean(itemKey, false), is(true));
}

@Test
public void testModule_shouldReturnNamedSharedPreferences() throws Exception {
//GIVEN
Application application = RuntimeEnvironment.application;

String sharedPreferencesName = "test";
String itemKey = "isValid";
SharedPreferences sharedPreferences = application.getSharedPreferences(sharedPreferencesName, Context.MODE_PRIVATE);
sharedPreferences.edit().putBoolean(itemKey, true).commit();

Scope appScope = Toothpick.openScope(application);
appScope.installModules(new SmoothieApplicationModule(application, sharedPreferencesName));

//WHEN
SharedPreferences sharedPreferencesFromScope = appScope.getInstance(SharedPreferences.class);

//THEN
assertThat(sharedPreferencesFromScope.getBoolean(itemKey, false), is(true));
}
}

0 comments on commit 8ce965c

Please sign in to comment.