-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created a wrapper for shared preference logic (#43)
* Created a wrapper for shared preference logic * Added tests * Extracted the interface for app preferences * Upgraded the version of mockito-core
- Loading branch information
1 parent
236aada
commit dc8a056
Showing
10 changed files
with
267 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 0 additions & 27 deletions
27
app/src/androidTest/java/com/omkarmoghe/pokemap/ExampleInstrumentedTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
app/src/main/java/com/omkarmoghe/pokemap/app_preferences/PokemapAppPreferences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} |
52 changes: 52 additions & 0 deletions
52
app/src/main/java/com/omkarmoghe/pokemap/app_preferences/PokemapSharedPreferences.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, ""); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 0 additions & 18 deletions
18
app/src/test/java/com/omkarmoghe/pokemap/ExampleUnitTest.java
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
dc8a056
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Crashes opening "settings"
dc8a056
There was a problem hiding this comment.
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