Commit 44d5a81c authored by Grigory Fedorov's avatar Grigory Fedorov

ChatViewerFragment: focus set to input text.

parent 2c500bfb
......@@ -48,7 +48,8 @@ import java.util.HashSet;
* @author alexander.ivanov
*/
public class ChatViewer extends ManagedActivity implements OnChatChangedListener,
OnContactChangedListener, OnAccountChangedListener, ViewPager.OnPageChangeListener {
OnContactChangedListener, OnAccountChangedListener, ViewPager.OnPageChangeListener,
ChatViewerAdapter.FinishUpdateListener {
/**
* Attention request.
......@@ -111,7 +112,8 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
setContentView(R.layout.activity_chat_viewer);
chatViewerAdapter = new ChatViewerAdapter(getFragmentManager());
chatViewerAdapter = new ChatViewerAdapter(getFragmentManager(),
actionWithAccount, actionWithUser, this);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(chatViewerAdapter);
......@@ -136,15 +138,11 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
exitOnSend = true;
}
}
// selectPage(actionWithAccount, actionWithUser);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// LogManager.i(this, "onSave: " + actionWithAccount + ":" + actionWithUser);
outState.putString(SAVED_ACCOUNT, actionWithAccount);
outState.putString(SAVED_USER, actionWithUser);
outState.putBoolean(SAVED_EXIT_ON_SEND, exitOnSend);
......@@ -165,6 +163,8 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
if (isFinishing())
return;
chatViewerAdapter.onChange();
String account = getAccount(intent);
String user = getUser(intent);
if (account == null || user == null) {
......@@ -318,6 +318,10 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
private void onChatSelected(String account, String user) {
LogManager.i(this, "onChatSelected. account: " + account + "; user: " + user);
actionWithAccount = account;
actionWithUser = user;
MessageManager.getInstance().setVisibleChat(account, user);
MessageArchiveManager.getInstance().requestHistory(
......@@ -345,8 +349,18 @@ public class ChatViewer extends ManagedActivity implements OnChatChangedListener
registeredChats.remove(chat);
}
@Override
public void onChatViewAdapterFinishUpdate() {
for (CurrentUpdatableChat chat : registeredChats) {
if (chat.isEqual(actionWithAccount, actionWithUser)) {
chat.setInputFocus();
}
}
}
public interface CurrentUpdatableChat {
public void updateChat(final boolean incoming);
public boolean isEqual(String account, String user);
public void setInputFocus();
}
}
......@@ -26,6 +26,7 @@ import android.widget.ListView;
import android.widget.TextView;
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.extension.archive.MessageArchiveManager;
......@@ -90,17 +91,22 @@ public class ChatViewerFragment extends Fragment implements ChatViewer.CurrentUp
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
avatarInflaterHelper = AbstractAvatarInflaterHelper.createAbstractContactInflaterHelper();
Bundle args = getArguments();
account = args.getString(ARGUMENT_ACCOUNT, null);
user = args.getString(ARGUMENT_USER, null);
LogManager.i(this, "onCreate. user: " + user);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
LogManager.i(this, "onCreateView. user: " + user);
shakeAnimation = AnimationUtils.loadAnimation(getActivity(), R.anim.shake);
/*
......@@ -217,6 +223,8 @@ public class ChatViewerFragment extends Fragment implements ChatViewer.CurrentUp
public void onResume() {
super.onResume();
LogManager.i(this, "onResume. user: " + user);
((ChatViewer)getActivity()).registerChat(this);
registerForContextMenu(listView);
......@@ -592,4 +600,9 @@ public class ChatViewerFragment extends Fragment implements ChatViewer.CurrentUp
public boolean isEqual(String account, String user) {
return this.account.equals(account) && this.user.equals(user);
}
@Override
public void setInputFocus() {
inputView.requestFocus();
}
}
......@@ -7,22 +7,45 @@ 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;
import com.xabber.android.ui.ChatViewerFragment;
import com.xabber.xmpp.address.Jid;
import java.util.ArrayList;
import java.util.Collection;
public class ChatViewerAdapter extends FragmentStatePagerAdapter implements UpdatableAdapter {
private ArrayList<AbstractChat> activeChats;
/**
* Intent sent while opening chat activity.
*/
private final AbstractChat intent;
public ChatViewerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
/**
* Position to insert intent.
*/
private final int intentPosition;
LogManager.i(this, "ChatViewerAdapter");
private ArrayList<AbstractChat> activeChats;
private FinishUpdateListener finishUpdateListener;
public ChatViewerAdapter(FragmentManager fragmentManager, String account, String user, FinishUpdateListener finishUpdateListener) {
super(fragmentManager);
this.finishUpdateListener = finishUpdateListener;
activeChats = new ArrayList<>();
intent = MessageManager.getInstance().getOrCreateChat(account,
Jid.getBareAddress(user));
Collection<? extends BaseEntity> activeChats = MessageManager
.getInstance().getActiveChats();
if (activeChats.contains(intent)) {
intentPosition = -1;
} else {
intentPosition = activeChats.size();
}
onChange();
}
......@@ -33,8 +56,6 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
@Override
public Fragment getItem(int i) {
LogManager.i(this, "getItem: " + i);
AbstractChat abstractChat = getChat(i);
......@@ -47,37 +68,45 @@ public class ChatViewerAdapter extends FragmentStatePagerAdapter implements Upda
@Override
public void onChange() {
LogManager.i(this, "onChange: ");
activeChats = new ArrayList<>(MessageManager.getInstance().getActiveChats());
if (intentPosition != -1) {
int index = activeChats.indexOf(intent);
AbstractChat chat;
if (index == -1) {
chat = intent;
} else {
chat = activeChats.remove(index);
}
activeChats.add(Math.min(intentPosition, activeChats.size()), chat);
}
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);
return -1;
}
@Override
public void setPrimaryItem(ViewGroup container, int position, Object object) {
super.setPrimaryItem(container, position, object);
LogManager.i(this, "setPrimaryItem position: " + position);
}
@Override
public void finishUpdate(ViewGroup container) {
super.finishUpdate(container);
finishUpdateListener.onChatViewAdapterFinishUpdate();
}
public interface FinishUpdateListener {
public void onChatViewAdapterFinishUpdate();
}
}
\ 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