mirror of
https://github.com/commons-app/apps-android-commons.git
synced 2025-10-29 22:03:55 +01:00
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:
parent
1b7b909107
commit
d4fa9cfa45
61 changed files with 908 additions and 585 deletions
|
|
@ -1,18 +1,15 @@
|
|||
package fr.free.nrw.commons.mwapi;
|
||||
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.net.Uri;
|
||||
import android.os.Build;
|
||||
import android.support.annotation.NonNull;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.support.annotation.VisibleForTesting;
|
||||
import android.text.TextUtils;
|
||||
import android.util.Log;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
|
||||
import fr.free.nrw.commons.campaigns.CampaignResponseDTO;
|
||||
import org.apache.http.HttpResponse;
|
||||
import org.apache.http.conn.ClientConnectionManager;
|
||||
import org.apache.http.conn.scheme.PlainSocketFactory;
|
||||
|
|
@ -45,12 +42,14 @@ import java.util.concurrent.Callable;
|
|||
import fr.free.nrw.commons.BuildConfig;
|
||||
import fr.free.nrw.commons.Media;
|
||||
import fr.free.nrw.commons.PageTitle;
|
||||
import fr.free.nrw.commons.achievements.FeaturedImages;
|
||||
import fr.free.nrw.commons.R;
|
||||
import fr.free.nrw.commons.achievements.FeaturedImages;
|
||||
import fr.free.nrw.commons.achievements.FeedbackResponse;
|
||||
import fr.free.nrw.commons.auth.AccountUtil;
|
||||
import fr.free.nrw.commons.campaigns.CampaignResponseDTO;
|
||||
import fr.free.nrw.commons.category.CategoryImageUtils;
|
||||
import fr.free.nrw.commons.category.QueryContinue;
|
||||
import fr.free.nrw.commons.kvstore.BasicKvStore;
|
||||
import fr.free.nrw.commons.notification.Notification;
|
||||
import fr.free.nrw.commons.notification.NotificationUtils;
|
||||
import fr.free.nrw.commons.utils.ContributionUtils;
|
||||
|
|
@ -64,6 +63,7 @@ import okhttp3.OkHttpClient;
|
|||
import okhttp3.Request;
|
||||
import okhttp3.Response;
|
||||
import timber.log.Timber;
|
||||
|
||||
import static fr.free.nrw.commons.utils.ContinueUtils.getQueryContinue;
|
||||
|
||||
/**
|
||||
|
|
@ -77,8 +77,8 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
private CustomMwApi api;
|
||||
private CustomMwApi wikidataApi;
|
||||
private Context context;
|
||||
private SharedPreferences defaultPreferences;
|
||||
private SharedPreferences categoryPreferences;
|
||||
private BasicKvStore defaultKvStore;
|
||||
private BasicKvStore categoryKvStore;
|
||||
private Gson gson;
|
||||
private final OkHttpClient okHttpClient;
|
||||
private final String WIKIMEDIA_CAMPAIGNS_BASE_URL =
|
||||
|
|
@ -89,8 +89,8 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
public ApacheHttpClientMediaWikiApi(Context context,
|
||||
String apiURL,
|
||||
String wikidatApiURL,
|
||||
SharedPreferences defaultPreferences,
|
||||
SharedPreferences categoryPreferences,
|
||||
BasicKvStore defaultKvStore,
|
||||
BasicKvStore categoryKvStore,
|
||||
Gson gson,
|
||||
OkHttpClient okHttpClient) {
|
||||
this.context = context;
|
||||
|
|
@ -108,8 +108,8 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
}
|
||||
api = new CustomMwApi(apiURL, httpClient);
|
||||
wikidataApi = new CustomMwApi(wikidatApiURL, httpClient);
|
||||
this.defaultPreferences = defaultPreferences;
|
||||
this.categoryPreferences = categoryPreferences;
|
||||
this.defaultKvStore = defaultKvStore;
|
||||
this.categoryKvStore = categoryKvStore;
|
||||
this.gson = gson;
|
||||
}
|
||||
|
||||
|
|
@ -201,15 +201,13 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
}
|
||||
|
||||
private void setAuthCookieOnLogin(boolean isLoggedIn) {
|
||||
SharedPreferences.Editor editor = defaultPreferences.edit();
|
||||
if (isLoggedIn) {
|
||||
editor.putBoolean("isUserLoggedIn", true);
|
||||
editor.putString("getAuthCookie", api.getAuthCookie());
|
||||
defaultKvStore.putBoolean("isUserLoggedIn", true);
|
||||
defaultKvStore.putString("getAuthCookie", api.getAuthCookie());
|
||||
} else {
|
||||
editor.putBoolean("isUserLoggedIn", false);
|
||||
editor.remove("getAuthCookie");
|
||||
defaultKvStore.putBoolean("isUserLoggedIn", false);
|
||||
defaultKvStore.remove("getAuthCookie");
|
||||
}
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
@ -369,7 +367,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
.get()
|
||||
.getNodes("/api/query/search/p/@title");
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain searchCategories", e);
|
||||
Timber.e(e, "Failed to obtain searchCategories");
|
||||
}
|
||||
|
||||
if (categoryNodes == null) {
|
||||
|
|
@ -401,7 +399,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
.get()
|
||||
.getNodes("/api/query/allcategories/c");
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain allCategories", e);
|
||||
Timber.e(e, "Failed to obtain allCategories");
|
||||
}
|
||||
|
||||
if (categoryNodes == null) {
|
||||
|
|
@ -515,7 +513,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
.get()
|
||||
.getNodes("/api/query/search/p/@title");
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain searchTitles", e);
|
||||
Timber.e(e, "Failed to obtain searchTitles");
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
|
|
@ -596,7 +594,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
.get()
|
||||
.getNode("/api/query/notifications/list");
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain searchCategories", e);
|
||||
Timber.e(e, "Failed to obtain searchCategories");
|
||||
}
|
||||
|
||||
if (notificationNode == null
|
||||
|
|
@ -633,7 +631,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
|
||||
apiResult = requestBuilder.get();
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain searchCategories", e);
|
||||
Timber.e(e, "Failed to obtain searchCategories");
|
||||
}
|
||||
|
||||
if (apiResult == null) {
|
||||
|
|
@ -673,7 +671,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
|
||||
apiResult = requestBuilder.get();
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain parent Categories", e);
|
||||
Timber.e(e, "Failed to obtain parent Categories");
|
||||
}
|
||||
|
||||
if (apiResult == null) {
|
||||
|
|
@ -723,7 +721,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
}
|
||||
apiResult = requestBuilder.get();
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain searchCategories", e);
|
||||
Timber.e(e, "Failed to obtain searchCategories");
|
||||
}
|
||||
|
||||
if (apiResult == null) {
|
||||
|
|
@ -775,7 +773,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
imageNodes= customApiResult.getNodes("/api/query/pages/page/@title");
|
||||
authorNodes= customApiResult.getNodes("/api/query/pages/page/imageinfo/ii/@user");
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain searchImages", e);
|
||||
Timber.e(e, "Failed to obtain searchImages");
|
||||
}
|
||||
|
||||
if (imageNodes == null) {
|
||||
|
|
@ -815,7 +813,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
.get()
|
||||
.getNodes("/api/query/search/p/@title");
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain searchCategories", e);
|
||||
Timber.e(e, "Failed to obtain searchCategories");
|
||||
}
|
||||
|
||||
if (categoryNodes == null) {
|
||||
|
|
@ -834,14 +832,12 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
/**
|
||||
* For APIs that return paginated responses, MediaWiki APIs uses the QueryContinue to facilitate fetching of subsequent pages
|
||||
* https://www.mediawiki.org/wiki/API:Raw_query_continue
|
||||
* After fetching images a page of image for a particular category, shared prefs are updated with the latest QueryContinue Values
|
||||
* After fetching images a page of image for a particular category, shared defaultKvStore are updated with the latest QueryContinue Values
|
||||
* @param keyword
|
||||
* @param queryContinue
|
||||
*/
|
||||
private void setQueryContinueValues(String keyword, QueryContinue queryContinue) {
|
||||
SharedPreferences.Editor editor = categoryPreferences.edit();
|
||||
editor.putString(keyword, gson.toJson(queryContinue));
|
||||
editor.apply();
|
||||
categoryKvStore.putString(keyword, gson.toJson(queryContinue));
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -851,7 +847,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
*/
|
||||
@Nullable
|
||||
private QueryContinue getQueryContinueValues(String keyword) {
|
||||
String queryContinueString = categoryPreferences.getString(keyword, null);
|
||||
String queryContinueString = categoryKvStore.getString(keyword, null);
|
||||
return gson.fromJson(queryContinueString, QueryContinue.class);
|
||||
}
|
||||
|
||||
|
|
@ -1038,7 +1034,7 @@ public class ApacheHttpClientMediaWikiApi implements MediaWikiApi {
|
|||
|
||||
apiResult = requestBuilder.get();
|
||||
} catch (IOException e) {
|
||||
Timber.e("Failed to obtain searchCategories", e);
|
||||
Timber.e(e, "Failed to obtain searchCategories");
|
||||
}
|
||||
|
||||
if (apiResult == null) {
|
||||
|
|
|
|||
|
|
@ -1,9 +1,9 @@
|
|||
package fr.free.nrw.commons.mwapi;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.Build;
|
||||
|
||||
import fr.free.nrw.commons.Utils;
|
||||
import fr.free.nrw.commons.kvstore.BasicKvStore;
|
||||
|
||||
public class EventLog {
|
||||
static final String DEVICE;
|
||||
|
|
@ -16,11 +16,11 @@ public class EventLog {
|
|||
}
|
||||
}
|
||||
|
||||
private static LogBuilder schema(String schema, long revision, MediaWikiApi mwApi, SharedPreferences prefs) {
|
||||
private static LogBuilder schema(String schema, long revision, MediaWikiApi mwApi, BasicKvStore prefs) {
|
||||
return new LogBuilder(schema, revision, mwApi, prefs);
|
||||
}
|
||||
|
||||
public static LogBuilder schema(Object[] scid, MediaWikiApi mwApi, SharedPreferences prefs) {
|
||||
public static LogBuilder schema(Object[] scid, MediaWikiApi mwApi, BasicKvStore prefs) {
|
||||
if (scid.length != 2) {
|
||||
throw new IllegalArgumentException("Needs an object array with schema as first param and revision as second");
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,5 @@
|
|||
package fr.free.nrw.commons.mwapi;
|
||||
|
||||
import android.content.SharedPreferences;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.Build;
|
||||
|
||||
|
|
@ -12,6 +11,7 @@ import java.net.URL;
|
|||
|
||||
import fr.free.nrw.commons.BuildConfig;
|
||||
import fr.free.nrw.commons.Utils;
|
||||
import fr.free.nrw.commons.kvstore.BasicKvStore;
|
||||
import fr.free.nrw.commons.settings.Prefs;
|
||||
|
||||
@SuppressWarnings("WeakerAccess")
|
||||
|
|
@ -20,7 +20,7 @@ public class LogBuilder {
|
|||
private final JSONObject data;
|
||||
private final long rev;
|
||||
private final String schema;
|
||||
private final SharedPreferences prefs;
|
||||
private final BasicKvStore prefs;
|
||||
|
||||
/**
|
||||
* Main constructor of LogBuilder
|
||||
|
|
@ -30,7 +30,7 @@ public class LogBuilder {
|
|||
* @param mwApi Wiki media API instance
|
||||
* @param prefs Instance of SharedPreferences
|
||||
*/
|
||||
LogBuilder(String schema, long revision, MediaWikiApi mwApi, SharedPreferences prefs) {
|
||||
LogBuilder(String schema, long revision, MediaWikiApi mwApi, BasicKvStore prefs) {
|
||||
this.prefs = prefs;
|
||||
this.data = new JSONObject();
|
||||
this.schema = schema;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue