1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 22:57:29 +00:00

Add nav controller livedata null checks. Fixes https://github.com/austinhuang0131/barinsta/issues/196

This commit is contained in:
Ammar Githam 2020-11-11 19:27:14 +09:00
parent 2c1f4aea9d
commit ebea1404c9

View File

@ -171,14 +171,16 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
public boolean onCreateOptionsMenu(final Menu menu) { public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu); getMenuInflater().inflate(R.menu.main_menu, menu);
searchMenuItem = menu.findItem(R.id.search); searchMenuItem = menu.findItem(R.id.search);
if (showSearch && currentNavControllerLiveData != null && currentNavControllerLiveData.getValue() != null) { if (showSearch && currentNavControllerLiveData != null) {
final NavController navController = currentNavControllerLiveData.getValue(); final NavController navController = currentNavControllerLiveData.getValue();
if (navController != null) {
final NavDestination currentDestination = navController.getCurrentDestination(); final NavDestination currentDestination = navController.getCurrentDestination();
if (currentDestination != null) { if (currentDestination != null) {
final int destinationId = currentDestination.getId(); final int destinationId = currentDestination.getId();
showSearch = destinationId == R.id.profileFragment; showSearch = destinationId == R.id.profileFragment;
} }
} }
}
if (!showSearch) { if (!showSearch) {
searchMenuItem.setVisible(false); searchMenuItem.setVisible(false);
return true; return true;
@ -206,10 +208,10 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
@Override @Override
public boolean onSupportNavigateUp() { public boolean onSupportNavigateUp() {
if (currentNavControllerLiveData != null && currentNavControllerLiveData.getValue() != null) { if (currentNavControllerLiveData == null) return false;
return currentNavControllerLiveData.getValue().navigateUp(); final NavController navController = currentNavControllerLiveData.getValue();
} if (navController == null) return false;
return false; return navController.navigateUp();
} }
@Override @Override
@ -260,8 +262,9 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
suggestionAdapter = new SuggestionsAdapter(this, (type, query) -> { suggestionAdapter = new SuggestionsAdapter(this, (type, query) -> {
if (searchMenuItem != null) searchMenuItem.collapseActionView(); if (searchMenuItem != null) searchMenuItem.collapseActionView();
if (searchView != null && !searchView.isIconified()) searchView.setIconified(true); if (searchView != null && !searchView.isIconified()) searchView.setIconified(true);
if (currentNavControllerLiveData != null && currentNavControllerLiveData.getValue() != null) { if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue(); final NavController navController = currentNavControllerLiveData.getValue();
if (navController == null) return;
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
switch (type) { switch (type) {
case TYPE_LOCATION: case TYPE_LOCATION:
@ -277,7 +280,6 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
navController.navigate(R.id.action_global_profileFragment, bundle); navController.navigate(R.id.action_global_profileFragment, bundle);
break; break;
} }
}
}); });
} }
@ -469,9 +471,7 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
} }
private void setupNavigation(final Toolbar toolbar, final NavController navController) { private void setupNavigation(final Toolbar toolbar, final NavController navController) {
if (navController == null) { if (navController == null) return;
return;
}
NavigationUI.setupWithNavController(toolbar, navController); NavigationUI.setupWithNavController(toolbar, navController);
navController.addOnDestinationChangedListener((controller, destination, arguments) -> { navController.addOnDestinationChangedListener((controller, destination, arguments) -> {
// below is a hack to check if we are at the end of the current stack, to setup the search view // below is a hack to check if we are at the end of the current stack, to setup the search view
@ -586,8 +586,9 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private void showProfileView(@NonNull final IntentModel intentModel) { private void showProfileView(@NonNull final IntentModel intentModel) {
final String username = intentModel.getText(); final String username = intentModel.getText();
// Log.d(TAG, "username: " + username); // Log.d(TAG, "username: " + username);
if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue(); final NavController navController = currentNavControllerLiveData.getValue();
if (currentNavControllerLiveData == null || navController == null) return; if (navController == null) return;
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
bundle.putString("username", "@" + username); bundle.putString("username", "@" + username);
navController.navigate(R.id.action_global_profileFragment, bundle); navController.navigate(R.id.action_global_profileFragment, bundle);
@ -613,8 +614,9 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private void showLocationView(@NonNull final IntentModel intentModel) { private void showLocationView(@NonNull final IntentModel intentModel) {
final String locationId = intentModel.getText(); final String locationId = intentModel.getText();
// Log.d(TAG, "locationId: " + locationId); // Log.d(TAG, "locationId: " + locationId);
if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue(); final NavController navController = currentNavControllerLiveData.getValue();
if (currentNavControllerLiveData == null || navController == null) return; if (navController == null) return;
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
bundle.putString("locationId", locationId); bundle.putString("locationId", locationId);
navController.navigate(R.id.action_global_locationFragment, bundle); navController.navigate(R.id.action_global_locationFragment, bundle);
@ -623,8 +625,9 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private void showHashtagView(@NonNull final IntentModel intentModel) { private void showHashtagView(@NonNull final IntentModel intentModel) {
final String hashtag = intentModel.getText(); final String hashtag = intentModel.getText();
// Log.d(TAG, "hashtag: " + hashtag); // Log.d(TAG, "hashtag: " + hashtag);
if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue(); final NavController navController = currentNavControllerLiveData.getValue();
if (currentNavControllerLiveData == null || navController == null) return; if (navController == null) return;
final Bundle bundle = new Bundle(); final Bundle bundle = new Bundle();
bundle.putString("hashtag", "#" + hashtag); bundle.putString("hashtag", "#" + hashtag);
navController.navigate(R.id.action_global_hashTagFragment, bundle); navController.navigate(R.id.action_global_hashTagFragment, bundle);
@ -633,8 +636,9 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private void showActivityView() { private void showActivityView() {
binding.bottomNavView.setSelectedItemId(R.id.more_nav_graph); binding.bottomNavView.setSelectedItemId(R.id.more_nav_graph);
binding.bottomNavView.post(() -> { binding.bottomNavView.post(() -> {
if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue(); final NavController navController = currentNavControllerLiveData.getValue();
if (currentNavControllerLiveData == null || navController == null) return; if (navController == null) return;
final NavDirections navDirections = MorePreferencesFragmentDirections.actionMorePreferencesFragmentToNotificationsViewer(); final NavDirections navDirections = MorePreferencesFragmentDirections.actionMorePreferencesFragmentToNotificationsViewer();
navController.navigate(navDirections); navController.navigate(navDirections);
}); });
@ -674,12 +678,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
public void setToolbar(final Toolbar toolbar) { public void setToolbar(final Toolbar toolbar) {
binding.appBarLayout.setVisibility(View.GONE); binding.appBarLayout.setVisibility(View.GONE);
setSupportActionBar(toolbar); setSupportActionBar(toolbar);
if (currentNavControllerLiveData == null) return;
setupNavigation(toolbar, currentNavControllerLiveData.getValue()); setupNavigation(toolbar, currentNavControllerLiveData.getValue());
} }
public void resetToolbar() { public void resetToolbar() {
binding.appBarLayout.setVisibility(View.VISIBLE); binding.appBarLayout.setVisibility(View.VISIBLE);
setSupportActionBar(binding.toolbar); setSupportActionBar(binding.toolbar);
if (currentNavControllerLiveData == null) return;
setupNavigation(binding.toolbar, currentNavControllerLiveData.getValue()); setupNavigation(binding.toolbar, currentNavControllerLiveData.getValue());
} }
} }