Skip to content

Commit

Permalink
chore: Use enhanced switch and pattern variables
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Niedermann <[email protected]>
  • Loading branch information
stefan-niedermann committed Jan 20, 2024
1 parent 0b15727 commit 2514960
Show file tree
Hide file tree
Showing 23 changed files with 132 additions and 215 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,10 @@ protected void onCreate(Bundle savedInstanceState) {
binding.pager.setAdapter(new TabsStateAdapter(this));
// generate title based on given position
new TabLayoutMediator(binding.tabs, binding.pager, (tab, position) -> {
switch (position) {
default: // Fall-through to credits tab
case POS_CREDITS:
tab.setText(R.string.about_credits_tab_title);
break;
case POS_CONTRIB:
tab.setText(R.string.about_contribution_tab_title);
break;
case POS_LICENSE:
tab.setText(R.string.about_license_tab_title);
break;
switch (position) { // Fall-through to credits tab
default -> tab.setText(R.string.about_credits_tab_title);
case POS_CONTRIB -> tab.setText(R.string.about_contribution_tab_title);
case POS_LICENSE -> tab.setText(R.string.about_license_tab_title);
}
}).attach();
}
Expand Down Expand Up @@ -74,17 +67,11 @@ public int getItemCount() {
@NonNull
@Override
public Fragment createFragment(int position) {
switch (position) {
default: // Fall-through to credits tab
case POS_CREDITS:
return new AboutFragmentCreditsTab();

case POS_CONTRIB:
return new AboutFragmentContributingTab();

case POS_LICENSE:
return new AboutFragmentLicenseTab();
}
return switch (position) { // Fall-through to credits tab
default -> new AboutFragmentCreditsTab();
case POS_CONTRIB -> new AboutFragmentContributingTab();
case POS_LICENSE -> new AboutFragmentLicenseTab();
};
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,7 @@ private Switch findSwitchWidget(View view) {
if (view instanceof Switch) {
return (Switch) view;
}
if (view instanceof ViewGroup) {
final var viewGroup = (ViewGroup) view;
if (view instanceof ViewGroup viewGroup) {
for (int i = 0; i < viewGroup.getChildCount(); i++) {
final var child = viewGroup.getChildAt(i);
if (child instanceof ViewGroup) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -397,17 +397,10 @@ public void onNoteUpdated(Note note) {
@Override
public void changeMode(@NonNull Mode mode, boolean reloadNote) {
switch (mode) {
case EDIT:
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_edit), reloadNote);
break;
case PREVIEW:
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_preview), reloadNote);
break;
case DIRECT_EDIT:
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_direct_edit), reloadNote);
break;
default:
throw new IllegalStateException("Unknown mode: " + mode);
case EDIT -> launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_edit), reloadNote);
case PREVIEW -> launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_preview), reloadNote);
case DIRECT_EDIT -> launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_direct_edit), reloadNote);
default -> throw new IllegalStateException("Unknown mode: " + mode);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.databinding.ItemCategoryBinding;
Expand Down Expand Up @@ -50,20 +51,20 @@ public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int positi
final var categoryViewHolder = (CategoryViewHolder) holder;

switch (category.id) {
case addItemId:
final var wrapDrawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, category.icon));
case addItemId -> {
final var wrapDrawable = DrawableCompat.wrap(Objects.requireNonNull(ContextCompat.getDrawable(context, category.icon)));
DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(context, R.color.icon_color_default));
categoryViewHolder.getIcon().setImageDrawable(wrapDrawable);
categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryAdded());
break;
case clearItemId:
}
case clearItemId -> {
categoryViewHolder.getIcon().setImageDrawable(ContextCompat.getDrawable(context, category.icon));
categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryCleared());
break;
default:
}
default -> {
categoryViewHolder.getIcon().setImageDrawable(ContextCompat.getDrawable(context, category.icon));
categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryChosen(category.label));
break;
}
}
categoryViewHolder.getCategory().setText(NoteUtil.extendCategory(category.label));
if (category.count != null && category.count > 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,19 +88,13 @@ public void setThrowables(@NonNull List<Throwable> throwables) {
} else if (throwable instanceof NextcloudHttpRequestFailedException) {
final int statusCode = ((NextcloudHttpRequestFailedException) throwable).getStatusCode();
switch (statusCode) {
case 302:
case 302 -> {
add(R.string.error_dialog_server_app_enabled);
add(R.string.error_dialog_redirect);
break;
case 500:
add(R.string.error_dialog_check_server_logs);
break;
case 503:
add(R.string.error_dialog_check_maintenance);
break;
case 507:
add(R.string.error_dialog_insufficient_storage);
break;
}
case 500 -> add(R.string.error_dialog_check_server_logs);
case 503 -> add(R.string.error_dialog_check_maintenance);
case 507 -> add(R.string.error_dialog_insufficient_storage);
}
} else if (throwable instanceof UnknownErrorException) {
if ("com.nextcloud.android.sso.QueryParam".equals(throwable.getMessage())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -231,26 +231,21 @@ protected void onCreate(Bundle savedInstanceState) {
fabCreate.show();

switch (selectedCategory.getType()) {
case RECENT: {
case RECENT -> {
activityBinding.searchText.setText(getString(R.string.search_in_all));
break;
}
case FAVORITES: {
case FAVORITES -> {
activityBinding.searchText.setText(getString(R.string.search_in_category, getString(R.string.label_favorites)));
break;
}
case UNCATEGORIZED: {
case UNCATEGORIZED -> {
activityBinding.searchText.setText(getString(R.string.search_in_category, getString(R.string.action_uncategorized)));
break;
}
case DEFAULT_CATEGORY:
default: {
default -> {
final String category = selectedCategory.getCategory();
if (category == null) {
throw new IllegalStateException(NavigationCategory.class.getSimpleName() + " type is " + DEFAULT_CATEGORY + ", but category is null.");
}
activityBinding.searchText.setText(getString(R.string.search_in_category, NoteUtil.extendCategory(category)));
break;
}
}

Expand Down Expand Up @@ -368,7 +363,7 @@ protected void onResume() {
try {
// It is possible that after the deletion of the last account, this onResponse gets called before the ImportAccountActivity gets started.
if (SingleAccountHelper.getCurrentSingleSignOnAccount(this) != null) {
mainViewModel.synchronizeNotes(currentAccount, new IResponseCallback<Void>() {
mainViewModel.synchronizeNotes(currentAccount, new IResponseCallback<>() {
@Override
public void onSuccess(Void v) {
Log.d(TAG, "Successfully synchronized notes for " + currentAccount.getAccountName());
Expand Down Expand Up @@ -505,7 +500,7 @@ public void onError(@NonNull Throwable t) {

tracker = ItemSelectionTracker.build(listView, adapter);
adapter.setTracker(tracker);
tracker.addObserver(new SelectionTracker.SelectionObserver<Long>() {
tracker.addObserver(new SelectionTracker.SelectionObserver<>() {
@Override
public void onSelectionChanged() {
super.onSelectionChanged();
Expand Down Expand Up @@ -541,19 +536,16 @@ private void selectItem(NavigationItem item, boolean closeNavigation) {
// update current selection
if (item.type != null) {
switch (item.type) {
case RECENT: {
case RECENT -> {
mainViewModel.postSelectedCategory(new NavigationCategory(RECENT));
break;
}
case FAVORITES: {
case FAVORITES -> {
mainViewModel.postSelectedCategory(new NavigationCategory(FAVORITES));
break;
}
case UNCATEGORIZED: {
case UNCATEGORIZED -> {
mainViewModel.postSelectedCategory(new NavigationCategory(UNCATEGORIZED));
break;
}
default: {
default -> {
if (item.getClass() == NavigationItem.CategoryNavigationItem.class) {
mainViewModel.postSelectedCategory(new NavigationCategory(((NavigationItem.CategoryNavigationItem) item).accountId, ((NavigationItem.CategoryNavigationItem) item).category));
} else {
Expand Down Expand Up @@ -670,20 +662,18 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

switch (requestCode) {
case REQUEST_CODE_CREATE_NOTE: {
case REQUEST_CODE_CREATE_NOTE -> {
listView.scrollToPosition(0);
break;
}
case REQUEST_CODE_SERVER_SETTINGS: {
case REQUEST_CODE_SERVER_SETTINGS -> {
// Recreate activity completely, because theme switching makes problems when only invalidating the views.
// @see https://github.com/nextcloud/notes-android/issues/529
if (RESULT_OK == resultCode) {
ActivityCompat.recreate(this);
return;
}
break;
}
default: {
default -> {
try {
AccountImporter.onActivityResult(requestCode, resultCode, data, this, (ssoAccount) -> {
CapabilitiesWorker.update(this);
Expand All @@ -695,7 +685,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.i(TAG, "Refreshing capabilities for " + ssoAccount.name);
final var capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount, null, ApiProvider.getInstance());
final String displayName = CapabilitiesClient.getDisplayName(getApplicationContext(), ssoAccount, ApiProvider.getInstance());
final var status$ = mainViewModel.addAccount(ssoAccount.url, ssoAccount.userId, ssoAccount.name, capabilities, displayName, new IResponseCallback<Account>() {
final var status$ = mainViewModel.addAccount(ssoAccount.url, ssoAccount.userId, ssoAccount.name, capabilities, displayName, new IResponseCallback<>() {
@Override
public void onSuccess(Account result) {
executor.submit(() -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,14 +155,10 @@ public void postSelectedCategory(@NonNull NavigationCategory selectedCategory) {

// Close sub categories
switch (selectedCategory.getType()) {
case RECENT:
case FAVORITES:
case UNCATEGORIZED: {
case RECENT, FAVORITES, UNCATEGORIZED -> {
postExpandedCategory(null);
break;
}
case DEFAULT_CATEGORY:
default: {
default -> {
final String category = selectedCategory.getCategory();
if (category == null) {
postExpandedCategory(null);
Expand All @@ -175,7 +171,6 @@ public void postSelectedCategory(@NonNull NavigationCategory selectedCategory) {
postExpandedCategory(null);
}
}
break;
}
}
}
Expand Down Expand Up @@ -230,29 +225,25 @@ public LiveData<List<Item>> getNotesListLiveData() {
Log.v(TAG, "[getNotesListLiveData] - sortMethod: " + sortingMethod.second);
final LiveData<List<Note>> fromDatabase;
switch (selectedCategory.getType()) {
case RECENT: {
case RECENT -> {
Log.v(TAG, "[getNotesListLiveData] - category: " + RECENT);
fromDatabase = sortingMethod.second == SORT_MODIFIED_DESC
? repo.searchRecentByModified$(accountId, searchQueryOrWildcard)
: repo.searchRecentLexicographically$(accountId, searchQueryOrWildcard);
break;
}
case FAVORITES: {
case FAVORITES -> {
Log.v(TAG, "[getNotesListLiveData] - category: " + FAVORITES);
fromDatabase = sortingMethod.second == SORT_MODIFIED_DESC
? repo.searchFavoritesByModified$(accountId, searchQueryOrWildcard)
: repo.searchFavoritesLexicographically$(accountId, searchQueryOrWildcard);
break;
}
case UNCATEGORIZED: {
case UNCATEGORIZED -> {
Log.v(TAG, "[getNotesListLiveData] - category: " + UNCATEGORIZED);
fromDatabase = sortingMethod.second == SORT_MODIFIED_DESC
? repo.searchUncategorizedByModified$(accountId, searchQueryOrWildcard)
: repo.searchUncategorizedLexicographically$(accountId, searchQueryOrWildcard);
break;
}
case DEFAULT_CATEGORY:
default: {
default -> {
final String category = selectedCategory.getCategory();
if (category == null) {
throw new IllegalStateException(NavigationCategory.class.getSimpleName() + " type is " + DEFAULT_CATEGORY + ", but category is null.");
Expand All @@ -261,7 +252,6 @@ public LiveData<List<Item>> getNotesListLiveData() {
fromDatabase = sortingMethod.second == SORT_MODIFIED_DESC
? repo.searchCategoryByModified$(accountId, searchQueryOrWildcard, category)
: repo.searchCategoryLexicographically$(accountId, searchQueryOrWildcard, category);
break;
}
}

Expand Down Expand Up @@ -382,7 +372,7 @@ private static List<NavigationItem> fromCategoriesWithNotesCount(@NonNull Contex

public void synchronizeCapabilitiesAndNotes(@NonNull Account localAccount, @NonNull IResponseCallback<Void> callback) {
Log.i(TAG, "[synchronizeCapabilitiesAndNotes] Synchronize capabilities for " + localAccount.getAccountName());
synchronizeCapabilities(localAccount, new IResponseCallback<Void>() {
synchronizeCapabilities(localAccount, new IResponseCallback<>() {
@Override
public void onSuccess(Void v) {
Log.i(TAG, "[synchronizeCapabilitiesAndNotes] Synchronize notes for " + localAccount.getAccountName());
Expand Down
Loading

0 comments on commit 2514960

Please sign in to comment.