Navigation bar is live

This commit is contained in:
Pierre HUBERT 2018-08-21 11:36:53 +02:00
parent d910565021
commit 52512b2343
6 changed files with 128 additions and 31 deletions

View File

@ -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();

View File

@ -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<NavigationBarItem> 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);
}
}

View File

@ -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);
}
}

View File

@ -5,6 +5,7 @@
android:orientation="vertical">
<org.communiquons.android.comunic.client.ui.views.NavigationBar
android:id="@+id/nav_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

View File

@ -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">
<ImageView
android:id="@+id/icon"

View File

@ -12,8 +12,11 @@
<color name="default_drawable_color">#000000</color>
<!-- Navigation bar -->
<color name="navbar_default">#FFFFFF</color>
<color name="navbar_selected">@color/colorPrimary</color>
<color name="navbar_fg_default">#FFFFFF</color>
<color name="navbar_fg_selected">@color/colorPrimary</color>
<color name="navbar_bg_default">@color/colorPrimary</color>
<color name="navbar_bg_selected">#FFFFFF</color>
<!-- Conversation messages -->
<color name="conversation_user_messages_background">#3F51B5</color>