From 52512b234331ef3daf6bfc1997b1127c3c36889a Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 21 Aug 2018 11:36:53 +0200 Subject: [PATCH] Navigation bar is live --- .../client/ui/activities/MainActivity.java | 29 ++++----- .../client/ui/views/NavigationBar.java | 63 ++++++++++++++++++- .../client/ui/views/NavigationBarItem.java | 55 ++++++++++++++-- app/src/main/res/layout/activity_main.xml | 1 + .../main/res/layout/navigation_bar_item.xml | 4 +- app/src/main/res/values/colors.xml | 7 ++- 6 files changed, 128 insertions(+), 31 deletions(-) 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 b712c75..a81e6a7 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 @@ -49,6 +49,7 @@ import org.communiquons.android.comunic.client.ui.listeners.onPostOpenListener; import org.communiquons.android.comunic.client.ui.listeners.openConversationListener; import org.communiquons.android.comunic.client.ui.listeners.updateConversationListener; import org.communiquons.android.comunic.client.ui.utils.UiUtils; +import org.communiquons.android.comunic.client.ui.views.NavigationBar; import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage; @@ -58,8 +59,8 @@ import org.communiquons.android.comunic.client.ui.views.WebUserAccountImage; * @author Pierre HUBERT */ public class MainActivity extends AppCompatActivity implements - NavigationView.OnNavigationItemSelectedListener, openConversationListener, - updateConversationListener, onOpenUsersPageListener, onPostOpenListener { + openConversationListener, updateConversationListener, onOpenUsersPageListener, + onPostOpenListener, NavigationBar.OnNavigationItemSelectedListener { /** * Debug tag @@ -91,6 +92,11 @@ public class MainActivity extends AppCompatActivity implements */ private ConversationsListHelper conversationsListHelper; + /** + * Main navigation bar + */ + private NavigationBar mNavBar; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -116,9 +122,6 @@ public class MainActivity extends AppCompatActivity implements //Set the content of the activity setContentView(R.layout.activity_main); - //Enable drawer - init_drawer(); - //Check for connectivity if (!APIRequestHelper.isAPIavailable(this)) { Toast.makeText(this, R.string.err_no_internet_connection, Toast.LENGTH_SHORT).show(); @@ -130,6 +133,10 @@ public class MainActivity extends AppCompatActivity implements //Initialize conversation list helper conversationsListHelper = new ConversationsListHelper(this, dbHelper); + //Use navigation bar + mNavBar = findViewById(R.id.nav_bar); + mNavBar.setOnNavigationItemSelectedListener(this); + //If it is the first time the application is launched, open notifications fragment if (savedInstanceState == null){ openNotificationsFragment(false); @@ -223,18 +230,6 @@ public class MainActivity extends AppCompatActivity implements - - - /** - * Drawer menu - */ - void init_drawer() { - - //TODO : remove - - } - - @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { int id = menuItem.getItemId(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBar.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBar.java index 3a1435d..ca5e926 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBar.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBar.java @@ -13,12 +13,14 @@ import android.widget.PopupMenu; import org.communiquons.android.comunic.client.R; +import java.util.ArrayList; + /** * Application navigation bar * * @author Pierre HUBERT */ -public class NavigationBar extends BaseFrameLayoutView { +public class NavigationBar extends BaseFrameLayoutView implements NavigationBarItem.OnNavigationBarItemClickListener { /** * Navigation bar items container @@ -31,10 +33,20 @@ public class NavigationBar extends BaseFrameLayoutView { private PopupMenu mPopupMenu; /** - * Associated mMenu + * Associated menu */ private Menu mMenu; + /** + * Navigation bar items + */ + private ArrayList mItems = new ArrayList<>(); + + /** + * Navigation selected item listener + */ + private OnNavigationItemSelectedListener mOnNavigationItemSelectedListener; + public NavigationBar(@NonNull Context context) { super(context); init(); @@ -59,7 +71,7 @@ public class NavigationBar extends BaseFrameLayoutView { View view = inflate(getContext(), R.layout.navigation_bar, this); mLinearLayout = view.findViewById(R.id.container); - //Process mMenu + //Inflate menu mPopupMenu = new PopupMenu(getContext(), null); mMenu = mPopupMenu.getMenu(); getActivity().getMenuInflater().inflate(R.menu.navigation_bar, mMenu); @@ -70,10 +82,55 @@ public class NavigationBar extends BaseFrameLayoutView { NavigationBarItem itemView = new NavigationBarItem(getContext()); mLinearLayout.addView(itemView, new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f)); + mItems.add(itemView); MenuItem item = mMenu.getItem(i); itemView.setIconDrawable(item.getIcon()); + itemView.setItemIndex(i); + itemView.setOnNavigationBarItemClickListener(this); + } } + + /** + * Set the currently selected item by its index in the list + * + * @param index The index of the item to mark as selected + */ + public void setIndexSelected(int index){ + + //Process the list of items + for(NavigationBarItem item : mItems) + item.setSelected(index == item.getItemIndex()); + + } + + public void setOnNavigationItemSelectedListener(OnNavigationItemSelectedListener onNavigationItemSelectedListener) { + this.mOnNavigationItemSelectedListener = onNavigationItemSelectedListener; + } + + @Override + public void onItemClick(int index) { + if(mOnNavigationItemSelectedListener == null) + return; + + if(mOnNavigationItemSelectedListener.onNavigationItemSelected(mMenu.getItem(index))) + setIndexSelected(index); + } + + /** + * Navigation item selected listener + */ + public interface OnNavigationItemSelectedListener { + + /** + * When an item is selected by the user + * + * @param item Selected MenuItem + * @return True to keep the item selected (and deselect other ones), false else + */ + boolean onNavigationItemSelected(MenuItem item); + + } } diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBarItem.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBarItem.java index 045abe0..3fdeaff 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBarItem.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBarItem.java @@ -4,8 +4,6 @@ import android.content.Context; import android.graphics.PorterDuff; import android.graphics.drawable.Drawable; import android.support.annotation.NonNull; -import android.util.Log; -import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; @@ -18,7 +16,7 @@ import org.communiquons.android.comunic.client.ui.utils.UiUtils; * * @author Pierre HUBERT */ -class NavigationBarItem extends BaseFrameLayoutView { +class NavigationBarItem extends BaseFrameLayoutView implements View.OnClickListener { /** * Debug tag @@ -35,6 +33,16 @@ class NavigationBarItem extends BaseFrameLayoutView { */ private Drawable mSrcDrawable; + /** + * Item index + */ + private int mItemIndex; + + /** + * Click listener + */ + private OnNavigationBarItemClickListener mOnNavigationBarItemClickListener; + /** * Selected state of the drawable */ @@ -46,7 +54,7 @@ class NavigationBarItem extends BaseFrameLayoutView { //Inflate view View view = inflate(getContext(), R.layout.navigation_bar_item, this); mIcon = view.findViewById(R.id.icon); - + view.setOnClickListener(this); } /** @@ -65,8 +73,12 @@ class NavigationBarItem extends BaseFrameLayoutView { public void draw(){ Drawable drawable = DrawableUtils.DuplicateDrawable(mSrcDrawable); - int color = isSelected() ? R.color.navbar_selected : R.color.navbar_default; - drawable.setColorFilter(UiUtils.getColor(getContext(), color), PorterDuff.Mode.SRC_IN); + int fgColor = isSelected() ? R.color.navbar_fg_selected : R.color.navbar_fg_default; + int bgColor = isSelected() ? R.color.navbar_bg_selected : R.color.navbar_bg_default; + + drawable.setColorFilter(UiUtils.getColor(getContext(), fgColor), PorterDuff.Mode.SRC_IN); + setBackgroundColor(UiUtils.getColor(getContext(), bgColor)); + mIcon.setImageDrawable(drawable); } @@ -77,5 +89,36 @@ class NavigationBarItem extends BaseFrameLayoutView { public void setSelected(boolean selected) { this.mSelected = selected; + draw(); + } + + public int getItemIndex() { + return mItemIndex; + } + + public void setItemIndex(int itemIndex) { + this.mItemIndex = itemIndex; + } + + public void setOnNavigationBarItemClickListener(OnNavigationBarItemClickListener onNavigationBarItemClickListener) { + this.mOnNavigationBarItemClickListener = onNavigationBarItemClickListener; + } + + @Override + public void onClick(View v) { + if(mOnNavigationBarItemClickListener != null) + mOnNavigationBarItemClickListener.onItemClick(getItemIndex()); + } + + /** + * Interface used to handle navigation bar items click + */ + interface OnNavigationBarItemClickListener { + /** + * Called on item click + * + * @param index The index of the clicked item + */ + void onItemClick(int index); } } diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index cff8c9a..9b27eee 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -5,6 +5,7 @@ android:orientation="vertical"> diff --git a/app/src/main/res/layout/navigation_bar_item.xml b/app/src/main/res/layout/navigation_bar_item.xml index f94d303..bb8fa70 100644 --- a/app/src/main/res/layout/navigation_bar_item.xml +++ b/app/src/main/res/layout/navigation_bar_item.xml @@ -4,9 +4,7 @@ android:layout_width="match_parent" android:layout_height="match_parent" xmlns:tools="http://schemas.android.com/tools" - android:background="@drawable/navbar_item_ripple_effect" - android:clickable="true" - android:focusable="true"> + android:background="@drawable/navbar_item_ripple_effect"> #000000 - #FFFFFF - @color/colorPrimary + #FFFFFF + @color/colorPrimary + @color/colorPrimary + #FFFFFF + #3F51B5