1
0
mirror of https://github.com/KokaKiwi/BarInsta synced 2024-11-22 14:47: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,12 +171,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
public boolean onCreateOptionsMenu(final Menu menu) {
getMenuInflater().inflate(R.menu.main_menu, menu);
searchMenuItem = menu.findItem(R.id.search);
if (showSearch && currentNavControllerLiveData != null && currentNavControllerLiveData.getValue() != null) {
if (showSearch && currentNavControllerLiveData != null) {
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) {
@ -206,10 +208,10 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
@Override
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
@ -260,23 +262,23 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
suggestionAdapter = new SuggestionsAdapter(this, (type, query) -> {
if (searchMenuItem != null) searchMenuItem.collapseActionView();
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) {
if (navController == null) {
return;
}
if (navController == null) return;
NavigationUI.setupWithNavController(toolbar, navController);
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
@ -586,8 +586,9 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private void showProfileView(@NonNull final IntentModel intentModel) {
final String username = intentModel.getText();
// Log.d(TAG, "username: " + username);
if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue();
if (currentNavControllerLiveData == null || navController == null) return;
if (navController == null) return;
final Bundle bundle = new Bundle();
bundle.putString("username", "@" + username);
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) {
final String locationId = intentModel.getText();
// Log.d(TAG, "locationId: " + locationId);
if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue();
if (currentNavControllerLiveData == null || navController == null) return;
if (navController == null) return;
final Bundle bundle = new Bundle();
bundle.putString("locationId", locationId);
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) {
final String hashtag = intentModel.getText();
// Log.d(TAG, "hashtag: " + hashtag);
if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue();
if (currentNavControllerLiveData == null || navController == null) return;
if (navController == null) return;
final Bundle bundle = new Bundle();
bundle.putString("hashtag", "#" + hashtag);
navController.navigate(R.id.action_global_hashTagFragment, bundle);
@ -633,8 +636,9 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
private void showActivityView() {
binding.bottomNavView.setSelectedItemId(R.id.more_nav_graph);
binding.bottomNavView.post(() -> {
if (currentNavControllerLiveData == null) return;
final NavController navController = currentNavControllerLiveData.getValue();
if (currentNavControllerLiveData == null || navController == null) return;
if (navController == null) return;
final NavDirections navDirections = MorePreferencesFragmentDirections.actionMorePreferencesFragmentToNotificationsViewer();
navController.navigate(navDirections);
});
@ -674,12 +678,14 @@ public class MainActivity extends BaseLanguageActivity implements FragmentManage
public void setToolbar(final Toolbar toolbar) {
binding.appBarLayout.setVisibility(View.GONE);
setSupportActionBar(toolbar);
if (currentNavControllerLiveData == null) return;
setupNavigation(toolbar, currentNavControllerLiveData.getValue());
}
public void resetToolbar() {
binding.appBarLayout.setVisibility(View.VISIBLE);
setSupportActionBar(binding.toolbar);
if (currentNavControllerLiveData == null) return;
setupNavigation(binding.toolbar, currentNavControllerLiveData.getValue());
}
}