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
......@@ -27,35 +27,23 @@ import android.widget.LinearLayout;
import com.xabber.android.R;
import com.xabber.android.data.ActivityManager;
import com.xabber.android.data.Application;
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.extension.attention.AttentionManager;
import com.xabber.android.data.intent.EntityIntentBuilder;
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.adapter.ChatScrollIndicatorAdapter;
import com.xabber.android.ui.adapter.ChatViewerAdapter;
import com.xabber.android.ui.helper.ChatScroller;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.android.ui.helper.StatusBarPainter;
import java.util.Collection;
import java.util.HashSet;
/**
* Chat activity.
* <p/>
*
* @author alexander.ivanov
*/
public class ChatViewer extends ManagedActivity implements OnChatChangedListener,
OnContactChangedListener, OnAccountChangedListener, ViewPager.OnPageChangeListener,
ChatViewerAdapter.FinishUpdateListener, RecentChatFragment.RecentChatFragmentInteractionListener,
ChatViewerFragment.ChatViewerFragmentListener {
public class ChatViewer extends ManagedActivity implements ChatScroller.ChatScrollerListener, ChatScroller.ChatScrollerProvider {
/**
* Attention request.
......@@ -73,21 +61,12 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
private static final String SAVED_SELECTED_USER = "com.xabber.android.ui.ChatViewer.SAVED_SELECTED_USER";
private static final String SAVED_EXIT_ON_SEND = "com.xabber.android.ui.ChatViewer.EXIT_ON_SEND";
ChatScrollIndicatorAdapter chatScrollIndicatorAdapter;
ChatViewerAdapter chatViewerAdapter;
ViewPager viewPager;
Collection<ChatViewerFragment> registeredChats = new HashSet<>();
Collection<RecentChatFragment> recentChatFragments = new HashSet<>();
private boolean exitOnSend;
private String extraText = null;
private BaseEntity initialChat = null;
private BaseEntity selectedChat = null;
private StatusBarPainter statusBarPainter;
private ChatScroller chatScroller;
private boolean isRecentChatsSelected;
private StatusBarPainter statusBarPainter;
private boolean isVisible;
private BaseEntity initialChat = null;
public static void hideKeyboard(Activity activity) {
// Check if no view has focus:
......@@ -179,6 +158,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
statusBarPainter = new StatusBarPainter(this);
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
LinearLayout chatScrollIndicatorLayout = (LinearLayout) findViewById(R.id.chat_scroll_indicator);
chatScroller = new ChatScroller(this, viewPager, chatScrollIndicatorLayout);
getInitialChatFromIntent();
getSelectedPageDataFromIntent();
......@@ -189,26 +173,28 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
initChats();
}
private void initChats() {
if (initialChat != null) {
chatViewerAdapter = new ChatViewerAdapter(getFragmentManager(), initialChat, this);
} else {
chatViewerAdapter = new ChatViewerAdapter(getFragmentManager(), this);
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (isFinishing()) {
return;
}
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(chatViewerAdapter);
viewPager.setOnPageChangeListener(this);
setIntent(intent);
getSelectedPageDataFromIntent();
if (SettingsManager.chatsShowBackground()) {
viewPager.setBackgroundDrawable(getResources().getDrawable(R.drawable.chat_background_repeat));
if (intent.getAction() != null && intent.getAction().equals(ACTION_SHORTCUT)) {
getInitialChatFromIntent();
initChats();
}
}
chatScrollIndicatorAdapter = new ChatScrollIndicatorAdapter(this,
(LinearLayout)findViewById(R.id.chat_scroll_indicator));
chatScrollIndicatorAdapter.update(chatViewerAdapter.getActiveChats());
private void initChats() {
chatScroller.initChats(initialChat);
}
private void getInitialChatFromIntent() {
Intent intent = getIntent();
String account = getAccount(intent);
......@@ -227,46 +213,27 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
switch (intent.getAction()) {
case ACTION_RECENT_CHATS:
isRecentChatsSelected = true;
selectedChat = null;
chatScroller.setSelectedChat(null);
break;
case ACTION_SPECIFIC_CHAT:
case ACTION_ATTENTION:
case Intent.ACTION_SEND:
case ACTION_SHORTCUT:
isRecentChatsSelected = false;
selectedChat = new BaseEntity(getAccount(intent), getUser(intent));
chatScroller.setSelectedChat(new BaseEntity(getAccount(intent), getUser(intent)));
break;
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (isFinishing()) {
return;
}
setIntent(intent);
getSelectedPageDataFromIntent();
if (intent.getAction() != null && intent.getAction().equals(ACTION_SHORTCUT)) {
getInitialChatFromIntent();
initChats();
}
}
private void restoreInstanceState(Bundle savedInstanceState) {
isRecentChatsSelected = savedInstanceState.getBoolean(SAVED_IS_RECENT_CHATS_SELECTED);
if (isRecentChatsSelected) {
selectedChat = null;
boolean isRecentChatSelected = savedInstanceState.getBoolean(SAVED_IS_RECENT_CHATS_SELECTED);
if (isRecentChatSelected) {
chatScroller.setSelectedChat(null);
} else {
selectedChat = new BaseEntity(savedInstanceState.getString(SAVED_SELECTED_ACCOUNT),
savedInstanceState.getString(SAVED_SELECTED_USER));
chatScroller.setSelectedChat(new BaseEntity(savedInstanceState.getString(SAVED_SELECTED_ACCOUNT),
savedInstanceState.getString(SAVED_SELECTED_USER)));
}
exitOnSend = savedInstanceState.getBoolean(SAVED_EXIT_ON_SEND);
chatScroller.setExitOnSend(savedInstanceState.getBoolean(SAVED_EXIT_ON_SEND));
String initialAccount = savedInstanceState.getString(SAVED_INITIAL_ACCOUNT);
String initialUser = savedInstanceState.getString(SAVED_INITIAL_USER);
......@@ -279,49 +246,47 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override
protected void onResume() {
super.onResume();
isVisible = true;
chatScroller.setIsVisible(true);
Application.getInstance().addUIListener(OnChatChangedListener.class, this);
Application.getInstance().addUIListener(OnContactChangedListener.class, this);
Application.getInstance().addUIListener(OnAccountChangedListener.class, this);
Application.getInstance().addUIListener(OnChatChangedListener.class, chatScroller);
Application.getInstance().addUIListener(OnContactChangedListener.class, chatScroller);
Application.getInstance().addUIListener(OnAccountChangedListener.class, chatScroller);
chatViewerAdapter.updateChats();
chatScrollIndicatorAdapter.update(chatViewerAdapter.getActiveChats());
selectPage();
chatScroller.update();
Intent intent = getIntent();
if (hasAttention(intent)) {
AttentionManager.getInstance().removeAccountNotifications(selectedChat);
AttentionManager.getInstance().removeAccountNotifications(chatScroller.getSelectedChat());
}
if (Intent.ACTION_SEND.equals(intent.getAction())) {
extraText = intent.getStringExtra(Intent.EXTRA_TEXT);
String extraText = intent.getStringExtra(Intent.EXTRA_TEXT);
if (extraText != null) {
intent.removeExtra(Intent.EXTRA_TEXT);
exitOnSend = true;
chatScroller.setExtraText(extraText);
chatScroller.setExitOnSend(true);
}
}
}
private void selectPage() {
if (isRecentChatsSelected) {
selectRecentChatsPage();
} else {
selectChatPage(selectedChat, false);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(SAVED_IS_RECENT_CHATS_SELECTED, isRecentChatsSelected);
if (!isRecentChatsSelected) {
BaseEntity selectedChat = chatScroller.getSelectedChat();
outState.putBoolean(SAVED_IS_RECENT_CHATS_SELECTED, selectedChat == null);
if (selectedChat != null) {
outState.putString(SAVED_SELECTED_ACCOUNT, selectedChat.getAccount());
outState.putString(SAVED_SELECTED_USER, selectedChat.getUser());
}
outState.putBoolean(SAVED_EXIT_ON_SEND, exitOnSend);
outState.putBoolean(SAVED_EXIT_ON_SEND, chatScroller.isExitOnSend());
outState.putString(SAVED_INITIAL_ACCOUNT, initialChat.getAccount());
outState.putString(SAVED_INITIAL_USER, initialChat.getUser());
......@@ -330,176 +295,23 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override
protected void onPause() {
super.onPause();
Application.getInstance().removeUIListener(OnChatChangedListener.class, this);
Application.getInstance().removeUIListener(OnContactChangedListener.class, this);
Application.getInstance().removeUIListener(OnAccountChangedListener.class, this);
MessageManager.getInstance().removeVisibleChat();
isVisible = false;
}
private void selectChatPage(BaseEntity chat, boolean smoothScroll) {
selectPage(chatViewerAdapter.getPageNumber(chat), smoothScroll);
}
private void selectRecentChatsPage() {
selectPage(chatViewerAdapter.getPageNumber(null), false);
}
private void selectPage(int position, boolean smoothScroll) {
onPageSelected(position);
viewPager.setCurrentItem(position, smoothScroll);
}
@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();
}
}
}
Application.getInstance().removeUIListener(OnChatChangedListener.class, chatScroller);
Application.getInstance().removeUIListener(OnContactChangedListener.class, chatScroller);
Application.getInstance().removeUIListener(OnAccountChangedListener.class, chatScroller);
chatScroller.setIsVisible(false);
}
@Override
public void onContactsChanged(Collection<BaseEntity> entities) {
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
updateStatusBar();
}
@Override
public void onAccountsChanged(Collection<String> accounts) {
updateRegisteredChats();
updateRegisteredRecentChatsFragments();
updateStatusBar();
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
hideKeyboard(this);
chatScrollIndicatorAdapter.select(chatViewerAdapter.getRealPagePosition(position));
selectedChat = chatViewerAdapter.getChatByPageNumber(position);
isRecentChatsSelected = selectedChat == null;
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();
}
private void updateStatusBar() {
if (isRecentChatsSelected) {
public void onAccountSelected(String account) {
if (account == null) {
statusBarPainter.updateWithDefaultColor();
} else {
statusBarPainter.updateWithAccountName(selectedChat.getAccount());
statusBarPainter.updateWithAccountName(account);
}
}
private void updateRegisteredChats() {
for (ChatViewerFragment chat : registeredChats) {
chat.updateChat();
}
}
private void updateRegisteredRecentChatsFragments() {
for (RecentChatFragment recentChatFragment : recentChatFragments) {
recentChatFragment.updateChats(chatViewerAdapter.getActiveChats());
}
}
@Override
public void onPageScrollStateChanged(int state) {
}
public void registerRecentChatsList(RecentChatFragment recentChatFragment) {
recentChatFragments.add(recentChatFragment);
}
public void unregisterRecentChatsList(RecentChatFragment recentChatFragment) {
recentChatFragments.remove(recentChatFragment);
}
@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;
}
}
@Override
public void onChatSelected(AbstractChat chat) {
selectChatPage(chat, true);
}
public ChatViewerAdapter getChatViewerAdapter() {
return chatViewerAdapter;
}
@Override
public void onCloseChat() {
close();
}
@Override
public void onMessageSent() {
if (exitOnSend) {
close();
}
}
@Override
public void registerChat(ChatViewerFragment chat) {
registeredChats.add(chat);
}
@Override
public void unregisterChat(ChatViewerFragment chat) {
registeredChats.remove(chat);
}
private void close() {
public void onClose() {
finish();
if (!Intent.ACTION_SEND.equals(getIntent().getAction())) {
ActivityManager.getInstance().clearStack(false);
......@@ -508,4 +320,9 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
}
}
}
@Override
public ChatScroller getChatScroller() {
return chatScroller;
}
}
......@@ -50,6 +50,7 @@ import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.adapter.ChatMessageAdapter;
import com.xabber.android.ui.dialog.ChatExportDialogFragment;
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.preferences.ChatContactSettings;
......@@ -70,7 +71,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
private ImageButton securityButton;
private Toolbar toolbar;
private ChatViewerFragmentListener listener;
private ChatScroller.ChatScrollerProvider listener = null;
private Animation shakeAnimation = null;
private RecyclerView recyclerView;
private View contactTitleView;
......@@ -92,12 +93,12 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (ChatViewerFragmentListener) activity;
} catch (ClassCastException e) {
if (!(activity instanceof ChatScroller.ChatScrollerProvider)) {
throw new ClassCastException(activity.toString()
+ " must implement ChatViewerFragmentListener");
+ " must implement ChatScrollerProvider");
}
listener = (ChatScroller.ChatScrollerProvider) activity;
}
@Override
......@@ -224,7 +225,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
@Override
public void onResume() {
super.onResume();
listener.registerChat(this);
listener.getChatScroller().registerChat(this);
updateChat();
restoreInputState();
}
......@@ -296,7 +297,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
public void onPause() {
super.onPause();
saveInputState();
listener.unregisterChat(this);
listener.getChatScroller().unregisterChat(this);
}
public void saveInputState() {
......@@ -315,7 +316,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
sendMessage(text);
listener.onMessageSent();
listener.getChatScroller().onMessageSent();
if (SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.always
|| (getActivity().getResources().getBoolean(R.bool.landscape)
......@@ -579,7 +580,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
private void closeChat(String account, String user) {
MessageManager.getInstance().closeChat(account, user);
NotificationManager.getInstance().removeMessageNotification(account, user);
listener.onCloseChat();
listener.getChatScroller().onCloseChat();
}
private void clearHistory(String account, String user) {
......
......@@ -16,12 +16,14 @@ import com.xabber.android.R;
import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.ui.adapter.ChatListAdapter;
import com.xabber.android.ui.helper.AccountPainter;
import com.xabber.android.ui.helper.ChatScroller;
import java.util.ArrayList;
import java.util.List;
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
......@@ -41,17 +43,16 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
setListAdapter(new ChatListAdapter(getActivity()));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (RecentChatFragmentInteractionListener) activity;
} catch (ClassCastException e) {
if (!(activity instanceof ChatScroller.ChatScrollerProvider)) {
throw new ClassCastException(activity.toString()
+ " must implement RecentChatFragmentInteractionListener");
+ " must implement ChatScrollerProvider");
}
listener = (ChatScroller.ChatScrollerProvider) activity;
}
@Override
......@@ -59,8 +60,7 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.recent_chats, container, false);
ArrayList<AbstractChat> activeChats = ((ChatViewer) getActivity()).getChatViewerAdapter().getActiveChats();
((ChatListAdapter) getListAdapter()).updateChats(activeChats);
updateChats();
if (getListAdapter().isEmpty()) {
Activity activity = getActivity();
......@@ -90,14 +90,14 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
public void onResume() {
super.onResume();
((ChatViewer)getActivity()).registerRecentChatsList(this);
listener.getChatScroller().registerRecentChatsList(this);
}
@Override
public void onPause() {
super.onPause();
((ChatViewer)getActivity()).unregisterRecentChatsList(this);
listener.getChatScroller().unregisterRecentChatsList(this);
}
@Override
......@@ -112,7 +112,7 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
super.onListItemClick(l, v, position, id);
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
return false;
}
public void updateChats(List<AbstractChat> chats) {
((ChatListAdapter) getListAdapter()).updateChats(chats);
public void updateChats() {
((ChatListAdapter) getListAdapter()).updateChats(listener.getChatScroller().getActiveChats());
}
public interface RecentChatFragmentInteractionListener {
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;
import com.xabber.android.data.message.MessageManager;
import com.xabber.android.ui.ChatViewerFragment;
import com.xabber.android.ui.RecentChatFragment;
import com.xabber.android.ui.helper.ChatScroller;
import com.xabber.xmpp.address.Jid;
import java.util.ArrayList;
......@@ -21,23 +22,22 @@ import static java.lang.Math.abs;
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.
*/
private final AbstractChat intent;
private ArrayList<AbstractChat> activeChats;
private FinishUpdateListener finishUpdateListener;
private static final int TOTAL_COUNT = 200;
private static final int OFFSET = TOTAL_COUNT / 2;
private RecentChatFragment.RecentChatFragmentInteractionListener recentChatFragmentInteractionListener;
private Fragment currentFragment;
public ChatViewerAdapter(FragmentManager fragmentManager, BaseEntity chat, FinishUpdateListener finishUpdateListener) {
public ChatViewerAdapter(FragmentManager fragmentManager, BaseEntity chat, ChatScroller chatScroller) {
super(fragmentManager);
this.finishUpdateListener = finishUpdateListener;
finishUpdateListener = chatScroller;
recentChatFragmentInteractionListener = chatScroller;
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
intent = MessageManager.getInstance().getOrCreateChat(chat.getAccount(), Jid.getBareAddress(chat.getUser()));
......@@ -45,9 +45,10 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter {
updateChats();
}
public ChatViewerAdapter(FragmentManager fragmentManager, FinishUpdateListener finishUpdateListener) {
public ChatViewerAdapter(FragmentManager fragmentManager, ChatScroller chatScroller) {
super(fragmentManager);
this.finishUpdateListener = finishUpdateListener;
finishUpdateListener = chatScroller;
recentChatFragmentInteractionListener = chatScroller;
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
intent = null;
......@@ -165,10 +166,6 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter {
finishUpdateListener.onChatViewAdapterFinishUpdate();
}
public interface FinishUpdateListener {
public void onChatViewAdapterFinishUpdate();
}
public ArrayList<AbstractChat> getActiveChats() {
return activeChats;
}
......@@ -193,4 +190,8 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter {
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