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 @@
android:name="android.support.PARENT_ACTIVITY"
android:value="com.xabber.android.ui.preferences.AccountList" />
</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
android:label="@string/occupant_list"
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 @@
*/
package com.xabber.android.ui;
import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
......@@ -86,46 +87,35 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
Intent intent = getIntent();
String account = getAccount(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)) {
AttentionManager.getInstance().removeAccountNotifications(account, user);
}
actionWithAccount = null;
actionWithUser = null;
if (savedInstanceState != null) {
actionWithAccount = savedInstanceState.getString(SAVED_ACCOUNT);
actionWithUser = savedInstanceState.getString(SAVED_USER);
if (account == null || user == null) {
account = savedInstanceState.getString(SAVED_ACCOUNT);
user = savedInstanceState.getString(SAVED_USER);
}
exitOnSend = savedInstanceState.getBoolean(SAVED_EXIT_ON_SEND);
}
if (actionWithAccount == null) {
actionWithAccount = account;
}
if (actionWithUser == null) {
actionWithUser = user;
}
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setContentView(R.layout.activity_chat_viewer);
chatViewerAdapter = new ChatViewerAdapter(getFragmentManager(),
actionWithAccount, actionWithUser, this);
if (account != null && user != null) {
chatViewerAdapter = new ChatViewerAdapter(getFragmentManager(),
account, user, this);
} else {
chatViewerAdapter = new ChatViewerAdapter(getFragmentManager(), this);
}
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(chatViewerAdapter);
viewPager.setOnPageChangeListener(this);
LogManager.i(this, "onCreate user: " + actionWithUser);
selectPage(false);
onChatSelected();
selectPage(account, user, false);
}
@Override
......@@ -179,25 +169,13 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
LogManager.i(this, "onNewIntent account: " + account + ", user: " + user);
actionWithUser = user;
actionWithAccount = account;
selectPage(false);
onChatSelected();
selectPage(account, user, false);
}
private void selectPage(boolean smoothScroll ) {
int position = chatViewerAdapter.getPageNumber(actionWithAccount, actionWithUser);
LogManager.i(this, "selectPage user: " + actionWithUser + " position: " + position);
private void selectPage(String account, String user, boolean smoothScroll) {
int position = chatViewerAdapter.getPageNumber(account, user);
viewPager.setCurrentItem(position, smoothScroll);
for (ChatViewerFragment chat : registeredChats) {
chat.updateChat(false);
}
onPageSelected(position);
}
private static String getAccount(Intent intent) {
......@@ -225,6 +203,10 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
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);
......@@ -277,9 +259,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
LogManager.i(this, "onContactsChanged");
chatViewerAdapter.onChange();
for (ChatViewerFragment chat : registeredChats) {
chat.updateChat(false);
}
updateRegisteredChats();
}
@Override
......@@ -287,9 +267,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
LogManager.i(this, "onAccountsChanged");
chatViewerAdapter.onChange();
for (ChatViewerFragment chat : registeredChats) {
chat.updateChat(false);
}
updateRegisteredChats();
}
void onSent() {
......@@ -315,8 +293,6 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override
public void onPageSelected(int position) {
AbstractChat selectedChat = chatViewerAdapter.getChatByPageNumber(position);
if (selectedChat == null) {
......@@ -324,29 +300,29 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
return;
}
actionWithAccount = selectedChat.getAccount();
actionWithUser = selectedChat.getUser();
LogManager.i(this, "onPageSelected position: " + position + " user: " + actionWithUser + " position: " + position);
String account = selectedChat.getAccount();
String user = selectedChat.getUser();
onChatSelected();
}
private void onChatSelected() {
LogManager.i(this, "onChatSelected user: " + actionWithUser);
final AbstractContact abstractContact
= RosterManager.getInstance().getBestContact(actionWithAccount, actionWithUser);
final AbstractContact abstractContact = RosterManager.getInstance().getBestContact(account, user);
setTitle(abstractContact.getName());
MessageManager.getInstance().setVisibleChat(actionWithAccount, actionWithUser);
MessageManager.getInstance().setVisibleChat(account, user);
MessageArchiveManager.getInstance().requestHistory(
actionWithAccount, actionWithUser, 0,
MessageManager.getInstance().getChat(actionWithAccount, actionWithUser).getRequiredMessageCount());
account, user, 0,
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
......@@ -366,6 +342,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
public void onChatViewAdapterFinishUpdate() {
LogManager.i(this, "onChatViewAdapterFinishUpdate position: user: " + actionWithUser);
insertExtraText();
Fragment currentFragment = chatViewerAdapter.getCurrentFragment();
if (currentFragment instanceof ChatViewerFragment) {
((ChatViewerFragment)currentFragment).setInputFocus();
}
}
private void insertExtraText() {
......@@ -390,14 +371,7 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
@Override
public void onRecentChatSelected(AbstractChat chat) {
actionWithAccount = chat.getAccount();
actionWithUser = chat.getUser();
LogManager.i(this, "onRecentChatSelected position: user: " + actionWithUser);
selectPage(true);
selectPage(chat.getAccount(), chat.getUser(), true);
}
@Override
......
......@@ -283,7 +283,7 @@ public class ContactList extends ManagedActivity implements OnChoosedListener, O
startActivity(MUCEditor.createIntent(this));
return true;
case R.id.action_chat_list:
startActivity(ChatList.createIntent(this));
startActivity(ChatViewer.createIntent(this));
return true;
case R.id.action_settings:
startActivity(PreferenceEditor.createIntent(this));
......
......@@ -7,6 +7,7 @@ import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.Toast;
import com.xabber.android.data.message.AbstractChat;
import com.xabber.android.ui.adapter.ChatListAdapter;
......@@ -63,6 +64,12 @@ public class RecentChatFragment extends ListFragment {
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);
}
......
......@@ -43,6 +43,15 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
onChange();
}
public ChatViewerAdapter(FragmentManager fragmentManager, FinishUpdateListener finishUpdateListener) {
super(fragmentManager);
this.finishUpdateListener = finishUpdateListener;
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
intent = null;
onChange();
}
@Override
public int getCount() {
// warning: scrolling to very high values (1,000,000+) results in
......@@ -51,9 +60,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
}
public int getRealCount() {
int realCount = activeChats.size();
return realCount + 1;
return activeChats.size() + 1;
}
@Override
......@@ -78,7 +85,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
public void onChange() {
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
if (!activeChats.contains(intent)) {
if (intent != null && !activeChats.contains(intent)) {
activeChats.add(intent);
}
......@@ -99,7 +106,7 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
}
}
return -1;
return 0;
}
public AbstractChat getChatByPageNumber(int virtualPosition) {
......@@ -127,11 +134,6 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
super.finishUpdate(container);
finishUpdateListener.onChatViewAdapterFinishUpdate();
if (currentFragment instanceof ChatViewerFragment) {
((ChatViewerFragment)currentFragment).updateChat(false);
((ChatViewerFragment)currentFragment).setInputFocus();
}
}
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