refactored Conversations Adapter into seperate class

This commit is contained in:
iNPUTmice 2014-08-15 13:18:15 +02:00
parent 0b8af155c1
commit 9c18d57e07
2 changed files with 114 additions and 93 deletions

View File

@ -12,6 +12,7 @@ import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message; import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.services.ImageProvider; import eu.siacs.conversations.services.ImageProvider;
import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate; import eu.siacs.conversations.services.XmppConnectionService.OnConversationUpdate;
import eu.siacs.conversations.ui.adapter.ConversationAdapter;
import eu.siacs.conversations.utils.ExceptionHelper; import eu.siacs.conversations.utils.ExceptionHelper;
import eu.siacs.conversations.utils.UIHelper; import eu.siacs.conversations.utils.UIHelper;
import android.net.Uri; import android.net.Uri;
@ -23,7 +24,6 @@ import android.app.ActionBar;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.app.FragmentTransaction; import android.app.FragmentTransaction;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.content.Context;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener; import android.content.DialogInterface.OnClickListener;
import android.content.IntentSender.SendIntentException; import android.content.IntentSender.SendIntentException;
@ -31,19 +31,15 @@ import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Resources; import android.content.res.Resources;
import android.graphics.Bitmap; import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable; import android.graphics.drawable.Drawable;
import android.support.v4.widget.SlidingPaneLayout; import android.support.v4.widget.SlidingPaneLayout;
import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener; import android.support.v4.widget.SlidingPaneLayout.PanelSlideListener;
import android.util.DisplayMetrics; import android.util.DisplayMetrics;
import android.view.KeyEvent; import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView; import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter; import android.widget.ArrayAdapter;
@ -51,7 +47,6 @@ import android.widget.CheckBox;
import android.widget.ListView; import android.widget.ListView;
import android.widget.PopupMenu; import android.widget.PopupMenu;
import android.widget.PopupMenu.OnMenuItemClickListener; import android.widget.PopupMenu.OnMenuItemClickListener;
import android.widget.TextView;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast; import android.widget.Toast;
@ -152,91 +147,10 @@ public class ConversationActivity extends XmppActivity {
listView = (ListView) findViewById(R.id.list); listView = (ListView) findViewById(R.id.list);
this.listAdapter = new ArrayAdapter<Conversation>(this, getActionBar().setDisplayHomeAsUpEnabled(false);
R.layout.conversation_list_row, conversationList) { getActionBar().setHomeButtonEnabled(false);
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (View) inflater.inflate(
R.layout.conversation_list_row, parent,false);
}
Conversation conv;
if (conversationList.size() > position) {
conv = getItem(position);
} else {
return view;
}
if (!spl.isSlideable()) {
if (conv == getSelectedConversation()) {
view.setBackgroundColor(0xffdddddd);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
TextView convName = (TextView) view
.findViewById(R.id.conversation_name);
convName.setText(conv.getName(useSubject));
TextView convLastMsg = (TextView) view
.findViewById(R.id.conversation_lastmsg);
ImageView imagePreview = (ImageView) view
.findViewById(R.id.conversation_lastimage);
Message latestMessage = conv.getLatestMessage();
if (latestMessage.getType() == Message.TYPE_TEXT || latestMessage.getType() == Message.TYPE_PRIVATE) {
if ((latestMessage.getEncryption() != Message.ENCRYPTION_PGP)
&& (latestMessage.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED)) {
convLastMsg.setText(conv.getLatestMessage().getBody());
} else {
convLastMsg
.setText(getText(R.string.encrypted_message_received));
}
convLastMsg.setVisibility(View.VISIBLE);
imagePreview.setVisibility(View.GONE);
} else if (latestMessage.getType() == Message.TYPE_IMAGE) {
if (latestMessage.getStatus() >= Message.STATUS_RECIEVED) {
convLastMsg.setVisibility(View.GONE);
imagePreview.setVisibility(View.VISIBLE);
loadBitmap(latestMessage, imagePreview);
} else {
convLastMsg.setVisibility(View.VISIBLE);
imagePreview.setVisibility(View.GONE);
if (latestMessage.getStatus() == Message.STATUS_RECEIVED_OFFER) {
convLastMsg
.setText(getText(R.string.image_offered_for_download));
} else if (latestMessage.getStatus() == Message.STATUS_RECIEVING) {
convLastMsg
.setText(getText(R.string.receiving_image));
} else {
convLastMsg.setText("");
}
}
}
if (!conv.isRead()) {
convName.setTypeface(null, Typeface.BOLD);
convLastMsg.setTypeface(null, Typeface.BOLD);
} else {
convName.setTypeface(null, Typeface.NORMAL);
convLastMsg.setTypeface(null, Typeface.NORMAL);
}
((TextView) view.findViewById(R.id.conversation_lastupdate))
.setText(UIHelper.readableTimeDifference(getContext(),
conv.getLatestMessage().getTimeSent()));
ImageView profilePicture = (ImageView) view
.findViewById(R.id.conversation_image);
profilePicture.setImageBitmap(conv.getImage(getApplicationContext(),56)); //;UIHelper.getContactPicture(conv,56, activity.getApplicationContext(), false));
return view;
}
};
this.listAdapter = new ConversationAdapter(this, conversationList);
listView.setAdapter(this.listAdapter); listView.setAdapter(this.listAdapter);
listView.setOnItemClickListener(new OnItemClickListener() { listView.setOnItemClickListener(new OnItemClickListener() {
@ -247,7 +161,7 @@ public class ConversationActivity extends XmppActivity {
paneShouldBeOpen = false; paneShouldBeOpen = false;
if (getSelectedConversation() != conversationList.get(position)) { if (getSelectedConversation() != conversationList.get(position)) {
setSelectedConversation(conversationList.get(position)); setSelectedConversation(conversationList.get(position));
swapConversationFragment(); // .onBackendConnected(conversationList.get(position)); swapConversationFragment();
} else { } else {
spl.closePane(); spl.closePane();
} }
@ -433,7 +347,7 @@ public class ConversationActivity extends XmppActivity {
switch (item.getItemId()) { switch (item.getItemId()) {
case android.R.id.home: case android.R.id.home:
spl.openPane(); spl.openPane();
break; return true;
case R.id.action_attach_file: case R.id.action_attach_file:
View menuAttachFile = findViewById(R.id.action_attach_file); View menuAttachFile = findViewById(R.id.action_attach_file);
if (menuAttachFile==null) { if (menuAttachFile==null) {

View File

@ -0,0 +1,107 @@
package eu.siacs.conversations.ui.adapter;
import java.util.List;
import eu.siacs.conversations.R;
import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.Message;
import eu.siacs.conversations.ui.ConversationActivity;
import eu.siacs.conversations.utils.UIHelper;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class ConversationAdapter extends ArrayAdapter<Conversation> {
ConversationActivity activity;
public ConversationAdapter(ConversationActivity activity,
List<Conversation> conversations) {
super(activity, 0, conversations);
this.activity = activity;
}
@Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = (View) inflater.inflate(R.layout.conversation_list_row,
parent, false);
}
Conversation conv = getItem(position);
if (!activity.getSlidingPaneLayout().isSlideable()) {
if (conv == activity.getSelectedConversation()) {
view.setBackgroundColor(0xffdddddd);
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
} else {
view.setBackgroundColor(Color.TRANSPARENT);
}
TextView convName = (TextView) view
.findViewById(R.id.conversation_name);
convName.setText(conv.getName(true));
TextView convLastMsg = (TextView) view
.findViewById(R.id.conversation_lastmsg);
ImageView imagePreview = (ImageView) view
.findViewById(R.id.conversation_lastimage);
Message latestMessage = conv.getLatestMessage();
if (latestMessage.getType() == Message.TYPE_TEXT
|| latestMessage.getType() == Message.TYPE_PRIVATE) {
if ((latestMessage.getEncryption() != Message.ENCRYPTION_PGP)
&& (latestMessage.getEncryption() != Message.ENCRYPTION_DECRYPTION_FAILED)) {
convLastMsg.setText(conv.getLatestMessage().getBody());
} else {
convLastMsg.setText(activity
.getText(R.string.encrypted_message_received));
}
convLastMsg.setVisibility(View.VISIBLE);
imagePreview.setVisibility(View.GONE);
} else if (latestMessage.getType() == Message.TYPE_IMAGE) {
if (latestMessage.getStatus() >= Message.STATUS_RECIEVED) {
convLastMsg.setVisibility(View.GONE);
imagePreview.setVisibility(View.VISIBLE);
activity.loadBitmap(latestMessage, imagePreview);
} else {
convLastMsg.setVisibility(View.VISIBLE);
imagePreview.setVisibility(View.GONE);
if (latestMessage.getStatus() == Message.STATUS_RECEIVED_OFFER) {
convLastMsg.setText(activity
.getText(R.string.image_offered_for_download));
} else if (latestMessage.getStatus() == Message.STATUS_RECIEVING) {
convLastMsg.setText(activity
.getText(R.string.receiving_image));
} else {
convLastMsg.setText("");
}
}
}
if (!conv.isRead()) {
convName.setTypeface(null, Typeface.BOLD);
convLastMsg.setTypeface(null, Typeface.BOLD);
} else {
convName.setTypeface(null, Typeface.NORMAL);
convLastMsg.setTypeface(null, Typeface.NORMAL);
}
((TextView) view.findViewById(R.id.conversation_lastupdate))
.setText(UIHelper.readableTimeDifference(getContext(), conv
.getLatestMessage().getTimeSent()));
ImageView profilePicture = (ImageView) view
.findViewById(R.id.conversation_image);
profilePicture.setImageBitmap(conv.getImage(activity, 56));
return view;
}
}