From: Damyan Ivanov Date: Tue, 2 Feb 2021 06:00:49 +0000 (+0200) Subject: guard against ncurrent navigation controller destination being null X-Git-Tag: v0.17.0~172 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=1688588d70ab1f75c0369080087af46958aacbbe;p=mobile-ledger.git guard against ncurrent navigation controller destination being null --- diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java deleted file mode 100644 index 945123d9..00000000 --- a/app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternsActivity.java +++ /dev/null @@ -1,123 +0,0 @@ -/* - * Copyright © 2021 Damyan Ivanov. - * This file is part of MoLe. - * MoLe is free software: you can distribute it and/or modify it - * under the term of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your opinion), any later version. - * - * MoLe is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License terms for details. - * - * You should have received a copy of the GNU General Public License - * along with MoLe. If not, see . - */ - -package net.ktnx.mobileledger.ui.patterns; - -import android.os.Bundle; -import android.view.Menu; -import android.view.MenuItem; - -import androidx.appcompat.app.ActionBar; -import androidx.lifecycle.ViewModelProvider; -import androidx.lifecycle.ViewModelStoreOwner; -import androidx.navigation.NavController; -import androidx.navigation.fragment.NavHostFragment; - -import net.ktnx.mobileledger.R; -import net.ktnx.mobileledger.databinding.ActivityPatternsBinding; -import net.ktnx.mobileledger.ui.activity.CrashReportingActivity; -import net.ktnx.mobileledger.utils.Logger; - -import java.util.Objects; - -public class PatternsActivity extends CrashReportingActivity - implements PatternListFragment.OnPatternListFragmentInteractionListener { - public static final String ARG_ADD_PATTERN = "add-pattern"; - private ActivityPatternsBinding b; - private NavController navController; - @Override - public boolean onCreateOptionsMenu(Menu menu) { - super.onCreateOptionsMenu(menu); - getMenuInflater().inflate(R.menu.pattern_list_menu, menu); - - return true; - } - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - b = ActivityPatternsBinding.inflate(getLayoutInflater()); - setContentView(b.getRoot()); - setSupportActionBar(b.toolbar); - // Show the Up button in the action bar. - ActionBar actionBar = getSupportActionBar(); - if (actionBar != null) { - actionBar.setDisplayHomeAsUpEnabled(true); - } - - NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull( - getSupportFragmentManager().findFragmentById(R.id.fragment_container)); - navController = navHostFragment.getNavController(); - - navController.addOnDestinationChangedListener((controller, destination, arguments) -> { - if (destination.getId() == R.id.patternListFragment) { - b.fabAdd.show(); - b.fabSave.hide(); - b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns)); - } - if (destination.getId() == R.id.patternDetailsFragment) { - b.fabAdd.hide(); - b.fabSave.show(); - } - }); - - b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns)); - - b.fabAdd.setOnClickListener(v -> onEditPattern(null)); - b.fabSave.setOnClickListener(v -> onSavePattern()); - } - @Override - public boolean onOptionsItemSelected(MenuItem item) { - if (item.getItemId() == android.R.id.home) { - if (navController.getCurrentDestination() - .getId() == R.id.patternDetailsFragment) - navController.popBackStack(); - else - finish(); - - return true; - } - return super.onOptionsItemSelected(item); - } - - @Override - public void onEditPattern(Long id) { - if (id == null) { - navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment); - b.toolbarLayout.setTitle(getString(R.string.title_new_pattern)); - } - else { - Bundle bundle = new Bundle(); - bundle.putLong(PatternDetailsFragment.ARG_PATTERN_ID, id); - navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment, - bundle); - b.toolbarLayout.setTitle(getString(R.string.title_edit_pattern)); - } - } - @Override - public void onSavePattern() { - final ViewModelStoreOwner viewModelStoreOwner = - navController.getViewModelStoreOwner(R.id.pattern_list_navigation); - PatternDetailsViewModel model = - new ViewModelProvider(viewModelStoreOwner).get(PatternDetailsViewModel.class); - Logger.debug("flow", "PatternsActivity.onSavePattern(): model=" + model); - model.onSavePattern(); - navController.navigateUp(); - } - public NavController getNavController() { - return navController; - } -} \ No newline at end of file diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java new file mode 100644 index 00000000..0f1d20f4 --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/ui/templates/TemplatesActivity.java @@ -0,0 +1,124 @@ +/* + * Copyright © 2021 Damyan Ivanov. + * This file is part of MoLe. + * MoLe is free software: you can distribute it and/or modify it + * under the term of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * MoLe is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with MoLe. If not, see . + */ + +package net.ktnx.mobileledger.ui.patterns; + +import android.os.Bundle; +import android.view.Menu; +import android.view.MenuItem; + +import androidx.appcompat.app.ActionBar; +import androidx.lifecycle.ViewModelProvider; +import androidx.lifecycle.ViewModelStoreOwner; +import androidx.navigation.NavController; +import androidx.navigation.fragment.NavHostFragment; + +import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.databinding.ActivityPatternsBinding; +import net.ktnx.mobileledger.ui.activity.CrashReportingActivity; +import net.ktnx.mobileledger.utils.Logger; + +import java.util.Objects; + +public class PatternsActivity extends CrashReportingActivity + implements PatternListFragment.OnPatternListFragmentInteractionListener { + public static final String ARG_ADD_PATTERN = "add-pattern"; + private ActivityPatternsBinding b; + private NavController navController; + @Override + public boolean onCreateOptionsMenu(Menu menu) { + super.onCreateOptionsMenu(menu); + getMenuInflater().inflate(R.menu.pattern_list_menu, menu); + + return true; + } + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + b = ActivityPatternsBinding.inflate(getLayoutInflater()); + setContentView(b.getRoot()); + setSupportActionBar(b.toolbar); + // Show the Up button in the action bar. + ActionBar actionBar = getSupportActionBar(); + if (actionBar != null) { + actionBar.setDisplayHomeAsUpEnabled(true); + } + + NavHostFragment navHostFragment = (NavHostFragment) Objects.requireNonNull( + getSupportFragmentManager().findFragmentById(R.id.fragment_container)); + navController = navHostFragment.getNavController(); + + navController.addOnDestinationChangedListener((controller, destination, arguments) -> { + if (destination.getId() == R.id.patternListFragment) { + b.fabAdd.show(); + b.fabSave.hide(); + b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns)); + } + if (destination.getId() == R.id.patternDetailsFragment) { + b.fabAdd.hide(); + b.fabSave.show(); + } + }); + + b.toolbarLayout.setTitle(getString(R.string.title_activity_patterns)); + + b.fabAdd.setOnClickListener(v -> onEditPattern(null)); + b.fabSave.setOnClickListener(v -> onSavePattern()); + } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + final NavDestination currentDestination = navController.getCurrentDestination(); + if (currentDestination != null && + currentDestination.getId() == R.id.patternDetailsFragment) + navController.popBackStack(); + else + finish(); + + return true; + } + return super.onOptionsItemSelected(item); + } + + @Override + public void onEditPattern(Long id) { + if (id == null) { + navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment); + b.toolbarLayout.setTitle(getString(R.string.title_new_pattern)); + } + else { + Bundle bundle = new Bundle(); + bundle.putLong(PatternDetailsFragment.ARG_PATTERN_ID, id); + navController.navigate(R.id.action_patternListFragment_to_patternDetailsFragment, + bundle); + b.toolbarLayout.setTitle(getString(R.string.title_edit_pattern)); + } + } + @Override + public void onSavePattern() { + final ViewModelStoreOwner viewModelStoreOwner = + navController.getViewModelStoreOwner(R.id.pattern_list_navigation); + PatternDetailsViewModel model = + new ViewModelProvider(viewModelStoreOwner).get(PatternDetailsViewModel.class); + Logger.debug("flow", "PatternsActivity.onSavePattern(): model=" + model); + model.onSavePattern(); + navController.navigateUp(); + } + public NavController getNavController() { + return navController; + } +} \ No newline at end of file