use of org.telegram.ui.Adapters.SearchAdapter in project Telegram-FOSS by Telegram-FOSS-Team.
the class ContactsActivity method createView.
@Override
public View createView(Context context) {
searching = false;
searchWas = false;
actionBar.setBackButtonImage(R.drawable.ic_ab_back);
actionBar.setAllowOverlayTitle(true);
if (destroyAfterSelect) {
if (returnAsResult) {
actionBar.setTitle(LocaleController.getString("SelectContact", R.string.SelectContact));
} else {
if (createSecretChat) {
actionBar.setTitle(LocaleController.getString("NewSecretChat", R.string.NewSecretChat));
} else {
actionBar.setTitle(LocaleController.getString("NewMessageTitle", R.string.NewMessageTitle));
}
}
} else {
actionBar.setTitle(LocaleController.getString("Contacts", R.string.Contacts));
}
actionBar.setActionBarMenuOnItemClick(new ActionBar.ActionBarMenuOnItemClick() {
@Override
public void onItemClick(int id) {
if (id == -1) {
finishFragment();
} else if (id == sort_button) {
SharedConfig.toggleSortContactsByName();
sortByName = SharedConfig.sortContactsByName;
listViewAdapter.setSortType(sortByName ? 1 : 2, false);
sortItem.setIcon(sortByName ? R.drawable.contacts_sort_time : R.drawable.contacts_sort_name);
}
}
});
ActionBarMenu menu = actionBar.createMenu();
ActionBarMenuItem item = menu.addItem(search_button, R.drawable.ic_ab_search).setIsSearchField(true).setActionBarMenuItemSearchListener(new ActionBarMenuItem.ActionBarMenuItemSearchListener() {
@Override
public void onSearchExpand() {
searching = true;
if (floatingButtonContainer != null) {
floatingButtonContainer.setVisibility(View.GONE);
}
if (sortItem != null) {
sortItem.setVisibility(View.GONE);
}
}
@Override
public void onSearchCollapse() {
searchListViewAdapter.searchDialogs(null);
searching = false;
searchWas = false;
listView.setAdapter(listViewAdapter);
listView.setSectionsType(1);
listViewAdapter.notifyDataSetChanged();
listView.setFastScrollVisible(true);
listView.setVerticalScrollBarEnabled(false);
// emptyView.setText(LocaleController.getString("NoContacts", R.string.NoContacts));
if (floatingButtonContainer != null) {
floatingButtonContainer.setVisibility(View.VISIBLE);
floatingHidden = true;
floatingButtonContainer.setTranslationY(AndroidUtilities.dp(100));
hideFloatingButton(false);
}
if (sortItem != null) {
sortItem.setVisibility(View.VISIBLE);
}
}
@Override
public void onTextChanged(EditText editText) {
if (searchListViewAdapter == null) {
return;
}
String text = editText.getText().toString();
if (text.length() != 0) {
searchWas = true;
if (listView != null) {
listView.setAdapter(searchListViewAdapter);
listView.setSectionsType(0);
searchListViewAdapter.notifyDataSetChanged();
listView.setFastScrollVisible(false);
listView.setVerticalScrollBarEnabled(true);
}
emptyView.showProgress(true, true);
searchListViewAdapter.searchDialogs(text);
} else {
if (listView != null) {
listView.setAdapter(listViewAdapter);
listView.setSectionsType(1);
}
}
}
});
item.setSearchFieldHint(LocaleController.getString("Search", R.string.Search));
item.setContentDescription(LocaleController.getString("Search", R.string.Search));
if (!createSecretChat && !returnAsResult) {
sortItem = menu.addItem(sort_button, sortByName ? R.drawable.contacts_sort_time : R.drawable.contacts_sort_name);
sortItem.setContentDescription(LocaleController.getString("AccDescrContactSorting", R.string.AccDescrContactSorting));
}
searchListViewAdapter = new SearchAdapter(context, ignoreUsers, allowUsernameSearch, false, false, allowBots, allowSelf, true, 0) {
@Override
protected void onSearchProgressChanged() {
if (!searchInProgress() && getItemCount() == 0) {
emptyView.showProgress(false, true);
}
showItemsAnimated();
}
};
int inviteViaLink;
if (chatId != 0) {
TLRPC.Chat chat = getMessagesController().getChat(chatId);
inviteViaLink = ChatObject.canUserDoAdminAction(chat, ChatObject.ACTION_INVITE) ? 1 : 0;
} else if (channelId != 0) {
TLRPC.Chat chat = getMessagesController().getChat(channelId);
inviteViaLink = ChatObject.canUserDoAdminAction(chat, ChatObject.ACTION_INVITE) && TextUtils.isEmpty(chat.username) ? 2 : 0;
} else {
inviteViaLink = 0;
}
try {
hasGps = ApplicationLoader.applicationContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_LOCATION_GPS);
} catch (Throwable e) {
hasGps = false;
}
listViewAdapter = new ContactsAdapter(context, onlyUsers ? 1 : 0, needPhonebook, ignoreUsers, inviteViaLink, hasGps) {
@Override
public void notifyDataSetChanged() {
super.notifyDataSetChanged();
if (listView != null && listView.getAdapter() == this) {
int count = super.getItemCount();
if (needPhonebook) {
// emptyView.setVisibility(count == 2 ? View.VISIBLE : View.GONE);
listView.setFastScrollVisible(count != 2);
} else {
// emptyView.setVisibility(count == 0 ? View.VISIBLE : View.GONE);
listView.setFastScrollVisible(count != 0);
}
}
}
};
listViewAdapter.setSortType(sortItem != null ? (sortByName ? 1 : 2) : 0, false);
listViewAdapter.setDisableSections(disableSections);
fragmentView = new FrameLayout(context) {
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
if (listView.getAdapter() == listViewAdapter) {
if (emptyView.getVisibility() == VISIBLE) {
emptyView.setTranslationY(AndroidUtilities.dp(74));
}
} else {
emptyView.setTranslationY(AndroidUtilities.dp(0));
}
}
};
FrameLayout frameLayout = (FrameLayout) fragmentView;
FlickerLoadingView flickerLoadingView = new FlickerLoadingView(context);
flickerLoadingView.setViewType(FlickerLoadingView.USERS_TYPE);
flickerLoadingView.showDate(false);
emptyView = new StickerEmptyView(context, flickerLoadingView, StickerEmptyView.STICKER_TYPE_SEARCH);
emptyView.addView(flickerLoadingView, 0);
emptyView.setAnimateLayoutChange(true);
emptyView.showProgress(true, false);
emptyView.title.setText(LocaleController.getString("NoResult", R.string.NoResult));
emptyView.subtitle.setText(LocaleController.getString("SearchEmptyViewFilteredSubtitle2", R.string.SearchEmptyViewFilteredSubtitle2));
frameLayout.addView(emptyView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
listView = new RecyclerListView(context) {
@Override
public void setPadding(int left, int top, int right, int bottom) {
super.setPadding(left, top, right, bottom);
if (emptyView != null) {
emptyView.setPadding(left, top, right, bottom);
}
}
};
listView.setSectionsType(1);
listView.setVerticalScrollBarEnabled(false);
listView.setFastScrollEnabled(RecyclerListView.FastScroll.LETTER_TYPE);
listView.setLayoutManager(layoutManager = new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false));
listView.setAdapter(listViewAdapter);
frameLayout.addView(listView, LayoutHelper.createFrame(LayoutHelper.MATCH_PARENT, LayoutHelper.MATCH_PARENT));
listView.setEmptyView(emptyView);
listView.setAnimateEmptyView(true, 0);
listView.setOnItemClickListener((view, position) -> {
if (listView.getAdapter() == searchListViewAdapter) {
Object object = searchListViewAdapter.getItem(position);
if (object instanceof TLRPC.User) {
TLRPC.User user = (TLRPC.User) object;
if (searchListViewAdapter.isGlobalSearch(position)) {
ArrayList<TLRPC.User> users = new ArrayList<>();
users.add(user);
getMessagesController().putUsers(users, false);
MessagesStorage.getInstance(currentAccount).putUsersAndChats(users, null, false, true);
}
if (returnAsResult) {
if (ignoreUsers != null && ignoreUsers.indexOfKey(user.id) >= 0) {
return;
}
didSelectResult(user, true, null);
} else {
if (createSecretChat) {
if (user.id == UserConfig.getInstance(currentAccount).getClientUserId()) {
return;
}
creatingChat = true;
SecretChatHelper.getInstance(currentAccount).startSecretChat(getParentActivity(), user);
} else {
Bundle args = new Bundle();
args.putLong("user_id", user.id);
if (getMessagesController().checkCanOpenChat(args, ContactsActivity.this)) {
presentFragment(new ChatActivity(args), true);
}
}
}
} else if (object instanceof String) {
String str = (String) object;
if (!str.equals("section")) {
NewContactActivity activity = new NewContactActivity();
activity.setInitialPhoneNumber(str, true);
presentFragment(activity);
}
}
} else {
int section = listViewAdapter.getSectionForPosition(position);
int row = listViewAdapter.getPositionInSectionForPosition(position);
if (row < 0 || section < 0) {
return;
}
if ((!onlyUsers || inviteViaLink != 0) && section == 0) {
if (needPhonebook) {
if (row == 0) {
presentFragment(new InviteContactsActivity());
} else if (row == 1 && hasGps) {
if (Build.VERSION.SDK_INT >= 23) {
Activity activity = getParentActivity();
if (activity != null) {
if (activity.checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
presentFragment(new ActionIntroActivity(ActionIntroActivity.ACTION_TYPE_NEARBY_LOCATION_ACCESS));
return;
}
}
}
boolean enabled = true;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
LocationManager lm = (LocationManager) ApplicationLoader.applicationContext.getSystemService(Context.LOCATION_SERVICE);
enabled = lm.isLocationEnabled();
} else if (Build.VERSION.SDK_INT >= 19) {
try {
int mode = Settings.Secure.getInt(ApplicationLoader.applicationContext.getContentResolver(), Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
enabled = (mode != Settings.Secure.LOCATION_MODE_OFF);
} catch (Throwable e) {
FileLog.e(e);
}
}
if (!enabled) {
presentFragment(new ActionIntroActivity(ActionIntroActivity.ACTION_TYPE_NEARBY_LOCATION_ENABLED));
return;
}
presentFragment(new PeopleNearbyActivity());
}
} else if (inviteViaLink != 0) {
if (row == 0) {
presentFragment(new GroupInviteActivity(chatId != 0 ? chatId : channelId));
}
} else {
if (row == 0) {
Bundle args = new Bundle();
presentFragment(new GroupCreateActivity(args), false);
} else if (row == 1) {
Bundle args = new Bundle();
args.putBoolean("onlyUsers", true);
args.putBoolean("destroyAfterSelect", true);
args.putBoolean("createSecretChat", true);
args.putBoolean("allowBots", false);
args.putBoolean("allowSelf", false);
presentFragment(new ContactsActivity(args), false);
} else if (row == 2) {
SharedPreferences preferences = MessagesController.getGlobalMainSettings();
if (!BuildVars.DEBUG_VERSION && preferences.getBoolean("channel_intro", false)) {
Bundle args = new Bundle();
args.putInt("step", 0);
presentFragment(new ChannelCreateActivity(args));
} else {
presentFragment(new ActionIntroActivity(ActionIntroActivity.ACTION_TYPE_CHANNEL_CREATE));
preferences.edit().putBoolean("channel_intro", true).commit();
}
}
}
} else {
Object item1 = listViewAdapter.getItem(section, row);
if (item1 instanceof TLRPC.User) {
TLRPC.User user = (TLRPC.User) item1;
if (returnAsResult) {
if (ignoreUsers != null && ignoreUsers.indexOfKey(user.id) >= 0) {
return;
}
didSelectResult(user, true, null);
} else {
if (createSecretChat) {
creatingChat = true;
SecretChatHelper.getInstance(currentAccount).startSecretChat(getParentActivity(), user);
} else {
Bundle args = new Bundle();
args.putLong("user_id", user.id);
if (getMessagesController().checkCanOpenChat(args, ContactsActivity.this)) {
presentFragment(new ChatActivity(args), true);
}
}
}
} else if (item1 instanceof ContactsController.Contact) {
ContactsController.Contact contact = (ContactsController.Contact) item1;
String usePhone = null;
if (!contact.phones.isEmpty()) {
usePhone = contact.phones.get(0);
}
if (usePhone == null || getParentActivity() == null) {
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(getParentActivity());
builder.setMessage(LocaleController.getString("InviteUser", R.string.InviteUser));
builder.setTitle(LocaleController.getString("AppName", R.string.AppName));
final String arg1 = usePhone;
builder.setPositiveButton(LocaleController.getString("OK", R.string.OK), (dialogInterface, i) -> {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.fromParts("sms", arg1, null));
intent.putExtra("sms_body", ContactsController.getInstance(currentAccount).getInviteText(1));
getParentActivity().startActivityForResult(intent, 500);
} catch (Exception e) {
FileLog.e(e);
}
});
builder.setNegativeButton(LocaleController.getString("Cancel", R.string.Cancel), null);
showDialog(builder.create());
}
}
}
});
listView.setOnScrollListener(new RecyclerView.OnScrollListener() {
private boolean scrollingManually;
@Override
public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
if (searching && searchWas) {
AndroidUtilities.hideKeyboard(getParentActivity().getCurrentFocus());
}
scrollingManually = true;
} else {
scrollingManually = false;
}
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
if (floatingButtonContainer != null && floatingButtonContainer.getVisibility() != View.GONE) {
int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
final View topChild = recyclerView.getChildAt(0);
int firstViewTop = 0;
if (topChild != null) {
firstViewTop = topChild.getTop();
}
boolean goingDown;
boolean changed = true;
if (prevPosition == firstVisibleItem) {
final int topDelta = prevTop - firstViewTop;
goingDown = firstViewTop < prevTop;
changed = Math.abs(topDelta) > 1;
} else {
goingDown = firstVisibleItem > prevPosition;
}
if (changed && scrollUpdated && (goingDown || scrollingManually)) {
hideFloatingButton(goingDown);
}
prevPosition = firstVisibleItem;
prevTop = firstViewTop;
scrollUpdated = true;
}
}
});
if (!createSecretChat && !returnAsResult) {
floatingButtonContainer = new FrameLayout(context);
frameLayout.addView(floatingButtonContainer, LayoutHelper.createFrame((Build.VERSION.SDK_INT >= 21 ? 56 : 60) + 20, (Build.VERSION.SDK_INT >= 21 ? 56 : 60) + 20, (LocaleController.isRTL ? Gravity.LEFT : Gravity.RIGHT) | Gravity.BOTTOM, LocaleController.isRTL ? 4 : 0, 0, LocaleController.isRTL ? 0 : 4, 0));
floatingButtonContainer.setOnClickListener(v -> presentFragment(new NewContactActivity()));
floatingButton = new RLottieImageView(context);
floatingButton.setScaleType(ImageView.ScaleType.CENTER);
Drawable drawable = Theme.createSimpleSelectorCircleDrawable(AndroidUtilities.dp(56), Theme.getColor(Theme.key_chats_actionBackground), Theme.getColor(Theme.key_chats_actionPressedBackground));
if (Build.VERSION.SDK_INT < 21) {
Drawable shadowDrawable = context.getResources().getDrawable(R.drawable.floating_shadow).mutate();
shadowDrawable.setColorFilter(new PorterDuffColorFilter(0xff000000, PorterDuff.Mode.MULTIPLY));
CombinedDrawable combinedDrawable = new CombinedDrawable(shadowDrawable, drawable, 0, 0);
combinedDrawable.setIconSize(AndroidUtilities.dp(56), AndroidUtilities.dp(56));
drawable = combinedDrawable;
}
floatingButton.setBackgroundDrawable(drawable);
floatingButton.setColorFilter(new PorterDuffColorFilter(Theme.getColor(Theme.key_chats_actionIcon), PorterDuff.Mode.MULTIPLY));
SharedPreferences preferences = MessagesController.getGlobalMainSettings();
boolean configAnimationsEnabled = preferences.getBoolean("view_animations", true);
floatingButton.setAnimation(configAnimationsEnabled ? R.raw.write_contacts_fab_icon : R.raw.write_contacts_fab_icon_reverse, 52, 52);
floatingButtonContainer.setContentDescription(LocaleController.getString("CreateNewContact", R.string.CreateNewContact));
if (Build.VERSION.SDK_INT >= 21) {
StateListAnimator animator = new StateListAnimator();
animator.addState(new int[] { android.R.attr.state_pressed }, ObjectAnimator.ofFloat(floatingButton, View.TRANSLATION_Z, AndroidUtilities.dp(2), AndroidUtilities.dp(4)).setDuration(200));
animator.addState(new int[] {}, ObjectAnimator.ofFloat(floatingButton, View.TRANSLATION_Z, AndroidUtilities.dp(4), AndroidUtilities.dp(2)).setDuration(200));
floatingButton.setStateListAnimator(animator);
floatingButton.setOutlineProvider(new ViewOutlineProvider() {
@SuppressLint("NewApi")
@Override
public void getOutline(View view, Outline outline) {
outline.setOval(0, 0, AndroidUtilities.dp(56), AndroidUtilities.dp(56));
}
});
}
floatingButtonContainer.addView(floatingButton, LayoutHelper.createFrame((Build.VERSION.SDK_INT >= 21 ? 56 : 60), (Build.VERSION.SDK_INT >= 21 ? 56 : 60), Gravity.LEFT | Gravity.TOP, 10, 6, 10, 0));
}
if (initialSearchString != null) {
actionBar.openSearchField(initialSearchString, false);
initialSearchString = null;
}
return fragmentView;
}
Aggregations