Browse Source

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

renovate/org.robolectric-robolectric-4.x
Ammar Githam 4 years ago
parent
commit
ebea1404c9
  1. 72
      app/src/main/java/awais/instagrabber/activities/MainActivity.java

72
app/src/main/java/awais/instagrabber/activities/MainActivity.java

@ -171,12 +171,14 @@ 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();
final NavDestination currentDestination = navController.getCurrentDestination();
if (currentDestination != null) {
final int destinationId = currentDestination.getId();
showSearch = destinationId == R.id.profileFragment;
if (navController != null) {
final NavDestination currentDestination = navController.getCurrentDestination();
if (currentDestination != null) {
final int destinationId = currentDestination.getId();
showSearch = destinationId == R.id.profileFragment;
}
} }
} }
if (!showSearch) { if (!showSearch) {
@ -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) {
return currentNavControllerLiveData.getValue().navigateUp();
}
return false;
if (currentNavControllerLiveData == null) return false;
final NavController navController = currentNavControllerLiveData.getValue();
if (navController == null) return false;
return navController.navigateUp();
} }
@Override @Override
@ -260,23 +262,23 @@ 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) {
final NavController navController = currentNavControllerLiveData.getValue();
final Bundle bundle = new Bundle();
switch (type) {
case TYPE_LOCATION:
bundle.putString("locationId", query);
navController.navigate(R.id.action_global_locationFragment, bundle);
break;
case TYPE_HASHTAG:
bundle.putString("hashtag", query);
navController.navigate(R.id.action_global_hashTagFragment, bundle);
break;
case TYPE_USER:
bundle.putString("username", query);
navController.navigate(R.id.action_global_profileFragment, bundle);
break;
}
if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue();
if (navController == null) return;
final Bundle bundle = new Bundle();
switch (type) {
case TYPE_LOCATION:
bundle.putString("locationId", query);
navController.navigate(R.id.action_global_locationFragment, bundle);
break;
case TYPE_HASHTAG:
bundle.putString("hashtag", query);
navController.navigate(R.id.action_global_hashTagFragment, bundle);
break;
case TYPE_USER:
bundle.putString("username", query);
navController.navigate(R.id.action_global_profileFragment, bundle);
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) {
return;
}
if (navController == null) 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());
} }
} }
Loading…
Cancel
Save