Skip to content

Commit 6978d00

Browse files
authored
Fix 4511 Update Nearby list according to chip state (commons-app#4605)
* Exists and Need Photo list updated * WLM filter added * Minor bug fix nad code convention * Code clean up * Renamed NeedsPhoto and isExists
1 parent 401f70c commit 6978d00

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

app/src/main/java/fr/free/nrw/commons/nearby/fragments/NearbyParentFragment.java

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@
119119
import java.io.IOException;
120120
import java.util.ArrayList;
121121
import java.util.Date;
122+
import java.util.Iterator;
122123
import java.util.List;
123124
import java.util.Locale;
124125
import java.util.concurrent.TimeUnit;
@@ -246,6 +247,10 @@ public class NearbyParentFragment extends CommonsDaggerSupportFragment
246247
* WLM URL
247248
*/
248249
public static final String WLM_URL = "https://commons.wikimedia.org/wiki/Commons:Mobile_app/Contributing_to_WLM_using_the_app";
250+
/**
251+
* Saves response of list of places for the first time
252+
*/
253+
private List<Place> places;
249254

250255
@NonNull
251256
public static NearbyParentFragment newInstance() {
@@ -636,6 +641,8 @@ private void initFilterChips() {
636641
checkBoxTriStates.setState(CheckBoxTriStates.UNKNOWN);
637642
NearbyFilterState.setNeedPhotoSelected(isChecked);
638643
presenter.filterByMarkerType(nearbyFilterSearchRecyclerViewAdapter.selectedLabels, checkBoxTriStates.getState(), true, false);
644+
updatePlaceList(chipNeedsPhoto.isChecked(),
645+
chipExists.isChecked(), chipWlm.isChecked());
639646
} else {
640647
chipNeedsPhoto.setChecked(!isChecked);
641648
}
@@ -647,6 +654,8 @@ private void initFilterChips() {
647654
checkBoxTriStates.setState(CheckBoxTriStates.UNKNOWN);
648655
NearbyFilterState.setExistsSelected(isChecked);
649656
presenter.filterByMarkerType(nearbyFilterSearchRecyclerViewAdapter.selectedLabels, checkBoxTriStates.getState(), true, false);
657+
updatePlaceList(chipNeedsPhoto.isChecked(),
658+
chipExists.isChecked(), chipWlm.isChecked());
650659
} else {
651660
chipExists.setChecked(!isChecked);
652661
}
@@ -658,12 +667,66 @@ private void initFilterChips() {
658667
checkBoxTriStates.setState(CheckBoxTriStates.UNKNOWN);
659668
NearbyFilterState.setWlmSelected(isChecked);
660669
presenter.filterByMarkerType(nearbyFilterSearchRecyclerViewAdapter.selectedLabels, checkBoxTriStates.getState(), true, false);
670+
updatePlaceList(chipNeedsPhoto.isChecked(),
671+
chipExists.isChecked(), chipWlm.isChecked());
661672
}else{
662673
chipWlm.setChecked(!isChecked);
663674
}
664675
});
665676
}
666677

678+
/**
679+
* Updates Nearby place list according to available chip states
680+
*
681+
* @param needsPhoto is chipNeedsPhoto checked
682+
* @param exists is chipExists checked
683+
* @param isWlm is chipWlm checked
684+
*/
685+
private void updatePlaceList(final boolean needsPhoto, final boolean exists,
686+
final boolean isWlm) {
687+
final List<Place> updatedPlaces = new ArrayList<>();
688+
689+
if (needsPhoto) {
690+
for (final Place place :
691+
places) {
692+
if (place.pic.trim().isEmpty() && !updatedPlaces.contains(place)) {
693+
updatedPlaces.add(place);
694+
}
695+
}
696+
} else {
697+
updatedPlaces.addAll(places);
698+
}
699+
700+
if (exists) {
701+
for(final Iterator<Place> placeIterator = updatedPlaces.iterator();
702+
placeIterator.hasNext();){
703+
final Place place = placeIterator.next();
704+
if (!place.exists) {
705+
placeIterator.remove();
706+
}
707+
}
708+
}
709+
710+
if (!isWlm) {
711+
for (final Place place :
712+
places) {
713+
if (place.isMonument() && updatedPlaces.contains(place)) {
714+
updatedPlaces.remove(place);
715+
}
716+
}
717+
} else {
718+
for (final Place place :
719+
places) {
720+
if (place.isMonument() && !updatedPlaces.contains(place)) {
721+
updatedPlaces.add(place);
722+
}
723+
}
724+
}
725+
726+
adapter.setItems(updatedPlaces);
727+
noResultsView.setVisibility(updatedPlaces.isEmpty() ? View.VISIBLE : View.GONE);
728+
}
729+
667730
/**
668731
* Defines how bottom sheets will act on click
669732
*/
@@ -785,6 +848,7 @@ public void centerMapToPlace(final Place place) {
785848

786849
@Override
787850
public void updateListFragment(final List<Place> placeList) {
851+
places = placeList;
788852
adapter.setItems(placeList);
789853
noResultsView.setVisibility(placeList.isEmpty() ? View.VISIBLE : View.GONE);
790854
}

0 commit comments

Comments
 (0)