use of org.telegram.ui.Cells.TextCheckbox2Cell in project Telegram-FOSS by Telegram-FOSS-Team.
the class RestrictedLanguagesSelectActivity 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("DoNotTranslate", R.string.DoNotTranslate));
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) -> {
if (getParentActivity() == null || parentLayout == null || !(view instanceof TextCheckbox2Cell)) {
return;
}
boolean search = listView.getAdapter() == searchListViewAdapter;
if (!search)
position--;
LocaleController.LocaleInfo localeInfo;
if (search) {
localeInfo = searchResult.get(position);
} else {
localeInfo = sortedLanguages.get(position);
}
if (localeInfo != null) {
LocaleController.LocaleInfo currentLocaleInfo = LocaleController.getInstance().getCurrentLocaleInfo();
String langCode = localeInfo.pluralLangCode;
if (langCode != null && langCode.equals(currentLocaleInfo.pluralLangCode)) {
AndroidUtilities.shakeView(((TextCheckbox2Cell) view).checkbox, 2, 0);
return;
}
boolean value = selectedLanguages.contains(langCode);
HashSet<String> newSelectedLanguages = new HashSet<String>(selectedLanguages);
if (value)
newSelectedLanguages.removeIf(s -> s != null && s.equals(langCode));
else
newSelectedLanguages.add(langCode);
if (newSelectedLanguages.size() == 1 && newSelectedLanguages.contains(currentLocaleInfo.pluralLangCode))
preferences.edit().remove("translate_button_restricted_languages").apply();
else
preferences.edit().putStringSet("translate_button_restricted_languages", newSelectedLanguages).apply();
}
});
listView.setOnItemLongClickListener((view, position) -> {
if (getParentActivity() == null || parentLayout == null || !(view instanceof TextCheckbox2Cell)) {
return false;
}
boolean search = listView.getAdapter() == searchListViewAdapter;
if (!search)
position--;
LocaleController.LocaleInfo localeInfo;
if (search) {
localeInfo = searchResult.get(position);
} else {
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));
}
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;
}
Aggregations