aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/ui/AbstractSearchableListItemActivity.java
blob: 01246ad929fce97cdb3bf6043538966788a7e2cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
package de.pixart.messenger.ui;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

import de.pixart.messenger.R;
import de.pixart.messenger.databinding.ActivityChooseContactBinding;
import de.pixart.messenger.entities.ListItem;
import de.pixart.messenger.ui.adapter.ListItemAdapter;

public abstract class AbstractSearchableListItemActivity extends XmppActivity implements TextView.OnEditorActionListener {
    protected ActivityChooseContactBinding binding;
    private final List<ListItem> listItems = new ArrayList<>();
    private ArrayAdapter<ListItem> mListItemsAdapter;

    private EditText mSearchEditText;

    private final MenuItem.OnActionExpandListener mOnActionExpandListener = new MenuItem.OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(final MenuItem item) {
            mSearchEditText.post(() -> {
                mSearchEditText.requestFocus();
                final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.showSoftInput(mSearchEditText, InputMethodManager.SHOW_IMPLICIT);
            });

            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(final MenuItem item) {
            final InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(mSearchEditText.getWindowToken(), InputMethodManager.HIDE_IMPLICIT_ONLY);
            mSearchEditText.setText("");
            filterContacts();
            return true;
        }
    };

    private final TextWatcher mSearchTextWatcher = new TextWatcher() {

        @Override
        public void afterTextChanged(final Editable editable) {
            filterContacts(editable.toString());
        }

        @Override
        public void beforeTextChanged(final CharSequence s, final int start, final int count,
                                      final int after) {
        }

        @Override
        public void onTextChanged(final CharSequence s, final int start, final int before,
                                  final int count) {
        }
    };

    public ListView getListView() {
        return binding.chooseContactList;
    }

    public List<ListItem> getListItems() {
        return listItems;
    }

    public EditText getSearchEditText() {
        return mSearchEditText;
    }

    public ArrayAdapter<ListItem> getListItemAdapter() {
        return mListItemsAdapter;
    }

    @Override
    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.binding = DataBindingUtil.setContentView(this, R.layout.activity_choose_contact);
        setSupportActionBar((Toolbar) binding.toolbar);
        configureActionBar(getSupportActionBar());
        this.binding.chooseContactList.setFastScrollEnabled(true);
        mListItemsAdapter = new ListItemAdapter(this, listItems);
        this.binding.chooseContactList.setAdapter(mListItemsAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.choose_contact, menu);
        final MenuItem menuSearchView = menu.findItem(R.id.action_search);
        final View mSearchView = menuSearchView.getActionView();
        mSearchEditText = mSearchView.findViewById(R.id.search_field);
        mSearchEditText.addTextChangedListener(mSearchTextWatcher);
        mSearchEditText.setHint(R.string.search_contacts);
        mSearchEditText.setOnEditorActionListener(this);
        menuSearchView.setOnActionExpandListener(mOnActionExpandListener);
        return true;
    }

    protected void filterContacts() {
        final String needle = mSearchEditText != null ? mSearchEditText.getText().toString() : null;
        if (needle != null && !needle.isEmpty()) {
            filterContacts(needle);
        } else {
            filterContacts(null);
        }
    }

    protected abstract void filterContacts(final String needle);

    @Override
    void onBackendConnected() {
        filterContacts();
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        return false;
    }
}