From 68198998fc43ea24da061d0d88660acf128f45ea Mon Sep 17 00:00:00 2001 From: Pierre HUBERT Date: Tue, 21 Aug 2018 11:00:25 +0200 Subject: [PATCH] Created new navbar --- .../client/ui/activities/MainActivity.java | 81 +------------------ .../client/ui/utils/DrawableUtils.java | 25 ++++++ .../client/ui/views/BaseFrameLayoutView.java | 51 ++++++++++++ .../client/ui/views/NavigationBar.java | 79 ++++++++++++++++++ .../client/ui/views/NavigationBarItem.java | 81 +++++++++++++++++++ app/src/main/res/drawable/ic_friends.xml | 12 +-- .../res/drawable/ic_menu_conversations.xml | 2 +- app/src/main/res/drawable/ic_menu_home.xml | 2 +- app/src/main/res/drawable/ic_notif.xml | 2 +- .../{ic_sync_black_24dp.xml => ic_sync.xml} | 2 +- app/src/main/res/drawable/ic_user.xml | 12 +-- .../drawable/navbar_item_ripple_effect.xml | 11 +++ app/src/main/res/layout/activity_main.xml | 38 +++------ app/src/main/res/layout/navigation_bar.xml | 10 +++ .../main/res/layout/navigation_bar_item.xml | 25 ++++++ ...ity_main_drawer.xml => navigation_bar.xml} | 2 +- app/src/main/res/values/colors.xml | 6 ++ app/src/main/res/values/dimens.xml | 3 + 18 files changed, 321 insertions(+), 123 deletions(-) create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/utils/DrawableUtils.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/views/BaseFrameLayoutView.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBar.java create mode 100644 app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBarItem.java rename app/src/main/res/drawable/{ic_sync_black_24dp.xml => ic_sync.xml} (89%) create mode 100644 app/src/main/res/drawable/navbar_item_ripple_effect.xml create mode 100644 app/src/main/res/layout/navigation_bar.xml create mode 100644 app/src/main/res/layout/navigation_bar_item.xml rename app/src/main/res/menu/{activity_main_drawer.xml => navigation_bar.xml} (94%) 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 5d42f0d..b712c75 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 @@ -91,11 +91,6 @@ public class MainActivity extends AppCompatActivity implements */ private ConversationsListHelper conversationsListHelper; - /** - * Bottom navigation view - */ - private DrawerLayout mDrawer; - @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -197,12 +192,6 @@ public class MainActivity extends AppCompatActivity implements //Get action id int id = item.getItemId(); - //To toggle drawer - if (id == android.R.id.home) { - toggleDrawer(); - return true; - } - //To search a user if (id == R.id.action_search_user) { searchUser(); @@ -241,65 +230,10 @@ public class MainActivity extends AppCompatActivity implements */ void init_drawer() { - assert getSupportActionBar() != null; - getSupportActionBar().setDisplayHomeAsUpEnabled(true); + //TODO : remove - mDrawer = findViewById(R.id.drawer_layout); - ActionBarDrawerToggle toggle = new ActionBarDrawerToggle( - this, mDrawer, R.string.navigation_drawer_open, R.string.navigation_drawer_close); - mDrawer.addDrawerListener(toggle); - toggle.syncState(); - - NavigationView navigationView = findViewById(R.id.nav_view); - navigationView.setNavigationItemSelectedListener(this); - - //Get information about the user - new Thread(new Runnable() { - @Override - public void run() { - final UserInfo info = new GetUsersHelper(getApplicationContext()).getSingle( - new AccountUtils(getApplicationContext()).get_current_user_id(), false); - - //Apply user information - if(mDrawer != null){ - mDrawer.post(new Runnable() { - @Override - public void run() { - applyUserInfoInDrawer(info); - } - }); - } - } - }).start(); } - /** - * Apply current user information in the drawer - * - * @param info Information about the user to apply - */ - private void applyUserInfoInDrawer(@Nullable UserInfo info){ - - //Check for errors - if(info == null){ - Toast.makeText(MainActivity.this, R.string.err_get_user_info, - Toast.LENGTH_SHORT).show(); - return; - } - - //Apply user information - ((TextView)findViewById(R.id.current_user_name)).setText(info.getDisplayFullName()); - ((WebUserAccountImage)findViewById(R.id.current_user_account_image)).setUser(info); - } - - @Override - public void onBackPressed() { - if (mDrawer.isDrawerOpen(GravityCompat.START)) { - mDrawer.closeDrawer(GravityCompat.START); - } else { - super.onBackPressed(); - } - } @Override public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) { @@ -330,21 +264,10 @@ public class MainActivity extends AppCompatActivity implements openConversationsListFragment(); } - - mDrawer.closeDrawer(GravityCompat.START); return true; } - /** - * Toggle drawer state - */ - void toggleDrawer(){ - if (mDrawer.isDrawerOpen(GravityCompat.START)) { - mDrawer.closeDrawer(GravityCompat.START); - } else { - mDrawer.openDrawer(GravityCompat.START); - } - } + diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/DrawableUtils.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/DrawableUtils.java new file mode 100644 index 0000000..26301bf --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/utils/DrawableUtils.java @@ -0,0 +1,25 @@ +package org.communiquons.android.comunic.client.ui.utils; + +import android.graphics.drawable.Drawable; + +import java.util.Objects; + +/** + * Drawable utilities + * + * @author Pierre HUBERT + */ +public class DrawableUtils { + + /** + * Duplicate a {@link Drawable} object + * + * + * @param source The drawable to duplicate + * @return Generated drawable copy + */ + public static Drawable DuplicateDrawable(Drawable source){ + return Objects.requireNonNull(source.getConstantState()).newDrawable().mutate(); + } + +} diff --git a/app/src/main/java/org/communiquons/android/comunic/client/ui/views/BaseFrameLayoutView.java b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/BaseFrameLayoutView.java new file mode 100644 index 0000000..36a68c5 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/BaseFrameLayoutView.java @@ -0,0 +1,51 @@ +package org.communiquons.android.comunic.client.ui.views; + +import android.app.Activity; +import android.content.Context; +import android.content.ContextWrapper; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.AttributeSet; +import android.widget.FrameLayout; + +/** + * Base extensible FrameLayoutView + * + * @author Pierre HUBERT + */ +abstract class BaseFrameLayoutView extends FrameLayout { + + public BaseFrameLayoutView(@NonNull Context context) { + super(context); + } + + public BaseFrameLayoutView(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + } + + public BaseFrameLayoutView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + } + + public BaseFrameLayoutView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) { + super(context, attrs, defStyleAttr, defStyleRes); + } + + /** + * Get hosting activity + * + * @return Hosting activity, if found, null else + */ + protected Activity getActivity(){ + Context context = getContext(); + + while(context instanceof ContextWrapper){ + if(context instanceof Activity) + return (Activity)context; + + context = ((ContextWrapper)context).getBaseContext(); + } + + return null; + } +} 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 new file mode 100644 index 0000000..3a1435d --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBar.java @@ -0,0 +1,79 @@ +package org.communiquons.android.comunic.client.ui.views; + +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.Nullable; +import android.util.AttributeSet; +import android.view.Menu; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.LinearLayout; +import android.widget.PopupMenu; + +import org.communiquons.android.comunic.client.R; + +/** + * Application navigation bar + * + * @author Pierre HUBERT + */ +public class NavigationBar extends BaseFrameLayoutView { + + /** + * Navigation bar items container + */ + private LinearLayout mLinearLayout; + + /** + * Popup mMenu used to inflate mMenu + */ + private PopupMenu mPopupMenu; + + /** + * Associated mMenu + */ + private Menu mMenu; + + public NavigationBar(@NonNull Context context) { + super(context); + init(); + } + + public NavigationBar(@NonNull Context context, @Nullable AttributeSet attrs) { + super(context, attrs); + init(); + } + + public NavigationBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) { + super(context, attrs, defStyleAttr); + init(); + } + + /** + * Initialize view + */ + private void init(){ + + //Inflate main view + View view = inflate(getContext(), R.layout.navigation_bar, this); + mLinearLayout = view.findViewById(R.id.container); + + //Process mMenu + mPopupMenu = new PopupMenu(getContext(), null); + mMenu = mPopupMenu.getMenu(); + getActivity().getMenuInflater().inflate(R.menu.navigation_bar, mMenu); + + for(int i = 0; i < mMenu.size(); i++){ + + //Inflate view + NavigationBarItem itemView = new NavigationBarItem(getContext()); + mLinearLayout.addView(itemView, + new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.MATCH_PARENT, 1f)); + + MenuItem item = mMenu.getItem(i); + + itemView.setIconDrawable(item.getIcon()); + } + } +} 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 new file mode 100644 index 0000000..045abe0 --- /dev/null +++ b/app/src/main/java/org/communiquons/android/comunic/client/ui/views/NavigationBarItem.java @@ -0,0 +1,81 @@ +package org.communiquons.android.comunic.client.ui.views; + +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; + +import org.communiquons.android.comunic.client.R; +import org.communiquons.android.comunic.client.ui.utils.DrawableUtils; +import org.communiquons.android.comunic.client.ui.utils.UiUtils; + +/** + * Navigation bar item view + * + * @author Pierre HUBERT + */ +class NavigationBarItem extends BaseFrameLayoutView { + + /** + * Debug tag + */ + private static final String TAG = NavigationBarItem.class.getCanonicalName(); + + /** + * Image icon + */ + private ImageView mIcon; + + /** + * Source drawable + */ + private Drawable mSrcDrawable; + + /** + * Selected state of the drawable + */ + private boolean mSelected; + + public NavigationBarItem(@NonNull Context context) { + super(context); + + //Inflate view + View view = inflate(getContext(), R.layout.navigation_bar_item, this); + mIcon = view.findViewById(R.id.icon); + + } + + /** + * Set item icon drawable + * + * @param iconDrawable The drawable to apply + */ + public void setIconDrawable(Drawable iconDrawable){ + mSrcDrawable = iconDrawable; + draw(); + } + + /** + * Draw (refresh) the view + */ + 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); + + mIcon.setImageDrawable(drawable); + } + + public boolean isSelected() { + return mSelected; + } + + public void setSelected(boolean selected) { + this.mSelected = selected; + } +} diff --git a/app/src/main/res/drawable/ic_friends.xml b/app/src/main/res/drawable/ic_friends.xml index 39f26f0..1cedace 100644 --- a/app/src/main/res/drawable/ic_friends.xml +++ b/app/src/main/res/drawable/ic_friends.xml @@ -1,9 +1,9 @@ + android:width="2048dp" + android:height="1792dp" + android:viewportWidth="2048.0" + android:viewportHeight="1792.0"> + android:fillColor="@color/default_drawable_color" + android:pathData="M657 896q-162 5-265 128h-134q-82 0-138-40.5t-56-118.5q0-353 124-353 6 0 43.5 21t97.5 42.5 119 21.5q67 0 133-23-5 37-5 66 0 139 81 256zm1071 637q0 120-73 189.5t-194 69.5h-874q-121 0-194-69.5t-73-189.5q0-53 3.5-103.5t14-109 26.5-108.5 43-97.5 62-81 85.5-53.5 111.5-20q10 0 43 21.5t73 48 107 48 135 21.5 135-21.5 107-48 73-48 43-21.5q61 0 111.5 20t85.5 53.5 62 81 43 97.5 26.5 108.5 14 109 3.5 103.5zm-1024-1277q0 106-75 181t-181 75-181-75-75-181 75-181 181-75 181 75 75 181zm704 384q0 159-112.5 271.5t-271.5 112.5-271.5-112.5-112.5-271.5 112.5-271.5 271.5-112.5 271.5 112.5 112.5 271.5zm576 225q0 78-56 118.5t-138 40.5h-134q-103-123-265-128 81-117 81-256 0-29-5-66 66 23 133 23 59 0 119-21.5t97.5-42.5 43.5-21q124 0 124 353zm-128-609q0 106-75 181t-181 75-181-75-75-181 75-181 181-75 181 75 75 181z"/> diff --git a/app/src/main/res/drawable/ic_menu_conversations.xml b/app/src/main/res/drawable/ic_menu_conversations.xml index 1ce9ca4..4b07e24 100644 --- a/app/src/main/res/drawable/ic_menu_conversations.xml +++ b/app/src/main/res/drawable/ic_menu_conversations.xml @@ -4,6 +4,6 @@ android:viewportWidth="24.0" android:viewportHeight="24.0"> diff --git a/app/src/main/res/drawable/ic_menu_home.xml b/app/src/main/res/drawable/ic_menu_home.xml index 32638c2..cacbb1e 100644 --- a/app/src/main/res/drawable/ic_menu_home.xml +++ b/app/src/main/res/drawable/ic_menu_home.xml @@ -5,7 +5,7 @@ android:viewportWidth="48.0"> diff --git a/app/src/main/res/drawable/ic_notif.xml b/app/src/main/res/drawable/ic_notif.xml index e1ef4f3..6a74c13 100644 --- a/app/src/main/res/drawable/ic_notif.xml +++ b/app/src/main/res/drawable/ic_notif.xml @@ -5,7 +5,7 @@ android:viewportWidth="24.0"> diff --git a/app/src/main/res/drawable/ic_sync_black_24dp.xml b/app/src/main/res/drawable/ic_sync.xml similarity index 89% rename from app/src/main/res/drawable/ic_sync_black_24dp.xml rename to app/src/main/res/drawable/ic_sync.xml index 2aef437..51b5bdd 100644 --- a/app/src/main/res/drawable/ic_sync_black_24dp.xml +++ b/app/src/main/res/drawable/ic_sync.xml @@ -4,6 +4,6 @@ android:viewportHeight="24.0" android:viewportWidth="24.0"> \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_user.xml b/app/src/main/res/drawable/ic_user.xml index be59808..74d15c1 100644 --- a/app/src/main/res/drawable/ic_user.xml +++ b/app/src/main/res/drawable/ic_user.xml @@ -1,9 +1,9 @@ + android:width="48dp" + android:height="48dp" + android:viewportWidth="1792.0" + android:viewportHeight="1792.0"> + android:fillColor="@color/default_drawable_color" + android:pathData="M1536 1399q0 109-62.5 187t-150.5 78h-854q-88 0-150.5-78t-62.5-187q0-85 8.5-160.5t31.5-152 58.5-131 94-89 134.5-34.5q131 128 313 128t313-128q76 0 134.5 34.5t94 89 58.5 131 31.5 152 8.5 160.5zm-256-887q0 159-112.5 271.5t-271.5 112.5-271.5-112.5-112.5-271.5 112.5-271.5 271.5-112.5 271.5 112.5 112.5 271.5z"/> diff --git a/app/src/main/res/drawable/navbar_item_ripple_effect.xml b/app/src/main/res/drawable/navbar_item_ripple_effect.xml new file mode 100644 index 0000000..32fb8ec --- /dev/null +++ b/app/src/main/res/drawable/navbar_item_ripple_effect.xml @@ -0,0 +1,11 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 7154c3a..cff8c9a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,34 +1,18 @@ - + android:orientation="vertical"> - - + android:layout_height="wrap_content" /> - + + - - - - - \ No newline at end of file + \ No newline at end of file diff --git a/app/src/main/res/layout/navigation_bar.xml b/app/src/main/res/layout/navigation_bar.xml new file mode 100644 index 0000000..4d2973c --- /dev/null +++ b/app/src/main/res/layout/navigation_bar.xml @@ -0,0 +1,10 @@ + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/navigation_bar_item.xml b/app/src/main/res/layout/navigation_bar_item.xml new file mode 100644 index 0000000..f94d303 --- /dev/null +++ b/app/src/main/res/layout/navigation_bar_item.xml @@ -0,0 +1,25 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/menu/activity_main_drawer.xml b/app/src/main/res/menu/navigation_bar.xml similarity index 94% rename from app/src/main/res/menu/activity_main_drawer.xml rename to app/src/main/res/menu/navigation_bar.xml index 200af94..1fc3163 100644 --- a/app/src/main/res/menu/activity_main_drawer.xml +++ b/app/src/main/res/menu/navigation_bar.xml @@ -20,7 +20,7 @@ + android:icon="@drawable/ic_sync" /> #5b5b5b #303f9f + #000000 + + + #FFFFFF + @color/colorPrimary + #3F51B5 #FFFFFF diff --git a/app/src/main/res/values/dimens.xml b/app/src/main/res/values/dimens.xml index 0c40d08..34c00b9 100644 --- a/app/src/main/res/values/dimens.xml +++ b/app/src/main/res/values/dimens.xml @@ -29,4 +29,7 @@ 20dp 20dp + + + 30dp