Bottom navigation item selected auto-update

This commit is contained in:
Pierre 2017-12-25 18:17:30 +01:00
parent f3a49693a2
commit 7e451d73a9
5 changed files with 60 additions and 26 deletions

View File

@ -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
*/

View File

@ -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

View File

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

View File

@ -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<Integer, UserInfo> 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<FriendUser> 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<Integer, UserInfo> 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<FriendUser> friendsUserList = FriendsUtils.merge_friends_user_infos_list(
friendsList,
info
);
//Refresh friends list
apply_friends_list(friendsUserList);
}
});
}
}.execute();

View File

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