From 5343372f218b52bb6530e22be7c58d573b7d5303 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 16 Dec 2017 12:43:06 +0100 Subject: [PATCH] Created Conversation Fragment --- .../android/comunic/client/MainActivity.java | 29 ++++++++- .../ConversationsListHelper.java | 16 +++++ .../fragments/ConversationFragment.java | 65 +++++++++++++++++++ .../fragments/ConversationsListFragment.java | 40 +++++++++++- .../main/res/layout/fragment_conversation.xml | 11 ++++ 5 files changed, 158 insertions(+), 3 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java create mode 100644 app/src/main/res/layout/fragment_conversation.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java index 9d551b8..6c03033 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/MainActivity.java @@ -16,7 +16,9 @@ import org.communiquons.android.comunic.client.api.APIRequest; import org.communiquons.android.comunic.client.data.Account.Account; import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.DatabaseHelper; +import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper; import org.communiquons.android.comunic.client.data.friendsList.FriendRefreshLoopRunnable; +import org.communiquons.android.comunic.client.fragments.ConversationFragment; import org.communiquons.android.comunic.client.fragments.ConversationsListFragment; import org.communiquons.android.comunic.client.fragments.FriendsListFragment; import org.communiquons.android.comunic.client.fragments.UserInfosFragment; @@ -27,7 +29,8 @@ import org.communiquons.android.comunic.client.fragments.UserInfosFragment; * * @author Pierre HUBERT */ -public class MainActivity extends AppCompatActivity { +public class MainActivity extends AppCompatActivity + implements ConversationsListHelper.openConversationListener { /** * Account object @@ -236,4 +239,28 @@ public class MainActivity extends AppCompatActivity { transaction.addToBackStack(null); transaction.commit(); } + + /** + * Open a conversation + * + * @param id The ID of the conversation to open + */ + @Override + public void openConversation(int id) { + + //Set the arguments for the conversation + Bundle args = new Bundle(); + args.putInt(ConversationFragment.ARG_CONVERSATION_ID ,id); + + //Create the fragment + ConversationFragment conversationFragment = new ConversationFragment(); + conversationFragment.setArguments(args); + + //Display it + FragmentTransaction transaction = getFragmentManager().beginTransaction(); + transaction.replace(R.id.main_fragment, conversationFragment); + transaction.addToBackStack(null); + transaction.commit(); + + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java index 0363819..30857b4 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/data/conversations/ConversationsListHelper.java @@ -137,4 +137,20 @@ public class ConversationsListHelper { return info; } + /** + * Handles the opening of a conversation + * + * This interface should be implemented in all the activity that should handle such action + */ + public interface openConversationListener { + + /** + * Open the conversation specified by its ID + * + * @param id The ID of the conversation to open + */ + void openConversation(int id); + + } + } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java new file mode 100644 index 0000000..2140c7a --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationFragment.java @@ -0,0 +1,65 @@ +package org.communiquons.android.comunic.client.fragments; + +import android.app.Fragment; +import android.os.Bundle; +import android.support.annotation.Nullable; +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Toast; + +import org.communiquons.android.comunic.client.R; + +/** + * Conversation fragment + * + * Display a conversation, its message. Allow the user to send and receive messages + * + * @author Pierre HUBERT + * Created by pierre on 12/16/17. + */ + +public class ConversationFragment extends Fragment { + + /** + * Debug tag + */ + private static final String TAG = "ConversationFragment"; + + /** + * The conversation ID argument + */ + public static final String ARG_CONVERSATION_ID = "conversation_id"; + + /** + * The conversation ID + */ + private int conversation_id; + + + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + //Get the conversation ID + conversation_id = getArguments().getInt(ARG_CONVERSATION_ID); + + if(conversation_id < 1){ + throw new RuntimeException(TAG + " requires a valid conversation ID when created !"); + } + + } + + @Nullable + @Override + public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) { + return inflater.inflate(R.layout.fragment_conversation, container, false); + } + + @Override + public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { + super.onViewCreated(view, savedInstanceState); + + Toast.makeText(getActivity(), "Open : " + conversation_id, Toast.LENGTH_SHORT).show(); + } +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java index 5b09e91..44e533b 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/ConversationsListFragment.java @@ -1,6 +1,8 @@ package org.communiquons.android.comunic.client.fragments; +import android.app.Activity; import android.app.Fragment; +import android.content.Context; import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.Nullable; @@ -9,6 +11,7 @@ import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; +import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; @@ -19,6 +22,8 @@ import org.communiquons.android.comunic.client.data.UsersInfo.UserInfo; import org.communiquons.android.comunic.client.data.conversations.ConversationsInfo; import org.communiquons.android.comunic.client.data.conversations.ConversationsListAdapter; import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper; +import org.communiquons.android.comunic.client.data.conversations.ConversationsListHelper + .openConversationListener; import java.util.ArrayList; @@ -31,7 +36,7 @@ import java.util.ArrayList; * Created by pierre on 12/6/17. */ -public class ConversationsListFragment extends Fragment { +public class ConversationsListFragment extends Fragment implements AdapterView.OnItemClickListener { /** * Debug tag @@ -58,6 +63,11 @@ public class ConversationsListFragment extends Fragment { */ private ListView conversationsListView; + /** + * Conversation opener + */ + private openConversationListener openConvListener; + /** * Conversation list adapter */ @@ -103,12 +113,20 @@ public class ConversationsListFragment extends Fragment { } }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); - + //Set the open conversation listener + try { + openConvListener = (openConversationListener) getActivity(); + } catch (ClassCastException e){ + throw new ClassCastException(getActivity().toString() + + " must implement OpenConversationListener"); + } } /** * Process the conversation list * + * This method must be called on a separate thread + * * @param list The list of conversations */ public void process_conversations_list(ArrayList list){ @@ -211,5 +229,23 @@ public class ConversationsListFragment extends Fragment { //Attach it to the view conversationsListView.setAdapter(conversationsListAdapter); + + conversationsListView.setOnItemClickListener(this); + } + + /** + * Handles the click on a conversation to open it + * + * {@inheritDoc} + */ + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + + //Get the clicked conversation + ConversationsInfo conv = convList.get(position); + + //Open the specified conversation + openConvListener.openConversation(conv.getID()); + } } diff --git a/app/src/main/res/layout/fragment_conversation.xml b/app/src/main/res/layout/fragment_conversation.xml new file mode 100644 index 0000000..6714e1f --- /dev/null +++ b/app/src/main/res/layout/fragment_conversation.xml @@ -0,0 +1,11 @@ + + + + + + \ No newline at end of file