Commit 5e556a65 authored by Grigory Fedorov's avatar Grigory Fedorov

ChatManager stores information about initial and selected chats.

ChatScrollerActivity is basic for ContactList and ChatViewer.
All chat intents received by ChatIntentActivity (base for ContactList), ChatViewer called from ContactList if needed.
More stable, but lack of functions (send/alert intents, etc).
parent f4b716b6
......@@ -21,7 +21,7 @@ import com.xabber.android.data.Application;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.notification.EntityNotificationItem;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.ChatViewer;
import com.xabber.android.ui.ChatIntentActivity;
public class AttentionRequest extends BaseEntity implements
EntityNotificationItem {
......@@ -32,7 +32,7 @@ public class AttentionRequest extends BaseEntity implements
@Override
public Intent getIntent() {
return ChatViewer.createAttentionRequestIntent(
return ChatIntentActivity.createAttentionRequestIntent(
Application.getInstance(), account, user);
}
......
......@@ -72,6 +72,11 @@ public class ChatManager implements OnLoadListener, OnAccountRemovedListener {
*/
private final NestedMap<Uri> sounds;
private BaseEntity initialChat = null;
private BaseEntity selectedChat = null;
private ChatManager() {
chatInputs = new NestedMap<ChatInput>();
privateChats = new NestedMap<Object>();
......@@ -391,4 +396,19 @@ public class ChatManager implements OnLoadListener, OnAccountRemovedListener {
});
}
public BaseEntity getSelectedChat() {
return selectedChat;
}
public void setSelectedChat(BaseEntity selectedChat) {
this.selectedChat = selectedChat;
}
public BaseEntity getInitialChat() {
return initialChat;
}
public void setInitialChat(BaseEntity initialChat) {
this.initialChat = initialChat;
}
}
......@@ -14,7 +14,7 @@ import com.xabber.android.data.extension.muc.MUCManager;
import com.xabber.android.data.message.MessageItem;
import com.xabber.android.data.message.chat.ChatManager;
import com.xabber.android.data.roster.RosterManager;
import com.xabber.android.ui.ChatViewer;
import com.xabber.android.ui.ChatIntentActivity;
import com.xabber.android.ui.ContactList;
import com.xabber.android.ui.helper.AccountPainter;
import com.xabber.android.utils.StringUtils;
......@@ -199,7 +199,7 @@ public class MessageNotificationCreator {
Intent backIntent = ContactList.createIntent(application);
backIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent intent = ChatViewer.createClearTopIntent(application, message.getAccount(), message.getUser());
Intent intent = ChatIntentActivity.createClearTopIntent(application, message.getAccount(), message.getUser());
return PendingIntent.getActivities(application, UNIQUE_REQUEST_CODE++,
new Intent[]{backIntent, intent}, PendingIntent.FLAG_ONE_SHOT);
}
......
package com.xabber.android.ui;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import com.xabber.android.R;
import com.xabber.android.data.entity.BaseEntity;
import com.xabber.android.data.extension.attention.AttentionManager;
import com.xabber.android.data.intent.EntityIntentBuilder;
import com.xabber.android.data.message.chat.ChatManager;
import com.xabber.android.ui.preferences.AboutViewer;
import com.xabber.android.ui.preferences.AccountEditor;
import com.xabber.android.ui.preferences.PreferenceEditor;
public abstract class ChatIntentActivity extends ChatScrollerActivity implements ContactListDrawerFragment.ContactListDrawerListener {
protected static final String ACTION_ATTENTION = "com.xabber.android.data.ATTENTION";
protected static final String ACTION_SPECIFIC_CHAT = "com.xabber.android.data.ACTION_SPECIFIC_CHAT";
protected static final String ACTION_SHORTCUT = "com.xabber.android.data.ACTION_SHORTCUT";
protected ActionBarDrawerToggle drawerToggle;
protected DrawerLayout drawerLayout;
protected Toolbar toolbar;
public static Intent createSpecificChatIntent(Context context, String account, String user) {
Intent intent = new EntityIntentBuilder(context, ContactList.class).setAccount(account).setUser(user).build();
intent.setAction(ACTION_SPECIFIC_CHAT);
return intent;
}
public static Intent createClearTopIntent(Context context, String account, String user) {
Intent intent = createSpecificChatIntent(context, account, user);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
return intent;
}
public static Intent createShortCutIntent(Context context, String account, String user) {
Intent intent = createClearTopIntent(context, account, user);
intent.setAction(ACTION_SHORTCUT);
return intent;
}
public static Intent createSendIntent(Context context, String account, String user, String text) {
Intent intent = createSpecificChatIntent(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 = createClearTopIntent(context, account, user);
intent.setAction(ACTION_ATTENTION);
return intent;
}
private static boolean hasAttention(Intent intent) {
return ACTION_ATTENTION.equals(intent.getAction());
}
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");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
toolbar = (Toolbar) findViewById(R.id.toolbar_default);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
if (savedInstanceState == null) {
Intent intent = getIntent();
BaseEntity chatFromIntent = getChatFromIntent(intent);
if (chatFromIntent != null) {
ChatManager.getInstance().setInitialChat(chatFromIntent);
ChatManager.getInstance().setSelectedChat(chatFromIntent);
}
}
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
BaseEntity chatFromIntent = getChatFromIntent(intent);
ChatManager.getInstance().setSelectedChat(chatFromIntent);
if (ACTION_SHORTCUT.equals(intent.getAction())) {
ChatManager.getInstance().setInitialChat(chatFromIntent);
}
}
private BaseEntity getChatFromIntent(Intent intent) {
String account = getAccount(intent);
String user = getUser(intent);
if (account != null && user != null) {
return new BaseEntity(account, user);
}
return null;
}
@Override
protected void onResume() {
super.onResume();
Intent intent = getIntent();
if (hasAttention(intent)) {
AttentionManager.getInstance().removeAccountNotifications(ChatManager.getInstance().getSelectedChat());
}
}
@Override
public void onContactListDrawerListener(int viewId) {
drawerLayout.closeDrawers();
switch (viewId) {
case R.id.drawer_action_settings:
startActivity(PreferenceEditor.createIntent(this));
break;
case R.id.drawer_action_about:
startActivity(AboutViewer.createIntent(this));
break;
case R.id.drawer_action_exit:
exit();
break;
}
}
abstract void exit();
@Override
public void onAccountSelected(String account) {
drawerLayout.closeDrawers();
startActivity(AccountEditor.createIntent(this, account));
}
}
package com.xabber.android.ui;
import com.xabber.android.data.Application;
import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.message.OnChatChangedListener;
import com.xabber.android.data.roster.OnContactChangedListener;
import com.xabber.android.ui.helper.ChatScroller;
import com.xabber.android.ui.helper.ManagedActivity;
public abstract class ChatScrollerActivity extends ManagedActivity
implements ChatScroller.ChatScrollerListener, ChatScroller.ChatScrollerProvider {
ChatScroller chatScroller = new ChatScroller(this);
@Override
public ChatScroller getChatScroller() {
return chatScroller;
}
@Override
protected void onResume() {
super.onResume();
Application.getInstance().addUIListener(OnChatChangedListener.class, chatScroller);
Application.getInstance().addUIListener(OnContactChangedListener.class, chatScroller);
Application.getInstance().addUIListener(OnAccountChangedListener.class, chatScroller);
chatScroller.update();
}
@Override
protected void onPause() {
super.onPause();
Application.getInstance().removeUIListener(OnChatChangedListener.class, chatScroller);
Application.getInstance().removeUIListener(OnContactChangedListener.class, chatScroller);
Application.getInstance().removeUIListener(OnAccountChangedListener.class, chatScroller);
chatScroller.onHide();
}
}
......@@ -126,13 +126,16 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
toolbar = (Toolbar) view.findViewById(R.id.toolbar_default);
toolbar.inflateMenu(R.menu.chat);
toolbar.setOnMenuItemClickListener(this);
toolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(getActivity());
}
});
if (!getResources().getBoolean(R.bool.landscape)) {
toolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(getActivity());
}
});
}
setHasOptionsMenu(true);
......@@ -221,6 +224,13 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
restoreInputState();
}
@Override
public void onPause() {
super.onPause();
saveInputState();
listener.getChatScroller().unregisterChat(this);
}
@Override
public void onDetach() {
......@@ -269,7 +279,6 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
}
}
public void restoreInputState() {
skipOnTextChanges = true;
......@@ -284,13 +293,6 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
}
}
@Override
public void onPause() {
super.onPause();
saveInputState();
listener.getChatScroller().unregisterChat(this);
}
public void saveInputState() {
ChatManager.getInstance().setTyped(account, user, inputView.getText().toString(),
inputView.getSelectionStart(), inputView.getSelectionEnd());
......@@ -312,7 +314,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
if (SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.always
|| (getActivity().getResources().getBoolean(R.bool.landscape)
&& SettingsManager.chatsHideKeyboard() == SettingsManager.ChatsHideKeyboard.landscape)) {
ChatViewer.hideKeyboard(getActivity());
ChatScroller.hideKeyboard(getActivity());
}
}
......@@ -572,7 +574,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.getChatScroller().onCloseChat();
listener.getChatScroller().onCloseChat(new BaseEntity(account, user));
}
private void clearHistory(String account, String user) {
......@@ -621,7 +623,7 @@ public class ChatViewerFragment extends Fragment implements PopupMenu.OnMenuItem
}
public interface ChatViewerFragmentListener {
void onCloseChat();
void onCloseChat(BaseEntity chat);
void onMessageSent();
......
......@@ -28,7 +28,6 @@ import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.view.MenuItemCompat;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
......@@ -45,7 +44,6 @@ import android.widget.Toast;
import com.xabber.android.R;
import com.xabber.android.data.ActivityManager;
import com.xabber.android.data.Application;
import com.xabber.android.data.LogManager;
import com.xabber.android.data.NetworkException;
import com.xabber.android.data.SettingsManager;
import com.xabber.android.data.account.AccountManager;
......@@ -56,6 +54,7 @@ import com.xabber.android.data.extension.muc.MUCManager;
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.chat.ChatManager;
import com.xabber.android.data.notification.NotificationManager;
import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.RosterContact;
......@@ -66,7 +65,6 @@ import com.xabber.android.ui.dialog.AccountChooseDialogFragment.OnChoosedListene
import com.xabber.android.ui.dialog.ContactIntegrationDialogFragment;
import com.xabber.android.ui.dialog.StartAtBootDialogFragment;
import com.xabber.android.ui.helper.BarPainter;
import com.xabber.android.ui.helper.ChatScroller;
import com.xabber.xmpp.address.Jid;
import com.xabber.xmpp.uri.XMPPUri;
......@@ -78,9 +76,8 @@ import java.util.Collection;
*
* @author alexander.ivanov
*/
public class ContactList extends MainBasicActivity implements OnAccountChangedListener,
View.OnClickListener, OnChoosedListener, OnContactClickListener, ChatScroller.ChatScrollerListener,
ChatScroller.ChatScrollerProvider, Toolbar.OnMenuItemClickListener {
public class ContactList extends ChatIntentActivity implements OnAccountChangedListener,
View.OnClickListener, OnChoosedListener, OnContactClickListener, Toolbar.OnMenuItemClickListener {
/**
* Select contact to be invited to the room was requested.
......@@ -109,7 +106,6 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
private SearchView searchView;
private BarPainter barPainter;
private boolean isDualPanelView;
private ChatScroller chatScroller;
public static Intent createPersistentIntent(Context context) {
Intent intent = new Intent(context, ContactList.class);
......@@ -158,8 +154,8 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
LinearLayout chatScrollIndicatorLayout = (LinearLayout) findViewById(R.id.chat_scroll_indicator);
chatScroller = new ChatScroller(this, viewPager, chatScrollIndicatorLayout);
chatScroller.initChats(null);
chatScroller.createView(viewPager, chatScrollIndicatorLayout);
chatScroller.initChats();
}
toolbar.setOnClickListener(this);
......@@ -194,7 +190,6 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
super.onNewIntent(intent);
setIntent(intent);
action = getIntent().getAction();
getIntent().setAction(null);
}
@Override
......@@ -255,10 +250,10 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
*/
private void openChat(BaseEntity baseEntity, String text) {
if (text == null) {
startActivity(ChatViewer.createSendIntent(this,
startActivity(ChatIntentActivity.createSendIntent(this,
baseEntity.getAccount(), baseEntity.getUser(), null));
} else {
startActivity(ChatViewer.createSendIntent(this,
startActivity(ChatIntentActivity.createSendIntent(this,
baseEntity.getAccount(), baseEntity.getUser(), text));
}
finish();
......@@ -313,6 +308,11 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
}
break;
}
case ACTION_SPECIFIC_CHAT:
case ACTION_ATTENTION:
case ACTION_SHORTCUT:
startChat(ChatManager.getInstance().getSelectedChat());
}
}
......@@ -332,9 +332,6 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
}
}
if (isDualPanelView) {
chatScroller.update();
}
}
@Override
......@@ -431,7 +428,7 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
startActivity(ConferenceAdd.createIntent(this));
return true;
case R.id.action_chat_list:
startActivity(ChatViewer.createRecentChatsIntent(this));
startChat(null);
return true;
default:
return super.onOptionsItemSelected(item);
......@@ -480,6 +477,13 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
removeMessageNotification(chat.getAccount(), chat.getUser());
}
getContactListFragment().getAdapter().onChange();
ChatManager.getInstance().setInitialChat(null);
ChatManager.getInstance().setSelectedChat(null);
if (isDualPanelView) {
chatScroller.initChats();
chatScroller.update();
}
}
private ContactListFragment getContactListFragment() {
......@@ -523,14 +527,8 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
@Override
public void onContactClick(AbstractContact abstractContact) {
if (action == null) {
if (isDualPanelView) {
chatScroller.setSelectedChat(abstractContact);
chatScroller.update();
} else {
startActivity(ChatViewer.createSpecificChatIntent(this,
abstractContact.getAccount(), abstractContact.getUser()));
return;
}
startChat(abstractContact);
return;
}
switch (action) {
case ACTION_ROOM_INVITE: {
......@@ -550,7 +548,7 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
}
case Intent.ACTION_SEND:
action = null;
startActivity(ChatViewer.createSendIntent(this,
startActivity(ChatIntentActivity.createSendIntent(this,
abstractContact.getAccount(), abstractContact.getUser(), sendText));
finish();
break;
......@@ -560,22 +558,25 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
break;
}
default:
if (isDualPanelView) {
chatScroller.initChats(abstractContact);
chatScroller.setSelectedChat(abstractContact);
chatScroller.update();
} else {
startActivity(ChatViewer.createSpecificChatIntent(this,
abstractContact.getAccount(), abstractContact.getUser()));
return;
}
startChat(abstractContact);
break;
}
}
private void startChat(BaseEntity abstractContact) {
ChatManager.getInstance().setInitialChat(abstractContact);
ChatManager.getInstance().setSelectedChat(abstractContact);
if (isDualPanelView) {
chatScroller.initChats();
chatScroller.update();
} else {
startActivity(ChatViewer.createChatViewerIntent(this));
}
}
private void createShortcut(AbstractContact abstractContact) {
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, ChatViewer.createShortCutIntent(this,
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, ChatIntentActivity.createShortCutIntent(this,
abstractContact.getAccount(), abstractContact.getUser()));
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, abstractContact.getName());
Bitmap bitmap;
......@@ -586,7 +587,7 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
bitmap = AvatarManager.getInstance().getUserBitmap(abstractContact.getUser());
}
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON,
AvatarManager.getInstance().createShortcutBitmap(bitmap));
AvatarManager.getInstance().createShortcutBitmap(bitmap));
setResult(RESULT_OK, intent);
}
......@@ -611,12 +612,16 @@ public class ContactList extends MainBasicActivity implements OnAccountChangedLi
}
@Override
public void onClose() {
public void onClose(BaseEntity chat) {
}
ChatManager.getInstance().setSelectedChat(null);
@Override
public ChatScroller getChatScroller() {
return chatScroller;
if (ChatManager.getInstance().getInitialChat().equals(chat)) {
ChatManager.getInstance().setInitialChat(null);
chatScroller.initChats();
}
chatScroller.update();
}
}
package com.xabber.android.ui;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.widget.Toolbar;
import com.xabber.android.R;
import com.xabber.android.ui.helper.ManagedActivity;
import com.xabber.android.ui.preferences.AboutViewer;
import com.xabber.android.ui.preferences.AccountEditor;
import com.xabber.android.ui.preferences.PreferenceEditor;
public class MainBasicActivity extends ManagedActivity implements ContactListDrawerFragment.ContactListDrawerListener {
protected ActionBarDrawerToggle drawerToggle;
protected DrawerLayout drawerLayout;
protected Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact_list);
toolbar = (Toolbar) findViewById(R.id.toolbar_default);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
}
@Override
public void onContactListDrawerListener(int viewId) {
drawerLayout.closeDrawers();
switch (viewId) {
case R.id.drawer_action_settings:
startActivity(PreferenceEditor.createIntent(this));
break;
case R.id.drawer_action_about:
startActivity(AboutViewer.createIntent(this));
break;
case R.id.drawer_action_exit:
exit();
break;
}
}
protected void exit() {
}
@Override
public void onAccountSelected(String account) {
drawerLayout.closeDrawers();
startActivity(AccountEditor.createIntent(this, account));
}
}
......@@ -23,7 +23,8 @@ import java.util.List;
public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuItemClickListener {
private ChatScroller.ChatScrollerProvider listener = null;
private AccountPainter accountPainter;
private Toolbar toolbar;
/**
* Mandatory empty constructor for the fragment manager to instantiate the
......@@ -60,28 +61,24 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
super.onCreateView(inflater, container, savedInstanceState);
View rootView = inflater.inflate(R.layout.recent_chats, container, false);
updateChats();
toolbar = (Toolbar) rootView.findViewById(R.id.toolbar_default);
toolbar.setTitle(R.string.recent_chats);
if (getListAdapter().isEmpty()) {
Activity activity = getActivity();
Toast.makeText(activity, R.string.chat_list_is_empty, Toast.LENGTH_LONG).show();
// activity.finish();
if (!getResources().getBoolean(R.bool.landscape)) {
toolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(getActivity());
}
});
}
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar_default);
toolbar.setTitle(R.string.recent_chats);
toolbar.setNavigationIcon(R.drawable.ic_arrow_left_white_24dp);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
NavUtils.navigateUpFromSameTask(getActivity());
}
});
toolbar.inflateMenu(R.menu.recent_chats);
toolbar.setOnMenuItemClickListener(this);
AccountPainter accountPainter = new AccountPainter(getActivity());
toolbar.setBackgroundColor(accountPainter.getDefaultMainColor());
accountPainter = new AccountPainter(getActivity());
updateChats();
return rootView;
}
......@@ -92,6 +89,7 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
if (null != listener.getChatScroller()) {
listener.getChatScroller().registerRecentChatsList(this);
}
toolbar.setBackgroundColor(accountPainter.getDefaultMainColor());
}
@Override
......@@ -129,7 +127,7 @@ public class RecentChatFragment extends ListFragment implements Toolbar.OnMenuIt
}
public void updateChats() {
if (listener.getChatScroller() != null) {
if (listener.getChatScroller().isInitialized()) {
((ChatListAdapter) getListAdapter()).updateChats(listener.getChatScroller().getActiveChats());
}
}
......
......@@ -6,6 +6,7 @@ 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.entity.BaseEntity;
import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.data.message.MessageManager;
......@@ -79,6 +80,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter {
}
public boolean updateChats() {
LogManager.i(this, "updateChats");
ArrayList<AbstractChat> newChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
......@@ -99,11 +101,20 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter {
}
activeChats = newChats;
LogManager.i(this, "activeChats size " + activeChats.size());
notifyDataSetChanged();
return true;
}
public void clear() {
LogManager.i(this, "clear");
activeChats.clear();
notifyDataSetChanged();
}
private boolean isChatsEquals(ArrayList<AbstractChat> newChats) {
if (newChats.size() != activeChats.size()) {
return false;
......
......@@ -2,7 +2,10 @@ package com.xabber.android.ui.helper;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.LinearLayout;
import com.xabber.android.R;
......@@ -13,9 +16,9 @@ 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.message.chat.ChatManager;
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;
......@@ -34,62 +37,49 @@ public class ChatScroller implements
RecentChatFragment.RecentChatFragmentInteractionListener,
ChatViewerFragment.ChatViewerFragmentListener {
private final ChatManager chatManager;
Activity activity;
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;
public ChatScroller(Activity activity) {
chatManager = ChatManager.getInstance();
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 static void hideKeyboard(Activity activity) {
// Check if no view has focus:
View view = activity.getCurrentFocus();
if (view != null) {
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}
}
public boolean isExitOnSend() {
return exitOnSend;
public void createView(ViewPager viewPager, LinearLayout chatScrollIndicatorLayout) {
this.viewPager = viewPager;
this.chatScrollIndicatorAdapter = new ChatScrollIndicatorAdapter(activity, chatScrollIndicatorLayout);
}
public void setExitOnSend(boolean exitOnSend) {
this.exitOnSend = exitOnSend;
public void onHide() {
MessageManager.getInstance().removeVisibleChat();
}
public void setExtraText(String extraText) {
this.extraText = extraText;
}
public void initChats() {
public void initChats(BaseEntity initialChat) {
if (initialChat != null) {
chatViewerAdapter = new ChatViewerAdapter(activity.getFragmentManager(), initialChat, this);
if (chatManager.getInitialChat() != null) {
chatViewerAdapter = new ChatViewerAdapter(activity.getFragmentManager(), chatManager.getInitialChat(), this);
} else {
chatViewerAdapter = new ChatViewerAdapter(activity.getFragmentManager(), this);
}
viewPager.setAdapter(chatViewerAdapter);
viewPager.setOnPageChangeListener(this);
......@@ -101,16 +91,20 @@ public class ChatScroller implements
}
public void update() {
if (!isInitialized()) {
return;
}
chatViewerAdapter.updateChats();
chatScrollIndicatorAdapter.update(chatViewerAdapter.getActiveChats());
selectPage();
}
private void selectPage() {
if (isRecentChatsSelected) {
if (chatManager.getSelectedChat() == null) {
selectRecentChatsPage();
} else {
selectChatPage(selectedChat, false);
selectChatPage(chatManager.getSelectedChat(), false);
}
}
......@@ -140,7 +134,8 @@ public class ChatScroller implements
}
private void updateStatusBar() {
if (isRecentChatsSelected) {
BaseEntity selectedChat = chatManager.getSelectedChat();
if (selectedChat == null) {
listener.onStatusBarNeedPaint(null);
} else {
listener.onStatusBarNeedPaint(selectedChat.getAccount());
......@@ -153,6 +148,10 @@ public class ChatScroller implements
@Override
public void onChatChanged(final String account, final String user, final boolean incoming) {
if (!isInitialized()) {
return;
}
if (chatViewerAdapter.updateChats()) {
chatScrollIndicatorAdapter.update(chatViewerAdapter.getActiveChats());
selectPage();
......@@ -162,7 +161,7 @@ public class ChatScroller implements
updateStatusBar();
for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(selectedChat) && incoming) {
if (chat.isEqual(new BaseEntity(account, user)) && incoming) {
chat.playIncomingAnimation();
}
}
......@@ -202,18 +201,17 @@ public class ChatScroller implements
@Override
public void onPageSelected(int position) {
ChatViewer.hideKeyboard(activity);
hideKeyboard(activity);
chatScrollIndicatorAdapter.select(chatViewerAdapter.getRealPagePosition(position));
setSelectedChat(chatViewerAdapter.getChatByPageNumber(position));
chatManager.setSelectedChat(chatViewerAdapter.getChatByPageNumber(position));
if (isRecentChatsSelected) {
if (chatManager.getSelectedChat() == null) {
MessageManager.getInstance().removeVisibleChat();
} else {
if (isVisible) {
MessageManager.getInstance().setVisibleChat(selectedChat);
}
BaseEntity selectedChat = chatManager.getSelectedChat();
MessageManager.getInstance().setVisibleChat(selectedChat);
MessageArchiveManager.getInstance().requestHistory(selectedChat.getAccount(), selectedChat.getUser(), 0,
MessageManager.getInstance().getChat(selectedChat.getAccount(), selectedChat.getUser()).getRequiredMessageCount());
......@@ -253,18 +251,17 @@ public class ChatScroller implements
/**
* ChatViewerFragmentListener
* @param baseEntity
*/
@Override
public void onCloseChat() {
listener.onClose();
public void onCloseChat(BaseEntity chat) {
update();
listener.onClose(chat);
}
@Override
public void onMessageSent() {
if (exitOnSend) {
listener.onClose();
}
}
@Override
......@@ -295,7 +292,7 @@ public class ChatScroller implements
boolean isExtraTextInserted = false;
for (ChatViewerFragment chat : registeredChats) {
if (chat.isEqual(selectedChat)) {
if (chat.isEqual(ChatManager.getInstance().getSelectedChat())) {
chat.setInputText(extraText);
isExtraTextInserted = true;
}
......@@ -306,14 +303,17 @@ public class ChatScroller implements
}
}
public boolean isInitialized() {
return chatViewerAdapter != null;
}
public interface ChatScrollerListener {
void onStatusBarNeedPaint(String account);
void onClose();
void onClose(BaseEntity chat);
}
public interface ChatScrollerProvider {
ChatScroller getChatScroller();
}
}
......@@ -37,7 +37,7 @@ import com.xabber.android.data.roster.AbstractContact;
import com.xabber.android.data.roster.GroupManager;
import com.xabber.android.data.roster.PresenceManager;
import com.xabber.android.data.roster.ShowOfflineMode;
import com.xabber.android.ui.ChatViewer;
import com.xabber.android.ui.ChatIntentActivity;
import com.xabber.android.ui.ConferenceAdd;
import com.xabber.android.ui.ContactAdd;
import com.xabber.android.ui.ContactEditor;
......@@ -72,7 +72,7 @@ public class ContextMenuHelper {
@Override
public boolean onMenuItemClick(MenuItem item) {
MessageManager.getInstance().openChat(account, user);
activity.startActivity(ChatViewer.createSpecificChatIntent(
activity.startActivity(ChatIntentActivity.createSpecificChatIntent(
activity, account, user));
return true;
}
......
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape_mode">true</bool>
</resources>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<resources>
<bool name="is_landscape_mode">false</bool>
</resources>
\ 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