Search in sources :

Example 1 with SideArrayAdapter

use of me.ccrama.redditslide.Adapters.SideArrayAdapter in project Slide by ccrama.

the class MainActivity method setDrawerSubList.

public void setDrawerSubList() {
    ArrayList<String> copy;
    if (NetworkUtil.isConnected(this)) {
        copy = new ArrayList<>(usedArray);
    } else {
        copy = UserSubscriptions.getAllUserSubreddits(this);
    }
    copy.removeAll(Arrays.asList("", null));
    sideArrayAdapter = new SideArrayAdapter(this, copy, UserSubscriptions.getAllSubreddits(this), drawerSubList);
    drawerSubList.setAdapter(sideArrayAdapter);
    if ((SettingValues.subredditSearchMethod != Constants.SUBREDDIT_SEARCH_METHOD_TOOLBAR)) {
        drawerSearch = headerMain.findViewById(R.id.sort);
        drawerSearch.setVisibility(View.VISIBLE);
        drawerSubList.setFocusable(false);
        headerMain.findViewById(R.id.close_search_drawer).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                drawerSearch.setText("");
            }
        });
        drawerSearch.setOnFocusChangeListener(new View.OnFocusChangeListener() {

            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
                    drawerSubList.smoothScrollToPositionFromTop(1, drawerSearch.getHeight(), 100);
                } else {
                    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
                }
            }
        });
        drawerSearch.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                    // If it the input text doesn't match a subreddit from the list exactly, openInSubView is true
                    if (sideArrayAdapter.fitems == null || sideArrayAdapter.openInSubView || !usedArray.contains(drawerSearch.getText().toString().toLowerCase(Locale.ENGLISH))) {
                        Intent inte = new Intent(MainActivity.this, SubredditView.class);
                        inte.putExtra(SubredditView.EXTRA_SUBREDDIT, drawerSearch.getText().toString());
                        MainActivity.this.startActivityForResult(inte, 2001);
                    } else {
                        if (commentPager && adapter instanceof MainPagerAdapterComment) {
                            openingComments = null;
                            toOpenComments = -1;
                            ((MainPagerAdapterComment) adapter).size = (usedArray.size() + 1);
                            adapter.notifyDataSetChanged();
                            if (usedArray.contains(drawerSearch.getText().toString().toLowerCase(Locale.ENGLISH))) {
                                doPageSelectedComments(usedArray.indexOf(drawerSearch.getText().toString().toLowerCase(Locale.ENGLISH)));
                            } else {
                                doPageSelectedComments(usedArray.indexOf(sideArrayAdapter.fitems.get(0)));
                            }
                        }
                        if (usedArray.contains(drawerSearch.getText().toString().toLowerCase(Locale.ENGLISH))) {
                            pager.setCurrentItem(usedArray.indexOf(drawerSearch.getText().toString().toLowerCase(Locale.ENGLISH)));
                        } else {
                            pager.setCurrentItem(usedArray.indexOf(sideArrayAdapter.fitems.get(0)));
                        }
                        drawerLayout.closeDrawers();
                        drawerSearch.setText("");
                        View view = MainActivity.this.getCurrentFocus();
                        if (view != null) {
                            KeyboardUtil.hideKeyboard(MainActivity.this, view.getWindowToken(), 0);
                        }
                    }
                }
                return false;
            }
        });
        final View close = findViewById(R.id.close_search_drawer);
        close.setVisibility(View.GONE);
        drawerSearch.addTextChangedListener(new SimpleTextWatcher() {

            @Override
            public void afterTextChanged(Editable editable) {
                final String result = editable.toString();
                if (result.isEmpty()) {
                    close.setVisibility(View.GONE);
                } else {
                    close.setVisibility(View.VISIBLE);
                }
                sideArrayAdapter.getFilter().filter(result);
            }
        });
    } else {
        if (drawerSearch != null) {
            drawerSearch.setOnClickListener(// remove the touch listener on the drawer search field
            null);
            drawerSearch.setVisibility(View.GONE);
        }
    }
}
Also used : Intent(android.content.Intent) SideArrayAdapter(me.ccrama.redditslide.Adapters.SideArrayAdapter) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) SubmissionsView(me.ccrama.redditslide.Fragments.SubmissionsView) CardView(androidx.cardview.widget.CardView) AutoCompleteTextView(android.widget.AutoCompleteTextView) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) Point(android.graphics.Point) KeyEvent(android.view.KeyEvent) SimpleTextWatcher(me.ccrama.redditslide.util.stubs.SimpleTextWatcher) Editable(android.text.Editable) AutoCompleteTextView(android.widget.AutoCompleteTextView) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) TextView(android.widget.TextView)

Example 2 with SideArrayAdapter

use of me.ccrama.redditslide.Adapters.SideArrayAdapter in project Slide by ccrama.

the class MainActivity method setupSubredditSearchToolbar.

/**
 * If the user has the Subreddit Search method set to "long press on toolbar title", an
 * OnLongClickListener needs to be set for the toolbar as well as handling all of the relevant
 * onClicks for the views of the search bar.
 */
private void setupSubredditSearchToolbar() {
    if (!NetworkUtil.isConnected(this)) {
        if (findViewById(R.id.drawer_divider) != null) {
            findViewById(R.id.drawer_divider).setVisibility(View.GONE);
        }
    } else {
        if ((SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_TOOLBAR || SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_BOTH) && usedArray != null && !usedArray.isEmpty()) {
            if (findViewById(R.id.drawer_divider) != null) {
                if (SettingValues.subredditSearchMethod == Constants.SUBREDDIT_SEARCH_METHOD_BOTH) {
                    findViewById(R.id.drawer_divider).setVisibility(View.GONE);
                } else {
                    findViewById(R.id.drawer_divider).setVisibility(View.VISIBLE);
                }
            }
            final ListView TOOLBAR_SEARCH_SUGGEST_LIST = (ListView) findViewById(R.id.toolbar_search_suggestions_list);
            final ArrayList<String> subs_copy = new ArrayList<>(usedArray);
            final SideArrayAdapter TOOLBAR_SEARCH_SUGGEST_ADAPTER = new SideArrayAdapter(this, subs_copy, UserSubscriptions.getAllSubreddits(this), TOOLBAR_SEARCH_SUGGEST_LIST);
            if (TOOLBAR_SEARCH_SUGGEST_LIST != null) {
                TOOLBAR_SEARCH_SUGGEST_LIST.setAdapter(TOOLBAR_SEARCH_SUGGEST_ADAPTER);
            }
            if (mToolbar != null) {
                mToolbar.setOnLongClickListener(new View.OnLongClickListener() {

                    @Override
                    public boolean onLongClick(View v) {
                        final AutoCompleteTextView GO_TO_SUB_FIELD = (AutoCompleteTextView) findViewById(R.id.toolbar_search);
                        final ImageView CLOSE_BUTTON = (ImageView) findViewById(R.id.close_search_toolbar);
                        final CardView SUGGESTIONS_BACKGROUND = (CardView) findViewById(R.id.toolbar_search_suggestions);
                        // if the view mode is set to Subreddit Tabs, save the title ("Slide" or "Slide (debug)")
                        tabViewModeTitle = (!SettingValues.single) ? getSupportActionBar().getTitle().toString() : null;
                        getSupportActionBar().setTitle(// clear title to make room for search field
                        "");
                        if (GO_TO_SUB_FIELD != null && CLOSE_BUTTON != null && SUGGESTIONS_BACKGROUND != null) {
                            GO_TO_SUB_FIELD.setVisibility(View.VISIBLE);
                            CLOSE_BUTTON.setVisibility(View.VISIBLE);
                            SUGGESTIONS_BACKGROUND.setVisibility(View.VISIBLE);
                            // run enter animations
                            enterAnimationsForToolbarSearch(ANIMATE_DURATION, SUGGESTIONS_BACKGROUND, GO_TO_SUB_FIELD, CLOSE_BUTTON);
                            // Get focus of the search field and show the keyboard
                            GO_TO_SUB_FIELD.requestFocus();
                            KeyboardUtil.toggleKeyboard(MainActivity.this, InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
                            // Close the search UI and keyboard when clicking the close button
                            CLOSE_BUTTON.setOnClickListener(new View.OnClickListener() {

                                @Override
                                public void onClick(View v) {
                                    final View view = MainActivity.this.getCurrentFocus();
                                    if (view != null) {
                                        // Hide the keyboard
                                        KeyboardUtil.hideKeyboard(MainActivity.this, view.getWindowToken(), 0);
                                    }
                                    // run the exit animations
                                    exitAnimationsForToolbarSearch(ANIMATE_DURATION, SUGGESTIONS_BACKGROUND, GO_TO_SUB_FIELD, CLOSE_BUTTON);
                                    // clear sub text when close button is clicked
                                    GO_TO_SUB_FIELD.setText("");
                                }
                            });
                            GO_TO_SUB_FIELD.setOnEditorActionListener(new TextView.OnEditorActionListener() {

                                @Override
                                public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                                    if (arg1 == EditorInfo.IME_ACTION_SEARCH) {
                                        // If it the input text doesn't match a subreddit from the list exactly, openInSubView is true
                                        if (sideArrayAdapter.fitems == null || sideArrayAdapter.openInSubView || !usedArray.contains(GO_TO_SUB_FIELD.getText().toString().toLowerCase(Locale.ENGLISH))) {
                                            Intent intent = new Intent(MainActivity.this, SubredditView.class);
                                            intent.putExtra(SubredditView.EXTRA_SUBREDDIT, GO_TO_SUB_FIELD.getText().toString());
                                            MainActivity.this.startActivityForResult(intent, 2002);
                                        } else {
                                            if (commentPager && adapter instanceof MainPagerAdapterComment) {
                                                openingComments = null;
                                                toOpenComments = -1;
                                                ((MainPagerAdapterComment) adapter).size = (usedArray.size() + 1);
                                                adapter.notifyDataSetChanged();
                                                if (usedArray.contains(GO_TO_SUB_FIELD.getText().toString().toLowerCase(Locale.ENGLISH))) {
                                                    doPageSelectedComments(usedArray.indexOf(GO_TO_SUB_FIELD.getText().toString().toLowerCase(Locale.ENGLISH)));
                                                } else {
                                                    doPageSelectedComments(usedArray.indexOf(sideArrayAdapter.fitems.get(0)));
                                                }
                                            }
                                            if (usedArray.contains(GO_TO_SUB_FIELD.getText().toString().toLowerCase(Locale.ENGLISH))) {
                                                pager.setCurrentItem(usedArray.indexOf(GO_TO_SUB_FIELD.getText().toString().toLowerCase(Locale.ENGLISH)));
                                            } else {
                                                pager.setCurrentItem(usedArray.indexOf(sideArrayAdapter.fitems.get(0)));
                                            }
                                        }
                                        View view = MainActivity.this.getCurrentFocus();
                                        if (view != null) {
                                            // Hide the keyboard
                                            KeyboardUtil.hideKeyboard(MainActivity.this, view.getWindowToken(), 0);
                                        }
                                        SUGGESTIONS_BACKGROUND.setVisibility(View.GONE);
                                        GO_TO_SUB_FIELD.setVisibility(View.GONE);
                                        CLOSE_BUTTON.setVisibility(View.GONE);
                                        if (SettingValues.single) {
                                            getSupportActionBar().setTitle(selectedSub);
                                        } else {
                                            // Set the title back to "Slide" or "Slide (debug)"
                                            getSupportActionBar().setTitle(tabViewModeTitle);
                                        }
                                    }
                                    return false;
                                }
                            });
                            GO_TO_SUB_FIELD.addTextChangedListener(new SimpleTextWatcher() {

                                @Override
                                public void afterTextChanged(Editable editable) {
                                    final String RESULT = GO_TO_SUB_FIELD.getText().toString().replaceAll(" ", "");
                                    TOOLBAR_SEARCH_SUGGEST_ADAPTER.getFilter().filter(RESULT);
                                }
                            });
                        }
                        return true;
                    }
                });
            }
        }
    }
}
Also used : ArrayList(java.util.ArrayList) CaseInsensitiveArrayList(me.ccrama.redditslide.CaseInsensitiveArrayList) CardView(androidx.cardview.widget.CardView) Intent(android.content.Intent) SideArrayAdapter(me.ccrama.redditslide.Adapters.SideArrayAdapter) ImageView(android.widget.ImageView) HorizontalScrollView(android.widget.HorizontalScrollView) SubmissionsView(me.ccrama.redditslide.Fragments.SubmissionsView) CardView(androidx.cardview.widget.CardView) AutoCompleteTextView(android.widget.AutoCompleteTextView) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) View(android.view.View) TextView(android.widget.TextView) ListView(android.widget.ListView) KeyEvent(android.view.KeyEvent) ListView(android.widget.ListView) SimpleTextWatcher(me.ccrama.redditslide.util.stubs.SimpleTextWatcher) Editable(android.text.Editable) AutoCompleteTextView(android.widget.AutoCompleteTextView) SpoilerRobotoTextView(me.ccrama.redditslide.SpoilerRobotoTextView) TextView(android.widget.TextView) ImageView(android.widget.ImageView) AutoCompleteTextView(android.widget.AutoCompleteTextView)

Aggregations

Intent (android.content.Intent)2 Editable (android.text.Editable)2 KeyEvent (android.view.KeyEvent)2 View (android.view.View)2 AutoCompleteTextView (android.widget.AutoCompleteTextView)2 HorizontalScrollView (android.widget.HorizontalScrollView)2 ImageView (android.widget.ImageView)2 ListView (android.widget.ListView)2 TextView (android.widget.TextView)2 CardView (androidx.cardview.widget.CardView)2 SideArrayAdapter (me.ccrama.redditslide.Adapters.SideArrayAdapter)2 SubmissionsView (me.ccrama.redditslide.Fragments.SubmissionsView)2 SpoilerRobotoTextView (me.ccrama.redditslide.SpoilerRobotoTextView)2 SimpleTextWatcher (me.ccrama.redditslide.util.stubs.SimpleTextWatcher)2 Point (android.graphics.Point)1 ArrayList (java.util.ArrayList)1 CaseInsensitiveArrayList (me.ccrama.redditslide.CaseInsensitiveArrayList)1