From e90abce0e97f809b666eeb00a34645b66e72fc26 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 12 Jul 2017 16:03:24 +0300 Subject: [PATCH 1/4] Fix #4086 --- .../search/QuickSearchDialogFragment.java | 71 ++++++++++++------- 1 file changed, 47 insertions(+), 24 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 36666db1ae..2f1412d7a0 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -93,6 +93,7 @@ import net.osmand.util.MapUtils; import java.io.File; import java.io.IOException; +import java.lang.ref.WeakReference; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; @@ -456,30 +457,11 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC new OnClickListener() { @Override public void onClick(View v) { - - new DialogFragment() { - @NonNull - @Override - public Dialog onCreateDialog(Bundle savedInstanceState) { - AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); - builder.setTitle(R.string.confirmation_to_delete_history_items) - .setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialog, int which) { - SearchHistoryHelper helper = SearchHistoryHelper.getInstance(app); - List selectedItems = historySearchFragment.getListAdapter().getSelectedItems(); - for (QuickSearchListItem searchListItem : selectedItems) { - HistoryEntry historyEntry = (HistoryEntry) searchListItem.getSearchResult().object; - helper.remove(historyEntry); - } - reloadHistory(); - enableSelectionMode(false, -1); - } - }) - .setNegativeButton(R.string.shared_string_no, null); - return builder.create(); - } - }.show(getChildFragmentManager(), "DeleteHistoryConfirmationFragment"); + DeleteDialogFragment deleteDialog = new DeleteDialogFragment(); + deleteDialog.setApp(app); + deleteDialog.setHistorySearchFragment(historySearchFragment); + deleteDialog.setQuickSearchDialogFragment(QuickSearchDialogFragment.this); + deleteDialog.show(getChildFragmentManager(), "DeleteHistoryConfirmationFragment"); } } ); @@ -2108,4 +2090,45 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC super(TopToolbarControllerType.QUICK_SEARCH); } } + + public static class DeleteDialogFragment extends DialogFragment { + + private WeakReference app; + private WeakReference quickSearchDialogFragment; + private WeakReference historySearchFragment; + + public void setApp(OsmandApplication app) { + this.app = new WeakReference<>(app); + } + + public void setQuickSearchDialogFragment(QuickSearchDialogFragment quickSearchDialogFragment) { + this.quickSearchDialogFragment = new WeakReference<>(quickSearchDialogFragment); + } + + public void setHistorySearchFragment(QuickSearchHistoryListFragment historySearchFragment) { + this.historySearchFragment = new WeakReference<>(historySearchFragment); + } + + @NonNull + @Override + public Dialog onCreateDialog(Bundle savedInstanceState) { + AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); + builder.setTitle(R.string.confirmation_to_delete_history_items) + .setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { + @Override + public void onClick(DialogInterface dialog, int which) { + SearchHistoryHelper helper = SearchHistoryHelper.getInstance(app.get()); + List selectedItems = historySearchFragment.get().getListAdapter().getSelectedItems(); + for (QuickSearchListItem searchListItem : selectedItems) { + HistoryEntry historyEntry = (HistoryEntry) searchListItem.getSearchResult().object; + helper.remove(historyEntry); + } + quickSearchDialogFragment.get().reloadHistory(); + quickSearchDialogFragment.get().enableSelectionMode(false, -1); + } + }) + .setNegativeButton(R.string.shared_string_no, null); + return builder.create(); + } + } } From f89b12bd87a8905f1d7e7467ce3fbf72a947b7b3 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 12 Jul 2017 16:51:34 +0300 Subject: [PATCH 2/4] Fix #4086 v2 --- .../search/QuickSearchDialogFragment.java | 31 +++++++------------ 1 file changed, 11 insertions(+), 20 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 2f1412d7a0..50fb161ffd 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -93,7 +93,6 @@ import net.osmand.util.MapUtils; import java.io.File; import java.io.IOException; -import java.lang.ref.WeakReference; import java.text.MessageFormat; import java.util.ArrayList; import java.util.List; @@ -458,9 +457,8 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC @Override public void onClick(View v) { DeleteDialogFragment deleteDialog = new DeleteDialogFragment(); - deleteDialog.setApp(app); - deleteDialog.setHistorySearchFragment(historySearchFragment); - deleteDialog.setQuickSearchDialogFragment(QuickSearchDialogFragment.this); + deleteDialog.setSearchHistoryHelper(SearchHistoryHelper.getInstance(app)); + deleteDialog.setSelectedItems(historySearchFragment.getListAdapter().getSelectedItems()); deleteDialog.show(getChildFragmentManager(), "DeleteHistoryConfirmationFragment"); } } @@ -2093,20 +2091,15 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC public static class DeleteDialogFragment extends DialogFragment { - private WeakReference app; - private WeakReference quickSearchDialogFragment; - private WeakReference historySearchFragment; + private SearchHistoryHelper searchHistoryHelper; + private List selectedItems; - public void setApp(OsmandApplication app) { - this.app = new WeakReference<>(app); + public void setSearchHistoryHelper(SearchHistoryHelper searchHistoryHelper) { + this.searchHistoryHelper = searchHistoryHelper; } - public void setQuickSearchDialogFragment(QuickSearchDialogFragment quickSearchDialogFragment) { - this.quickSearchDialogFragment = new WeakReference<>(quickSearchDialogFragment); - } - - public void setHistorySearchFragment(QuickSearchHistoryListFragment historySearchFragment) { - this.historySearchFragment = new WeakReference<>(historySearchFragment); + public void setSelectedItems(List selectedItems) { + this.selectedItems = selectedItems; } @NonNull @@ -2117,14 +2110,12 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC .setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - SearchHistoryHelper helper = SearchHistoryHelper.getInstance(app.get()); - List selectedItems = historySearchFragment.get().getListAdapter().getSelectedItems(); for (QuickSearchListItem searchListItem : selectedItems) { HistoryEntry historyEntry = (HistoryEntry) searchListItem.getSearchResult().object; - helper.remove(historyEntry); + searchHistoryHelper.remove(historyEntry); } - quickSearchDialogFragment.get().reloadHistory(); - quickSearchDialogFragment.get().enableSelectionMode(false, -1); + ((QuickSearchDialogFragment) getParentFragment()).reloadHistory(); + ((QuickSearchDialogFragment) getParentFragment()).enableSelectionMode(false, -1); } }) .setNegativeButton(R.string.shared_string_no, null); From 719760eed330bad1fb46a6d4a3fbd5b56282dcf4 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 12 Jul 2017 16:56:20 +0300 Subject: [PATCH 3/4] Add check --- .../net/osmand/plus/search/QuickSearchDialogFragment.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index 50fb161ffd..fcdba4e867 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -2114,8 +2114,10 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC HistoryEntry historyEntry = (HistoryEntry) searchListItem.getSearchResult().object; searchHistoryHelper.remove(historyEntry); } - ((QuickSearchDialogFragment) getParentFragment()).reloadHistory(); - ((QuickSearchDialogFragment) getParentFragment()).enableSelectionMode(false, -1); + if (getParentFragment() instanceof QuickSearchDialogFragment) { + ((QuickSearchDialogFragment) getParentFragment()).reloadHistory(); + ((QuickSearchDialogFragment) getParentFragment()).enableSelectionMode(false, -1); + } } }) .setNegativeButton(R.string.shared_string_no, null); From d4f94ae098c09d297ebbd7d290c001364f4c69c1 Mon Sep 17 00:00:00 2001 From: Alexander Sytnyk Date: Wed, 12 Jul 2017 17:18:58 +0300 Subject: [PATCH 4/4] Remove unnecessary filed --- .../search/QuickSearchDialogFragment.java | 20 ++++++++----------- 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java index fcdba4e867..95c3981cbb 100644 --- a/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java +++ b/OsmAnd/src/net/osmand/plus/search/QuickSearchDialogFragment.java @@ -457,7 +457,6 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC @Override public void onClick(View v) { DeleteDialogFragment deleteDialog = new DeleteDialogFragment(); - deleteDialog.setSearchHistoryHelper(SearchHistoryHelper.getInstance(app)); deleteDialog.setSelectedItems(historySearchFragment.getListAdapter().getSelectedItems()); deleteDialog.show(getChildFragmentManager(), "DeleteHistoryConfirmationFragment"); } @@ -2091,13 +2090,8 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC public static class DeleteDialogFragment extends DialogFragment { - private SearchHistoryHelper searchHistoryHelper; private List selectedItems; - public void setSearchHistoryHelper(SearchHistoryHelper searchHistoryHelper) { - this.searchHistoryHelper = searchHistoryHelper; - } - public void setSelectedItems(List selectedItems) { this.selectedItems = selectedItems; } @@ -2110,13 +2104,15 @@ public class QuickSearchDialogFragment extends DialogFragment implements OsmAndC .setPositiveButton(R.string.shared_string_yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - for (QuickSearchListItem searchListItem : selectedItems) { - HistoryEntry historyEntry = (HistoryEntry) searchListItem.getSearchResult().object; - searchHistoryHelper.remove(historyEntry); - } if (getParentFragment() instanceof QuickSearchDialogFragment) { - ((QuickSearchDialogFragment) getParentFragment()).reloadHistory(); - ((QuickSearchDialogFragment) getParentFragment()).enableSelectionMode(false, -1); + QuickSearchDialogFragment parentFragment = (QuickSearchDialogFragment) getParentFragment(); + SearchHistoryHelper helper = SearchHistoryHelper.getInstance(parentFragment.getMyApplication()); + for (QuickSearchListItem searchListItem : selectedItems) { + HistoryEntry historyEntry = (HistoryEntry) searchListItem.getSearchResult().object; + helper.remove(historyEntry); + } + parentFragment.reloadHistory(); + parentFragment.enableSelectionMode(false, -1); } } })