Show wikidata edits done through the app (#2431)

* Show wikidata edits done through the app

* Minor changes based on code review
This commit is contained in:
Vivek Maskara 2019-02-12 22:28:22 +05:30 committed by Josephine Lim
parent aee8839eea
commit 73bb0c52c9
8 changed files with 138 additions and 18 deletions

View file

@ -3,7 +3,6 @@ package fr.free.nrw.commons.achievements;
import android.accounts.Account;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
@ -27,8 +26,6 @@ import android.widget.TextView;
import com.dinuscxj.progressbar.CircleProgressBar;
import org.w3c.dom.Text;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
@ -42,11 +39,10 @@ import butterknife.OnClick;
import fr.free.nrw.commons.R;
import fr.free.nrw.commons.Utils;
import fr.free.nrw.commons.auth.SessionManager;
import fr.free.nrw.commons.mwapi.MediaWikiApi;
import fr.free.nrw.commons.mwapi.OkHttpJsonApiClient;
import fr.free.nrw.commons.theme.NavigationBaseActivity;
import fr.free.nrw.commons.utils.StringUtils;
import fr.free.nrw.commons.utils.ViewUtil;
import io.reactivex.Single;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.schedulers.Schedulers;
@ -98,6 +94,10 @@ public class AchievementsActivity extends NavigationBaseActivity {
TextView imageRevertedText;
@BindView(R.id.images_upload_text_param)
TextView imageUploadedText;
@BindView(R.id.wikidata_edits)
TextView wikidataEditsText;
@Inject
SessionManager sessionManager;
@Inject
@ -138,6 +138,7 @@ public class AchievementsActivity extends NavigationBaseActivity {
hideLayouts();
setAchievements();
setWikidataEditCount();
initDrawer();
}
@ -229,6 +230,18 @@ public class AchievementsActivity extends NavigationBaseActivity {
}
}
@SuppressLint("CheckResult")
private void setWikidataEditCount() {
String userName = sessionManager.getUserName();
if (StringUtils.isNullOrWhiteSpace(userName)) {
return;
}
okHttpJsonApiClient.getWikidataEdits(userName)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(edits -> wikidataEditsText.setText(String.valueOf(edits)));
}
private void showSnackBarWithRetry() {
progressBar.setVisibility(View.GONE);
ViewUtil.showDismissibleSnackBar(findViewById(android.R.id.content),

View file

@ -34,7 +34,9 @@ public class NetworkingModule {
private static final String WIKIDATA_SPARQL_QUERY_URL = "https://query.wikidata.org/sparql";
private final String WIKIMEDIA_CAMPAIGNS_BASE_URL =
"https://raw.githubusercontent.com/commons-app/campaigns/master/campaigns.json";
private static final String TOOLS_FORGE_URL = "https://tools.wmflabs.org/";
private static final String TOOLS_FORGE_URL = "https://tools.wmflabs.org/urbanecmbot/commonsmisc";
private static final String TEST_TOOLS_FORGE_URL = "https://tools.wmflabs.org/commons-android-app/tool-commons-android-app";
public static final long OK_HTTP_CACHE_SIZE = 10 * 1024 * 1024;

View file

@ -26,6 +26,7 @@ import fr.free.nrw.commons.nearby.model.NearbyResponse;
import fr.free.nrw.commons.nearby.model.NearbyResultItem;
import fr.free.nrw.commons.upload.FileUtils;
import fr.free.nrw.commons.utils.DateUtils;
import fr.free.nrw.commons.wikidata.model.GetWikidataEditCountResponse;
import io.reactivex.Observable;
import io.reactivex.Single;
import okhttp3.HttpUrl;
@ -63,7 +64,7 @@ public class OkHttpJsonApiClient {
public Single<Integer> getUploadCount(String userName) {
HttpUrl.Builder urlBuilder = wikiMediaToolforgeUrl.newBuilder();
urlBuilder
.addPathSegments("urbanecmbot/commonsmisc/uploadsbyuser.py")
.addPathSegments("/uploadsbyuser.py")
.addQueryParameter("user", userName);
Request request = new Request.Builder()
.url(urlBuilder.build())
@ -72,12 +73,38 @@ public class OkHttpJsonApiClient {
return Single.fromCallable(() -> {
Response response = okHttpClient.newCall(request).execute();
if (response != null && response.isSuccessful()) {
return Integer.parseInt(response.body().string().trim());
}
return 0;
});
}
@NonNull
public Single<Integer> getWikidataEdits(String userName) {
HttpUrl.Builder urlBuilder = wikiMediaToolforgeUrl.newBuilder();
urlBuilder
.addPathSegments("/wikidataedits.py")
.addQueryParameter("user", userName);
Request request = new Request.Builder()
.url(urlBuilder.build())
.build();
return Single.fromCallable(() -> {
Response response = okHttpClient.newCall(request).execute();
if (response != null &&
response.isSuccessful() && response.body() != null) {
String json = response.body().string();
if (json == null) {
return 0;
}
GetWikidataEditCountResponse countResponse = gson.fromJson(json, GetWikidataEditCountResponse.class);
return countResponse.getWikidataEditCount();
}
return 0;
});
}
/**
* This takes userName as input, which is then used to fetch the feedback/achievements
* statistics using OkHttp and JavaRx. This function return JSONObject
@ -87,7 +114,7 @@ public class OkHttpJsonApiClient {
*/
public Single<FeedbackResponse> getAchievements(String userName) {
final String fetchAchievementUrlTemplate =
wikiMediaToolforgeUrl + "urbanecmbot/commonsmisc/feedback.py";
wikiMediaToolforgeUrl + "/feedback.py";
return Single.fromCallable(() -> {
String url = String.format(
Locale.ENGLISH,

View file

@ -0,0 +1,16 @@
package fr.free.nrw.commons.wikidata.model;
import com.google.gson.annotations.SerializedName;
public class GetWikidataEditCountResponse {
@SerializedName("edits")
private final int wikidataEditCount;
public GetWikidataEditCountResponse(int wikidataEditCount) {
this.wikidataEditCount = wikidataEditCount;
}
public int getWikidataEditCount() {
return wikidataEditCount;
}
}

View file

@ -0,0 +1,10 @@
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="@color/primaryColor"
android:pathData="M3,17.25V21h3.75L17.81,9.94l-3.75,-3.75L3,17.25zM20.71,7.04c0.39,-0.39 0.39,-1.02 0,-1.41l-2.34,-2.34c-0.39,-0.39 -1.02,-0.39 -1.41,0l-1.83,1.83 3.75,3.75 1.83,-1.83z"/>
</vector>

View file

@ -1,6 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
@ -254,6 +255,7 @@
<ImageView
android:layout_width="@dimen/overflow_icon_dimen"
android:layout_height="@dimen/overflow_icon_dimen"
android:layout_centerVertical="true"
android:id="@+id/featured_image_icon"
app:srcCompat="@drawable/featured" />
@ -264,6 +266,7 @@
android:layout_toRightOf="@+id/featured_image_icon"
android:layout_marginTop="@dimen/activity_margin_horizontal"
android:layout_marginStart="@dimen/activity_margin_horizontal"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/activity_margin_horizontal"
android:text="@string/statistics_featured" />
@ -272,13 +275,14 @@
android:layout_height="wrap_content"
style="?android:textAppearanceMedium"
android:layout_alignParentRight="true"
android:layout_marginEnd="44dp"
android:layout_marginTop="@dimen/activity_margin_horizontal"
android:layout_marginStart="@dimen/activity_margin_horizontal"
android:text="2"
android:layout_centerVertical="true"
tools:text="2"
android:id="@+id/image_featured"
android:layout_marginLeft="@dimen/activity_margin_horizontal"
android:layout_marginRight="44dp" />
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp" />
</RelativeLayout>
@ -286,14 +290,16 @@
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_margin_horizontal"
android:layout_marginEnd="@dimen/activity_margin_horizontal"
android:layout_marginRight="@dimen/activity_margin_horizontal"
android:layout_marginStart="@dimen/activity_margin_horizontal">
android:layout_marginStart="@dimen/activity_margin_horizontal"
android:layout_marginTop="@dimen/activity_margin_horizontal"
android:layout_marginEnd="@dimen/activity_margin_horizontal">
<ImageView
android:layout_width="@dimen/overflow_icon_dimen"
android:layout_height="@dimen/overflow_icon_dimen"
app:srcCompat="@drawable/ic_thanks"
android:layout_centerVertical="true"
android:id="@+id/thanks_image_icon"
android:scaleType="centerCrop" />
@ -304,6 +310,7 @@
android:layout_marginTop="@dimen/activity_margin_horizontal"
android:layout_toRightOf="@+id/thanks_image_icon"
android:layout_marginStart="@dimen/activity_margin_horizontal"
android:layout_centerVertical="true"
android:layout_marginLeft="@dimen/activity_margin_horizontal"
android:text="@string/statistics_thanks" />
@ -312,13 +319,57 @@
android:layout_height="wrap_content"
style="?android:textAppearanceMedium"
android:layout_alignParentRight="true"
android:layout_marginEnd="44dp"
android:layout_marginTop="@dimen/activity_margin_horizontal"
android:layout_marginStart="@dimen/activity_margin_horizontal"
android:layout_marginLeft="@dimen/activity_margin_horizontal"
android:text="2"
android:layout_centerVertical="true"
tools:text="2"
android:id="@+id/thanks_received"
android:layout_marginRight="44dp" />
android:layout_marginEnd="24dp"
android:layout_marginRight="24dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/activity_margin_horizontal"
android:layout_marginRight="@dimen/activity_margin_horizontal"
android:layout_marginEnd="@dimen/activity_margin_horizontal"
android:layout_marginStart="@dimen/activity_margin_horizontal"
android:layout_marginTop="@dimen/activity_margin_horizontal">
<ImageView
android:layout_width="@dimen/overflow_icon_dimen"
android:layout_height="@dimen/overflow_icon_dimen"
android:id="@+id/wikidata_edits_icon"
android:layout_centerVertical="true"
app:srcCompat="@drawable/edit_ascent_color" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:textAppearanceMedium"
android:layout_toRightOf="@+id/wikidata_edits_icon"
android:layout_marginTop="@dimen/activity_margin_horizontal"
android:layout_marginStart="@dimen/activity_margin_horizontal"
android:layout_marginLeft="@dimen/activity_margin_horizontal"
android:layout_centerVertical="true"
android:text="@string/statistics_wikidata_edits" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:textAppearanceMedium"
android:layout_alignParentRight="true"
android:layout_marginLeft="@dimen/activity_margin_horizontal"
android:layout_marginEnd="24dp"
android:layout_marginTop="@dimen/activity_margin_horizontal"
android:layout_marginStart="@dimen/activity_margin_horizontal"
android:layout_centerVertical="true"
tools:text="2"
android:id="@+id/wikidata_edits"
android:layout_marginRight="24dp" />
</RelativeLayout>
</LinearLayout>

View file

@ -2,8 +2,8 @@
<dimen name="bottom_peak_height">240dp</dimen>
<!-- Standard margin / padding for the containing activity -->
<dimen name="activity_margin_horizontal">16dp</dimen>
<dimen name="activity_margin_vertical">16dp</dimen>
<dimen name="activity_margin_horizontal">8dp</dimen>
<dimen name="activity_margin_vertical">8dp</dimen>
<dimen name="achievements_activity_margin_vertical">8dp</dimen>
<!-- Standard margins / padding -->

View file

@ -357,6 +357,7 @@
<string name="statistics">STATISTICS</string>
<string name="statistics_thanks">Thanks Received</string>
<string name="statistics_featured">Featured Images</string>
<string name="statistics_wikidata_edits">Images via \"Nearby Places\"</string>
<string name="level">LEVEL</string>
<string name="images_uploaded">Images Uploaded</string>
<string name="image_reverts">Images Not Reverted</string>