2018-02-12 02:08:56 +01:00
|
|
|
/*
|
|
|
|
* Copyright 2018 Paul Schaub
|
|
|
|
*
|
|
|
|
* This code is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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, write to the Free Software Foundation,
|
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2018-02-10 17:34:01 +01:00
|
|
|
package de.vanitasvitae.slam.mvp.view;
|
2018-01-31 14:40:01 +01:00
|
|
|
|
2018-02-13 21:02:46 +01:00
|
|
|
import android.content.Intent;
|
2018-01-31 14:40:01 +01:00
|
|
|
import android.os.Bundle;
|
2018-02-13 21:02:46 +01:00
|
|
|
import android.support.v4.app.Fragment;
|
2018-01-31 14:40:01 +01:00
|
|
|
import android.support.v7.widget.LinearLayoutManager;
|
|
|
|
import android.support.v7.widget.RecyclerView;
|
|
|
|
import android.view.LayoutInflater;
|
|
|
|
import android.view.View;
|
|
|
|
import android.view.ViewGroup;
|
|
|
|
import android.widget.TextView;
|
|
|
|
|
2018-02-12 02:08:56 +01:00
|
|
|
import org.jivesoftware.smack.packet.Message;
|
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
|
|
import org.jxmpp.jid.impl.JidCreate;
|
|
|
|
import org.jxmpp.stringprep.XmppStringprepException;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Map;
|
|
|
|
|
2018-01-31 14:40:01 +01:00
|
|
|
import butterknife.BindView;
|
|
|
|
import butterknife.ButterKnife;
|
|
|
|
import de.vanitasvitae.slam.R;
|
2018-02-12 02:08:56 +01:00
|
|
|
import de.vanitasvitae.slam.mvp.PresenterFactory;
|
|
|
|
import de.vanitasvitae.slam.mvp.contracts.ConversationContract;
|
2018-02-23 02:23:45 +01:00
|
|
|
import de.vanitasvitae.slam.mvp.view.message.MessageAdapter;
|
|
|
|
import de.vanitasvitae.slam.mvp.view.message.MessageView;
|
|
|
|
import de.vanitasvitae.slam.xmpp.message.AbstractMessage;
|
2018-01-31 14:40:01 +01:00
|
|
|
|
|
|
|
/**
|
2018-02-10 17:34:01 +01:00
|
|
|
* Fragment that shows the conversation with a user.
|
2018-01-31 14:40:01 +01:00
|
|
|
*/
|
2018-02-12 02:08:56 +01:00
|
|
|
public class ConversationFragment extends Fragment implements ConversationContract.View {
|
|
|
|
|
|
|
|
public static final String KEY_JID = "conversationfragment_jid";
|
2018-01-31 14:40:01 +01:00
|
|
|
|
|
|
|
@BindView(R.id.recycler_chat)
|
|
|
|
RecyclerView recyclerView;
|
|
|
|
|
2018-08-01 11:43:18 +02:00
|
|
|
private ConversationContract.Presenter presenter;
|
2018-02-12 02:08:56 +01:00
|
|
|
|
|
|
|
Map<String, Integer> messageIdIndizes = new HashMap<>();
|
2018-02-23 02:23:45 +01:00
|
|
|
List<AbstractMessage> messages = new ArrayList<>();
|
2018-02-12 02:08:56 +01:00
|
|
|
|
2018-02-23 02:23:45 +01:00
|
|
|
private final MessageAdapter messageAdapter = new MessageAdapter(getContext()) {
|
2018-02-12 02:08:56 +01:00
|
|
|
|
|
|
|
@Override
|
2018-02-23 02:23:45 +01:00
|
|
|
public void onBindViewHolder(MessageView holder, int position) {
|
|
|
|
super.onBindViewHolder(holder, position);
|
2018-02-13 21:02:46 +01:00
|
|
|
holder.setOnAvatarClickListener(new View.OnClickListener() {
|
|
|
|
@Override
|
|
|
|
public void onClick(View v) {
|
|
|
|
navigateToContactProfile();
|
|
|
|
}
|
|
|
|
});
|
2018-02-12 02:08:56 +01:00
|
|
|
}
|
|
|
|
|
2018-02-23 02:23:45 +01:00
|
|
|
@Override
|
|
|
|
public AbstractMessage getItemAt(int position) {
|
|
|
|
return messages.get(position);
|
|
|
|
}
|
|
|
|
|
2018-02-12 02:08:56 +01:00
|
|
|
@Override
|
|
|
|
public int getItemCount() {
|
|
|
|
return messages.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
public ConversationFragment() {
|
|
|
|
super();
|
2018-08-01 11:43:18 +02:00
|
|
|
setPresenter(PresenterFactory.getInstance().createConversationPresenter(this));
|
2018-02-12 02:08:56 +01:00
|
|
|
}
|
|
|
|
|
2018-01-31 14:40:01 +01:00
|
|
|
@Override
|
|
|
|
public View onCreateView(LayoutInflater inflater, ViewGroup container,
|
|
|
|
Bundle savedInstanceState) {
|
2018-02-13 21:02:46 +01:00
|
|
|
View view = inflater.inflate(R.layout.fragment_conversation, container, false);
|
2018-01-31 14:40:01 +01:00
|
|
|
ButterKnife.bind(this, view);
|
2018-02-12 02:08:56 +01:00
|
|
|
|
|
|
|
Bundle arguments = getArguments();
|
|
|
|
String jidString = arguments.getString(KEY_JID);
|
|
|
|
if (jidString != null) {
|
|
|
|
try {
|
|
|
|
EntityBareJid jid = JidCreate.entityBareFrom(jidString);
|
|
|
|
presenter.setPeersJid(jid);
|
|
|
|
} catch (XmppStringprepException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-31 14:40:01 +01:00
|
|
|
return view;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onViewCreated(final View view, Bundle savedInstanceState) {
|
|
|
|
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
|
2018-02-23 02:23:45 +01:00
|
|
|
recyclerView.setAdapter(messageAdapter);
|
2018-02-12 02:08:56 +01:00
|
|
|
recyclerView.getAdapter().notifyDataSetChanged();
|
|
|
|
}
|
2018-01-31 14:40:01 +01:00
|
|
|
|
2018-08-01 11:43:18 +02:00
|
|
|
@Override
|
|
|
|
public void setPresenter(ConversationContract.Presenter presenter) {
|
|
|
|
this.presenter = presenter;
|
|
|
|
}
|
|
|
|
|
2018-02-12 02:08:56 +01:00
|
|
|
@Override
|
2018-02-23 02:23:45 +01:00
|
|
|
public void addMessageItems(List<AbstractMessage> messages, boolean end) {
|
2018-02-12 02:08:56 +01:00
|
|
|
if (end) {
|
|
|
|
this.messages.addAll(messages);
|
|
|
|
} else {
|
|
|
|
this.messages.addAll(0, messages);
|
|
|
|
}
|
2018-02-23 02:23:45 +01:00
|
|
|
messageAdapter.notifyDataSetChanged();
|
2018-02-12 02:08:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void highlightMessageItem() {
|
2018-01-31 14:40:01 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-02-12 02:08:56 +01:00
|
|
|
@Override
|
|
|
|
public void correctMessageItem() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void navigateToContactProfile() {
|
2018-02-13 21:02:46 +01:00
|
|
|
startActivity(new Intent(getContext(), ContactDetailActivity.class));
|
2018-02-12 02:08:56 +01:00
|
|
|
}
|
2018-01-31 14:40:01 +01:00
|
|
|
}
|