Skip to content

Commit

Permalink
Bring existing filter editor to front when trying to edit it twice
Browse files Browse the repository at this point in the history
While the second editor did nothing, it was still a sub-par UX. New
behavior is much friendlier.

Issue: #155
  • Loading branch information
mlopatkin committed Jan 9, 2022
1 parent 344cd5b commit 6ecf056
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 9 deletions.
13 changes: 12 additions & 1 deletion src/name/mlopatkin/andlogview/filters/MainFilterController.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import name.mlopatkin.andlogview.liblogcat.LogRecord;
import name.mlopatkin.andlogview.search.RequestCompilationException;
import name.mlopatkin.andlogview.ui.filterdialog.FilterDialogFactory;
import name.mlopatkin.andlogview.ui.filterdialog.FilterDialogHandle;
import name.mlopatkin.andlogview.ui.filterdialog.FilterFromDialog;
import name.mlopatkin.andlogview.ui.filterpanel.FilterCreator;
import name.mlopatkin.andlogview.ui.filterpanel.FilterPanelModel;
Expand All @@ -30,6 +31,7 @@
import name.mlopatkin.andlogview.utils.Threads;

import org.apache.log4j.Logger;
import org.checkerframework.checker.nullness.qual.Nullable;

import java.util.ArrayList;
import java.util.List;
Expand Down Expand Up @@ -212,13 +214,22 @@ public BaseToggleFilter<T> addToCollection() {
}

private class DialogPanelFilter extends BaseToggleFilter<FilterFromDialog> {
private @Nullable FilterDialogHandle dialogHandle;

protected DialogPanelFilter(FilterCollection<? super FilterFromDialog> collection, FilterFromDialog filter) {
super(collection, filter.getMode(), filter);
}

@Override
public void openFilterEditor() {
dialogFactory.startEditFilterDialog(this.filter).thenAccept(newFilter -> {
FilterDialogHandle currentDialogHandle = dialogHandle;
if (currentDialogHandle != null) {
currentDialogHandle.bringToFront();
return;
}
dialogHandle = currentDialogHandle = dialogFactory.startEditFilterDialog(this.filter);
currentDialogHandle.getResult().thenAccept(newFilter -> {
dialogHandle = null;
if (newFilter.isPresent()) {
DialogPanelFilter newPanelFilter = createDialogPanelFilter(newFilter.get());
replaceMeWith(newPanelFilter);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,8 @@ public void showError(String text) {
ErrorDialogsHelper.showError(this, text);
}

@Override
public void bringToFront() {
this.toFront();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ public CompletionStage<Optional<FilterFromDialog>> startCreateFilterDialogWithIn
return FilterDialogPresenter.create(dialogView, filter).show();
}

public CompletionStage<Optional<FilterFromDialog>> startEditFilterDialog(FilterFromDialog filter) {
public FilterDialogHandle startEditFilterDialog(FilterFromDialog filter) {
FilterDialog dialogView = filterDialogViewFactory.get();
dialogView.setTitle(EDIT_FILTER_DIALOG_TITLE);
return FilterDialogPresenter.create(dialogView, filter).show();
FilterDialogPresenter dialogPresenter = FilterDialogPresenter.create(dialogView, filter);
dialogPresenter.show();
return dialogPresenter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2022 the Andlogview authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package name.mlopatkin.andlogview.ui.filterdialog;

import java.util.Optional;
import java.util.concurrent.CompletionStage;

public interface FilterDialogHandle {
void bringToFront();

CompletionStage<Optional<FilterFromDialog>> getResult();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
/**
* Presenter for the FilterEditor dialog. Allows to open dialog to create new or edit existing filter.
*/
class FilterDialogPresenter {
class FilterDialogPresenter implements FilterDialogHandle {
/**
* Presenter talks to view through this interface.
*/
Expand Down Expand Up @@ -76,6 +76,8 @@ public interface FilterDialogView {
void hide();

void showError(String text);

void bringToFront();
}

private final FilterDialogView dialogView;
Expand Down Expand Up @@ -166,6 +168,17 @@ private void onDialogDiscard() {
editingPromise.complete(Optional.empty());
}

@Override
public void bringToFront() {
dialogView.bringToFront();
}

@Override
public CompletionStage<Optional<FilterFromDialog>> getResult() {
Preconditions.checkState(editingPromise != null, "Dialog is not shown");
return editingPromise;
}

public CompletionStage<Optional<FilterFromDialog>> show() {
Preconditions.checkState(editingPromise == null, "Dialog is already shown");
dialogView.show();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -37,6 +38,7 @@
import name.mlopatkin.andlogview.filters.MainFilterController.SavedFilterData;
import name.mlopatkin.andlogview.liblogcat.LogRecord;
import name.mlopatkin.andlogview.ui.filterdialog.FilterDialogFactory;
import name.mlopatkin.andlogview.ui.filterdialog.FilterDialogHandle;
import name.mlopatkin.andlogview.ui.filterdialog.FilterFromDialog;
import name.mlopatkin.andlogview.ui.filterpanel.FilterPanelModel;
import name.mlopatkin.andlogview.ui.filterpanel.PanelFilter;
Expand Down Expand Up @@ -151,10 +153,10 @@ public void testSavingAndInitializngFromSaved() throws Exception {

List<SavedFilterData> savedFilterData = savedFilterDataCaptor.getValue();

mockStorage = Mockito.mock(ConfigStorage.class);
mockStorage = mock(ConfigStorage.class);
when(mockStorage.loadConfig(Mockito.<FilterListSerializer>any())).thenReturn(savedFilterData);

filterPanelModel = Mockito.mock(FilterPanelModel.class);
filterPanelModel = mock(FilterPanelModel.class);
controller = new MainFilterController(
filterPanelModel, indexFilterCollection, dialogFactory, mockStorage, filterImpl);

Expand Down Expand Up @@ -191,8 +193,9 @@ private PanelFilter createFilterWithDialog(MainFilterController controller, Filt
}

private PanelFilter editFilterWithDialog(FilterFromDialog newFilter, PanelFilter oldFilter) {
when(dialogFactory.startEditFilterDialog(any())).thenReturn(
CompletableFuture.completedFuture(Optional.of(newFilter)));
FilterDialogHandle dialogHandle = mock(FilterDialogHandle.class);
when(dialogHandle.getResult()).thenReturn(CompletableFuture.completedFuture(Optional.of(newFilter)));
when(dialogFactory.startEditFilterDialog(any())).thenReturn(dialogHandle);
oldFilter.openFilterEditor();

order.verify(filterPanelModel).replaceFilter(eq(oldFilter), panelFilterCaptor.capture());
Expand All @@ -201,7 +204,9 @@ private PanelFilter editFilterWithDialog(FilterFromDialog newFilter, PanelFilter

private CompletableFuture<Optional<FilterFromDialog>> openFilterDialog(PanelFilter filter) {
CompletableFuture<Optional<FilterFromDialog>> future = new CompletableFuture<>();
when(dialogFactory.startEditFilterDialog(any())).thenReturn(future);
FilterDialogHandle dialogHandle = mock(FilterDialogHandle.class);
when(dialogHandle.getResult()).thenReturn(future);
when(dialogFactory.startEditFilterDialog(any())).thenReturn(dialogHandle);
filter.openFilterEditor();

return future;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public void showError(String text) {
errorText = text;
}

@Override
public void bringToFront() {
}

public void commit() {
commitAction.run();
}
Expand Down

0 comments on commit 6ecf056

Please sign in to comment.