diff options
author | Christian Schneppe <christian@pix-art.de> | 2018-05-02 21:56:35 +0200 |
---|---|---|
committer | Christian Schneppe <christian@pix-art.de> | 2018-05-02 21:56:35 +0200 |
commit | 3dd18f9567ba42feee0566cf1b22632110251009 (patch) | |
tree | 473bd4294e28af83b32de51870865b2f14922de0 /src/main/java/de/pixart/messenger/ui | |
parent | 553755d757b381b047b702f715247f2e50b03c4a (diff) |
persist search across device rotations
Diffstat (limited to 'src/main/java/de/pixart/messenger/ui')
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/SearchActivity.java | 42 | ||||
-rw-r--r-- | src/main/java/de/pixart/messenger/ui/util/ChangeWatcher.java | 3 |
2 files changed, 40 insertions, 5 deletions
diff --git a/src/main/java/de/pixart/messenger/ui/SearchActivity.java b/src/main/java/de/pixart/messenger/ui/SearchActivity.java index 7ce4ddc98..f7e9ccd68 100644 --- a/src/main/java/de/pixart/messenger/ui/SearchActivity.java +++ b/src/main/java/de/pixart/messenger/ui/SearchActivity.java @@ -60,6 +60,7 @@ import de.pixart.messenger.ui.util.Color; import de.pixart.messenger.ui.util.DateSeparator; import de.pixart.messenger.ui.util.Drawable; import de.pixart.messenger.ui.util.ListViewUtils; +import de.pixart.messenger.ui.util.PendingItem; import de.pixart.messenger.ui.util.ShareUtil; import de.pixart.messenger.utils.FtsUtils; import de.pixart.messenger.utils.MessageUtils; @@ -69,15 +70,22 @@ import static de.pixart.messenger.ui.util.SoftKeyboardUtils.showKeyboard; public class SearchActivity extends XmppActivity implements TextWatcher, OnSearchResultsAvailable, MessageAdapter.OnContactPictureClicked { + private static final String EXTRA_SEARCH_TERM = "search-term"; private ActivitySearchBinding binding; private MessageAdapter messageListAdapter; private final List<Message> messages = new ArrayList<>(); private WeakReference<Message> selectedMessageReference = new WeakReference<>(null); private final ChangeWatcher<List<String>> currentSearch = new ChangeWatcher<>(); + private final PendingItem<String> pendingSearchTerm = new PendingItem<>(); + private final PendingItem<List<String>> pendingSearch = new PendingItem<>(); @Override - public void onCreate(final Bundle savedInstanceState) { - super.onCreate(savedInstanceState); + public void onCreate(final Bundle bundle) { + final String searchTerm = bundle == null ? null : bundle.getString(EXTRA_SEARCH_TERM); + if (searchTerm != null) { + pendingSearchTerm.push(searchTerm); + } + super.onCreate(bundle); this.binding = DataBindingUtil.setContentView(this, R.layout.activity_search); setSupportActionBar((Toolbar) this.binding.toolbar); configureActionBar(getSupportActionBar()); @@ -90,8 +98,20 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc @Override public boolean onCreateOptionsMenu(final Menu menu) { getMenuInflater().inflate(R.menu.activity_search, menu); - MenuItem searchActionMenuItem = menu.findItem(R.id.action_search); - EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field); + final MenuItem searchActionMenuItem = menu.findItem(R.id.action_search); + final EditText searchField = searchActionMenuItem.getActionView().findViewById(R.id.search_field); + final String term = pendingSearchTerm.pop(); + if (term != null) { + searchField.append(term); + List<String> searchTerm = FtsUtils.parse(term); + if (xmppConnectionService != null) { + if (currentSearch.watch(searchTerm)) { + xmppConnectionService.search(searchTerm, this); + } + } else { + pendingSearch.push(searchTerm); + } + } searchField.addTextChangedListener(this); searchField.setHint(R.string.search_messages); searchField.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_AUTO_COMPLETE); @@ -150,6 +170,15 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc return super.onContextItemSelected(item); } + @Override + public void onSaveInstanceState(Bundle bundle) { + List<String> term = currentSearch.get(); + if (term != null && term.size() > 0) { + bundle.putString(EXTRA_SEARCH_TERM, FtsUtils.toUserEnteredString(term)); + } + super.onSaveInstanceState(bundle); + } + private void quote(Message message) { switchToConversationAndQuote(wrap(message.getConversation()), MessageUtils.prepareQuote(message)); } @@ -173,7 +202,10 @@ public class SearchActivity extends XmppActivity implements TextWatcher, OnSearc @Override void onBackendConnected() { - + final List<String> searchTerm = pendingSearch.pop(); + if (searchTerm != null && currentSearch.watch(searchTerm)) { + xmppConnectionService.search(searchTerm, this); + } } private void changeBackground(boolean hasSearch, boolean hasResults) { diff --git a/src/main/java/de/pixart/messenger/ui/util/ChangeWatcher.java b/src/main/java/de/pixart/messenger/ui/util/ChangeWatcher.java index c21c2fbb7..5277a42bf 100644 --- a/src/main/java/de/pixart/messenger/ui/util/ChangeWatcher.java +++ b/src/main/java/de/pixart/messenger/ui/util/ChangeWatcher.java @@ -44,4 +44,7 @@ public class ChangeWatcher<T> { } } + public T get() { + return object; + } }
\ No newline at end of file |