Commit 7a8b27e9 authored by Grigory Fedorov's avatar Grigory Fedorov

ChatViewerFragment interacts with ChatViewer via listener interface.

ChatViewer empty createIntent method renamed to recentChats intent.
parent e400a2c1
......@@ -681,8 +681,7 @@ public class MessageArchiveManager implements OnPacketListener,
* @param newCount
* @param incomingCount
*/
public void requestHistory(String account, String bareAddress,
int newCount, int incomingCount) {
public void requestHistory(String account, String bareAddress, int newCount, int incomingCount) {
if (AccountManager.getInstance().getArchiveMode(account) != ArchiveMode.server
|| (newCount <= 0 && incomingCount <= 0))
return;
......
......@@ -15,10 +15,7 @@
package com.xabber.android.ui;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
......@@ -62,6 +59,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
* Attention request.
*/
private static final String ACTION_ATTENTION = "com.xabber.android.data.ATTENTION";
private static final String ACTION_RECENT_CHATS = "com.xabber.android.data.RECENT_CHATS";
private static final String SAVED_ACCOUNT = "com.xabber.android.ui.ChatViewer.SAVED_ACCOUNT";
private static final String SAVED_USER = "com.xabber.android.ui.ChatViewer.SAVED_USER";
......@@ -134,6 +132,24 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
selectPage(account, user, false);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (isFinishing())
return;
chatViewerAdapter.updateChats();
chatScrollIndicatorAdapter.update(chatViewerAdapter.getRealCount());
String account = getAccount(intent);
String user = getUser(intent);
if (account == null || user == null) {
return;
}
selectPage(account, user, false);
}
@Override
protected void onResume() {
super.onResume();
......@@ -173,24 +189,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
MessageManager.getInstance().removeVisibleChat();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (isFinishing())
return;
chatViewerAdapter.updateChats();
chatScrollIndicatorAdapter.update(chatViewerAdapter.getRealCount());
String account = getAccount(intent);
String user = getUser(intent);
if (account == null || user == null) {
return;
}
selectPage(account, user, false);
}
private void selectPage(String account, String user, boolean smoothScroll) {
int position = chatViewerAdapter.getPageNumber(account, user);
selectPage(position, smoothScroll);
......@@ -201,65 +199,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
viewPager.setCurrentItem(position, smoothScroll);
}
private static String getAccount(Intent intent) {
String value = EntityIntentBuilder.getAccount(intent);
if (value != null)
return value;
// Backward compatibility.
return intent.getStringExtra("com.xabber.android.data.account");
}
private static String getUser(Intent intent) {
String value = EntityIntentBuilder.getUser(intent);
if (value != null)
return value;
// Backward compatibility.
return intent.getStringExtra("com.xabber.android.data.user");
}
private static boolean hasAttention(Intent intent) {
return ACTION_ATTENTION.equals(intent.getAction());
}
public static Intent createIntent(Context context, String account, String user) {
return new EntityIntentBuilder(context, ChatViewer.class).setAccount(account).setUser(user).build();
}
public static Intent createIntent(Context context) {
return new EntityIntentBuilder(context, ChatViewer.class).build();
}
public static Intent createClearTopIntent(Context context, String account, String user) {
Intent intent = createIntent(context, account, user);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
/**
* Create intent to send message.
* <p/>
* Contact list will not be shown on when chat will be closed.
*
* @param context
* @param account
* @param user
* @param text if <code>null</code> then user will be able to send a number
* of messages. Else only one message can be send.
* @return
*/
public static Intent createSendIntent(Context context, String account, String user, String text) {
Intent intent = ChatViewer.createIntent(context, account, user);
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, text);
return intent;
}
public static Intent createAttentionRequestIntent(Context context, String account, String user) {
Intent intent = ChatViewer.createClearTopIntent(context, account, user);
intent.setAction(ACTION_ATTENTION);
return intent;
}
@Override
public void onChatChanged(final String account, final String user, final boolean incoming) {
String currentAccount = null;
......@@ -312,22 +251,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
}
}
void onSent() {
if (exitOnSend) {
close();
}
}
void close() {
finish();
if (!Intent.ACTION_SEND.equals(getIntent().getAction())) {
ActivityManager.getInstance().clearStack(false);
if (!ActivityManager.getInstance().hasContactList(this)) {
startActivity(ContactList.createIntent(this));
}
}
}
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
......@@ -360,8 +283,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
MessageManager.getInstance().setVisibleChat(actionWithAccount, actionWithUser);
MessageArchiveManager.getInstance().requestHistory(
actionWithAccount, actionWithUser, 0,
MessageArchiveManager.getInstance().requestHistory( actionWithAccount, actionWithUser, 0,
MessageManager.getInstance().getChat(actionWithAccount, actionWithUser).getRequiredMessageCount());
NotificationManager.getInstance().removeMessageNotification(actionWithAccount, actionWithUser);
......@@ -387,15 +309,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
public void onPageScrollStateChanged(int state) {
}
public void registerChat(ChatViewerFragment chat) {
registeredChats.add(chat);
}
public void unregisterChat(ChatViewerFragment chat) {
registeredChats.remove(chat);
}
public void registerRecentChatsList(RecentChatFragment recentChatFragment) {
recentChatFragments.add(recentChatFragment);
}
......@@ -452,4 +365,92 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
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() {
finish();
if (!Intent.ACTION_SEND.equals(getIntent().getAction())) {
ActivityManager.getInstance().clearStack(false);
if (!ActivityManager.getInstance().hasContactList(this)) {
startActivity(ContactList.createIntent(this));
}
}
}
private static String getAccount(Intent intent) {
String value = EntityIntentBuilder.getAccount(intent);
if (value != null)
return value;
// Backward compatibility.
return intent.getStringExtra("com.xabber.android.data.account");
}
private static String getUser(Intent intent) {
String value = EntityIntentBuilder.getUser(intent);
if (value != null)
return value;
// Backward compatibility.
return intent.getStringExtra("com.xabber.android.data.user");
}
private static boolean hasAttention(Intent intent) {
return ACTION_ATTENTION.equals(intent.getAction());
}
public static Intent createIntent(Context context, String account, String user) {
return new EntityIntentBuilder(context, ChatViewer.class).setAccount(account).setUser(user).build();
}
public static Intent createRecentChatsIntent(Context context) {
return new EntityIntentBuilder(context, ChatViewer.class).build();
}
public static Intent createClearTopIntent(Context context, String account, String user) {
Intent intent = createIntent(context, account, user);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
/**
* Create intent to send message.
* <p/>
* Contact list will not be shown on when chat will be closed.
*
* @param context
* @param account
* @param user
* @param text if <code>null</code> then user will be able to send a number
* of messages. Else only one message can be send.
* @return
*/
public static Intent createSendIntent(Context context, String account, String user, String text) {
Intent intent = ChatViewer.createIntent(context, account, user);
intent.setAction(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_TEXT, text);
return intent;
}
public static Intent createAttentionRequestIntent(Context context, String account, String user) {
Intent intent = ChatViewer.createClearTopIntent(context, account, user);
intent.setAction(ACTION_ATTENTION);
return intent;
}
}
......@@ -89,6 +89,18 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
return fragment;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (ChatViewerFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ChatViewerFragmentListener");
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -210,17 +222,6 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
listener = (ChatViewerFragmentListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement ChatViewerFragmentListener");
}
}
@Override
public void onDetach() {
......@@ -273,7 +274,7 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
@Override
public void onResume() {
super.onResume();
((ChatViewer)getActivity()).registerChat(this);
listener.registerChat(this);
updateChat();
......@@ -299,7 +300,7 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
public void onPause() {
super.onPause();
saveInputState();
((ChatViewer)getActivity()).unregisterChat(this);
listener.unregisterChat(this);
}
public void saveInputState() {
......@@ -318,7 +319,7 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
sendMessage(text);
((ChatViewer) getActivity()).onSent();
listener.onMessageSent();
if (SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.always
|| (getActivity().getResources().getBoolean(R.bool.landscape)
......@@ -613,6 +614,9 @@ public class ChatViewerFragment extends Fragment implements AdapterView.OnItemCl
public interface ChatViewerFragmentListener {
void onCloseChat();
void onMessageSent();
void registerChat(ChatViewerFragment chat);
void unregisterChat(ChatViewerFragment chat);
}
public void playIncomingAnimation() {
......
......@@ -366,7 +366,7 @@ public class ContactList extends ManagedActivity implements OnAccountChangedList
startActivity(MUCEditor.createIntent(this));
return true;
case R.id.action_chat_list:
startActivity(ChatViewer.createIntent(this));
startActivity(ChatViewer.createRecentChatsIntent(this));
return true;
case R.id.action_settings:
startActivity(PreferenceEditor.createIntent(this));
......
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