refactor: Use Java enhanced switch

This commit is contained in:
Nik Clayton 2023-09-20 11:52:56 +02:00
parent 1a817d9b29
commit e762008a2a
4 changed files with 105 additions and 147 deletions

View File

@ -163,26 +163,14 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab
}
private static @StyleRes int textStyle(@NonNull String name) {
int style;
switch (name) {
case "smallest":
style = R.style.TextSizeSmallest;
break;
case "small":
style = R.style.TextSizeSmall;
break;
case "medium":
default:
style = R.style.TextSizeMedium;
break;
case "large":
style = R.style.TextSizeLarge;
break;
case "largest":
style = R.style.TextSizeLargest;
break;
}
return style;
return switch (name) {
case "smallest" -> R.style.TextSizeSmallest;
case "small" -> R.style.TextSizeSmall;
case "medium" -> R.style.TextSizeMedium;
case "large" -> R.style.TextSizeLarge;
case "largest" -> R.style.TextSizeLargest;
default -> R.style.TextSizeMedium;
};
}
public void startActivityWithSlideInAnimation(@NonNull Intent intent) {
@ -232,10 +220,11 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab
AccountEntity activeAccount = accountManager.getActiveAccount();
switch (accounts.size()) {
case 1:
case 1 -> {
listener.onAccountSelected(activeAccount);
return;
case 2:
}
case 2 -> {
if (!showActiveAccount) {
for (AccountEntity account : accounts) {
if (activeAccount != account) {
@ -244,7 +233,7 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab
}
}
}
break;
}
}
if (!showActiveAccount && activeAccount != null) {
@ -262,20 +251,22 @@ public abstract class BaseActivity extends AppCompatActivity implements Injectab
public @Nullable String getOpenAsText() {
List<AccountEntity> accounts = accountManager.getAllAccountsOrderedByActive();
switch (accounts.size()) {
case 0:
case 1:
case 0, 1 -> {
return null;
case 2:
}
case 2 -> {
for (AccountEntity account : accounts) {
if (account != accountManager.getActiveAccount()) {
return String.format(getString(R.string.action_open_as), account.getFullName());
}
}
return null;
default:
}
default -> {
return String.format(getString(R.string.action_open_as), "");
}
}
}
public void openAsAccount(@NonNull String url, @NonNull AccountEntity account) {
accountManager.setActiveAccount(account.getId());

View File

@ -568,17 +568,12 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
@DrawableRes
private static int getLabelIcon(@NonNull Attachment.Type type) {
switch (type) {
case IMAGE:
return R.drawable.ic_photo_24dp;
case GIFV:
case VIDEO:
return R.drawable.ic_videocam_24dp;
case AUDIO:
return R.drawable.ic_music_box_24dp;
default:
return R.drawable.ic_attach_file_24dp;
}
return switch (type) {
case IMAGE -> R.drawable.ic_photo_24dp;
case GIFV, VIDEO -> R.drawable.ic_videocam_24dp;
case AUDIO -> R.drawable.ic_music_box_24dp;
default -> R.drawable.ic_attach_file_24dp;
};
}
private void updateMediaLabel(int index, boolean sensitive, boolean showingContent) {
@ -935,21 +930,14 @@ public abstract class StatusBaseViewHolder extends RecyclerView.ViewHolder {
int resource;
switch (visibility) {
case PUBLIC:
resource = R.string.description_visibility_public;
break;
case UNLISTED:
resource = R.string.description_visibility_unlisted;
break;
case PRIVATE:
resource = R.string.description_visibility_private;
break;
case DIRECT:
resource = R.string.description_visibility_direct;
break;
default:
case PUBLIC -> resource = R.string.description_visibility_public;
case UNLISTED -> resource = R.string.description_visibility_unlisted;
case PRIVATE -> resource = R.string.description_visibility_private;
case DIRECT -> resource = R.string.description_visibility_direct;
default -> {
return "";
}
}
return context.getString(resource);
}

View File

@ -180,21 +180,14 @@ public class StatusDetailedViewHolder extends StatusBaseViewHolder {
int visibilityIcon;
switch (visibility) {
case PUBLIC:
visibilityIcon = R.drawable.ic_public_24dp;
break;
case UNLISTED:
visibilityIcon = R.drawable.ic_lock_open_24dp;
break;
case PRIVATE:
visibilityIcon = R.drawable.ic_lock_outline_24dp;
break;
case DIRECT:
visibilityIcon = R.drawable.ic_email_24dp;
break;
default:
case PUBLIC -> visibilityIcon = R.drawable.ic_public_24dp;
case UNLISTED -> visibilityIcon = R.drawable.ic_lock_open_24dp;
case PRIVATE -> visibilityIcon = R.drawable.ic_lock_outline_24dp;
case DIRECT -> visibilityIcon = R.drawable.ic_email_24dp;
default -> {
return null;
}
}
final Drawable visibilityDrawable = AppCompatResources.getDrawable(
this.metaInfo.getContext(), visibilityIcon

View File

@ -670,30 +670,19 @@ public class NotificationHelper {
return channel != null && channel.getImportance() > NotificationManager.IMPORTANCE_NONE;
}
switch (type) {
case MENTION:
return account.getNotificationsMentioned();
case STATUS:
return account.getNotificationsSubscriptions();
case FOLLOW:
return account.getNotificationsFollowed();
case FOLLOW_REQUEST:
return account.getNotificationsFollowRequested();
case REBLOG:
return account.getNotificationsReblogged();
case FAVOURITE:
return account.getNotificationsFavorited();
case POLL:
return account.getNotificationsPolls();
case SIGN_UP:
return account.getNotificationsSignUps();
case UPDATE:
return account.getNotificationsUpdates();
case REPORT:
return account.getNotificationsReports();
default:
return false;
}
return switch (type) {
case MENTION -> account.getNotificationsMentioned();
case STATUS -> account.getNotificationsSubscriptions();
case FOLLOW -> account.getNotificationsFollowed();
case FOLLOW_REQUEST -> account.getNotificationsFollowRequested();
case REBLOG -> account.getNotificationsReblogged();
case FAVOURITE -> account.getNotificationsFavorited();
case POLL -> account.getNotificationsPolls();
case SIGN_UP -> account.getNotificationsSignUps();
case UPDATE -> account.getNotificationsUpdates();
case REPORT -> account.getNotificationsReports();
default -> false;
};
}
@Nullable
@ -703,31 +692,19 @@ public class NotificationHelper {
@Nullable
private static String getChannelId(@NonNull AccountEntity account, @NonNull Notification.Type type) {
switch (type) {
case MENTION:
return CHANNEL_MENTION + account.getIdentifier();
case STATUS:
return CHANNEL_SUBSCRIPTIONS + account.getIdentifier();
case FOLLOW:
return CHANNEL_FOLLOW + account.getIdentifier();
case FOLLOW_REQUEST:
return CHANNEL_FOLLOW_REQUEST + account.getIdentifier();
case REBLOG:
return CHANNEL_BOOST + account.getIdentifier();
case FAVOURITE:
return CHANNEL_FAVOURITE + account.getIdentifier();
case POLL:
return CHANNEL_POLL + account.getIdentifier();
case SIGN_UP:
return CHANNEL_SIGN_UP + account.getIdentifier();
case UPDATE:
return CHANNEL_UPDATES + account.getIdentifier();
case REPORT:
return CHANNEL_REPORT + account.getIdentifier();
default:
return null;
}
return switch (type) {
case MENTION -> CHANNEL_MENTION + account.getIdentifier();
case STATUS -> CHANNEL_SUBSCRIPTIONS + account.getIdentifier();
case FOLLOW -> CHANNEL_FOLLOW + account.getIdentifier();
case FOLLOW_REQUEST -> CHANNEL_FOLLOW_REQUEST + account.getIdentifier();
case REBLOG -> CHANNEL_BOOST + account.getIdentifier();
case FAVOURITE -> CHANNEL_FAVOURITE + account.getIdentifier();
case POLL -> CHANNEL_POLL + account.getIdentifier();
case SIGN_UP -> CHANNEL_SIGN_UP + account.getIdentifier();
case UPDATE -> CHANNEL_UPDATES + account.getIdentifier();
case REPORT -> CHANNEL_REPORT + account.getIdentifier();
default -> null;
};
}
private static void setSoundVibrationLight(@NonNull AccountEntity account, @NonNull NotificationCompat.Builder builder) {
@ -781,57 +758,64 @@ public class NotificationHelper {
private static String titleForType(@NonNull Context context, @NonNull Notification notification, @NonNull AccountEntity account) {
String accountName = StringUtils.unicodeWrap(notification.getAccount().getName());
switch (notification.getType()) {
case MENTION:
case MENTION -> {
return String.format(context.getString(R.string.notification_mention_format),
accountName);
case STATUS:
}
case STATUS -> {
return String.format(context.getString(R.string.notification_subscription_format),
accountName);
case FOLLOW:
}
case FOLLOW -> {
return String.format(context.getString(R.string.notification_follow_format),
accountName);
case FOLLOW_REQUEST:
}
case FOLLOW_REQUEST -> {
return String.format(context.getString(R.string.notification_follow_request_format),
accountName);
case FAVOURITE:
}
case FAVOURITE -> {
return String.format(context.getString(R.string.notification_favourite_format),
accountName);
case REBLOG:
}
case REBLOG -> {
return String.format(context.getString(R.string.notification_reblog_format),
accountName);
case POLL:
}
case POLL -> {
if (notification.getStatus().getAccount().getId().equals(account.getAccountId())) {
return context.getString(R.string.poll_ended_created);
} else {
return context.getString(R.string.poll_ended_voted);
}
case SIGN_UP:
}
case SIGN_UP -> {
return String.format(context.getString(R.string.notification_sign_up_format), accountName);
case UPDATE:
}
case UPDATE -> {
return String.format(context.getString(R.string.notification_update_format), accountName);
case REPORT:
}
case REPORT -> {
return context.getString(R.string.notification_report_format, account.getDomain());
}
}
return null;
}
@Nullable
private static String bodyForType(@NonNull Notification notification, @NonNull Context context, Boolean alwaysOpenSpoiler) {
switch (notification.getType()) {
case FOLLOW:
case FOLLOW_REQUEST:
case SIGN_UP:
case FOLLOW, FOLLOW_REQUEST, SIGN_UP -> {
return "@" + notification.getAccount().getUsername();
case MENTION:
case FAVOURITE:
case REBLOG:
case STATUS:
}
case MENTION, FAVOURITE, REBLOG, STATUS -> {
if (!TextUtils.isEmpty(notification.getStatus().getSpoilerText()) && !alwaysOpenSpoiler) {
return notification.getStatus().getSpoilerText();
} else {
return parseAsMastodonHtml(notification.getStatus().getContent()).toString();
}
case POLL:
}
case POLL -> {
if (!TextUtils.isEmpty(notification.getStatus().getSpoilerText()) && !alwaysOpenSpoiler) {
return notification.getStatus().getSpoilerText();
} else {
@ -849,13 +833,15 @@ public class NotificationHelper {
}
return builder.toString();
}
case REPORT:
}
case REPORT -> {
return context.getString(
R.string.notification_header_report_format,
StringUtils.unicodeWrap(notification.getAccount().getName()),
StringUtils.unicodeWrap(notification.getReport().getTargetAccount().getName())
);
}
}
return null;
}