From 94f8ecf7463e977557728b8bae443a18e118c216 Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 21 Aug 2018 14:25:08 +0200 Subject: [PATCH] Added toolbar in conversation fragment. --- .../client/ui/activities/MainActivity.java | 13 ++++++ .../ui/fragments/ConversationFragment.java | 44 +++++++++++++++++-- app/src/main/res/drawable/ic_back.xml | 9 ++++ .../main/res/layout/fragment_conversation.xml | 21 +++++++-- 4 files changed, 79 insertions(+), 8 deletions(-) create mode 100644 app/src/main/res/drawable/ic_back.xml diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java index bcc674c..6c02288 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/activities/MainActivity.java @@ -355,6 +355,19 @@ public class MainActivity extends AppCompatActivity implements .create().show(); } + /** + * Go backward in the application + * + * @param activity Activity object + */ + public static void goBackward(@NonNull Activity activity){ + if(!(activity instanceof MainActivity)) + throw new RuntimeException("Called goBackward using an Activity different " + + "from MainActivity!"); + + ((MainActivity)activity).getSupportFragmentManager().popBackStack(); + } + /** * Open the friends list fragment */ diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java index 2d522c3..db42690 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/fragments/ConversationFragment.java @@ -1,6 +1,9 @@ package org.communiquons.android.comunic.client.ui.fragments; import android.app.AlertDialog; +import android.graphics.PorterDuff; +import android.graphics.drawable.Drawable; +import android.support.design.widget.AppBarLayout; import android.support.v4.app.Fragment; import android.content.DialogInterface; import android.content.Intent; @@ -9,8 +12,10 @@ import android.os.AsyncTask; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; +import android.support.v7.widget.Toolbar; import android.util.ArrayMap; import android.util.Log; import android.view.KeyEvent; @@ -99,6 +104,11 @@ public class ConversationFragment extends Fragment */ private ConversationRefreshRunnable refreshRunnable; + /** + * Fragment toolbar + */ + private Toolbar mToolbar; + /** * Fragment main progress bar */ @@ -227,12 +237,26 @@ public class ConversationFragment extends Fragment no_msg_notice = view.findViewById(R.id.fragment_conversation_noMsgYet); display_not_msg_notice(false); - //Conversation messages listView + //Get views convMessRecyclerView = view.findViewById(R.id.fragment_conversation_messageslist); + mToolbar = view.findViewById(R.id.toolbar); //Need user ID int userID = new AccountUtils(getActivity()).get_current_user_id(); + //Initialize toolbar + Drawable backDrawable = UiUtils.getDrawable(getActivity(), R.drawable.ic_back); + backDrawable.setColorFilter(UiUtils.getColor(getActivity(), android.R.color.white), + PorterDuff.Mode.SRC_IN); + mToolbar.setNavigationIcon(backDrawable); + mToolbar.setNavigationOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + MainActivity.goBackward(getActivity()); + } + }); + + //Create the adapter convMessAdapter = new ConversationMessageAdapter(getActivity(), messagesList, userID); @@ -291,7 +315,6 @@ public class ConversationFragment extends Fragment //Hide new message sending wheel new_message_progress_bar.setVisibility(View.GONE); - //Set a listener to detect when the user reaches the top of the conversation convMessRecyclerView.setOnScrollChangeDetectListener(this); } @@ -307,7 +330,7 @@ public class ConversationFragment extends Fragment new Thread(refreshRunnable).start(); //Update conversation title - getActivity().setTitle(R.string.fragment_conversation_title); + setTitle(UiUtils.getString(getActivity(), R.string.fragment_conversation_title)); MainActivity.SetNavbarSelectedOption(getActivity(), R.id.action_conversations); @@ -451,7 +474,7 @@ public class ConversationFragment extends Fragment conversationInfo = info; //Update the name of the conversation - getActivity().setTitle(info.getDisplayName()); + setTitle(info.getDisplayName()); } @@ -612,6 +635,19 @@ public class ConversationFragment extends Fragment no_msg_notice.setVisibility(visible ? View.VISIBLE : View.GONE); } + /** + * Update the title of the fragment + * + * @param title New title + */ + private void setTitle(String title){ + if(getActivity() == null) + return; + + getActivity().setTitle(title); + mToolbar.setTitle(title); + } + /** * This method is called when the user reach the top of the conversation */ diff --git a/app/src/main/res/drawable/ic_back.xml b/app/src/main/res/drawable/ic_back.xml new file mode 100644 index 0000000..c2e1faf --- /dev/null +++ b/app/src/main/res/drawable/ic_back.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/fragment_conversation.xml b/app/src/main/res/layout/fragment_conversation.xml index c2274ff..68cebeb 100644 --- a/app/src/main/res/layout/fragment_conversation.xml +++ b/app/src/main/res/layout/fragment_conversation.xml @@ -1,16 +1,15 @@ + android:layout_height="match_parent" + xmlns:app="http://schemas.android.com/apk/res-auto"> + android:layout_centerInParent="true" /> + + + + + +