Skip to content

Commit fc827d7

Browse files
stefan-niedermannAndyScherzinger
authored andcommitted
chore: Use enhanced switch and pattern variables
Signed-off-by: Stefan Niedermann <info@niedermann.it>
1 parent 064d86d commit fc827d7

23 files changed

+132
-215
lines changed

app/src/main/java/it/niedermann/owncloud/notes/about/AboutActivity.java

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,10 @@ protected void onCreate(Bundle savedInstanceState) {
3434
binding.pager.setAdapter(new TabsStateAdapter(this));
3535
// generate title based on given position
3636
new TabLayoutMediator(binding.tabs, binding.pager, (tab, position) -> {
37-
switch (position) {
38-
default: // Fall-through to credits tab
39-
case POS_CREDITS:
40-
tab.setText(R.string.about_credits_tab_title);
41-
break;
42-
case POS_CONTRIB:
43-
tab.setText(R.string.about_contribution_tab_title);
44-
break;
45-
case POS_LICENSE:
46-
tab.setText(R.string.about_license_tab_title);
47-
break;
37+
switch (position) { // Fall-through to credits tab
38+
default -> tab.setText(R.string.about_credits_tab_title);
39+
case POS_CONTRIB -> tab.setText(R.string.about_contribution_tab_title);
40+
case POS_LICENSE -> tab.setText(R.string.about_license_tab_title);
4841
}
4942
}).attach();
5043
}
@@ -74,17 +67,11 @@ public int getItemCount() {
7467
@NonNull
7568
@Override
7669
public Fragment createFragment(int position) {
77-
switch (position) {
78-
default: // Fall-through to credits tab
79-
case POS_CREDITS:
80-
return new AboutFragmentCreditsTab();
81-
82-
case POS_CONTRIB:
83-
return new AboutFragmentContributingTab();
84-
85-
case POS_LICENSE:
86-
return new AboutFragmentLicenseTab();
87-
}
70+
return switch (position) { // Fall-through to credits tab
71+
default -> new AboutFragmentCreditsTab();
72+
case POS_CONTRIB -> new AboutFragmentContributingTab();
73+
case POS_LICENSE -> new AboutFragmentLicenseTab();
74+
};
8875
}
8976
}
9077

app/src/main/java/it/niedermann/owncloud/notes/branding/BrandedSwitchPreference.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ private Switch findSwitchWidget(View view) {
7474
if (view instanceof Switch) {
7575
return (Switch) view;
7676
}
77-
if (view instanceof ViewGroup) {
78-
final var viewGroup = (ViewGroup) view;
77+
if (view instanceof ViewGroup viewGroup) {
7978
for (int i = 0; i < viewGroup.getChildCount(); i++) {
8079
final var child = viewGroup.getChildAt(i);
8180
if (child instanceof ViewGroup) {

app/src/main/java/it/niedermann/owncloud/notes/edit/EditNoteActivity.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -397,17 +397,10 @@ public void onNoteUpdated(Note note) {
397397
@Override
398398
public void changeMode(@NonNull Mode mode, boolean reloadNote) {
399399
switch (mode) {
400-
case EDIT:
401-
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_edit), reloadNote);
402-
break;
403-
case PREVIEW:
404-
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_preview), reloadNote);
405-
break;
406-
case DIRECT_EDIT:
407-
launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_direct_edit), reloadNote);
408-
break;
409-
default:
410-
throw new IllegalStateException("Unknown mode: " + mode);
400+
case EDIT -> launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_edit), reloadNote);
401+
case PREVIEW -> launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_preview), reloadNote);
402+
case DIRECT_EDIT -> launchExistingNote(getAccountId(), getNoteId(), getString(R.string.pref_value_mode_direct_edit), reloadNote);
403+
default -> throw new IllegalStateException("Unknown mode: " + mode);
411404
}
412405
}
413406

app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryAdapter.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import java.util.ArrayList;
1818
import java.util.List;
19+
import java.util.Objects;
1920

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

5253
switch (category.id) {
53-
case addItemId:
54-
final var wrapDrawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, category.icon));
54+
case addItemId -> {
55+
final var wrapDrawable = DrawableCompat.wrap(Objects.requireNonNull(ContextCompat.getDrawable(context, category.icon)));
5556
DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(context, R.color.icon_color_default));
5657
categoryViewHolder.getIcon().setImageDrawable(wrapDrawable);
5758
categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryAdded());
58-
break;
59-
case clearItemId:
59+
}
60+
case clearItemId -> {
6061
categoryViewHolder.getIcon().setImageDrawable(ContextCompat.getDrawable(context, category.icon));
6162
categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryCleared());
62-
break;
63-
default:
63+
}
64+
default -> {
6465
categoryViewHolder.getIcon().setImageDrawable(ContextCompat.getDrawable(context, category.icon));
6566
categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryChosen(category.label));
66-
break;
67+
}
6768
}
6869
categoryViewHolder.getCategory().setText(NoteUtil.extendCategory(category.label));
6970
if (category.count != null && category.count > 0) {

app/src/main/java/it/niedermann/owncloud/notes/exception/tips/TipsAdapter.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,19 +88,13 @@ public void setThrowables(@NonNull List<Throwable> throwables) {
8888
} else if (throwable instanceof NextcloudHttpRequestFailedException) {
8989
final int statusCode = ((NextcloudHttpRequestFailedException) throwable).getStatusCode();
9090
switch (statusCode) {
91-
case 302:
91+
case 302 -> {
9292
add(R.string.error_dialog_server_app_enabled);
9393
add(R.string.error_dialog_redirect);
94-
break;
95-
case 500:
96-
add(R.string.error_dialog_check_server_logs);
97-
break;
98-
case 503:
99-
add(R.string.error_dialog_check_maintenance);
100-
break;
101-
case 507:
102-
add(R.string.error_dialog_insufficient_storage);
103-
break;
94+
}
95+
case 500 -> add(R.string.error_dialog_check_server_logs);
96+
case 503 -> add(R.string.error_dialog_check_maintenance);
97+
case 507 -> add(R.string.error_dialog_insufficient_storage);
10498
}
10599
} else if (throwable instanceof UnknownErrorException) {
106100
if ("com.nextcloud.android.sso.QueryParam".equals(throwable.getMessage())) {

app/src/main/java/it/niedermann/owncloud/notes/main/MainActivity.java

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -231,26 +231,21 @@ protected void onCreate(Bundle savedInstanceState) {
231231
fabCreate.show();
232232

233233
switch (selectedCategory.getType()) {
234-
case RECENT: {
234+
case RECENT -> {
235235
activityBinding.searchText.setText(getString(R.string.search_in_all));
236-
break;
237236
}
238-
case FAVORITES: {
237+
case FAVORITES -> {
239238
activityBinding.searchText.setText(getString(R.string.search_in_category, getString(R.string.label_favorites)));
240-
break;
241239
}
242-
case UNCATEGORIZED: {
240+
case UNCATEGORIZED -> {
243241
activityBinding.searchText.setText(getString(R.string.search_in_category, getString(R.string.action_uncategorized)));
244-
break;
245242
}
246-
case DEFAULT_CATEGORY:
247-
default: {
243+
default -> {
248244
final String category = selectedCategory.getCategory();
249245
if (category == null) {
250246
throw new IllegalStateException(NavigationCategory.class.getSimpleName() + " type is " + DEFAULT_CATEGORY + ", but category is null.");
251247
}
252248
activityBinding.searchText.setText(getString(R.string.search_in_category, NoteUtil.extendCategory(category)));
253-
break;
254249
}
255250
}
256251

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

506501
tracker = ItemSelectionTracker.build(listView, adapter);
507502
adapter.setTracker(tracker);
508-
tracker.addObserver(new SelectionTracker.SelectionObserver<Long>() {
503+
tracker.addObserver(new SelectionTracker.SelectionObserver<>() {
509504
@Override
510505
public void onSelectionChanged() {
511506
super.onSelectionChanged();
@@ -541,19 +536,16 @@ private void selectItem(NavigationItem item, boolean closeNavigation) {
541536
// update current selection
542537
if (item.type != null) {
543538
switch (item.type) {
544-
case RECENT: {
539+
case RECENT -> {
545540
mainViewModel.postSelectedCategory(new NavigationCategory(RECENT));
546-
break;
547541
}
548-
case FAVORITES: {
542+
case FAVORITES -> {
549543
mainViewModel.postSelectedCategory(new NavigationCategory(FAVORITES));
550-
break;
551544
}
552-
case UNCATEGORIZED: {
545+
case UNCATEGORIZED -> {
553546
mainViewModel.postSelectedCategory(new NavigationCategory(UNCATEGORIZED));
554-
break;
555547
}
556-
default: {
548+
default -> {
557549
if (item.getClass() == NavigationItem.CategoryNavigationItem.class) {
558550
mainViewModel.postSelectedCategory(new NavigationCategory(((NavigationItem.CategoryNavigationItem) item).accountId, ((NavigationItem.CategoryNavigationItem) item).category));
559551
} else {
@@ -670,20 +662,18 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
670662
super.onActivityResult(requestCode, resultCode, data);
671663

672664
switch (requestCode) {
673-
case REQUEST_CODE_CREATE_NOTE: {
665+
case REQUEST_CODE_CREATE_NOTE -> {
674666
listView.scrollToPosition(0);
675-
break;
676667
}
677-
case REQUEST_CODE_SERVER_SETTINGS: {
668+
case REQUEST_CODE_SERVER_SETTINGS -> {
678669
// Recreate activity completely, because theme switching makes problems when only invalidating the views.
679670
// @see https://github.com/nextcloud/notes-android/issues/529
680671
if (RESULT_OK == resultCode) {
681672
ActivityCompat.recreate(this);
682673
return;
683674
}
684-
break;
685675
}
686-
default: {
676+
default -> {
687677
try {
688678
AccountImporter.onActivityResult(requestCode, resultCode, data, this, (ssoAccount) -> {
689679
CapabilitiesWorker.update(this);
@@ -695,7 +685,7 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
695685
Log.i(TAG, "Refreshing capabilities for " + ssoAccount.name);
696686
final var capabilities = CapabilitiesClient.getCapabilities(getApplicationContext(), ssoAccount, null, ApiProvider.getInstance());
697687
final String displayName = CapabilitiesClient.getDisplayName(getApplicationContext(), ssoAccount, ApiProvider.getInstance());
698-
final var status$ = mainViewModel.addAccount(ssoAccount.url, ssoAccount.userId, ssoAccount.name, capabilities, displayName, new IResponseCallback<Account>() {
688+
final var status$ = mainViewModel.addAccount(ssoAccount.url, ssoAccount.userId, ssoAccount.name, capabilities, displayName, new IResponseCallback<>() {
699689
@Override
700690
public void onSuccess(Account result) {
701691
executor.submit(() -> {

app/src/main/java/it/niedermann/owncloud/notes/main/MainViewModel.java

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -155,14 +155,10 @@ public void postSelectedCategory(@NonNull NavigationCategory selectedCategory) {
155155

156156
// Close sub categories
157157
switch (selectedCategory.getType()) {
158-
case RECENT:
159-
case FAVORITES:
160-
case UNCATEGORIZED: {
158+
case RECENT, FAVORITES, UNCATEGORIZED -> {
161159
postExpandedCategory(null);
162-
break;
163160
}
164-
case DEFAULT_CATEGORY:
165-
default: {
161+
default -> {
166162
final String category = selectedCategory.getCategory();
167163
if (category == null) {
168164
postExpandedCategory(null);
@@ -175,7 +171,6 @@ public void postSelectedCategory(@NonNull NavigationCategory selectedCategory) {
175171
postExpandedCategory(null);
176172
}
177173
}
178-
break;
179174
}
180175
}
181176
}
@@ -230,29 +225,25 @@ public LiveData<List<Item>> getNotesListLiveData() {
230225
Log.v(TAG, "[getNotesListLiveData] - sortMethod: " + sortingMethod.second);
231226
final LiveData<List<Note>> fromDatabase;
232227
switch (selectedCategory.getType()) {
233-
case RECENT: {
228+
case RECENT -> {
234229
Log.v(TAG, "[getNotesListLiveData] - category: " + RECENT);
235230
fromDatabase = sortingMethod.second == SORT_MODIFIED_DESC
236231
? repo.searchRecentByModified$(accountId, searchQueryOrWildcard)
237232
: repo.searchRecentLexicographically$(accountId, searchQueryOrWildcard);
238-
break;
239233
}
240-
case FAVORITES: {
234+
case FAVORITES -> {
241235
Log.v(TAG, "[getNotesListLiveData] - category: " + FAVORITES);
242236
fromDatabase = sortingMethod.second == SORT_MODIFIED_DESC
243237
? repo.searchFavoritesByModified$(accountId, searchQueryOrWildcard)
244238
: repo.searchFavoritesLexicographically$(accountId, searchQueryOrWildcard);
245-
break;
246239
}
247-
case UNCATEGORIZED: {
240+
case UNCATEGORIZED -> {
248241
Log.v(TAG, "[getNotesListLiveData] - category: " + UNCATEGORIZED);
249242
fromDatabase = sortingMethod.second == SORT_MODIFIED_DESC
250243
? repo.searchUncategorizedByModified$(accountId, searchQueryOrWildcard)
251244
: repo.searchUncategorizedLexicographically$(accountId, searchQueryOrWildcard);
252-
break;
253245
}
254-
case DEFAULT_CATEGORY:
255-
default: {
246+
default -> {
256247
final String category = selectedCategory.getCategory();
257248
if (category == null) {
258249
throw new IllegalStateException(NavigationCategory.class.getSimpleName() + " type is " + DEFAULT_CATEGORY + ", but category is null.");
@@ -261,7 +252,6 @@ public LiveData<List<Item>> getNotesListLiveData() {
261252
fromDatabase = sortingMethod.second == SORT_MODIFIED_DESC
262253
? repo.searchCategoryByModified$(accountId, searchQueryOrWildcard, category)
263254
: repo.searchCategoryLexicographically$(accountId, searchQueryOrWildcard, category);
264-
break;
265255
}
266256
}
267257

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

383373
public void synchronizeCapabilitiesAndNotes(@NonNull Account localAccount, @NonNull IResponseCallback<Void> callback) {
384374
Log.i(TAG, "[synchronizeCapabilitiesAndNotes] Synchronize capabilities for " + localAccount.getAccountName());
385-
synchronizeCapabilities(localAccount, new IResponseCallback<Void>() {
375+
synchronizeCapabilities(localAccount, new IResponseCallback<>() {
386376
@Override
387377
public void onSuccess(Void v) {
388378
Log.i(TAG, "[synchronizeCapabilitiesAndNotes] Synchronize notes for " + localAccount.getAccountName());

0 commit comments

Comments
 (0)