Use wrapper for interacting with shared preferences (#2288)

* Use wrapper for accessing shared preferences across the app

* Use Json kv store for storing place object

* Fix tests

* Fix test failure

* Fix UI tests
This commit is contained in:
Vivek Maskara 2019-01-14 00:13:21 +05:30 committed by Ashish Kumar
parent 1b7b909107
commit d4fa9cfa45
61 changed files with 908 additions and 585 deletions

View file

@ -7,7 +7,6 @@ import android.accounts.AccountManager;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.ColorRes;
@ -45,6 +44,7 @@ import fr.free.nrw.commons.WelcomeActivity;
import fr.free.nrw.commons.category.CategoryImagesActivity;
import fr.free.nrw.commons.contributions.MainActivity;
import fr.free.nrw.commons.di.ApplicationlessInjection;
import fr.free.nrw.commons.kvstore.BasicKvStore;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.theme.NavigationBaseActivity;
import fr.free.nrw.commons.ui.widget.HtmlTextView;
@ -67,8 +67,12 @@ public class LoginActivity extends AccountAuthenticatorActivity {
@Inject MediaWikiApi mwApi;
@Inject SessionManager sessionManager;
@Inject @Named("application_preferences") SharedPreferences prefs;
@Inject @Named("default_preferences") SharedPreferences defaultPrefs;
@Inject
@Named("application_preferences")
BasicKvStore applicationKvStore;
@Inject
@Named("default_preferences")
BasicKvStore defaultKvStore;
@BindView(R.id.loginButton) Button loginButton;
@BindView(R.id.signupButton) Button signupButton;
@ -95,16 +99,17 @@ public class LoginActivity extends AccountAuthenticatorActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTheme(Utils.isDarkTheme(this) ? R.style.DarkAppTheme : R.style.LightAppTheme);
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
ApplicationlessInjection
.getInstance(this.getApplicationContext())
.getCommonsApplicationComponent()
.inject(this);
boolean isDarkTheme = defaultKvStore.getBoolean("theme", false);
setTheme(isDarkTheme ? R.style.DarkAppTheme : R.style.LightAppTheme);
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
ButterKnife.bind(this);
@ -152,7 +157,7 @@ public class LoginActivity extends AccountAuthenticatorActivity {
* It redirects the user to Explore Activity.
*/
private void skipLogin() {
prefs.edit().putBoolean("login_skipped", true).apply();
applicationKvStore.putBoolean("login_skipped", true);
CategoryImagesActivity.startYourself(this, getString(R.string.title_activity_explore), FEATURED_IMAGES_CATEGORY);
finish();
@ -176,19 +181,19 @@ public class LoginActivity extends AccountAuthenticatorActivity {
@Override
protected void onResume() {
super.onResume();
if (prefs.getBoolean("firstrun", true)) {
if (applicationKvStore.getBoolean("firstrun", true)) {
WelcomeActivity.startYourself(this);
}
if (sessionManager.getCurrentAccount() != null
&& sessionManager.isUserLoggedIn()
&& sessionManager.getCachedAuthCookie() != null) {
prefs.edit().putBoolean("login_skipped", false).apply();
applicationKvStore.putBoolean("login_skipped", false);
sessionManager.revalidateAuthToken();
startMainActivity();
}
if (prefs.getBoolean("login_skipped", false)){
if (applicationKvStore.getBoolean("login_skipped", false)) {
skipLogin();
}

View file

@ -5,12 +5,12 @@ import android.accounts.AccountAuthenticatorResponse;
import android.accounts.AccountManager;
import android.content.ContentResolver;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import javax.annotation.Nullable;
import fr.free.nrw.commons.BuildConfig;
import fr.free.nrw.commons.kvstore.BasicKvStore;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import io.reactivex.Completable;
import io.reactivex.Observable;
@ -27,17 +27,17 @@ public class SessionManager {
private final Context context;
private final MediaWikiApi mediaWikiApi;
private Account currentAccount; // Unlike a savings account... ;-)
private SharedPreferences sharedPreferences;
private BasicKvStore defaultKvStore;
private static final String KEY_RAWUSERNAME = "rawusername";
private Bundle userdata = new Bundle();
public SessionManager(Context context,
MediaWikiApi mediaWikiApi,
SharedPreferences sharedPreferences) {
BasicKvStore defaultKvStore) {
this.context = context;
this.mediaWikiApi = mediaWikiApi;
this.currentAccount = null;
this.sharedPreferences = sharedPreferences;
this.defaultKvStore = defaultKvStore;
}
/**
@ -154,11 +154,11 @@ public class SessionManager {
}
public String getCachedAuthCookie() {
return sharedPreferences.getString("getAuthCookie", null);
return defaultKvStore.getString("getAuthCookie", null);
}
public boolean isUserLoggedIn() {
return sharedPreferences.getBoolean("isUserLoggedIn", false);
return defaultKvStore.getBoolean("isUserLoggedIn", false);
}
public void forceLogin(Context context) {