Commit f24bde0c authored by Grigory Fedorov's avatar Grigory Fedorov

View pager and FragmentStatePagerAdapter used to scroll active chats.

parent 68946a30
...@@ -27,4 +27,5 @@ android { ...@@ -27,4 +27,5 @@ android {
dependencies { dependencies {
compile files('libs/otr4j.jar') compile files('libs/otr4j.jar')
compile 'com.android.support:appcompat-v7:21.0.3' compile 'com.android.support:appcompat-v7:21.0.3'
compile 'com.android.support:support-v13:21.0.3'
} }
...@@ -17,6 +17,7 @@ package com.xabber.android.ui; ...@@ -17,6 +17,7 @@ package com.xabber.android.ui;
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.support.v4.view.ViewPager;
import com.xabber.android.data.ActivityManager; import com.xabber.android.data.ActivityManager;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
...@@ -26,14 +27,17 @@ import com.xabber.android.data.entity.BaseEntity; ...@@ -26,14 +27,17 @@ import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.extension.archive.MessageArchiveManager; import com.xabber.android.data.extension.archive.MessageArchiveManager;
import com.xabber.android.data.extension.attention.AttentionManager; import com.xabber.android.data.extension.attention.AttentionManager;
import com.xabber.android.data.intent.EntityIntentBuilder; 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.MessageManager;
import com.xabber.android.data.message.OnChatChangedListener; import com.xabber.android.data.message.OnChatChangedListener;
import com.xabber.android.data.notification.NotificationManager; import com.xabber.android.data.notification.NotificationManager;
import com.xabber.android.data.roster.OnContactChangedListener; import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.ui.adapter.ChatViewerAdapter;
import com.xabber.android.ui.helper.ManagedActivity; import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
import java.util.Collection; import java.util.Collection;
import java.util.HashSet;
/** /**
* Chat activity. * Chat activity.
...@@ -42,7 +46,7 @@ import java.util.Collection; ...@@ -42,7 +46,7 @@ import java.util.Collection;
* @author alexander.ivanov * @author alexander.ivanov
*/ */
public class ChatViewer extends ManagedActivity implements OnChatChangedListener, public class ChatViewer extends ManagedActivity implements OnChatChangedListener,
OnContactChangedListener, OnAccountChangedListener { OnContactChangedListener, OnAccountChangedListener, ViewPager.OnPageChangeListener {
/** /**
* Attention request. * Attention request.
...@@ -53,11 +57,18 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -53,11 +57,18 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
private static final String SAVED_USER = "com.xabber.android.ui.ChatViewer.SAVED_USER"; private static final String SAVED_USER = "com.xabber.android.ui.ChatViewer.SAVED_USER";
private static final String SAVED_EXIT_ON_SEND = "com.xabber.android.ui.ChatViewer.EXIT_ON_SEND"; private static final String SAVED_EXIT_ON_SEND = "com.xabber.android.ui.ChatViewer.EXIT_ON_SEND";
private String actionWithAccount;
private String actionWithUser;
private boolean exitOnSend; private boolean exitOnSend;
ChatViewerAdapter chatViewerAdapter;
ViewPager viewPager;
Collection<CurrentUpdatableChat> registeredChats = new HashSet<>();
private String actionWithAccount = null;
private String actionWithUser = null;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
...@@ -69,7 +80,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -69,7 +80,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
Intent intent = getIntent(); Intent intent = getIntent();
String account = getAccount(intent); String account = getAccount(intent);
String user = getUser(intent); String user = getUser(intent);
LogManager.i(this, "Intent: " + account + ":" + user); LogManager.i(this, "onCreate account: " + account + ", user: " + user);
if (account == null || user == null) { if (account == null || user == null) {
Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND); Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
...@@ -82,8 +93,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -82,8 +93,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
actionWithAccount = null; actionWithAccount = null;
actionWithUser = null; actionWithUser = null;
setContentView(R.layout.activity_preferences);
if (savedInstanceState != null) { if (savedInstanceState != null) {
actionWithAccount = savedInstanceState.getString(SAVED_ACCOUNT); actionWithAccount = savedInstanceState.getString(SAVED_ACCOUNT);
actionWithUser = savedInstanceState.getString(SAVED_USER); actionWithUser = savedInstanceState.getString(SAVED_USER);
...@@ -98,26 +107,24 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -98,26 +107,24 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
if (savedInstanceState == null) { setContentView(R.layout.activity_chat_viewer);
getFragmentManager().beginTransaction()
.add(R.id.preferences_activity_container,
ChatViewerFragment.newInstance(actionWithAccount, actionWithUser)).commit();
} chatViewerAdapter = new ChatViewerAdapter(getFragmentManager());
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(chatViewerAdapter);
viewPager.setOnPageChangeListener(this);
selectPage(actionWithAccount, actionWithUser);
} }
@Override @Override
protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
Application.getInstance().addUIListener(OnChatChangedListener.class, Application.getInstance().addUIListener(OnChatChangedListener.class, this);
this); Application.getInstance().addUIListener(OnContactChangedListener.class, this);
Application.getInstance().addUIListener(OnContactChangedListener.class, Application.getInstance().addUIListener(OnAccountChangedListener.class, this);
this);
Application.getInstance().addUIListener(OnAccountChangedListener.class,
this);
((ChatViewerFragment)getFragmentManager()
.findFragmentById(R.id.preferences_activity_container)).onChange();
Intent intent = getIntent(); Intent intent = getIntent();
if (Intent.ACTION_SEND.equals(intent.getAction())) { if (Intent.ACTION_SEND.equals(intent.getAction())) {
...@@ -128,13 +135,14 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -128,13 +135,14 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
} }
} }
update(); // selectPage(actionWithAccount, actionWithUser);
} }
@Override @Override
protected void onSaveInstanceState(Bundle outState) { protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); super.onSaveInstanceState(outState);
LogManager.i(this, "onSave: " + actionWithAccount + ":" + actionWithUser); // LogManager.i(this, "onSave: " + actionWithAccount + ":" + actionWithUser);
outState.putString(SAVED_ACCOUNT, actionWithAccount); outState.putString(SAVED_ACCOUNT, actionWithAccount);
outState.putString(SAVED_USER, actionWithUser); outState.putString(SAVED_USER, actionWithUser);
outState.putBoolean(SAVED_EXIT_ON_SEND, exitOnSend); outState.putBoolean(SAVED_EXIT_ON_SEND, exitOnSend);
...@@ -143,12 +151,9 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -143,12 +151,9 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override @Override
protected void onPause() { protected void onPause() {
super.onPause(); super.onPause();
Application.getInstance().removeUIListener(OnChatChangedListener.class, Application.getInstance().removeUIListener(OnChatChangedListener.class, this);
this); Application.getInstance().removeUIListener(OnContactChangedListener.class, this);
Application.getInstance().removeUIListener( Application.getInstance().removeUIListener(OnAccountChangedListener.class, this);
OnContactChangedListener.class, this);
Application.getInstance().removeUIListener(
OnAccountChangedListener.class, this);
MessageManager.getInstance().removeVisibleChat(); MessageManager.getInstance().removeVisibleChat();
} }
...@@ -164,14 +169,21 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -164,14 +169,21 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
return; return;
} }
actionWithAccount = account; LogManager.i(this, "onNewIntent account: " + account + ", user: " + user);
actionWithUser = user;
selectPage(account, user);
if (hasAttention(intent))
AttentionManager.getInstance().removeAccountNotifications(account, user);
getFragmentManager().beginTransaction().replace(R.id.preferences_activity_container, }
ChatViewerFragment.newInstance(actionWithAccount, actionWithUser)).commit();
private void selectPage(String account, String user) {
LogManager.i(this, "selectPage: account: " + account + ", user: " + user);
int position = chatViewerAdapter.getPosition(account, user);
viewPager.setCurrentItem(position);
onChatSelected(account, user);
} }
private static String getAccount(Intent intent) { private static String getAccount(Intent intent) {
...@@ -236,27 +248,35 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -236,27 +248,35 @@ 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) {
LogManager.i(this, "updateChat account: " + account + ", user: " + user);
update(); chatViewerAdapter.onChange();
((ChatViewerFragment)getFragmentManager()
.findFragmentById(R.id.preferences_activity_container)).onChatChange(incoming); for (CurrentUpdatableChat chat : registeredChats) {
if (chat.isEqual(account, user)) {
chat.updateChat(incoming);
}
}
} }
@Override @Override
public void onContactsChanged(Collection<BaseEntity> entities) { public void onContactsChanged(Collection<BaseEntity> entities) {
update(); chatViewerAdapter.onChange();
((ChatViewerFragment)getFragmentManager() for (CurrentUpdatableChat chat : registeredChats) {
.findFragmentById(R.id.preferences_activity_container)).onChange(); chat.updateChat(false);
}
} }
@Override @Override
public void onAccountsChanged(Collection<String> accounts) { public void onAccountsChanged(Collection<String> accounts) {
update(); chatViewerAdapter.onChange();
((ChatViewerFragment)getFragmentManager() for (CurrentUpdatableChat chat : registeredChats) {
.findFragmentById(R.id.preferences_activity_container)).onChange(); chat.updateChat(false);
}
} }
void onSent() { void onSent() {
...@@ -275,17 +295,52 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -275,17 +295,52 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
} }
} }
void update() { @Override
MessageManager.getInstance().setVisibleChat(actionWithAccount, actionWithUser); public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
LogManager.i(this, "onPageSelected: " + position);
AbstractChat chat = chatViewerAdapter.getChat(position);
String account = chat.getAccount();
String user = chat.getUser();
actionWithAccount = account;
actionWithUser = user;
onChatSelected(account, user);
}
private void onChatSelected(String account, String user) {
LogManager.i(this, "onChatSelected. account: " + account + "; user: " + user);
MessageManager.getInstance().setVisibleChat(account, user);
MessageArchiveManager.getInstance().requestHistory( MessageArchiveManager.getInstance().requestHistory(
actionWithAccount, account, user, 0,
actionWithUser, MessageManager.getInstance().getChat(account, user).getRequiredMessageCount());
0,
MessageManager.getInstance() NotificationManager.getInstance().removeMessageNotification(account, user);
.getChat(actionWithAccount, actionWithUser) }
.getRequiredMessageCount());
NotificationManager.getInstance().removeMessageNotification( @Override
actionWithAccount, actionWithUser); public void onPageScrollStateChanged(int state) {
}
public void registerChat(CurrentUpdatableChat chat) {
registeredChats.add(chat);
}
public void unregisterChat(CurrentUpdatableChat chat) {
registeredChats.remove(chat);
}
public interface CurrentUpdatableChat {
public void updateChat(final boolean incoming);
public boolean isEqual(String account, String user);
} }
} }
...@@ -25,7 +25,6 @@ import android.widget.ListView; ...@@ -25,7 +25,6 @@ import android.widget.ListView;
import android.widget.TextView; import android.widget.TextView;
import com.xabber.android.data.Application; import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.NetworkException; import com.xabber.android.data.NetworkException;
import com.xabber.android.data.SettingsManager; import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.extension.archive.MessageArchiveManager; import com.xabber.android.data.extension.archive.MessageArchiveManager;
...@@ -51,10 +50,13 @@ import com.xabber.android.ui.helper.ContactTitleInflater; ...@@ -51,10 +50,13 @@ import com.xabber.android.ui.helper.ContactTitleInflater;
import com.xabber.android.ui.preferences.ChatEditor; import com.xabber.android.ui.preferences.ChatEditor;
import com.xabber.androiddev.R; import com.xabber.androiddev.R;
public class ChatViewerFragment extends Fragment { public class ChatViewerFragment extends Fragment implements ChatViewer.CurrentUpdatableChat {
public static final String ARGUMENT_ACCOUNT = "ARGUMENT_ACCOUNT"; public static final String ARGUMENT_ACCOUNT = "ARGUMENT_ACCOUNT";
public static final String ARGUMENT_USER = "ARGUMENT_USER"; public static final String ARGUMENT_USER = "ARGUMENT_USER";
private static final int MINIMUM_MESSAGES_TO_LOAD = 10;
private TextView pageView; private TextView pageView;
private View titleView; private View titleView;
private EditText inputView; private EditText inputView;
...@@ -70,18 +72,16 @@ public class ChatViewerFragment extends Fragment { ...@@ -70,18 +72,16 @@ public class ChatViewerFragment extends Fragment {
private AbstractAvatarInflaterHelper avatarInflaterHelper; private AbstractAvatarInflaterHelper avatarInflaterHelper;
private static final int MINIMUM_MESSAGES_TO_LOAD = 10;
private String account; private String account;
private String user; private String user;
public static ChatViewerFragment newInstance(String account, String user) { public static ChatViewerFragment newInstance(String account, String user) {
ChatViewerFragment fragment = new ChatViewerFragment(); ChatViewerFragment fragment = new ChatViewerFragment();
Bundle args = new Bundle(); Bundle arguments = new Bundle();
args.putString(ARGUMENT_ACCOUNT, account); arguments.putString(ARGUMENT_ACCOUNT, account);
args.putString(ARGUMENT_USER, user); arguments.putString(ARGUMENT_USER, user);
fragment.setArguments(args); fragment.setArguments(arguments);
return fragment; return fragment;
} }
...@@ -90,6 +90,10 @@ public class ChatViewerFragment extends Fragment { ...@@ -90,6 +90,10 @@ public class ChatViewerFragment extends Fragment {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
avatarInflaterHelper = AbstractAvatarInflaterHelper.createAbstractContactInflaterHelper(); avatarInflaterHelper = AbstractAvatarInflaterHelper.createAbstractContactInflaterHelper();
Bundle args = getArguments();
account = args.getString(ARGUMENT_ACCOUNT, null);
user = args.getString(ARGUMENT_USER, null);
} }
@Override @Override
...@@ -118,14 +122,19 @@ public class ChatViewerFragment extends Fragment { ...@@ -118,14 +122,19 @@ 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.setAdapter(chatMessageAdapter);
pageView = (TextView) view.findViewById(R.id.chat_page); pageView = (TextView) view.findViewById(R.id.chat_page);
titleView = view.findViewById(R.id.title); titleView = view.findViewById(R.id.title);
inputView = (EditText) view.findViewById(R.id.chat_input); inputView = (EditText) view.findViewById(R.id.chat_input);
listView = (ListView) view.findViewById(android.R.id.list);
listView.setAdapter(chatMessageAdapter);
view.findViewById(R.id.chat_send).setOnClickListener( view.findViewById(R.id.chat_send).setOnClickListener(
new View.OnClickListener() { new View.OnClickListener() {
...@@ -175,13 +184,11 @@ public class ChatViewerFragment extends Fragment { ...@@ -175,13 +184,11 @@ public class ChatViewerFragment extends Fragment {
inputView.addTextChangedListener(new TextWatcher() { inputView.addTextChangedListener(new TextWatcher() {
@Override @Override
public void onTextChanged(CharSequence s, int start, int before, public void onTextChanged(CharSequence s, int start, int before, int count) {
int count) {
} }
@Override @Override
public void beforeTextChanged(CharSequence s, int start, int count, public void beforeTextChanged(CharSequence s, int start, int count, int after) {
int after) {
} }
@Override @Override
...@@ -196,87 +203,92 @@ public class ChatViewerFragment extends Fragment { ...@@ -196,87 +203,92 @@ public class ChatViewerFragment extends Fragment {
}); });
registerForContextMenu(listView); registerForContextMenu(listView);
Bundle args = getArguments();
account = args.getString(ARGUMENT_ACCOUNT, null);
user = args.getString(ARGUMENT_USER, null);
setHasOptionsMenu(true); setHasOptionsMenu(true);
setChat(account, user); updateView();
chatMessageAdapter.onChange(); chatMessageAdapter.onChange();
return view; return view;
} }
@Override
public void onResume() {
super.onResume();
((ChatViewer)getActivity()).registerChat(this);
}
@Override
public void onPause() {
super.onPause();
((ChatViewer)getActivity()).unregisterChat(this);
}
private void sendMessage() { private void sendMessage() {
String text = inputView.getText().toString(); String text = inputView.getText().toString();
int start = 0; int start = 0;
int end = text.length(); int end = text.length();
while (start < end
&& (text.charAt(start) == ' ' || text.charAt(start) == '\n')) while (start < end && (text.charAt(start) == ' ' || text.charAt(start) == '\n')) {
start += 1; start += 1;
while (start < end }
&& (text.charAt(end - 1) == ' ' || text.charAt(end - 1) == '\n')) while (start < end && (text.charAt(end - 1) == ' ' || text.charAt(end - 1) == '\n')) {
end -= 1; end -= 1;
}
text = text.substring(start, end); text = text.substring(start, end);
if ("".equals(text))
if ("".equals(text)) {
return; return;
}
skipOnTextChanges = true; skipOnTextChanges = true;
inputView.setText(""); inputView.setText("");
skipOnTextChanges = false; skipOnTextChanges = false;
sendMessage(text); sendMessage(text);
((ChatViewer) getActivity()).onSent(); ((ChatViewer) getActivity()).onSent();
if (SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.always if (SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.always
|| (getActivity().getResources().getBoolean(R.bool.landscape) && SettingsManager || (getActivity().getResources().getBoolean(R.bool.landscape)
.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.landscape)) { && SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.landscape)) {
InputMethodManager imm = (InputMethodManager) getActivity() InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(inputView.getWindowToken(), 0); imm.hideSoftInputFromWindow(inputView.getWindowToken(), 0);
} }
} }
private void sendMessage(String text) { private void sendMessage(String text) {
final String account = chatMessageAdapter.getAccount();
final String user = chatMessageAdapter.getUser();
MessageManager.getInstance().sendMessage(account, user, text); MessageManager.getInstance().sendMessage(account, user, text);
onChatChange(false); updateChat(false);
} }
public void onChatChange(boolean incomingMessage) {
if (incomingMessage) {
titleView.findViewById(R.id.name_holder).startAnimation(
shakeAnimation);
}
setChat(account, user);
chatMessageAdapter.onChange();
}
public void onChange() { private void updateMessages() {
setChat(account, user);
chatMessageAdapter.onChange(); chatMessageAdapter.onChange();
} }
public void setChat(String account, String user) { private void updateView() {
final AbstractContact abstractContact = RosterManager.getInstance() final AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
.getBestContact(account, user);
LogManager.i(this, "setChat " + chatMessageAdapter.getUser() + " in " + chatMessageAdapter.getAccount());
skipOnTextChanges = true; skipOnTextChanges = true;
inputView.setText(ChatManager.getInstance().getTypedMessage(account, user)); inputView.setText(ChatManager.getInstance().getTypedMessage(account, user));
inputView.setSelection(ChatManager.getInstance().getSelectionStart(account, user), inputView.setSelection(ChatManager.getInstance().getSelectionStart(account, user),
ChatManager.getInstance().getSelectionEnd(account, user)); ChatManager.getInstance().getSelectionEnd(account, user));
skipOnTextChanges = false; skipOnTextChanges = false;
chatMessageAdapter.setChat(account, user);
listView.setAdapter(listView.getAdapter());
ContactTitleInflater.updateTitle(titleView, getActivity(), abstractContact); ContactTitleInflater.updateTitle(titleView, getActivity(), abstractContact);
avatarInflaterHelper.updateAvatar((ImageView) titleView.findViewById(R.id.avatar), abstractContact); avatarInflaterHelper.updateAvatar((ImageView) titleView.findViewById(R.id.avatar), abstractContact);
SecurityLevel securityLevel = OTRManager.getInstance().getSecurityLevel(account, user); SecurityLevel securityLevel = OTRManager.getInstance().getSecurityLevel(account, user);
SettingsManager.SecurityOtrMode securityOtrMode = SettingsManager.securityOtrMode(); SettingsManager.SecurityOtrMode securityOtrMode = SettingsManager.securityOtrMode();
ImageView securityView = (ImageView) titleView ImageView securityView = (ImageView) titleView.findViewById(R.id.security);
.findViewById(R.id.security);
if (securityLevel == SecurityLevel.plain if (securityLevel == SecurityLevel.plain
&& (securityOtrMode == SettingsManager.SecurityOtrMode.disabled && (securityOtrMode == SettingsManager.SecurityOtrMode.disabled
|| securityOtrMode == SettingsManager.SecurityOtrMode.manual)) { || securityOtrMode == SettingsManager.SecurityOtrMode.manual)) {
...@@ -329,7 +341,7 @@ public class ChatViewerFragment extends Fragment { ...@@ -329,7 +341,7 @@ public class ChatViewerFragment extends Fragment {
MessageManager.getInstance().requestToLoadLocalHistory(account, user); MessageManager.getInstance().requestToLoadLocalHistory(account, user);
MessageArchiveManager.getInstance() MessageArchiveManager.getInstance()
.requestHistory(account, user, MINIMUM_MESSAGES_TO_LOAD, 0); .requestHistory(account, user, MINIMUM_MESSAGES_TO_LOAD, 0);
onChatChange(false); updateChat(false);
return true; return true;
} }
}); });
...@@ -383,7 +395,7 @@ public class ChatViewerFragment extends Fragment { ...@@ -383,7 +395,7 @@ public class ChatViewerFragment extends Fragment {
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
MessageManager.getInstance() MessageManager.getInstance()
.clearHistory(account, user); .clearHistory(account, user);
onChatChange(false); updateChat(false);
return false; return false;
} }
}); });
...@@ -537,7 +549,7 @@ public class ChatViewerFragment extends Fragment { ...@@ -537,7 +549,7 @@ public class ChatViewerFragment extends Fragment {
@Override @Override
public boolean onMenuItemClick(MenuItem item) { public boolean onMenuItemClick(MenuItem item) {
MessageManager.getInstance().removeMessage(message); MessageManager.getInstance().removeMessage(message);
onChatChange(false); updateChat(false);
return true; return true;
} }
...@@ -563,4 +575,19 @@ public class ChatViewerFragment extends Fragment { ...@@ -563,4 +575,19 @@ public class ChatViewerFragment extends Fragment {
inputView.setText(before + additional + after); inputView.setText(before + additional + after);
inputView.setSelection(selection + additional.length()); inputView.setSelection(selection + additional.length());
} }
@Override
public void updateChat(boolean incomingMessage) {
if (incomingMessage) {
titleView.findViewById(R.id.name_holder).startAnimation(shakeAnimation);
}
updateMessages();
updateView();
}
@Override
public boolean isEqual(String account, String user) {
return this.account.equals(account) && this.user.equals(user);
}
} }
...@@ -309,8 +309,7 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter ...@@ -309,8 +309,7 @@ public class ChatMessageAdapter extends BaseAdapter implements UpdatableAdapter
@Override @Override
public void onChange() { public void onChange() {
messages = new ArrayList<>(MessageManager.getInstance() messages = new ArrayList<>(MessageManager.getInstance().getMessages(account, user));
.getMessages(account, user));
hint = getHint(); hint = getHint();
notifyDataSetChanged(); notifyDataSetChanged();
} }
......
package com.xabber.android.ui.adapter;
import android.app.Fragment;
import android.app.FragmentManager;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.data.message.MessageManager;
import com.xabber.android.ui.ChatViewerFragment;
import com.xabber.xmpp.address.Jid;
import java.util.ArrayList;
public class ChatViewerAdapter extends FragmentStatePagerAdapter implements UpdatableAdapter {
private ArrayList<AbstractChat> activeChats;
public ChatViewerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
LogManager.i(this, "ChatViewerAdapter");
onChange();
}
@Override
public int getCount() {
return activeChats.size();
}
@Override
public Fragment getItem(int i) {
LogManager.i(this, "getItem: " + i);
AbstractChat abstractChat = getChat(i);
return ChatViewerFragment.newInstance(abstractChat.getAccount(), abstractChat.getUser());
}
public AbstractChat getChat(int i) {
return activeChats.get(i);
}
@Override
public void onChange() {
LogManager.i(this, "onChange: ");
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
notifyDataSetChanged();
}
public int getPosition(String account, String user) {
LogManager.i(this, "getPosition: " + account + " : " + user);
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
for (int position = 0; position < activeChats.size(); position++) {
if (activeChats.get(position).equals(account, user)) {
return position;
}
}
LogManager.i(this, "creating new chat: " + account + " : " + user);
AbstractChat chat = MessageManager.getInstance().getOrCreateChat(account, Jid.getBareAddress(user));
activeChats.add(chat);
notifyDataSetChanged();
return activeChats.indexOf(chat);
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
LogManager.i(this, "setPrimaryItem position: " + position);
}
}
\ No newline at end of file
...@@ -50,46 +50,53 @@ public class ContactTitleInflater { ...@@ -50,46 +50,53 @@ public class ContactTitleInflater {
*/ */
public static void updateTitle(View titleView, final Activity activity, public static void updateTitle(View titleView, final Activity activity,
AbstractContact abstractContact) { AbstractContact abstractContact) {
final TypedArray typedArray = activity final TypedArray typedArray = activity.obtainStyledAttributes(R.styleable.ContactList);
.obtainStyledAttributes(R.styleable.ContactList);
final Drawable titleAccountBackground = typedArray final Drawable titleAccountBackground = typedArray
.getDrawable(R.styleable.ContactList_titleAccountBackground); .getDrawable(R.styleable.ContactList_titleAccountBackground);
typedArray.recycle(); typedArray.recycle();
final TextView nameView = (TextView) titleView.findViewById(R.id.name); final TextView nameView = (TextView) titleView.findViewById(R.id.name);
final ImageView avatarView = (ImageView) titleView final ImageView avatarView = (ImageView) titleView.findViewById(R.id.avatar);
.findViewById(R.id.avatar); final ImageView statusModeView = (ImageView) titleView.findViewById(R.id.status_mode);
final ImageView statusModeView = (ImageView) titleView
.findViewById(R.id.status_mode);
final TextView statusTextView = (TextView) titleView.findViewById(R.id.status_text); final TextView statusTextView = (TextView) titleView.findViewById(R.id.status_text);
final View shadowView = titleView.findViewById(R.id.shadow); final View shadowView = titleView.findViewById(R.id.shadow);
titleView.setBackgroundDrawable(titleAccountBackground); titleView.setBackgroundDrawable(titleAccountBackground);
nameView.setText(abstractContact.getName()); nameView.setText(abstractContact.getName());
statusModeView.setImageLevel(abstractContact.getStatusMode() statusModeView.setImageLevel(abstractContact.getStatusMode().getStatusLevel());
.getStatusLevel()); titleView.getBackground().setLevel(AccountManager.getInstance().getColorLevel(
titleView.getBackground().setLevel(
AccountManager.getInstance().getColorLevel(
abstractContact.getAccount())); abstractContact.getAccount()));
avatarView.setImageDrawable(abstractContact.getAvatar()); avatarView.setImageDrawable(abstractContact.getAvatar());
setStatusText(activity, abstractContact, statusTextView);
final Bitmap bitmap = BitmapFactory.decodeResource(activity.getResources(), R.drawable.shadow);
final BitmapDrawable shadowDrawable = new BitmapDrawable(bitmap);
shadowDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
shadowView.setBackgroundDrawable(shadowDrawable);
if (abstractContact.isConnected()) {
shadowView.setVisibility(View.GONE);
} else {
shadowView.setVisibility(View.VISIBLE);
}
}
private static void setStatusText(Activity activity, AbstractContact abstractContact,
TextView statusTextView) {
ChatState chatState = ChatStateManager.getInstance().getChatState( ChatState chatState = ChatStateManager.getInstance().getChatState(
abstractContact.getAccount(), abstractContact.getUser()); abstractContact.getAccount(), abstractContact.getUser());
final CharSequence statusText; final CharSequence statusText;
if (chatState == ChatState.composing) if (chatState == ChatState.composing) {
statusText = activity.getString(R.string.chat_state_composing); statusText = activity.getString(R.string.chat_state_composing);
else if (chatState == ChatState.paused) } else if (chatState == ChatState.paused) {
statusText = activity.getString(R.string.chat_state_paused); statusText = activity.getString(R.string.chat_state_paused);
else } else {
statusText = Emoticons.getSmiledText(activity, statusText = Emoticons.getSmiledText(activity, abstractContact.getStatusText());
abstractContact.getStatusText()); }
statusTextView.setText(statusText); statusTextView.setText(statusText);
final Bitmap bitmap = BitmapFactory.decodeResource(
activity.getResources(), R.drawable.shadow);
final BitmapDrawable shadowDrawable = new BitmapDrawable(bitmap);
shadowDrawable.setTileModeXY(TileMode.REPEAT, TileMode.REPEAT);
shadowView.setBackgroundDrawable(shadowDrawable);
if (abstractContact.isConnected())
shadowView.setVisibility(View.GONE);
else
shadowView.setVisibility(View.VISIBLE);
} }
} }
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v4.view.ViewPager>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!-- 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/.
-->
<com.xabber.android.ui.widget.PageSwitcher
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/switcher"
/>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<!-- 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/.
-->
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
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