Commit 617ba4b9 authored by Grigory Fedorov's avatar Grigory Fedorov

RecentChatFragment used as one of the page in the chat view pager.

Unstable, could hang up on chat select.
parent a5d5b4aa
...@@ -30,8 +30,7 @@ import android.util.AttributeSet; ...@@ -30,8 +30,7 @@ import android.util.AttributeSet;
import com.xabber.android.ui.adapter.ChatViewerAdapter; import com.xabber.android.ui.adapter.ChatViewerAdapter;
/** /**
* A {@link android.support.v4.view.ViewPager} that allows pseudo-infinite paging with a wrap-around effect. Should be used with an {@link * A {@link android.support.v4.view.ViewPager} that allows pseudo-infinite paging with a wrap-around effect.
* InfinitePagerAdapter}.
*/ */
public class InfiniteViewPager extends ViewPager { public class InfiniteViewPager extends ViewPager {
......
...@@ -14,28 +14,33 @@ ...@@ -14,28 +14,33 @@
*/ */
package com.xabber.android.ui; package com.xabber.android.ui;
import java.util.Collection;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
import android.view.View; import android.view.View;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
import com.xabber.android.data.account.OnAccountChangedListener; import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.entity.BaseEntity; import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.message.AbstractChat; 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.message.OnChatChangedListener;
import com.xabber.android.data.roster.OnContactChangedListener; import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.ui.adapter.ChatComparator;
import com.xabber.android.ui.adapter.ChatListAdapter; import com.xabber.android.ui.adapter.ChatListAdapter;
import com.xabber.android.ui.helper.ManagedListActivity; import com.xabber.android.ui.helper.ManagedListActivity;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
public class ChatList extends ManagedListActivity implements import java.util.ArrayList;
OnAccountChangedListener, OnContactChangedListener, import java.util.Collection;
OnChatChangedListener, OnItemClickListener { import java.util.Collections;
import java.util.List;
public class ChatList extends ManagedListActivity implements OnAccountChangedListener,
OnContactChangedListener, OnChatChangedListener, OnItemClickListener {
private ChatListAdapter listAdapter; private ChatListAdapter listAdapter;
...@@ -56,48 +61,39 @@ public class ChatList extends ManagedListActivity implements ...@@ -56,48 +61,39 @@ public class ChatList extends ManagedListActivity implements
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Application.getInstance().addUIListener(OnAccountChangedListener.class, Application.getInstance().addUIListener(OnAccountChangedListener.class, this);
this); Application.getInstance().addUIListener(OnContactChangedListener.class, this);
Application.getInstance().addUIListener(OnContactChangedListener.class, Application.getInstance().addUIListener(OnChatChangedListener.class, this);
this); updateAdapter();
Application.getInstance().addUIListener(OnChatChangedListener.class,
this);
listAdapter.onChange();
} }
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
Application.getInstance().removeUIListener( Application.getInstance().removeUIListener(OnAccountChangedListener.class, this);
OnAccountChangedListener.class, this); Application.getInstance().removeUIListener(OnContactChangedListener.class, this);
Application.getInstance().removeUIListener( Application.getInstance().removeUIListener(OnChatChangedListener.class, this);
OnContactChangedListener.class, this);
Application.getInstance().removeUIListener(OnChatChangedListener.class,
this);
} }
@Override @Override
public void onChatChanged(String account, String user, boolean incoming) { public void onChatChanged(String account, String user, boolean incoming) {
listAdapter.onChange(); updateAdapter();
} }
@Override @Override
public void onContactsChanged(Collection<BaseEntity> addresses) { public void onContactsChanged(Collection<BaseEntity> addresses) {
listAdapter.onChange(); updateAdapter();
} }
@Override @Override
public void onAccountsChanged(Collection<String> accounts) { public void onAccountsChanged(Collection<String> accounts) {
listAdapter.onChange(); updateAdapter();
} }
@Override @Override
public void onItemClick(AdapterView<?> parent, View view, int position, public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
long id) { AbstractChat chat = (AbstractChat) parent.getAdapter().getItem(position);
AbstractChat abstractChat = (AbstractChat) parent.getAdapter().getItem( startActivity(ChatViewer.createIntent(this, chat.getAccount(), chat.getUser()));
position);
startActivity(ChatViewer.createIntent(this, abstractChat.getAccount(),
abstractChat.getUser()));
finish(); finish();
} }
...@@ -105,4 +101,17 @@ public class ChatList extends ManagedListActivity implements ...@@ -105,4 +101,17 @@ public class ChatList extends ManagedListActivity implements
return new Intent(context, ChatList.class); return new Intent(context, ChatList.class);
} }
private void updateAdapter() {
List<AbstractChat> chats = new ArrayList<>();
chats.addAll(MessageManager.getInstance().getActiveChats());
if (chats.size() == 0) {
Toast.makeText(this, R.string.chat_list_is_empty, Toast.LENGTH_LONG).show();
finish();
}
Collections.sort(chats, ChatComparator.CHAT_COMPARATOR);
listAdapter.updateChats(chats);
}
} }
...@@ -49,7 +49,7 @@ import java.util.HashSet; ...@@ -49,7 +49,7 @@ import java.util.HashSet;
*/ */
public class ChatViewer extends ManagedActivity implements OnChatChangedListener, public class ChatViewer extends ManagedActivity implements OnChatChangedListener,
OnContactChangedListener, OnAccountChangedListener, ViewPager.OnPageChangeListener, OnContactChangedListener, OnAccountChangedListener, ViewPager.OnPageChangeListener,
ChatViewerAdapter.FinishUpdateListener { ChatViewerAdapter.FinishUpdateListener, RecentChatFragment.RecentChatFragmentInteractionListener {
/** /**
* Attention request. * Attention request.
...@@ -121,8 +121,10 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -121,8 +121,10 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
viewPager.setAdapter(chatViewerAdapter); viewPager.setAdapter(chatViewerAdapter);
viewPager.setOnPageChangeListener(this); viewPager.setOnPageChangeListener(this);
LogManager.i(this, "onCreate user: " + actionWithUser);
selectPage(actionWithAccount, actionWithUser); selectPage(false);
onChatSelected();
} }
@Override @Override
...@@ -176,19 +178,25 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -176,19 +178,25 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
LogManager.i(this, "onNewIntent account: " + account + ", user: " + user); LogManager.i(this, "onNewIntent account: " + account + ", user: " + user);
selectPage(account, user); actionWithUser = user;
actionWithAccount = user;
selectPage(false);
onChatSelected();
}
private void selectPage(boolean smoothScroll ) {
}
private void selectPage(String account, String user) { int position = chatViewerAdapter.getPageNumber(actionWithAccount, actionWithUser);
LogManager.i(this, "selectPage: account: " + account + ", user: " + user);
int position = chatViewerAdapter.getPosition(account, user); LogManager.i(this, "selectPage user: " + actionWithUser + " position: " + position);
viewPager.setCurrentItem(position); viewPager.setCurrentItem(position, smoothScroll);
onChatSelected(account, user); for (ChatViewerFragment chat : registeredChats) {
chat.updateChat(false);
}
} }
private static String getAccount(Intent intent) { private static String getAccount(Intent intent) {
...@@ -253,17 +261,34 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -253,17 +261,34 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override @Override
public void onChatChanged(final String account, final String user, public void onChatChanged(final String account, final String user,
final boolean incoming) { final boolean incoming) {
chatViewerAdapter.onChange(); LogManager.i(this, "onChatChanged " + user);
for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(account, user)) {
chat.updateChat(incoming);
}
}
} }
@Override @Override
public void onContactsChanged(Collection<BaseEntity> entities) { public void onContactsChanged(Collection<BaseEntity> entities) {
LogManager.i(this, "onContactsChanged");
chatViewerAdapter.onChange(); chatViewerAdapter.onChange();
for (ChatViewerFragment chat : registeredChats) {
chat.updateChat(false);
}
} }
@Override @Override
public void onAccountsChanged(Collection<String> accounts) { public void onAccountsChanged(Collection<String> accounts) {
LogManager.i(this, "onAccountsChanged");
chatViewerAdapter.onChange(); chatViewerAdapter.onChange();
for (ChatViewerFragment chat : registeredChats) {
chat.updateChat(false);
}
} }
void onSent() { void onSent() {
...@@ -289,35 +314,38 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -289,35 +314,38 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override @Override
public void onPageSelected(int position) { public void onPageSelected(int position) {
int realCount = chatViewerAdapter.getRealCount();
int virtualPosition = position % realCount;
AbstractChat chat = chatViewerAdapter.getChat(virtualPosition);
String account = chat.getAccount();
String user = chat.getUser();
actionWithAccount = account; AbstractChat selectedChat = chatViewerAdapter.getChatByPageNumber(position);
actionWithUser = user;
onChatSelected(account, user); if (selectedChat == null) {
setTitle(getString(R.string.chat_list));
return;
} }
private void onChatSelected(String account, String user) { actionWithAccount = selectedChat.getAccount();
actionWithAccount = account; actionWithUser = selectedChat.getUser();
actionWithUser = user;
MessageManager.getInstance().setVisibleChat(account, user); LogManager.i(this, "onPageSelected position: " + position + " user: " + actionWithUser + " position: " + position);
MessageArchiveManager.getInstance().requestHistory( onChatSelected();
account, user, 0, }
MessageManager.getInstance().getChat(account, user).getRequiredMessageCount());
NotificationManager.getInstance().removeMessageNotification(account, user); private void onChatSelected() {
LogManager.i(this, "onChatSelected user: " + actionWithUser);
final AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user); final AbstractContact abstractContact
= RosterManager.getInstance().getBestContact(actionWithAccount, actionWithUser);
setTitle(abstractContact.getName()); setTitle(abstractContact.getName());
MessageManager.getInstance().setVisibleChat(actionWithAccount, actionWithUser);
MessageArchiveManager.getInstance().requestHistory(
actionWithAccount, actionWithUser, 0,
MessageManager.getInstance().getChat(actionWithAccount, actionWithUser).getRequiredMessageCount());
NotificationManager.getInstance().removeMessageNotification(actionWithAccount, actionWithUser);
} }
@Override @Override
...@@ -335,15 +363,39 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -335,15 +363,39 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override @Override
public void onChatViewAdapterFinishUpdate() { public void onChatViewAdapterFinishUpdate() {
LogManager.i(this, "onChatViewAdapterFinishUpdate position: user: " + actionWithUser);
insertExtraText();
}
private void insertExtraText() {
if (extraText == null) {
return;
}
boolean isExtraTextInserted = false;
for (ChatViewerFragment chat : registeredChats) { for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(actionWithAccount, actionWithUser)) { if (chat.isEqual(actionWithAccount, actionWithUser)) {
chat.setInputFocus();
if (extraText != null) {
chat.setInputText(extraText); chat.setInputText(extraText);
extraText = null; isExtraTextInserted = true;
} }
} }
if (isExtraTextInserted) {
extraText = null;
}
} }
@Override
public void onRecentChatSelected(AbstractChat chat) {
actionWithAccount = chat.getAccount();
actionWithUser = chat.getUser();
LogManager.i(this, "onRecentChatSelected position: user: " + actionWithUser);
selectPage(true);
} }
} }
...@@ -53,6 +53,9 @@ import com.xabber.androiddev.R; ...@@ -53,6 +53,9 @@ import com.xabber.androiddev.R;
public class ChatViewerFragment extends Fragment { public class ChatViewerFragment extends Fragment {
public static final String ARGUMENT_ACCOUNT = "ARGUMENT_ACCOUNT";
public static final String ARGUMENT_USER = "ARGUMENT_USER";
private static final int MINIMUM_MESSAGES_TO_LOAD = 10; private static final int MINIMUM_MESSAGES_TO_LOAD = 10;
private TextView pageView; private TextView pageView;
...@@ -73,30 +76,31 @@ public class ChatViewerFragment extends Fragment { ...@@ -73,30 +76,31 @@ public class ChatViewerFragment extends Fragment {
private String account; private String account;
private String user; private String user;
private int activeChatIndex; public static ChatViewerFragment newInstance(String account, String user) {
private ActiveChatProvider activeChatProvider; ChatViewerFragment fragment = new ChatViewerFragment();
public static ChatViewerFragment newInstance() { Bundle arguments = new Bundle();
return new ChatViewerFragment(); arguments.putString(ARGUMENT_ACCOUNT, account);
arguments.putString(ARGUMENT_USER, user);
fragment.setArguments(arguments);
return fragment;
} }
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
avatarInflaterHelper = AbstractAvatarInflaterHelper.createAbstractContactInflaterHelper();
}
public void setActiveChat(ActiveChatProvider activeChatProvider, int activeChatIndex) { Bundle args = getArguments();
this.activeChatProvider = activeChatProvider; account = args.getString(ARGUMENT_ACCOUNT, null);
this.activeChatIndex = activeChatIndex; user = args.getString(ARGUMENT_USER, null);
avatarInflaterHelper = AbstractAvatarInflaterHelper.createAbstractContactInflaterHelper();
} }
@Override @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState); super.onCreateView(inflater, container, savedInstanceState);
updateUserAndAccount();
shakeAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.shake); shakeAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
/* /*
...@@ -123,6 +127,7 @@ public class ChatViewerFragment extends Fragment { ...@@ -123,6 +127,7 @@ public class ChatViewerFragment extends Fragment {
View view = inflater.inflate(R.layout.chat_viewer_item, container, false); View view = inflater.inflate(R.layout.chat_viewer_item, container, false);
chatMessageAdapter = new ChatMessageAdapter(getActivity()); chatMessageAdapter = new ChatMessageAdapter(getActivity());
chatMessageAdapter.setChat(account, user);
listView = (ListView) view.findViewById(android.R.id.list); listView = (ListView) view.findViewById(android.R.id.list);
listView.setAdapter(chatMessageAdapter); listView.setAdapter(chatMessageAdapter);
...@@ -208,12 +213,6 @@ public class ChatViewerFragment extends Fragment { ...@@ -208,12 +213,6 @@ public class ChatViewerFragment extends Fragment {
} }
private void updateUserAndAccount() {
AbstractChat activeChat = activeChatProvider.getActiveChat(activeChatIndex);
user = activeChat.getUser();
account = activeChat.getAccount();
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
...@@ -269,7 +268,7 @@ public class ChatViewerFragment extends Fragment { ...@@ -269,7 +268,7 @@ public class ChatViewerFragment extends Fragment {
} }
skipOnTextChanges = true; skipOnTextChanges = true;
inputView.setText(""); inputView.getText().clear();
skipOnTextChanges = false; skipOnTextChanges = false;
sendMessage(text); sendMessage(text);
...@@ -289,15 +288,8 @@ public class ChatViewerFragment extends Fragment { ...@@ -289,15 +288,8 @@ public class ChatViewerFragment extends Fragment {
updateChat(false); updateChat(false);
} }
private void updateMessages() {
chatMessageAdapter.onChange();
}
private void updateView() { private void updateView() {
updateUserAndAccount(); chatMessageAdapter.onChange();
chatMessageAdapter.setChat(account, user);
final AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user); final AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
...@@ -404,7 +396,7 @@ public class ChatViewerFragment extends Fragment { ...@@ -404,7 +396,7 @@ public class ChatViewerFragment extends Fragment {
new MenuItem.OnMenuItemClickListener() { new MenuItem.OnMenuItemClickListener() {
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
inputView.setText(""); inputView.getText().clear();
return true; return true;
} }
}); });
...@@ -587,9 +579,7 @@ public class ChatViewerFragment extends Fragment { ...@@ -587,9 +579,7 @@ public class ChatViewerFragment extends Fragment {
if (incomingMessage) { if (incomingMessage) {
titleView.findViewById(R.id.name_holder).startAnimation(shakeAnimation); titleView.findViewById(R.id.name_holder).startAnimation(shakeAnimation);
} }
updateUserAndAccount();
updateMessages();
updateView(); updateView();
} }
...@@ -605,7 +595,11 @@ public class ChatViewerFragment extends Fragment { ...@@ -605,7 +595,11 @@ public class ChatViewerFragment extends Fragment {
insertText(text); insertText(text);
} }
public interface ActiveChatProvider { public String getAccount() {
public AbstractChat getActiveChat(int index); return account;
}
public String getUser() {
return user;
} }
} }
package com.xabber.android.ui;
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.ui.adapter.ChatListAdapter;
import com.xabber.androiddev.R;
import java.util.List;
public class RecentChatFragment extends ListFragment {
private RecentChatFragmentInteractionListener listener;
private List<AbstractChat> initialChats = null;
public static RecentChatFragment newInstance() {
return new RecentChatFragment();
}
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public RecentChatFragment() {
}
public void setInitialChats(List<AbstractChat> initialChats) {
this.initialChats = initialChats;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ChatListAdapter(getActivity()));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (RecentChatFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement RecentChatFragmentInteractionListener");
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
if (initialChats != null) {
((ChatListAdapter) getListAdapter()).updateChats(initialChats);
initialChats = null;
}
return inflater.inflate(R.layout.list, container, false);
}
@Override
public void onDetach() {
super.onDetach();
}
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (null != listener) {
listener.onRecentChatSelected((AbstractChat) getListAdapter().getItem(position));
}
}
public interface RecentChatFragmentInteractionListener {
public void onRecentChatSelected(AbstractChat chat);
}
}
/**
* Copyright (c) 2013, Redsolution LTD. All rights reserved.
*
* This file is part of Xabber project; you can redistribute it and/or
* modify it under the terms of the GNU General Public License, Version 3.
*
* Xabber is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License,
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.xabber.android.ui.adapter; package com.xabber.android.ui.adapter;
import java.util.ArrayList; import android.content.Context;
import java.util.Collections; import android.view.LayoutInflater;
import android.app.Activity;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.BaseAdapter; import android.widget.BaseAdapter;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import com.xabber.android.data.message.AbstractChat; 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.data.notification.NotificationManager; import com.xabber.android.data.notification.NotificationManager;
import com.xabber.android.data.roster.AbstractContact; import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.RosterManager; import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.ChatList;
import com.xabber.android.ui.helper.AbstractAvatarInflaterHelper; import com.xabber.android.ui.helper.AbstractAvatarInflaterHelper;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
/** import java.util.ArrayList;
* Adapter for {@link ChatList}. import java.util.List;
*
* @author alexander.ivanov
*/
public class ChatListAdapter extends BaseAdapter implements UpdatableAdapter {
private final Activity activity; public class ChatListAdapter extends BaseAdapter {
private final ArrayList<AbstractChat> abstractChats; private List<AbstractChat> chats;
private final AbstractAvatarInflaterHelper avatarInflaterHelper; private final AbstractAvatarInflaterHelper avatarInflaterHelper;
public ChatListAdapter(Activity activity) { private final Context context;
this.activity = activity;
abstractChats = new ArrayList<AbstractChat>();
avatarInflaterHelper = AbstractAvatarInflaterHelper
.createAbstractContactInflaterHelper();
}
@Override
public void onChange() { public ChatListAdapter(Context context) {
abstractChats.clear(); this.context = context;
abstractChats.addAll(MessageManager.getInstance().getActiveChats()); chats = new ArrayList<>();
Collections.sort(abstractChats, ChatComparator.CHAT_COMPARATOR); avatarInflaterHelper = AbstractAvatarInflaterHelper.createAbstractContactInflaterHelper();
if (abstractChats.size() == 0) {
Toast.makeText(activity, R.string.chat_list_is_empty,
Toast.LENGTH_LONG).show();
activity.finish();
return;
} }
public void updateChats(List<AbstractChat> chats) {
this.chats.clear();
this.chats.addAll(chats);
notifyDataSetChanged(); notifyDataSetChanged();
} }
@Override @Override
public int getCount() { public int getCount() {
return abstractChats.size(); return chats.size();
} }
@Override @Override
public Object getItem(int position) { public Object getItem(int position) {
return abstractChats.get(position); return chats.get(position);
} }
@Override @Override
...@@ -87,33 +59,32 @@ public class ChatListAdapter extends BaseAdapter implements UpdatableAdapter { ...@@ -87,33 +59,32 @@ public class ChatListAdapter extends BaseAdapter implements UpdatableAdapter {
public View getView(int position, View convertView, ViewGroup parent) { public View getView(int position, View convertView, ViewGroup parent) {
final View view; final View view;
if (convertView == null) { if (convertView == null) {
view = activity.getLayoutInflater().inflate( view = LayoutInflater.from(context).inflate(R.layout.chat_list_item, parent, false);
R.layout.chat_list_item, parent, false);
} else { } else {
view = convertView; view = convertView;
} }
final AbstractChat abstractChat = (AbstractChat) getItem(position); final AbstractChat abstractChat = (AbstractChat) getItem(position);
final AbstractContact abstractContact = RosterManager.getInstance() final AbstractContact abstractContact = RosterManager.getInstance()
.getBestContact(abstractChat.getAccount(), .getBestContact(abstractChat.getAccount(), abstractChat.getUser());
abstractChat.getUser());
final ImageView colorView = (ImageView) view.findViewById(R.id.color); ((TextView) view.findViewById(R.id.name)).setText(abstractContact.getName());
((ImageView) view.findViewById(R.id.color)).setImageLevel(abstractContact.getColorLevel());
final ImageView avatarView = (ImageView) view.findViewById(R.id.avatar); final ImageView avatarView = (ImageView) view.findViewById(R.id.avatar);
final TextView nameView = (TextView) view.findViewById(R.id.name);
final TextView textView = (TextView) view.findViewById(R.id.text);
colorView.setImageLevel(abstractContact.getColorLevel());
avatarView.setImageDrawable(abstractContact.getAvatar()); avatarView.setImageDrawable(abstractContact.getAvatar());
avatarInflaterHelper.updateAvatar(avatarView, abstractContact); avatarInflaterHelper.updateAvatar(avatarView, abstractContact);
nameView.setText(abstractContact.getName());
final TextView textView = (TextView) view.findViewById(R.id.text);
String statusText = MessageManager.getInstance().getLastText( String statusText = MessageManager.getInstance().getLastText(
abstractContact.getAccount(), abstractContact.getUser()); abstractContact.getAccount(), abstractContact.getUser());
textView.setText(statusText); textView.setText(statusText);
boolean newMessages = NotificationManager.getInstance() boolean newMessages = NotificationManager.getInstance()
.getNotificationMessageCount(abstractChat.getAccount(), .getNotificationMessageCount(abstractChat.getAccount(), abstractChat.getUser()) > 0;
abstractChat.getUser()) > 0; textView.setTextAppearance(context, newMessages ? R.style.ChatList_Notification
textView.setTextAppearance(activity,
newMessages ? R.style.ChatList_Notification
: R.style.ChatList_Normal); : R.style.ChatList_Normal);
return view; return view;
} }
} }
...@@ -9,14 +9,14 @@ import android.view.ViewGroup; ...@@ -9,14 +9,14 @@ import android.view.ViewGroup;
import com.xabber.android.data.message.AbstractChat; 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.xmpp.address.Jid; import com.xabber.xmpp.address.Jid;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.Comparator; import java.util.Comparator;
public class ChatViewerAdapter extends FragmentStatePagerAdapter implements UpdatableAdapter, public class ChatViewerAdapter extends FragmentStatePagerAdapter implements UpdatableAdapter {
ChatViewerFragment.ActiveChatProvider {
/** /**
* Intent sent while opening chat activity. * Intent sent while opening chat activity.
...@@ -27,6 +27,8 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -27,6 +27,8 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
private FinishUpdateListener finishUpdateListener; private FinishUpdateListener finishUpdateListener;
private Fragment currentFragment;
public ChatViewerAdapter(FragmentManager fragmentManager, String account, String user, FinishUpdateListener finishUpdateListener) { public ChatViewerAdapter(FragmentManager fragmentManager, String account, String user, FinishUpdateListener finishUpdateListener) {
super(fragmentManager); super(fragmentManager);
this.finishUpdateListener = finishUpdateListener; this.finishUpdateListener = finishUpdateListener;
...@@ -49,38 +51,26 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -49,38 +51,26 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
public int getRealCount() { public int getRealCount() {
int realCount = activeChats.size(); int realCount = activeChats.size();
if (realCount == 1) {
return 4;
} else if (realCount == 2 || realCount == 3) {
return realCount * 2;
} else {
return realCount;
}
}
@Override return realCount + 1;
public Fragment getItem(int i) {
int position = i % activeChats.size();
ChatViewerFragment chatViewerFragment = ChatViewerFragment.newInstance();
chatViewerFragment.setActiveChat(this, position);
return chatViewerFragment;
} }
public AbstractChat getChat(int i) { @Override
int realCount = activeChats.size(); public Fragment getItem(int virtualPagePosition) {
int position; int chatIndex;
int realPosition = virtualPagePosition % getRealCount();
if (realCount == 1) { if (realPosition == 0) {
position = 0; RecentChatFragment activeChatFragment = RecentChatFragment.newInstance();
} else if (realCount == 2 || realCount == 3) { activeChatFragment.setInitialChats(activeChats);
position = i % realCount; return activeChatFragment;
} else { } else {
position = i; chatIndex = realPosition -1;
} }
return activeChats.get(position); AbstractChat chat = activeChats.get(chatIndex);
return ChatViewerFragment.newInstance(chat.getAccount(), chat.getUser());
} }
@Override @Override
...@@ -101,25 +91,46 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -101,25 +91,46 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
notifyDataSetChanged(); notifyDataSetChanged();
} }
public int getPosition(String account, String user) { public int getPageNumber(String account, String user) {
for (int position = 0; position < activeChats.size(); position++) { for (int position = 0; position < activeChats.size(); position++) {
if (activeChats.get(position).equals(account, user)) { if (activeChats.get(position).equals(account, user)) {
return position; return position + 1;
} }
} }
return -1; return -1;
} }
public AbstractChat getChatByPageNumber(int virtualPosition) {
int realPosition = virtualPosition % getRealCount();
int chatIndex = realPosition - 1;
if (chatIndex < 0) {
return null;
}
return activeChats.get(chatIndex);
}
@Override
public void startUpdate(ViewGroup container) {
if (currentFragment instanceof ChatViewerFragment) {
((ChatViewerFragment)currentFragment).saveInputState();
}
super.startUpdate(container);
}
@Override @Override
public void finishUpdate(ViewGroup container) { public void finishUpdate(ViewGroup container) {
super.finishUpdate(container); super.finishUpdate(container);
finishUpdateListener.onChatViewAdapterFinishUpdate(); finishUpdateListener.onChatViewAdapterFinishUpdate();
}
@Override if (currentFragment instanceof ChatViewerFragment) {
public AbstractChat getActiveChat(int index) { ((ChatViewerFragment)currentFragment).updateChat(false);
return getChat(index); ((ChatViewerFragment)currentFragment).setInputFocus();
}
} }
public interface FinishUpdateListener { public interface FinishUpdateListener {
...@@ -127,7 +138,15 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -127,7 +138,15 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
} }
@Override @Override
public int getItemPosition(Object object) { public void setPrimaryItem(ViewGroup container, int position, Object object) {
return POSITION_NONE; if (getCurrentFragment() != object) {
currentFragment = ((Fragment) object);
}
super.setPrimaryItem(container, position, object);
}
public Fragment getCurrentFragment() {
return currentFragment;
} }
} }
\ No newline at end of file
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