Skip to content

Commit ddb5834

Browse files
author
maskara
committed
Refresh nearby list on successful edit
1 parent b7c2e42 commit ddb5834

File tree

7 files changed

+75
-24
lines changed

7 files changed

+75
-24
lines changed

app/src/main/java/fr/free/nrw/commons/di/CommonsApplicationModule.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import fr.free.nrw.commons.mwapi.MediaWikiApi;
2525
import fr.free.nrw.commons.nearby.NearbyPlaces;
2626
import fr.free.nrw.commons.upload.UploadController;
27+
import fr.free.nrw.commons.wikidata.WikidataEditListener;
28+
import fr.free.nrw.commons.wikidata.WikidataEditListenerImpl;
2729

2830
import static android.content.Context.MODE_PRIVATE;
2931
import static fr.free.nrw.commons.contributions.ContributionsContentProvider.CONTRIBUTION_AUTHORITY;
@@ -165,4 +167,10 @@ public NearbyPlaces provideNearbyPlaces() {
165167
public LruCache<String, String> provideLruCache() {
166168
return new LruCache<>(1024);
167169
}
170+
171+
@Provides
172+
@Singleton
173+
public WikidataEditListener provideWikidataEditListener() {
174+
return new WikidataEditListenerImpl();
175+
}
168176
}

app/src/main/java/fr/free/nrw/commons/location/LocationServiceManager.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,7 @@ public enum LocationChangeType{
267267
LOCATION_SIGNIFICANTLY_CHANGED, //Went out of borders of nearby markers
268268
LOCATION_SLIGHTLY_CHANGED, //User might be walking or driving
269269
LOCATION_NOT_CHANGED,
270-
PERMISSION_JUST_GRANTED
270+
PERMISSION_JUST_GRANTED,
271+
MAP_UPDATED
271272
}
272273
}

app/src/main/java/fr/free/nrw/commons/mwapi/ApacheHttpClientMediaWikiApi.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -635,6 +635,7 @@ public UploadResult uploadFile(String filename,
635635
String resultStatus = result.getString("/api/upload/@result");
636636
if (!resultStatus.equals("Success")) {
637637
String errorCode = result.getString("/api/error/@code");
638+
Timber.e(errorCode);
638639
return new UploadResult(resultStatus, errorCode);
639640
} else {
640641
Date dateUploaded = parseMWDate(result.getString("/api/upload/imageinfo/@timestamp"));

app/src/main/java/fr/free/nrw/commons/nearby/NearbyActivity.java

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,16 @@
1515
import android.support.design.widget.BottomSheetBehavior;
1616
import android.support.v4.app.FragmentTransaction;
1717
import android.support.v7.app.AlertDialog;
18-
19-
import android.text.TextUtils;
2018
import android.view.Menu;
2119
import android.view.MenuInflater;
2220
import android.view.MenuItem;
2321
import android.view.View;
2422
import android.widget.LinearLayout;
2523
import android.widget.ProgressBar;
2624

27-
import android.widget.Toast;
2825
import com.google.gson.Gson;
2926
import com.google.gson.GsonBuilder;
3027

31-
import io.reactivex.functions.Consumer;
32-
import java.io.IOException;
33-
import java.net.ConnectException;
34-
import java.net.UnknownHostException;
3528
import java.util.List;
3629

3730
import javax.inject.Inject;
@@ -42,11 +35,13 @@
4235
import fr.free.nrw.commons.R;
4336
import fr.free.nrw.commons.location.LatLng;
4437
import fr.free.nrw.commons.location.LocationServiceManager;
38+
import fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType;
4539
import fr.free.nrw.commons.location.LocationUpdateListener;
4640
import fr.free.nrw.commons.theme.NavigationBaseActivity;
4741
import fr.free.nrw.commons.utils.NetworkUtils;
4842
import fr.free.nrw.commons.utils.UriSerializer;
4943
import fr.free.nrw.commons.utils.ViewUtil;
44+
import fr.free.nrw.commons.wikidata.WikidataEditListener;
5045
import io.reactivex.Observable;
5146
import io.reactivex.android.schedulers.AndroidSchedulers;
5247
import io.reactivex.disposables.Disposable;
@@ -55,8 +50,12 @@
5550
import uk.co.deanwild.materialshowcaseview.IShowcaseListener;
5651
import uk.co.deanwild.materialshowcaseview.MaterialShowcaseView;
5752

53+
import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.*;
54+
import static fr.free.nrw.commons.location.LocationServiceManager.LocationChangeType.MAP_UPDATED;
55+
5856

59-
public class NearbyActivity extends NavigationBaseActivity implements LocationUpdateListener {
57+
public class NearbyActivity extends NavigationBaseActivity implements LocationUpdateListener,
58+
WikidataEditListener.WikidataP18EditListener {
6059

6160
private static final int LOCATION_REQUEST = 1;
6261

@@ -76,6 +75,8 @@ public class NearbyActivity extends NavigationBaseActivity implements LocationUp
7675
LocationServiceManager locationManager;
7776
@Inject
7877
NearbyController nearbyController;
78+
@Inject WikidataEditListener wikidataEditListener;
79+
7980
@Inject
8081
@Named("application_preferences") SharedPreferences applicationPrefs;
8182
private LatLng curLatLng;
@@ -110,6 +111,7 @@ protected void onCreate(Bundle savedInstanceState) {
110111

111112
initBottomSheetBehaviour();
112113
initDrawer();
114+
wikidataEditListener.setAuthenticationStateListener(this);
113115
}
114116

115117
private void resumeFragment() {
@@ -219,7 +221,7 @@ public void onRequestPermissionsResult(int requestCode, @NonNull String[] permis
219221
//Still need to check if GPS is enabled
220222
checkGps();
221223
lastKnownLocation = locationManager.getLKL();
222-
refreshView(LocationServiceManager.LocationChangeType.PERMISSION_JUST_GRANTED);
224+
refreshView(PERMISSION_JUST_GRANTED);
223225
} else {
224226
//If permission not granted, go to page that says Nearby Places cannot be displayed
225227
hideProgressBar();
@@ -279,7 +281,7 @@ private void checkGps() {
279281
private void checkLocationPermission() {
280282
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
281283
if (locationManager.isLocationPermissionGranted()) {
282-
refreshView(LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED);
284+
refreshView(LOCATION_SIGNIFICANTLY_CHANGED);
283285
} else {
284286
// Should we show an explanation?
285287
if (locationManager.isPermissionExplanationRequired(this)) {
@@ -305,7 +307,7 @@ private void checkLocationPermission() {
305307
}
306308
}
307309
} else {
308-
refreshView(LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED);
310+
refreshView(LOCATION_SIGNIFICANTLY_CHANGED);
309311
}
310312
}
311313

@@ -314,7 +316,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
314316
super.onActivityResult(requestCode, resultCode, data);
315317
if (requestCode == 1) {
316318
Timber.d("User is back from Settings page");
317-
refreshView(LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED);
319+
refreshView(LOCATION_SIGNIFICANTLY_CHANGED);
318320
}
319321
}
320322

@@ -341,7 +343,7 @@ protected void onDestroy() {
341343
@Override
342344
protected void onResume() {
343345
super.onResume();
344-
lockNearbyView = true;
346+
lockNearbyView = false;
345347
checkGps();
346348
addNetworkBroadcastReceiver();
347349
}
@@ -373,8 +375,7 @@ private void addNetworkBroadcastReceiver() {
373375
@Override
374376
public void onReceive(Context context, Intent intent) {
375377
if (NetworkUtils.isInternetConnectionEstablished(NearbyActivity.this)) {
376-
refreshView(LocationServiceManager
377-
.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED);
378+
refreshView(LOCATION_SIGNIFICANTLY_CHANGED);
378379
} else {
379380
ViewUtil.showLongToast(NearbyActivity.this, getString(R.string.no_internet));
380381
}
@@ -390,7 +391,7 @@ public void onReceive(Context context, Intent intent) {
390391
*
391392
* @param locationChangeType defines if location shanged significantly or slightly
392393
*/
393-
private void refreshView(LocationServiceManager.LocationChangeType locationChangeType) {
394+
private void refreshView(LocationChangeType locationChangeType) {
394395
if (lockNearbyView) {
395396
return;
396397
}
@@ -403,12 +404,13 @@ private void refreshView(LocationServiceManager.LocationChangeType locationChang
403404
locationManager.registerLocationManager();
404405
LatLng lastLocation = locationManager.getLastLocation();
405406

406-
if (curLatLng != null && curLatLng.equals(lastLocation)) { //refresh view only if location has changed
407+
if (curLatLng != null && curLatLng.equals(lastLocation)
408+
&& !locationChangeType.equals(MAP_UPDATED)) { //refresh view only if location has changed
407409
return;
408410
}
409411
curLatLng = lastLocation;
410412

411-
if (locationChangeType.equals(LocationServiceManager.LocationChangeType.PERMISSION_JUST_GRANTED)) {
413+
if (locationChangeType.equals(PERMISSION_JUST_GRANTED)) {
412414
curLatLng = lastKnownLocation;
413415
}
414416

@@ -417,8 +419,9 @@ private void refreshView(LocationServiceManager.LocationChangeType locationChang
417419
return;
418420
}
419421

420-
if (locationChangeType.equals(LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED)
421-
|| locationChangeType.equals(LocationServiceManager.LocationChangeType.PERMISSION_JUST_GRANTED)) {
422+
if (locationChangeType.equals(LOCATION_SIGNIFICANTLY_CHANGED)
423+
|| locationChangeType.equals(PERMISSION_JUST_GRANTED)
424+
|| locationChangeType.equals(MAP_UPDATED)) {
422425
progressBar.setVisibility(View.VISIBLE);
423426

424427
//TODO: This hack inserts curLatLng before populatePlaces is called (see #1440). Ideally a proper fix should be found
@@ -440,7 +443,7 @@ private void refreshView(LocationServiceManager.LocationChangeType locationChang
440443
progressBar.setVisibility(View.GONE);
441444
});
442445
} else if (locationChangeType
443-
.equals(LocationServiceManager.LocationChangeType.LOCATION_SLIGHTLY_CHANGED)) {
446+
.equals(LOCATION_SLIGHTLY_CHANGED)) {
444447
Gson gson = new GsonBuilder()
445448
.registerTypeAdapter(Uri.class, new UriSerializer())
446449
.create();
@@ -652,12 +655,12 @@ private void setListFragment() {
652655

653656
@Override
654657
public void onLocationChangedSignificantly(LatLng latLng) {
655-
refreshView(LocationServiceManager.LocationChangeType.LOCATION_SIGNIFICANTLY_CHANGED);
658+
refreshView(LOCATION_SIGNIFICANTLY_CHANGED);
656659
}
657660

658661
@Override
659662
public void onLocationChangedSlightly(LatLng latLng) {
660-
refreshView(LocationServiceManager.LocationChangeType.LOCATION_SLIGHTLY_CHANGED);
663+
refreshView(LOCATION_SLIGHTLY_CHANGED);
661664
}
662665

663666
public void prepareViewsForSheetPosition(int bottomSheetState) {
@@ -667,4 +670,9 @@ public void prepareViewsForSheetPosition(int bottomSheetState) {
667670
private void showErrorMessage(String message) {
668671
ViewUtil.showLongToast(NearbyActivity.this, message);
669672
}
673+
674+
@Override
675+
public void onWikidataEditSuccessful() {
676+
refreshView(MAP_UPDATED);
677+
}
670678
}

app/src/main/java/fr/free/nrw/commons/upload/UploadService.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import fr.free.nrw.commons.mwapi.MediaWikiApi;
3838
import fr.free.nrw.commons.mwapi.UploadResult;
3939
import fr.free.nrw.commons.utils.ViewUtil;
40+
import fr.free.nrw.commons.wikidata.WikidataEditListener;
4041
import io.reactivex.Observable;
4142
import io.reactivex.android.schedulers.AndroidSchedulers;
4243
import io.reactivex.schedulers.Schedulers;
@@ -53,6 +54,7 @@ public class UploadService extends HandlerService<Contribution> {
5354
public static final String EXTRA_CAMPAIGN = EXTRA_PREFIX + ".campaign";
5455

5556
@Inject MediaWikiApi mwApi;
57+
@Inject WikidataEditListener wikidataEditListener;
5658
@Inject SessionManager sessionManager;
5759
@Inject @Named("default_preferences") SharedPreferences prefs;
5860
@Inject ContributionDao contributionDao;
@@ -295,6 +297,7 @@ private void editWikidataProperty(Contribution contribution) {
295297
.observeOn(AndroidSchedulers.mainThread())
296298
.subscribe(result -> {
297299
if(result) {
300+
wikidataEditListener.onSuccessfulWikidataEdit();
298301
ViewUtil.showLongToast(getBaseContext(), "Image added corresponding to wiki data entity successfully!");
299302
} else {
300303
Timber.d("Unable to make wiki data edit for entity %s", contribution.getWikiDataEntityId());
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package fr.free.nrw.commons.wikidata;
2+
3+
public abstract class WikidataEditListener {
4+
5+
protected WikidataP18EditListener wikidataP18EditListener;
6+
7+
public abstract void onSuccessfulWikidataEdit();
8+
9+
public void setAuthenticationStateListener(WikidataP18EditListener wikidataP18EditListener) {
10+
this.wikidataP18EditListener = wikidataP18EditListener;
11+
}
12+
13+
public interface WikidataP18EditListener {
14+
void onWikidataEditSuccessful();
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package fr.free.nrw.commons.wikidata;
2+
3+
public class WikidataEditListenerImpl extends WikidataEditListener {
4+
5+
public WikidataEditListenerImpl() {
6+
}
7+
8+
@Override
9+
public void onSuccessfulWikidataEdit() {
10+
if (wikidataP18EditListener != null) {
11+
wikidataP18EditListener.onWikidataEditSuccessful();
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)