Commit 23408a81 authored by Grigory Fedorov's avatar Grigory Fedorov

ContactList menu options "Chat list" now leads to recent chats page of ChatViewer.

parent 27abcc57
...@@ -110,16 +110,6 @@ ...@@ -110,16 +110,6 @@
android:name="android.support.PARENT_ACTIVITY" android:name="android.support.PARENT_ACTIVITY"
android:value="com.xabber.android.ui.preferences.AccountList" /> android:value="com.xabber.android.ui.preferences.AccountList" />
</activity> </activity>
<activity
android:label="@string/chat_list"
android:name="com.xabber.android.ui.ChatList"
android:parentActivityName="com.xabber.android.ui.ContactList"
>
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.xabber.android.ui.ContactList" />
</activity>
<activity <activity
android:label="@string/occupant_list" android:label="@string/occupant_list"
android:name="com.xabber.android.ui.OccupantList" android:name="com.xabber.android.ui.OccupantList"
......
/**
* 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;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
import com.xabber.android.data.Application;
import com.xabber.android.data.account.OnAccountChangedListener;
import com.xabber.android.data.entity.BaseEntity;
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.roster.OnContactChangedListener;
import com.xabber.android.ui.adapter.ChatComparator;
import com.xabber.android.ui.adapter.ChatListAdapter;
import com.xabber.android.ui.helper.ManagedListActivity;
import com.xabber.androiddev.R;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class ChatList extends ManagedListActivity implements OnAccountChangedListener,
OnContactChangedListener, OnChatChangedListener, OnItemClickListener {
private ChatListAdapter listAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isFinishing())
return;
setContentView(R.layout.list);
listAdapter = new ChatListAdapter(this);
setListAdapter(listAdapter);
getListView().setOnItemClickListener(this);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
@Override
protected void onResume() {
super.onResume();
Application.getInstance().addUIListener(OnAccountChangedListener.class, this);
Application.getInstance().addUIListener(OnContactChangedListener.class, this);
Application.getInstance().addUIListener(OnChatChangedListener.class, this);
updateAdapter();
}
@Override
protected void onPause() {
super.onPause();
Application.getInstance().removeUIListener(OnAccountChangedListener.class, this);
Application.getInstance().removeUIListener(OnContactChangedListener.class, this);
Application.getInstance().removeUIListener(OnChatChangedListener.class, this);
}
@Override
public void onChatChanged(String account, String user, boolean incoming) {
updateAdapter();
}
@Override
public void onContactsChanged(Collection<BaseEntity> addresses) {
updateAdapter();
}
@Override
public void onAccountsChanged(Collection<String> accounts) {
updateAdapter();
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
AbstractChat chat = (AbstractChat) parent.getAdapter().getItem(position);
startActivity(ChatViewer.createIntent(this, chat.getAccount(), chat.getUser()));
finish();
}
public static Intent createIntent(Context context) {
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);
}
}
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
*/ */
package com.xabber.android.ui; package com.xabber.android.ui;
import android.app.Fragment;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.os.Bundle; import android.os.Bundle;
...@@ -86,46 +87,35 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -86,46 +87,35 @@ 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, "onCreate account: " + account + ", user: " + user);
if (account == null || user == null) {
Application.getInstance().onError(R.string.ENTRY_IS_NOT_FOUND);
finish();
return;
}
if (hasAttention(intent)) { if (hasAttention(intent)) {
AttentionManager.getInstance().removeAccountNotifications(account, user); AttentionManager.getInstance().removeAccountNotifications(account, user);
} }
actionWithAccount = null;
actionWithUser = null;
if (savedInstanceState != null) { if (savedInstanceState != null) {
actionWithAccount = savedInstanceState.getString(SAVED_ACCOUNT); if (account == null || user == null) {
actionWithUser = savedInstanceState.getString(SAVED_USER); account = savedInstanceState.getString(SAVED_ACCOUNT);
user = savedInstanceState.getString(SAVED_USER);
}
exitOnSend = savedInstanceState.getBoolean(SAVED_EXIT_ON_SEND); exitOnSend = savedInstanceState.getBoolean(SAVED_EXIT_ON_SEND);
} }
if (actionWithAccount == null) {
actionWithAccount = account;
}
if (actionWithUser == null) {
actionWithUser = user;
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true); getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_chat_viewer); setContentView(R.layout.activity_chat_viewer);
chatViewerAdapter = new ChatViewerAdapter(getFragmentManager(), if (account != null && user != null) {
actionWithAccount, actionWithUser, this); chatViewerAdapter = new ChatViewerAdapter(getFragmentManager(),
account, user, this);
} else {
chatViewerAdapter = new ChatViewerAdapter(getFragmentManager(), this);
}
viewPager = (ViewPager) findViewById(R.id.pager); viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(chatViewerAdapter); viewPager.setAdapter(chatViewerAdapter);
viewPager.setOnPageChangeListener(this); viewPager.setOnPageChangeListener(this);
LogManager.i(this, "onCreate user: " + actionWithUser); selectPage(account, user, false);
selectPage(false);
onChatSelected();
} }
@Override @Override
...@@ -179,25 +169,13 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -179,25 +169,13 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
LogManager.i(this, "onNewIntent account: " + account + ", user: " + user); LogManager.i(this, "onNewIntent account: " + account + ", user: " + user);
actionWithUser = user; selectPage(account, user, false);
actionWithAccount = account;
selectPage(false);
onChatSelected();
} }
private void selectPage(boolean smoothScroll ) { private void selectPage(String account, String user, boolean smoothScroll) {
int position = chatViewerAdapter.getPageNumber(account, user);
int position = chatViewerAdapter.getPageNumber(actionWithAccount, actionWithUser);
LogManager.i(this, "selectPage user: " + actionWithUser + " position: " + position);
viewPager.setCurrentItem(position, smoothScroll); viewPager.setCurrentItem(position, smoothScroll);
onPageSelected(position);
for (ChatViewerFragment chat : registeredChats) {
chat.updateChat(false);
}
} }
private static String getAccount(Intent intent) { private static String getAccount(Intent intent) {
...@@ -225,6 +203,10 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -225,6 +203,10 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
return new EntityIntentBuilder(context, ChatViewer.class).setAccount(account).setUser(user).build(); 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, public static Intent createClearTopIntent(Context context, String account,
String user) { String user) {
Intent intent = createIntent(context, account, user); Intent intent = createIntent(context, account, user);
...@@ -277,9 +259,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -277,9 +259,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
LogManager.i(this, "onContactsChanged"); LogManager.i(this, "onContactsChanged");
chatViewerAdapter.onChange(); chatViewerAdapter.onChange();
for (ChatViewerFragment chat : registeredChats) { updateRegisteredChats();
chat.updateChat(false);
}
} }
@Override @Override
...@@ -287,9 +267,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -287,9 +267,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
LogManager.i(this, "onAccountsChanged"); LogManager.i(this, "onAccountsChanged");
chatViewerAdapter.onChange(); chatViewerAdapter.onChange();
for (ChatViewerFragment chat : registeredChats) { updateRegisteredChats();
chat.updateChat(false);
}
} }
void onSent() { void onSent() {
...@@ -315,8 +293,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -315,8 +293,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override @Override
public void onPageSelected(int position) { public void onPageSelected(int position) {
AbstractChat selectedChat = chatViewerAdapter.getChatByPageNumber(position); AbstractChat selectedChat = chatViewerAdapter.getChatByPageNumber(position);
if (selectedChat == null) { if (selectedChat == null) {
...@@ -324,29 +300,29 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -324,29 +300,29 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
return; return;
} }
actionWithAccount = selectedChat.getAccount(); String account = selectedChat.getAccount();
actionWithUser = selectedChat.getUser(); String user = selectedChat.getUser();
LogManager.i(this, "onPageSelected position: " + position + " user: " + actionWithUser + " position: " + position);
onChatSelected(); final AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
}
private void onChatSelected() {
LogManager.i(this, "onChatSelected user: " + actionWithUser);
final AbstractContact abstractContact
= RosterManager.getInstance().getBestContact(actionWithAccount, actionWithUser);
setTitle(abstractContact.getName()); setTitle(abstractContact.getName());
MessageManager.getInstance().setVisibleChat(actionWithAccount, actionWithUser); MessageManager.getInstance().setVisibleChat(account, user);
MessageArchiveManager.getInstance().requestHistory( MessageArchiveManager.getInstance().requestHistory(
actionWithAccount, actionWithUser, 0, account, user, 0,
MessageManager.getInstance().getChat(actionWithAccount, actionWithUser).getRequiredMessageCount()); MessageManager.getInstance().getChat(account, user).getRequiredMessageCount());
NotificationManager.getInstance().removeMessageNotification(actionWithAccount, actionWithUser); NotificationManager.getInstance().removeMessageNotification(account, user);
actionWithAccount = account;
actionWithUser = user;
}
private void updateRegisteredChats() {
for (ChatViewerFragment chat : registeredChats) {
chat.updateChat(false);
}
} }
@Override @Override
...@@ -366,6 +342,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -366,6 +342,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
public void onChatViewAdapterFinishUpdate() { public void onChatViewAdapterFinishUpdate() {
LogManager.i(this, "onChatViewAdapterFinishUpdate position: user: " + actionWithUser); LogManager.i(this, "onChatViewAdapterFinishUpdate position: user: " + actionWithUser);
insertExtraText(); insertExtraText();
Fragment currentFragment = chatViewerAdapter.getCurrentFragment();
if (currentFragment instanceof ChatViewerFragment) {
((ChatViewerFragment)currentFragment).setInputFocus();
}
} }
private void insertExtraText() { private void insertExtraText() {
...@@ -390,14 +371,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener ...@@ -390,14 +371,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override @Override
public void onRecentChatSelected(AbstractChat chat) { public void onRecentChatSelected(AbstractChat chat) {
selectPage(chat.getAccount(), chat.getUser(), true);
actionWithAccount = chat.getAccount();
actionWithUser = chat.getUser();
LogManager.i(this, "onRecentChatSelected position: user: " + actionWithUser);
selectPage(true);
} }
@Override @Override
......
...@@ -283,7 +283,7 @@ public class ContactList extends ManagedActivity implements OnChoosedListener, O ...@@ -283,7 +283,7 @@ public class ContactList extends ManagedActivity implements OnChoosedListener, O
startActivity(MUCEditor.createIntent(this)); startActivity(MUCEditor.createIntent(this));
return true; return true;
case R.id.action_chat_list: case R.id.action_chat_list:
startActivity(ChatList.createIntent(this)); startActivity(ChatViewer.createIntent(this));
return true; return true;
case R.id.action_settings: case R.id.action_settings:
startActivity(PreferenceEditor.createIntent(this)); startActivity(PreferenceEditor.createIntent(this));
......
...@@ -7,6 +7,7 @@ import android.view.LayoutInflater; ...@@ -7,6 +7,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ListView; import android.widget.ListView;
import android.widget.Toast;
import com.xabber.android.data.message.AbstractChat; import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.ui.adapter.ChatListAdapter; import com.xabber.android.ui.adapter.ChatListAdapter;
...@@ -63,6 +64,12 @@ public class RecentChatFragment extends ListFragment { ...@@ -63,6 +64,12 @@ public class RecentChatFragment extends ListFragment {
initialChats = null; initialChats = null;
} }
if (getListAdapter().isEmpty()) {
Activity activity = getActivity();
Toast.makeText(activity, R.string.chat_list_is_empty, Toast.LENGTH_LONG).show();
activity.finish();
}
return inflater.inflate(R.layout.list, container, false); return inflater.inflate(R.layout.list, container, false);
} }
......
...@@ -43,6 +43,15 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -43,6 +43,15 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
onChange(); onChange();
} }
public ChatViewerAdapter(FragmentManager fragmentManager, FinishUpdateListener finishUpdateListener) {
super(fragmentManager);
this.finishUpdateListener = finishUpdateListener;
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
intent = null;
onChange();
}
@Override @Override
public int getCount() { public int getCount() {
// warning: scrolling to very high values (1,000,000+) results in // warning: scrolling to very high values (1,000,000+) results in
...@@ -51,9 +60,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -51,9 +60,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
} }
public int getRealCount() { public int getRealCount() {
int realCount = activeChats.size(); return activeChats.size() + 1;
return realCount + 1;
} }
@Override @Override
...@@ -78,7 +85,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -78,7 +85,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
public void onChange() { public void onChange() {
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats()); activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
if (!activeChats.contains(intent)) { if (intent != null && !activeChats.contains(intent)) {
activeChats.add(intent); activeChats.add(intent);
} }
...@@ -99,7 +106,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -99,7 +106,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
} }
} }
return -1; return 0;
} }
public AbstractChat getChatByPageNumber(int virtualPosition) { public AbstractChat getChatByPageNumber(int virtualPosition) {
...@@ -127,11 +134,6 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda ...@@ -127,11 +134,6 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
super.finishUpdate(container); super.finishUpdate(container);
finishUpdateListener.onChatViewAdapterFinishUpdate(); finishUpdateListener.onChatViewAdapterFinishUpdate();
if (currentFragment instanceof ChatViewerFragment) {
((ChatViewerFragment)currentFragment).updateChat(false);
((ChatViewerFragment)currentFragment).setInputFocus();
}
} }
public interface FinishUpdateListener { public interface FinishUpdateListener {
......
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