Skip to content

Commit

Permalink
Keep map selection when searching (#3132)
Browse files Browse the repository at this point in the history
* #3092 keep map selection when searching if the string matches

* #3092 use biconsume subscriber to set map

* #3092 review actions, add two tests

---------

Co-authored-by: obydog002 <>
  • Loading branch information
obydog002 committed Feb 22, 2024
1 parent e3608b2 commit a5aca5d
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,13 @@ protected void initMapSelection() {
mapListView.getSelectionModel()
.selectedItemProperty()
.when(showing)
.subscribe(this::setSelectedMap);
.subscribe((oldItem, newItem) -> {
if (newItem == null && filteredMaps.contains(oldItem)) {
mapListView.getSelectionModel().select(oldItem);
} else {
setSelectedMap(newItem);
}
});

FilteredList<MapVersionBean> skirmishMaps = mapService.getInstalledMaps()
.filtered(mapVersion -> mapVersion.getMap().getMapType() == MapType.SKIRMISH);
Expand Down Expand Up @@ -501,7 +507,7 @@ public void setGamesRoot(StackPane root) {
}

/**
* @return returns true of the map was found and false if not
* @return returns true if the map was found and false if not
*/
boolean selectMap(String mapFolderName) {
Optional<MapVersionBean> mapBeanOptional = mapListView.getItems()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.faforever.client.theme.UiService;
import com.faforever.client.ui.dialog.Dialog;
import com.faforever.client.user.LoginService;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleBooleanProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.value.ObservableValue;
Expand All @@ -46,6 +47,7 @@
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiFunction;
import java.util.function.Predicate;

import static java.util.Arrays.asList;
import static java.util.Collections.singleton;
Expand Down Expand Up @@ -280,7 +282,6 @@ public void testButtonBindingIfNotConnecting() {
}

@Test
@Disabled("I will deal with this later")
public void testSelectLastMap() {
MapVersionBean lastMapBean = MapVersionBeanBuilder.create()
.defaultValues()
Expand Down Expand Up @@ -540,7 +541,7 @@ public void testOnGenerateMapClicked() {

@SuppressWarnings("unchecked")
@Test
public void testMapNameSearch() {
public void testMapNameSearchFilter() {
ArgumentCaptor<BiFunction<String, MapVersionBean, Boolean>> argumentCaptor = ArgumentCaptor.forClass(BiFunction.class);
verify(mapFilterController).addExternalFilter(any(ObservableValue.class),
argumentCaptor.capture());
Expand All @@ -558,4 +559,53 @@ public void testMapNameSearch() {
assertTrue(filter.apply("ua", mapVersionBean));
assertFalse(filter.apply("ap.v1000", mapVersionBean));
}

@Test
public void testMapNameSearchKeepsSelectedIfInMaps() {
ArgumentCaptor<BiFunction<String, MapVersionBean, Boolean>> argumentCaptor = ArgumentCaptor.forClass(BiFunction.class);
verify(mapFilterController).addExternalFilter(any(ObservableValue.class),
argumentCaptor.capture());
BiFunction<String, MapVersionBean, Boolean> filter = argumentCaptor.getValue();
ObjectProperty<Predicate<MapVersionBean>> predicate = mapFilterController.predicateProperty();
mapList.add(MapVersionBeanBuilder.create()
.defaultValues()
.map(MapBeanBuilder.create().defaultValues().displayName("Test1").get())
.get());

predicate.setValue((item) -> filter.apply("Test", item));
runOnFxThreadAndWait(() -> {
instance.mapListView.getSelectionModel().select(0);
instance.mapSearchTextField.setText("Test");
});

assertThat(instance.mapListView.getSelectionModel().getSelectedIndex(), is(0));

predicate.setValue((item) -> filter.apply("Test1", item));
runOnFxThreadAndWait(() -> {
instance.mapSearchTextField.setText("Test1");
});

assertThat(instance.mapListView.getSelectionModel().getSelectedIndex(), is(0));
}

@Test
public void testMapNameSearchClearsSelected() {
ArgumentCaptor<BiFunction<String, MapVersionBean, Boolean>> argumentCaptor = ArgumentCaptor.forClass(BiFunction.class);
verify(mapFilterController).addExternalFilter(any(ObservableValue.class),
argumentCaptor.capture());
BiFunction<String, MapVersionBean, Boolean> filter = argumentCaptor.getValue();
ObjectProperty<Predicate<MapVersionBean>> predicate = mapFilterController.predicateProperty();
mapList.add(MapVersionBeanBuilder.create()
.defaultValues()
.map(MapBeanBuilder.create().defaultValues().displayName("Test1").get())
.get());

predicate.setValue((item) -> filter.apply("Not in Filtered Maps", item));
runOnFxThreadAndWait(() -> {
instance.mapListView.getSelectionModel().select(0);
instance.mapSearchTextField.setText("Not in Filtered Maps");
});

assertThat(instance.mapListView.getSelectionModel().getSelectedIndex(), is(-1));
}
}

0 comments on commit a5aca5d

Please sign in to comment.