From 7e451d73a9410dbbb0fd1d8c630780e62d16fb2c Mon Sep 17 00:00:00 2001 From: Pierre Date: Mon, 25 Dec 2017 18:17:30 +0100 Subject: [PATCH] Bottom navigation item selected auto-update --- .../android/comunic/client/MainActivity.java | 19 ++++++-- .../fragments/ConversationFragment.java | 5 ++ .../fragments/ConversationsListFragment.java | 6 +++ .../client/fragments/FriendsListFragment.java | 47 ++++++++++--------- .../client/fragments/UserInfosFragment.java | 9 +++- 5 files changed, 60 insertions(+), 26 deletions(-) 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 6c03033..990ffe9 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 @@ -52,6 +52,11 @@ public class MainActivity extends AppCompatActivity */ private DatabaseHelper dbHelper; + /** + * Bottom navigation view + */ + private BottomNavigationView navigationView; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -141,9 +146,8 @@ public class MainActivity extends AppCompatActivity * Bottom menu creation */ void init_bottom_menu(){ - BottomNavigationView navigation = - (BottomNavigationView) findViewById(R.id.main_bottom_navigation); - navigation.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { + navigationView = (BottomNavigationView) findViewById(R.id.main_bottom_navigation); + navigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { @@ -173,6 +177,15 @@ public class MainActivity extends AppCompatActivity }); } + /** + * Set the currently selected item in the bottom navigation view + * + * @param item The ID of the item + */ + public void setSelectedNavigationItem(int item){ + navigationView.getMenu().findItem(item).setChecked(true); + } + /** * Ask user to confirm if he really what to sign out or not */ 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 index c53220f..06da9f3 100644 --- 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 @@ -21,6 +21,7 @@ import android.widget.ListView; import android.widget.ProgressBar; import android.widget.Toast; +import org.communiquons.android.comunic.client.MainActivity; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.DatabaseHelper; @@ -236,6 +237,10 @@ public class ConversationFragment extends Fragment //Create and start the thread new Thread(refreshRunnable).start(); + + //Update the bottom navigation menu + ((MainActivity) getActivity()) + .setSelectedNavigationItem(R.id.main_bottom_navigation_conversations); } @Override 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 174ab03..ea30fc3 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 @@ -15,6 +15,7 @@ import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; +import org.communiquons.android.comunic.client.MainActivity; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersHelper; @@ -126,7 +127,12 @@ public class ConversationsListFragment extends Fragment implements AdapterView.O public void onResume() { super.onResume(); + //Update activity title getActivity().setTitle(R.string.fragment_conversationslist_title); + + //Update the bottom navigation menu + ((MainActivity) getActivity()) + .setSelectedNavigationItem(R.id.main_bottom_navigation_conversations); } /** diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java index a6ad498..039c549 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/FriendsListFragment.java @@ -19,6 +19,7 @@ import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; +import org.communiquons.android.comunic.client.MainActivity; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.DatabaseHelper; import org.communiquons.android.comunic.client.data.UsersInfo.GetUsersInfos; @@ -109,6 +110,10 @@ public class FriendsListFragment extends Fragment { //Update the title of the application getActivity().setTitle(R.string.fragment_friendslist_title); + + //Update the bottom navigation menu + ((MainActivity) getActivity()) + .setSelectedNavigationItem(R.id.main_bottom_navigation_friends_list); } @Override @@ -141,29 +146,29 @@ public class FriendsListFragment extends Fragment { Toast.LENGTH_LONG).show(); return; } - else { - new GetUsersInfos(mContext, mDbHelper). - getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() { - @Override - public void callback(ArrayMap info) { - //Check for errors - if (info == null) { - Toast.makeText(mContext, R.string.fragment_friendslist_err_get_userinfos, - Toast.LENGTH_SHORT).show(); - return; - } - //Merge the user informations list and friends List into FriendInfo list - ArrayList friendsUserList = FriendsUtils.merge_friends_user_infos_list( - friendsList, - info - ); - - //Refresh friends list - apply_friends_list(friendsUserList); + new GetUsersInfos(mContext, mDbHelper). + getMultiple(FriendsUtils.getFriendsIDs(friendsList), new GetUsersInfos.getMultipleUserInfosCallback() { + @Override + public void callback(ArrayMap info) { + //Check for errors + if (info == null) { + Toast.makeText(mContext, R.string.fragment_friendslist_err_get_userinfos, + Toast.LENGTH_SHORT).show(); + return; } - }); - } + + //Merge the user informations list and friends List into FriendInfo list + ArrayList friendsUserList = FriendsUtils.merge_friends_user_infos_list( + friendsList, + info + ); + + //Refresh friends list + apply_friends_list(friendsUserList); + } + }); + } }.execute(); diff --git a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserInfosFragment.java b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserInfosFragment.java index 850b7aa..383ff40 100644 --- a/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserInfosFragment.java +++ b/app/src/main/java/org/communiquons/android/comunic/client/fragments/UserInfosFragment.java @@ -12,6 +12,7 @@ import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; +import org.communiquons.android.comunic.client.MainActivity; import org.communiquons.android.comunic.client.R; import org.communiquons.android.comunic.client.data.Account.AccountUtils; import org.communiquons.android.comunic.client.data.DatabaseHelper; @@ -59,8 +60,8 @@ public class UserInfosFragment extends Fragment { dbHelper = new DatabaseHelper(mContext); //Get required views - final ImageView imageView = (ImageView) view.findViewById(R.id.fragments_userinfos_account_image); - final TextView userNameView = (TextView) view.findViewById(R.id.fragments_userinfos_user_name); + final ImageView imageView = view.findViewById(R.id.fragments_userinfos_account_image); + final TextView userNameView = view.findViewById(R.id.fragments_userinfos_user_name); //Retrieve user informations in order to display them int user_id = new AccountUtils(mContext).get_current_user_id(); @@ -90,5 +91,9 @@ public class UserInfosFragment extends Fragment { //Update the title getActivity().setTitle(R.string.fragment_userinfos_title); + + //Update the bottom navigation menu + ((MainActivity) getActivity()) + .setSelectedNavigationItem(R.id.main_bottom_navigation_me_view); } }