Commit aef8e848 authored by Grigory Fedorov's avatar Grigory Fedorov

Preparing for landscape contact list and chat screen. New class ChatScroller...

Preparing for landscape contact list and chat screen. New class ChatScroller excluded from ChatViewer activity.
parent 535591fd
...@@ -50,6 +50,7 @@ import com.xabber.android.data.roster.RosterManager; ...@@ -50,6 +50,7 @@ import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.adapter.ChatMessageAdapter; import com.xabber.android.ui.adapter.ChatMessageAdapter;
import com.xabber.android.ui.dialog.ChatExportDialogFragment; import com.xabber.android.ui.dialog.ChatExportDialogFragment;
import com.xabber.android.ui.helper.AccountPainter; import com.xabber.android.ui.helper.AccountPainter;
import com.xabber.android.ui.helper.ChatScroller;
import com.xabber.android.ui.helper.ContactTitleInflater; import com.xabber.android.ui.helper.ContactTitleInflater;
import com.xabber.android.ui.preferences.ChatContactSettings; import com.xabber.android.ui.preferences.ChatContactSettings;
...@@ -70,7 +71,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -70,7 +71,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
private ImageButton securityButton; private ImageButton securityButton;
private Toolbar toolbar; private Toolbar toolbar;
private ChatViewerFragmentListener listener; private ChatScroller.ChatScrollerProvider listener = null;
private Animation shakeAnimation = null; private Animation shakeAnimation = null;
private RecyclerView recyclerView; private RecyclerView recyclerView;
private View contactTitleView; private View contactTitleView;
...@@ -92,12 +93,12 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -92,12 +93,12 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
try { if (!(activity instanceof ChatScroller.ChatScrollerProvider)) {
listener = (ChatViewerFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() throw new ClassCastException(activity.toString()
+ " must implement ChatViewerFragmentListener"); + " must implement ChatScrollerProvider");
} }
listener = (ChatScroller.ChatScrollerProvider) activity;
} }
@Override @Override
...@@ -224,7 +225,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -224,7 +225,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
listener.registerChat(this); listener.getChatScroller().registerChat(this);
updateChat(); updateChat();
restoreInputState(); restoreInputState();
} }
...@@ -296,7 +297,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -296,7 +297,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
saveInputState(); saveInputState();
listener.unregisterChat(this); listener.getChatScroller().unregisterChat(this);
} }
public void saveInputState() { public void saveInputState() {
...@@ -315,7 +316,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -315,7 +316,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
sendMessage(text); sendMessage(text);
listener.onMessageSent(); listener.getChatScroller().onMessageSent();
if (SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.always if (SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.always
|| (getActivity().getResources().getBoolean(R.bool.landscape) || (getActivity().getResources().getBoolean(R.bool.landscape)
...@@ -579,7 +580,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem ...@@ -579,7 +580,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
private void closeChat(String account, String user) { private void closeChat(String account, String user) {
MessageManager.getInstance().closeChat(account, user); MessageManager.getInstance().closeChat(account, user);
NotificationManager.getInstance().removeMessageNotification(account, user); NotificationManager.getInstance().removeMessageNotification(account, user);
listener.onCloseChat(); listener.getChatScroller().onCloseChat();
} }
private void clearHistory(String account, String user) { private void clearHistory(String account, String user) {
......
...@@ -16,12 +16,14 @@ import com.xabber.android.R; ...@@ -16,12 +16,14 @@ import com.xabber.android.R;
import com.xabber.android.data.message.AbstractChat; import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.ui.adapter.ChatListAdapter; import com.xabber.android.ui.adapter.ChatListAdapter;
import com.xabber.android.ui.helper.AccountPainter; import com.xabber.android.ui.helper.AccountPainter;
import com.xabber.android.ui.helper.ChatScroller;
import java.util.ArrayList;
import java.util.List; import java.util.List;
public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuItemClickListener { public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuItemClickListener {
private RecentChatFragmentInteractionListener listener;
private ChatScroller.ChatScrollerProvider listener = null;
/** /**
* Mandatory empty constructor for the fragment manager to instantiate the * Mandatory empty constructor for the fragment manager to instantiate the
...@@ -41,17 +43,16 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt ...@@ -41,17 +43,16 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
setListAdapter(new ChatListAdapter(getActivity())); setListAdapter(new ChatListAdapter(getActivity()));
} }
@Override @Override
public void onAttach(Activity activity) { public void onAttach(Activity activity) {
super.onAttach(activity); super.onAttach(activity);
try { if (!(activity instanceof ChatScroller.ChatScrollerProvider)) {
listener = (RecentChatFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() throw new ClassCastException(activity.toString()
+ " must implement RecentChatFragmentInteractionListener"); + " must implement ChatScrollerProvider");
} }
listener = (ChatScroller.ChatScrollerProvider) activity;
} }
@Override @Override
...@@ -59,8 +60,7 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt ...@@ -59,8 +60,7 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
super.onCreateView(inflater, container, savedInstanceState); super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.recent_chats, container, false); View rootView = inflater.inflate(R.layout.recent_chats, container, false);
ArrayList<AbstractChat> activeChats = ((ChatViewer) getActivity()).getChatViewerAdapter().getActiveChats(); updateChats();
((ChatListAdapter) getListAdapter()).updateChats(activeChats);
if (getListAdapter().isEmpty()) { if (getListAdapter().isEmpty()) {
Activity activity = getActivity(); Activity activity = getActivity();
...@@ -90,14 +90,14 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt ...@@ -90,14 +90,14 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
((ChatViewer)getActivity()).registerRecentChatsList(this); listener.getChatScroller().registerRecentChatsList(this);
} }
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
((ChatViewer)getActivity()).unregisterRecentChatsList(this); listener.getChatScroller().unregisterRecentChatsList(this);
} }
@Override @Override
...@@ -112,7 +112,7 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt ...@@ -112,7 +112,7 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
super.onListItemClick(l, v, position, id); super.onListItemClick(l, v, position, id);
if (null != listener) { if (null != listener) {
listener.onChatSelected((AbstractChat) getListAdapter().getItem(position)); listener.getChatScroller().onChatSelected((AbstractChat) getListAdapter().getItem(position));
} }
} }
...@@ -125,11 +125,17 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt ...@@ -125,11 +125,17 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
return false; return false;
} }
public void updateChats(List<AbstractChat> chats) { public void updateChats() {
((ChatListAdapter) getListAdapter()).updateChats(chats); ((ChatListAdapter) getListAdapter()).updateChats(listener.getChatScroller().getActiveChats());
} }
public interface RecentChatFragmentInteractionListener { public interface RecentChatFragmentInteractionListener {
void onChatSelected(AbstractChat chat); void onChatSelected(AbstractChat chat);
void registerRecentChatsList(RecentChatFragment fragment);
void unregisterRecentChatsList(RecentChatFragment fragment);
List<AbstractChat> getActiveChats();
} }
} }
...@@ -11,6 +11,7 @@ import com.xabber.android.data.message.AbstractChat; ...@@ -11,6 +11,7 @@ import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.data.message.MessageManager; import com.xabber.android.data.message.MessageManager;
import com.xabber.android.ui.ChatViewerFragment; import com.xabber.android.ui.ChatViewerFragment;
import com.xabber.android.ui.RecentChatFragment; import com.xabber.android.ui.RecentChatFragment;
import com.xabber.android.ui.helper.ChatScroller;
import com.xabber.xmpp.address.Jid; import com.xabber.xmpp.address.Jid;
import java.util.ArrayList; import java.util.ArrayList;
...@@ -21,23 +22,22 @@ import static java.lang.Math.abs; ...@@ -21,23 +22,22 @@ import static java.lang.Math.abs;
public class ChatViewerAdapter extends FragmentStatePagerAdapter { public class ChatViewerAdapter extends FragmentStatePagerAdapter {
private static final int TOTAL_COUNT = 200;
private static final int OFFSET = TOTAL_COUNT / 2;
/** /**
* Intent sent while opening chat activity. * Intent sent while opening chat activity.
*/ */
private final AbstractChat intent; private final AbstractChat intent;
private ArrayList<AbstractChat> activeChats; private ArrayList<AbstractChat> activeChats;
private FinishUpdateListener finishUpdateListener; private FinishUpdateListener finishUpdateListener;
private RecentChatFragment.RecentChatFragmentInteractionListener recentChatFragmentInteractionListener;
private static final int TOTAL_COUNT = 200;
private static final int OFFSET = TOTAL_COUNT / 2;
private Fragment currentFragment; private Fragment currentFragment;
public ChatViewerAdapter(FragmentManager fragmentManager, BaseEntity chat, FinishUpdateListener finishUpdateListener) { public ChatViewerAdapter(FragmentManager fragmentManager, BaseEntity chat, ChatScroller chatScroller) {
super(fragmentManager); super(fragmentManager);
this.finishUpdateListener = finishUpdateListener; finishUpdateListener = chatScroller;
recentChatFragmentInteractionListener = chatScroller;
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats()); activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
intent = MessageManager.getInstance().getOrCreateChat(chat.getAccount(), Jid.getBareAddress(chat.getUser())); intent = MessageManager.getInstance().getOrCreateChat(chat.getAccount(), Jid.getBareAddress(chat.getUser()));
...@@ -45,9 +45,10 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter { ...@@ -45,9 +45,10 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter {
updateChats(); updateChats();
} }
public ChatViewerAdapter(FragmentManager fragmentManager, FinishUpdateListener finishUpdateListener) { public ChatViewerAdapter(FragmentManager fragmentManager, ChatScroller chatScroller) {
super(fragmentManager); super(fragmentManager);
this.finishUpdateListener = finishUpdateListener; finishUpdateListener = chatScroller;
recentChatFragmentInteractionListener = chatScroller;
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats()); activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
intent = null; intent = null;
...@@ -165,10 +166,6 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter { ...@@ -165,10 +166,6 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter {
finishUpdateListener.onChatViewAdapterFinishUpdate(); finishUpdateListener.onChatViewAdapterFinishUpdate();
} }
public interface FinishUpdateListener {
public void onChatViewAdapterFinishUpdate();
}
public ArrayList<AbstractChat> getActiveChats() { public ArrayList<AbstractChat> getActiveChats() {
return activeChats; return activeChats;
} }
...@@ -193,4 +190,8 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter { ...@@ -193,4 +190,8 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter {
return currentFragment; return currentFragment;
} }
public interface FinishUpdateListener {
void onChatViewAdapterFinishUpdate();
}
} }
\ No newline at end of file
package com.xabber.android.ui.helper;
import android.app.Activity;
import android.support.v4.view.ViewPager;
import android.widget.LinearLayout;
import com.xabber.android.R;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.extension.archive.MessageArchiveManager;
import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.data.message.MessageManager;
import com.xabber.android.data.message.OnChatChangedListener;
import com.xabber.android.data.notification.NotificationManager;
import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.ui.ChatViewer;
import com.xabber.android.ui.ChatViewerFragment;
import com.xabber.android.ui.RecentChatFragment;
import com.xabber.android.ui.adapter.ChatScrollIndicatorAdapter;
import com.xabber.android.ui.adapter.ChatViewerAdapter;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
public class ChatScroller implements
OnChatChangedListener,
OnContactChangedListener,
OnAccountChangedListener,
ViewPager.OnPageChangeListener,
ChatViewerAdapter.FinishUpdateListener,
RecentChatFragment.RecentChatFragmentInteractionListener,
ChatViewerFragment.ChatViewerFragmentListener {
ViewPager viewPager;
ChatViewerAdapter chatViewerAdapter;
ChatScrollIndicatorAdapter chatScrollIndicatorAdapter;
Collection<ChatViewerFragment> registeredChats = new HashSet<>();
Collection<RecentChatFragment> recentChatFragments = new HashSet<>();
ChatScrollerListener listener;
Activity activity;
private boolean isRecentChatsSelected;
private BaseEntity selectedChat = null;
private boolean isVisible;
private String extraText = null;
private boolean exitOnSend = false;
public ChatScroller(Activity activity, ViewPager viewPager, LinearLayout chatScrollIndicatorLayout) {
this.viewPager = viewPager;
this.activity = activity;
this.listener = (ChatScrollerListener) activity;
this.chatScrollIndicatorAdapter = new ChatScrollIndicatorAdapter(activity, chatScrollIndicatorLayout);
}
public BaseEntity getSelectedChat() {
return selectedChat;
}
public void setSelectedChat(BaseEntity selectedChat) {
this.selectedChat = selectedChat;
isRecentChatsSelected = selectedChat == null;
}
public void setIsVisible(boolean isVisible) {
this.isVisible = isVisible;
if (!isVisible) {
MessageManager.getInstance().removeVisibleChat();
}
}
public boolean isExitOnSend() {
return exitOnSend;
}
public void setExitOnSend(boolean exitOnSend) {
this.exitOnSend = exitOnSend;
}
public void setExtraText(String extraText) {
this.extraText = extraText;
}
public void initChats(BaseEntity initialChat) {
if (initialChat != null) {
chatViewerAdapter = new ChatViewerAdapter(activity.getFragmentManager(), initialChat, this);
} else {
chatViewerAdapter = new ChatViewerAdapter(activity.getFragmentManager(), this);
}
viewPager.setAdapter(chatViewerAdapter);
viewPager.setOnPageChangeListener(this);
if (SettingsManager.chatsShowBackground()) {
viewPager.setBackgroundDrawable(activity.getResources().getDrawable(R.drawable.chat_background_repeat));
}
chatScrollIndicatorAdapter.update(chatViewerAdapter.getActiveChats());
}
public void update() {
chatViewerAdapter.updateChats();
chatScrollIndicatorAdapter.update(chatViewerAdapter.getActiveChats());
selectPage();
}
private void selectPage() {
if (isRecentChatsSelected) {
selectRecentChatsPage();
} else {
selectChatPage(selectedChat, false);
}
}
private void selectRecentChatsPage() {
selectPage(chatViewerAdapter.getPageNumber(null), false);
}
private void selectChatPage(BaseEntity chat, boolean smoothScroll) {
selectPage(chatViewerAdapter.getPageNumber(chat), smoothScroll);
}
private void selectPage(int position, boolean smoothScroll) {
onPageSelected(position);
viewPager.setCurrentItem(position, smoothScroll);
}
private void updateRegisteredChats() {
for (ChatViewerFragment chat : registeredChats) {
chat.updateChat();
}
}
private void updateRegisteredRecentChatsFragments() {
for (RecentChatFragment recentChatFragment : recentChatFragments) {
recentChatFragment.updateChats();
}
}
private void updateStatusBar() {
if (isRecentChatsSelected) {
listener.onAccountSelected(null);
} else {
listener.onAccountSelected(selectedChat.getAccount());
}
}
/**
* OnChatChangedListener
*/
@Override
public void onChatChanged(final String account, final String user, final boolean incoming) {
if (chatViewerAdapter.updateChats()) {
chatScrollIndicatorAdapter.update(chatViewerAdapter.getActiveChats());
selectPage();
} else {
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
updateStatusBar();
for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(selectedChat) && incoming) {
chat.playIncomingAnimation();
}
}
}
}
/**
* OnContactChangedListener
*/
@Override
public void onContactsChanged(Collection<BaseEntity> entities) {
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
updateStatusBar();
}
/**
* OnAccountChangedListener
*/
@Override
public void onAccountsChanged(Collection<String> accounts) {
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
updateStatusBar();
}
/**
* OnPageChangeListener
*/
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
ChatViewer.hideKeyboard(activity);
chatScrollIndicatorAdapter.select(chatViewerAdapter.getRealPagePosition(position));
setSelectedChat(chatViewerAdapter.getChatByPageNumber(position));
if (isRecentChatsSelected) {
MessageManager.getInstance().removeVisibleChat();
} else {
if (isVisible) {
MessageManager.getInstance().setVisibleChat(selectedChat);
}
MessageArchiveManager.getInstance().requestHistory(selectedChat.getAccount(), selectedChat.getUser(), 0,
MessageManager.getInstance().getChat(selectedChat.getAccount(), selectedChat.getUser()).getRequiredMessageCount());
NotificationManager.getInstance().removeMessageNotification(selectedChat.getAccount(), selectedChat.getUser());
}
updateStatusBar();
}
@Override
public void onPageScrollStateChanged(int state) {
}
/**
* RecentChatFragmentInteractionListener
*/
@Override
public void onChatSelected(AbstractChat chat) {
selectChatPage(chat, true);
}
@Override
public void registerRecentChatsList(RecentChatFragment recentChatFragment) {
recentChatFragments.add(recentChatFragment);
}
@Override
public void unregisterRecentChatsList(RecentChatFragment recentChatFragment) {
recentChatFragments.remove(recentChatFragment);
}
@Override
public List<AbstractChat> getActiveChats() {
return chatViewerAdapter.getActiveChats();
}
/**
* ChatViewerFragmentListener
*/
@Override
public void onCloseChat() {
listener.onClose();
}
@Override
public void onMessageSent() {
if (exitOnSend) {
listener.onClose();
}
}
@Override
public void registerChat(ChatViewerFragment chat) {
registeredChats.add(chat);
}
@Override
public void unregisterChat(ChatViewerFragment chat) {
registeredChats.remove(chat);
}
/**
* FinishUpdateListener
*/
@Override
public void onChatViewAdapterFinishUpdate() {
insertExtraText();
}
private void insertExtraText() {
if (extraText == null) {
return;
}
boolean isExtraTextInserted = false;
for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(selectedChat)) {
chat.setInputText(extraText);
isExtraTextInserted = true;
}
}
if (isExtraTextInserted) {
extraText = null;
}
}
public interface ChatScrollerListener {
void onAccountSelected(String account);
void onClose();
}
public interface ChatScrollerProvider {
ChatScroller getChatScroller();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment