Skip to content

Commit

Permalink
Created a wrapper for shared preference logic (#43)
Browse files Browse the repository at this point in the history
* Created a wrapper for shared preference logic

* Added tests

* Extracted the interface for app preferences

* Upgraded the version of mockito-core
  • Loading branch information
CanOrhan authored and omkarmoghe committed Jul 22, 2016
1 parent 236aada commit dc8a056
Show file tree
Hide file tree
Showing 10 changed files with 267 additions and 84 deletions.
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ android {
lintOptions {
abortOnError false
}
testOptions {
unitTests.returnDefaultValues = true
}
}

protobuf {
Expand Down Expand Up @@ -54,4 +57,8 @@ dependencies {
compile 'com.squareup.retrofit2:converter-gson:2.0.0'
compile 'com.squareup.okhttp:logging-interceptor:2.6.0'
testCompile 'junit:junit:4.12'
testCompile 'org.mockito:mockito-core:1.10.19'
testCompile 'org.powermock:powermock-module-junit4:1.6.5'
testCompile 'org.powermock:powermock-api-mockito:1.6.5'

}

This file was deleted.

44 changes: 10 additions & 34 deletions app/src/main/java/com/omkarmoghe/pokemap/MainActivity.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,11 @@
package com.omkarmoghe.pokemap;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
Expand All @@ -17,30 +14,26 @@
import com.omkarmoghe.pokemap.login.RequestCredentialsDialogFragment;
import com.omkarmoghe.pokemap.map.MapWrapperFragment;
import com.omkarmoghe.pokemap.settings.SettingsActivity;
import com.omkarmoghe.pokemap.app_preferences.PokemapAppPreferences;
import com.omkarmoghe.pokemap.app_preferences.PokemapSharedPreferences;

public class MainActivity extends BaseActivity {
private static final String TAG = "Pokemap";

public static final String TAG = "Pokemap";

// fragments
private MapWrapperFragment mMapWrapperFragment;

// Preferences
SharedPreferences pref;
private PokemapAppPreferences pref;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

pref = PreferenceManager.getDefaultSharedPreferences(this);
pref = new PokemapSharedPreferences(this);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

mMapWrapperFragment = MapWrapperFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.main_container, mMapWrapperFragment)
transaction.replace(R.id.main_container, MapWrapperFragment.newInstance())
.addToBackStack(null)
.commit();
login();
Expand All @@ -67,15 +60,10 @@ public boolean onOptionsItemSelected(MenuItem item) {


private void login() {

String username = pref.getString(getString(R.string.pref_username), "");
String password = pref.getString(getString(R.string.pref_password), "");

if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
if (!pref.isUsernameSet() || !pref.isPasswordSet()) {
requestLoginCredentials();
} else {
Log.d(TAG, "Username: " + username);
nianticManager.login(username, password, this);
nianticManager.login(pref.getUsername(), pref.getPassword(), this);
}
}

Expand All @@ -84,24 +72,13 @@ private void requestLoginCredentials() {
new RequestCredentialsDialogFragment.Listener() {
@Override
public void credentialsIntroduced(String username, String password) {
pref.edit().putString(getString(R.string.pref_username), username).apply();
pref.edit().putString(getString(R.string.pref_password), password).apply();

pref.setUsername(username);
pref.setPassword(password);
login();
}
}), "request_credentials").commit();
}

@Override
protected void onStart() {
super.onStart();
}

@Override
protected void onStop() {
super.onStop();
}

@Override
public void onBackPressed() {
this.finish();
Expand All @@ -117,6 +94,5 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
}
break;
}
//super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.omkarmoghe.pokemap.app_preferences;

import android.support.annotation.NonNull;

/**
* A contract which defines a user's app preferences
*/
public interface PokemapAppPreferences {
/**
* @return true if the username has been set
*/
boolean isUsernameSet();

/**
* @return true if password has been set
*/
boolean isPasswordSet();

/**
* @return the username stored or an empty @see java.lang.String
*/
String getUsername();

/**
* @param username that should be set
*/
void setUsername(@NonNull String username);

/**
* @param password that should be set
*/
void setPassword(@NonNull String password);

/**
* @return the password stored or an empty @see java.lang.String
*/
String getPassword();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package com.omkarmoghe.pokemap.app_preferences;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;

/**
* Provide convenience methods to access shared preferences
*/

public final class PokemapSharedPreferences implements PokemapAppPreferences {
private static final String USERNAME_KEY = "UsernameKey";
private static final String PASSWORD_KEY = "PasswordKey";

private final SharedPreferences sharedPreferences;

public PokemapSharedPreferences(@NonNull Context context) {
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}

@Override
public boolean isUsernameSet() {
return sharedPreferences.contains(USERNAME_KEY);
}

@Override
public boolean isPasswordSet() {
return sharedPreferences.contains(PASSWORD_KEY);
}


@Override
public String getUsername() {
return sharedPreferences.getString(USERNAME_KEY, "");
}

@Override
public void setUsername(@NonNull String username) {
sharedPreferences.edit().putString(USERNAME_KEY, username).apply();
}

@Override
public void setPassword(@NonNull String password) {
sharedPreferences.edit().putString(PASSWORD_KEY, password).apply();
}

@Override
public String getPassword() {
return sharedPreferences.getString(PASSWORD_KEY, "");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@

public class SettingsActivity extends AppCompatActivity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);

// Set toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

if(getSupportActionBar() != null)
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(R.id.settings_content, new SettingsFragment())
.commit();
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
<string name="pref_warning">NOTE: Do NOT use your main account for this application!
Instead, create and use a fake Pokémon Trainer Club account with a different password and email.</string>
<string name="pref_username">Set Username</string>
<string name="pref_username_key">UsernameKey</string>
<string name="pref_default_username">John Doe</string>
<string name="pref_password">Set Password</string>
<string name="pref_password_key">PasswordKey</string>
<string name="pref_default_password">***</string>

<string name="pref_cat_general">General</string>
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/res/xml/preferences.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
android:summary="@string/pref_warning" />

<EditTextPreference
android:key="@string/pref_username"
android:key="@string/pref_username_key"
android:title="@string/pref_username"
android:summary="@string/pref_default_username"/>

<EditTextPreference
android:key="@string/pref_password"
android:key="@string/pref_password_key"
android:title="@string/pref_password"
android:summary="@string/pref_default_password"
android:inputType="textPassword" />
Expand Down
18 changes: 0 additions & 18 deletions app/src/test/java/com/omkarmoghe/pokemap/ExampleUnitTest.java

This file was deleted.

Loading

2 comments on commit dc8a056

@Silenc3IsGold3n
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Crashes opening "settings"

@Mahonster
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also broke updating Username summary on change

Please sign in to comment.