aboutsummaryrefslogtreecommitdiffstats
path: root/src/de/gultsch/chat/ui/ConversationActivity.java
diff options
context:
space:
mode:
Diffstat (limited to 'src/de/gultsch/chat/ui/ConversationActivity.java')
-rw-r--r--src/de/gultsch/chat/ui/ConversationActivity.java139
1 files changed, 103 insertions, 36 deletions
diff --git a/src/de/gultsch/chat/ui/ConversationActivity.java b/src/de/gultsch/chat/ui/ConversationActivity.java
index 4f76e9c1..959e39a9 100644
--- a/src/de/gultsch/chat/ui/ConversationActivity.java
+++ b/src/de/gultsch/chat/ui/ConversationActivity.java
@@ -1,14 +1,13 @@
package de.gultsch.chat.ui;
import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Comparator;
import java.util.List;
import de.gultsch.chat.R;
import de.gultsch.chat.R.id;
-import de.gultsch.chat.entities.Account;
-import de.gultsch.chat.entities.Contact;
import de.gultsch.chat.entities.Conversation;
-import de.gultsch.chat.persistance.DatabaseBackend;
import de.gultsch.chat.utils.Beautifier;
import android.os.Bundle;
import android.app.FragmentTransaction;
@@ -34,13 +33,56 @@ import android.widget.ImageView;
public class ConversationActivity extends XmppActivity {
public static final String VIEW_CONVERSATION = "viewConversation";
- private static final String LOGTAG = "secureconversation";
protected static final String CONVERSATION = "conversationUuid";
protected SlidingPaneLayout spl;
- final List<Conversation> conversationList = new ArrayList<Conversation>();
+ private List<Conversation> conversationList = new ArrayList<Conversation>();
+ private int selectedConversation = 0;
+ private ListView listView;
+
+ private boolean paneShouldBeOpen = true;
+
+
+ public List<Conversation> getConversationList() {
+ return this.conversationList;
+ }
+ public int getSelectedConversation() {
+ return this.selectedConversation;
+ }
+
+ public ListView getConversationListView() {
+ return this.listView;
+ }
+
+ public SlidingPaneLayout getSlidingPaneLayout() {
+ return this.spl;
+ }
+
+ public boolean shouldPaneBeOpen() {
+ return paneShouldBeOpen;
+ }
+
+ public void updateConversationList() {
+ if (conversationList.size() >= 1) {
+ Conversation currentConv = conversationList.get(selectedConversation);
+ Collections.sort(this.conversationList, new Comparator<Conversation>() {
+ @Override
+ public int compare(Conversation lhs, Conversation rhs) {
+ return (int) (rhs.getLatestMessageDate() - lhs.getLatestMessageDate());
+ }
+ });
+ for(int i = 0; i < conversationList.size(); ++i) {
+ if (currentConv == conversationList.get(i)) {
+ selectedConversation = i;
+ break;
+ }
+ }
+ }
+ this.listView.invalidateViews();
+ }
+
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -48,7 +90,7 @@ public class ConversationActivity extends XmppActivity {
setContentView(R.layout.fragment_conversations_overview);
- final ListView listView = (ListView) findViewById(R.id.list);
+ listView = (ListView) findViewById(R.id.list);
listView.setAdapter(new ArrayAdapter<Conversation>(this,
R.layout.conversation_list_row, conversationList) {
@@ -61,8 +103,9 @@ public class ConversationActivity extends XmppActivity {
}
((TextView) view.findViewById(R.id.conversation_name))
.setText(getItem(position).getName());
+ ((TextView) view.findViewById(R.id.conversation_lastmsg)).setText(getItem(position).getLatestMessage());
((TextView) view.findViewById(R.id.conversation_lastupdate))
- .setText(Beautifier.readableTimeDifference(getItem(position).getCreated()));
+ .setText(Beautifier.readableTimeDifference(getItem(position).getLatestMessageDate()));
((ImageView) view.findViewById(R.id.conversation_image))
.setImageURI(getItem(position).getProfilePhotoUri());
return view;
@@ -75,11 +118,13 @@ public class ConversationActivity extends XmppActivity {
@Override
public void onItemClick(AdapterView<?> arg0, View clickedView,
int position, long arg3) {
- Log.d(LOGTAG, "List view was klicked on position " + position);
- swapConversationFragment(conversationList.get(position));
- getActionBar().setTitle(
- conversationList.get(position).getName());
- spl.closePane();
+ paneShouldBeOpen = false;
+ if (selectedConversation != position) {
+ selectedConversation = position;
+ swapConversationFragment(); //.onBackendConnected(conversationList.get(position));
+ } else {
+ spl.closePane();
+ }
}
});
spl = (SlidingPaneLayout) findViewById(id.slidingpanelayout);
@@ -91,6 +136,7 @@ public class ConversationActivity extends XmppActivity {
@Override
public void onPanelOpened(View arg0) {
+ paneShouldBeOpen = true;
getActionBar().setDisplayHomeAsUpEnabled(false);
getActionBar().setTitle(R.string.app_name);
invalidateOptionsMenu();
@@ -105,21 +151,13 @@ public class ConversationActivity extends XmppActivity {
focus.getWindowToken(),
InputMethodManager.HIDE_NOT_ALWAYS);
}
- listView.requestFocus();
}
@Override
public void onPanelClosed(View arg0) {
if (conversationList.size() > 0) {
getActionBar().setDisplayHomeAsUpEnabled(true);
- ConversationFragment convFrag = (ConversationFragment) getFragmentManager()
- .findFragmentById(R.id.selected_conversation);
- if (convFrag == null) {
- Log.d(LOGTAG, "conversation fragment was not found.");
- return; // just do nothing. at least dont crash
- }
- getActionBar().setTitle(
- convFrag.getConversation().getName());
+ getActionBar().setTitle(conversationList.get(selectedConversation).getName());
invalidateOptionsMenu();
}
}
@@ -134,7 +172,6 @@ public class ConversationActivity extends XmppActivity {
@Override
public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.conversations, menu);
if (spl.isOpen()) {
@@ -161,8 +198,23 @@ public class ConversationActivity extends XmppActivity {
break;
case R.id.action_add:
startActivity(new Intent(this, NewConversationActivity.class));
+ break;
case R.id.action_archive:
-
+ Conversation conv = getConversationList().get(selectedConversation);
+ conv.setStatus(Conversation.STATUS_ARCHIVED);
+ xmppConnectionService.updateConversation(conv);
+ conversationList.remove(selectedConversation);
+ selectedConversation = 0;
+ if (conversationList.size() >= 1) {
+ paneShouldBeOpen = true;
+ swapConversationFragment();
+ ((ArrayAdapter) listView.getAdapter()).notifyDataSetChanged();
+ spl.openPane();
+ } else {
+ startActivity(new Intent(this, NewConversationActivity.class));
+ finish();
+ }
+ //goto new
break;
default:
break;
@@ -170,14 +222,14 @@ public class ConversationActivity extends XmppActivity {
return super.onOptionsItemSelected(item);
}
- protected void swapConversationFragment(Conversation conv) {
- Log.d(LOGTAG, "swap conversation fragment to " + conv.getName());
+ protected ConversationFragment swapConversationFragment() {
ConversationFragment selectedFragment = new ConversationFragment();
- selectedFragment.setConversation(conv);
+
FragmentTransaction transaction = getFragmentManager()
.beginTransaction();
- transaction.replace(R.id.selected_conversation, selectedFragment);
+ transaction.replace(R.id.selected_conversation, selectedFragment,"conversation");
transaction.commit();
+ return selectedFragment;
}
@Override
@@ -202,25 +254,31 @@ public class ConversationActivity extends XmppActivity {
}
@Override
- void servConnected() {
+ void onBackendConnected() {
conversationList.clear();
conversationList.addAll(xmppConnectionService
.getConversations(Conversation.STATUS_AVAILABLE));
+
+ for(Conversation conversation : conversationList) {
+ conversation.setMessages(xmppConnectionService.getMessages(conversation));
+ }
- //spl.openPane();
+ this.updateConversationList();
if ((getIntent().getAction().equals(Intent.ACTION_VIEW) && (!handledViewIntent))) {
if (getIntent().getType().equals(
ConversationActivity.VIEW_CONVERSATION)) {
handledViewIntent = true;
- swapConversationFragment(conversationList.get(0));
- spl.closePane();
-
- // why do i even need this
- getActionBar().setDisplayHomeAsUpEnabled(true);
- getActionBar().setTitle(conversationList.get(0).getName());
+ String convToView = (String) getIntent().getExtras().get(CONVERSATION);
+ for(int i = 0; i < conversationList.size(); ++i) {
+ if (conversationList.get(i).getUuid().equals(convToView)) {
+ selectedConversation = i;
+ }
+ }
+ paneShouldBeOpen = false;
+ swapConversationFragment();
}
} else {
if (conversationList.size() <= 0) {
@@ -228,7 +286,16 @@ public class ConversationActivity extends XmppActivity {
startActivity(new Intent(this, NewConversationActivity.class));
finish();
} else {
- swapConversationFragment(conversationList.get(0));
+ //find currently loaded fragment
+ ConversationFragment selectedFragment = (ConversationFragment) getFragmentManager().findFragmentByTag("conversation");
+ if (selectedFragment!=null) {
+ Log.d("gultsch","ConversationActivity. found old fragment.");
+ selectedFragment.onBackendConnected();
+ } else {
+ Log.d("gultsch","conversationactivity. no old fragment found. creating new one");
+ Log.d("gultsch","selected conversation is #"+selectedConversation);
+ swapConversationFragment();
+ }
}
}
}