Search in sources :

Example 1 with TextRadioCell

use of org.telegram.ui.Cells.TextRadioCell in project Telegram-FOSS by Telegram-FOSS-Team.

the class LanguageSelectActivity method createView.

@Override
public View createView(Context context) {
    searching = false;
    searchWas = false;
    actionBar.setBackButtonImage(R.drawable.ic_ab_back);
    actionBar.setAllowOverlayTitle(true);
    actionBar.setTitle(LocaleController.getString("Language", R.string.Language));
    actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {

        @Override
        public void onItemClick(int id) {
            if (id == -1) {
                finishFragment();
            }
        }
    });
    ActionBarMenu menu = actionBar.createMenu();
    ActionBarMenuItem item = menu.addItem(0, R.drawable.ic_ab_search).setIsSearchField(true).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {

        @Override
        public void onSearchExpand() {
            searching = true;
        }

        @Override
        public void onSearchCollapse() {
            search(null);
            searching = false;
            searchWas = false;
            if (listView != null) {
                emptyView.setVisibility(View.GONE);
                listView.setAdapter(listAdapter);
            }
        }

        @Override
        public void onTextChanged(EditText editText) {
            String text = editText.getText().toString();
            search(text);
            if (text.length() != 0) {
                searchWas = true;
                if (listView != null) {
                    listView.setAdapter(searchListViewAdapter);
                }
            } else {
                searching = false;
                searchWas = false;
                if (listView != null) {
                    emptyView.setVisibility(View.GONE);
                    listView.setAdapter(listAdapter);
                }
            }
        }
    });
    item.setSearchFieldHint(LocaleController.getString("Search", R.string.Search));
    listAdapter = new ListAdapter(context, false);
    searchListViewAdapter = new ListAdapter(context, true);
    fragmentView = new FrameLayout(context);
    fragmentView.setBackgroundColor(Theme.getColor(Theme.key_windowBackgroundGray));
    FrameLayout frameLayout = (FrameLayout) fragmentView;
    emptyView = new EmptyTextProgressView(context);
    emptyView.setText(LocaleController.getString("NoResult", R.string.NoResult));
    emptyView.showTextView();
    emptyView.setShowAtCenter(true);
    frameLayout.addView(emptyView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    listView = new RecyclerListView(context);
    listView.setEmptyView(emptyView);
    listView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
    listView.setVerticalScrollBarEnabled(false);
    listView.setAdapter(listAdapter);
    frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
    listView.setOnItemClickListener((view, position) -> {
        try {
            if (getParentActivity() == null || parentLayout == null || !(view instanceof TextRadioCell)) {
                return;
            }
            boolean search = listView.getAdapter() == searchListViewAdapter;
            if (!search) {
                position -= 2;
            }
            LocaleController.LocaleInfo localeInfo;
            if (search) {
                localeInfo = searchResult.get(position);
            } else if (!unofficialLanguages.isEmpty() && position >= 0 && position < unofficialLanguages.size()) {
                localeInfo = unofficialLanguages.get(position);
            } else {
                if (!unofficialLanguages.isEmpty()) {
                    position -= unofficialLanguages.size() + 1;
                }
                localeInfo = sortedLanguages.get(position);
            }
            if (localeInfo != null) {
                LocaleController.LocaleInfo prevLocale = LocaleController.getInstance().getCurrentLocaleInfo();
                LocaleController.getInstance().applyLanguage(localeInfo, true, false, false, true, currentAccount);
                parentLayout.rebuildAllFragmentViews(false, false);
                String langCode = localeInfo.pluralLangCode, prevLangCode = prevLocale.pluralLangCode;
                SharedPreferences preferences = MessagesController.getGlobalMainSettings();
                HashSet<String> selectedLanguages = RestrictedLanguagesSelectActivity.getRestrictedLanguages();
                HashSet<String> newSelectedLanguages = new HashSet<String>(selectedLanguages);
                if (selectedLanguages.contains(langCode)) {
                    newSelectedLanguages.removeIf(s -> s != null && s.equals(langCode));
                    if (!selectedLanguages.contains(prevLangCode))
                        newSelectedLanguages.add(prevLangCode);
                }
                preferences.edit().putStringSet("translate_button_restricted_languages", newSelectedLanguages).apply();
                finishFragment();
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
    });
    listView.setOnItemLongClickListener((view, position) -> {
        try {
            if (getParentActivity() == null || parentLayout == null || !(view instanceof TextRadioCell)) {
                return false;
            }
            boolean search = listView.getAdapter() == searchListViewAdapter;
            if (!search) {
                position -= 2;
            }
            LocaleController.LocaleInfo localeInfo;
            if (search) {
                localeInfo = searchResult.get(position);
            } else if (!unofficialLanguages.isEmpty() && position >= 0 && position < unofficialLanguages.size()) {
                localeInfo = unofficialLanguages.get(position);
            } else {
                if (!unofficialLanguages.isEmpty()) {
                    position -= unofficialLanguages.size() + 1;
                }
                localeInfo = sortedLanguages.get(position);
            }
            if (localeInfo == null || localeInfo.pathToFile == null || localeInfo.isRemote() && localeInfo.serverIndex != Integer.MAX_VALUE) {
                return false;
            }
            final LocaleController.LocaleInfo finalLocaleInfo = localeInfo;
            AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
            builder.setTitle(LocaleController.getString("DeleteLocalizationTitle", R.string.DeleteLocalizationTitle));
            builder.setMessage(AndroidUtilities.replaceTags(LocaleController.formatString("DeleteLocalizationText", R.string.DeleteLocalizationText, localeInfo.name)));
            builder.setPositiveButton(LocaleController.getString("Delete", R.string.Delete), (dialogInterface, i) -> {
                if (LocaleController.getInstance().deleteLanguage(finalLocaleInfo, currentAccount)) {
                    fillLanguages();
                    if (searchResult != null) {
                        searchResult.remove(finalLocaleInfo);
                    }
                    if (listAdapter != null) {
                        listAdapter.notifyDataSetChanged();
                    }
                    if (searchListViewAdapter != null) {
                        searchListViewAdapter.notifyDataSetChanged();
                    }
                }
            });
            builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
            AlertDialog alertDialog = builder.create();
            showDialog(alertDialog);
            TextView button = (TextView) alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
            if (button != null) {
                button.setTextColor(Theme.getColor(Theme.key_dialogTextRed2));
            }
        } catch (Exception e) {
            FileLog.e(e);
        }
        return true;
    });
    listView.setOnScrollListener(new RecyclerView.OnScrollListener() {

        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
            }
        }
    });
    return fragmentView;
}
Also used : AlertDialog(org.telegram.ui.ActionBar.AlertDialog) RecyclerListView(org.telegram.ui.Components.RecyclerListView) LinearLayoutManager(androidx.recyclerview.widget.LinearLayoutManager) TextRadioCell(org.telegram.ui.Cells.TextRadioCell) TextView(android.widget.TextView) ActionBar(org.telegram.ui.ActionBar.ActionBar) HashSet(java.util.HashSet) EditText(android.widget.EditText) ActionBarMenuItem(org.telegram.ui.ActionBar.ActionBarMenuItem) LocaleController(org.telegram.messenger.LocaleController) SharedPreferences(android.content.SharedPreferences) EmptyTextProgressView(org.telegram.ui.Components.EmptyTextProgressView) ActionBarMenu(org.telegram.ui.ActionBar.ActionBarMenu) FrameLayout(android.widget.FrameLayout) RecyclerView(androidx.recyclerview.widget.RecyclerView)

Aggregations

SharedPreferences (android.content.SharedPreferences)1 EditText (android.widget.EditText)1 FrameLayout (android.widget.FrameLayout)1 TextView (android.widget.TextView)1 LinearLayoutManager (androidx.recyclerview.widget.LinearLayoutManager)1 RecyclerView (androidx.recyclerview.widget.RecyclerView)1 HashSet (java.util.HashSet)1 LocaleController (org.telegram.messenger.LocaleController)1 ActionBar (org.telegram.ui.ActionBar.ActionBar)1 ActionBarMenu (org.telegram.ui.ActionBar.ActionBarMenu)1 ActionBarMenuItem (org.telegram.ui.ActionBar.ActionBarMenuItem)1 AlertDialog (org.telegram.ui.ActionBar.AlertDialog)1 TextRadioCell (org.telegram.ui.Cells.TextRadioCell)1 EmptyTextProgressView (org.telegram.ui.Components.EmptyTextProgressView)1 RecyclerListView (org.telegram.ui.Components.RecyclerListView)1