From f1ab6833d3840efe047b95d1e3c6e72dd3f67815 Mon Sep 17 00:00:00 2001 From: FineFindus Date: Fri, 26 May 2023 20:22:53 +0200 Subject: [PATCH] feat: add content type setting Re-add settings from https://github.com/sk22/megalodon/commit/8b502b605c4a3616d6700ad8507205d96c6de946\#diff-29b258979e7004d7e5473b71e91747f57a36af0e0b07e0a119968a1e8e72669f --- .../fragments/settings/AccountFragment.java | 71 +++++++++++++++++-- .../settings/SettingsBaseFragment.java | 7 +- .../fragments/settings/TimeLineFragment.java | 10 +-- 3 files changed, 76 insertions(+), 12 deletions(-) diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/AccountFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/AccountFragment.java index a627fc136..98fe71255 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/AccountFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/AccountFragment.java @@ -3,11 +3,14 @@ package org.joinmastodon.android.fragments.settings; import android.content.Intent; import android.os.Bundle; import android.text.TextUtils; -import android.view.ViewGroup; +import android.view.Gravity; +import android.view.Menu; +import android.view.MenuItem; import android.widget.Button; -import android.widget.EditText; -import android.widget.FrameLayout; -import android.widget.Toast; +import android.widget.PopupMenu; + +import androidx.annotation.Nullable; +import androidx.annotation.StringRes; import org.joinmastodon.android.GlobalUserPreferences; import org.joinmastodon.android.MainActivity; @@ -16,6 +19,7 @@ import org.joinmastodon.android.api.requests.oauth.RevokeOauthToken; import org.joinmastodon.android.api.session.AccountSession; import org.joinmastodon.android.api.session.AccountSessionManager; import org.joinmastodon.android.fragments.onboarding.InstanceRulesFragment; +import org.joinmastodon.android.model.ContentType; import org.joinmastodon.android.ui.M3AlertDialogBuilder; import org.joinmastodon.android.ui.utils.UiUtils; import org.parceler.Parcels; @@ -25,12 +29,13 @@ import java.util.ArrayList; import me.grishka.appkit.Nav; import me.grishka.appkit.api.Callback; import me.grishka.appkit.api.ErrorResponse; -import me.grishka.appkit.utils.V; public class AccountFragment extends SettingsBaseFragment{ private SwitchItem glitchModeItem; + private ButtonItem defaultContentTypeButtonItem; + private Menu contentTypeMenu; @Override public void addItems(ArrayList items) { items.add(new HeaderItem(R.string.settings_account)); @@ -52,6 +57,34 @@ public class AccountFragment extends SettingsBaseFragment{ if (!TextUtils.isEmpty(getInstance().version)) items.add(new SmallTextItem(getString(R.string.sk_settings_server_version, getInstance().version))); items.add(new HeaderItem(R.string.sk_instance_features)); + items.add(new SwitchItem(R.string.sk_settings_content_types, R.string.sk_settings_content_types_explanation, 0, GlobalUserPreferences.accountsWithContentTypesEnabled.contains(accountID), (i)->{ + if (i.checked) { + GlobalUserPreferences.accountsWithContentTypesEnabled.add(accountID); + if (GlobalUserPreferences.accountsDefaultContentTypes.get(accountID) == null) { + GlobalUserPreferences.accountsDefaultContentTypes.put(accountID, ContentType.PLAIN); + } + } else { + GlobalUserPreferences.accountsWithContentTypesEnabled.remove(accountID); + GlobalUserPreferences.accountsDefaultContentTypes.remove(accountID); + } + if (list.findViewHolderForAdapterPosition(items.indexOf(defaultContentTypeButtonItem)) + instanceof ButtonViewHolder bvh) bvh.rebind(); + GlobalUserPreferences.save(); + })); + items.add(defaultContentTypeButtonItem = new ButtonItem(R.string.sk_settings_default_content_type, R.string.sk_settings_default_content_type_explanation, 0, b->{ + PopupMenu popupMenu=new PopupMenu(getActivity(), b, Gravity.CENTER_HORIZONTAL); + popupMenu.inflate(R.menu.compose_content_type); + popupMenu.setOnMenuItemClickListener(item -> this.onContentTypeChanged(item, b)); + b.setOnTouchListener(popupMenu.getDragToOpenListener()); + b.setOnClickListener(v->popupMenu.show()); + ContentType contentType = GlobalUserPreferences.accountsDefaultContentTypes.get(accountID); + b.setText(getContentTypeString(contentType)); + contentTypeMenu = popupMenu.getMenu(); + contentTypeMenu.findItem(ContentType.getContentTypeRes(contentType)).setChecked(true); + ContentType.adaptMenuToInstance(contentTypeMenu, getInstance()); + contentTypeMenu.findItem(R.id.content_type_null).setVisible( + !GlobalUserPreferences.accountsWithContentTypesEnabled.contains(accountID)); + })); items.add(new SwitchItem(R.string.sk_settings_support_local_only, R.string.sk_settings_local_only_explanation, 0, GlobalUserPreferences.accountsWithLocalOnlySupport.contains(accountID), i->{ glitchModeItem.enabled = i.checked; if (i.checked) { @@ -117,4 +150,32 @@ public class AccountFragment extends SettingsBaseFragment{ Intent intent=new Intent(getActivity(), MainActivity.class); startActivity(intent); } + + private @StringRes int getContentTypeString(@Nullable ContentType contentType) { + if (contentType == null) return R.string.sk_content_type_unspecified; + return switch (contentType) { + case PLAIN -> R.string.sk_content_type_plain; + case HTML -> R.string.sk_content_type_html; + case MARKDOWN -> R.string.sk_content_type_markdown; + case BBCODE -> R.string.sk_content_type_bbcode; + case MISSKEY_MARKDOWN -> R.string.sk_content_type_mfm; + }; + } + + private boolean onContentTypeChanged(MenuItem item, Button btn){ + int id = item.getItemId(); + ContentType contentType = switch (id) { + case R.id.content_type_plain -> ContentType.PLAIN; + case R.id.content_type_html -> ContentType.HTML; + case R.id.content_type_markdown -> ContentType.MARKDOWN; + case R.id.content_type_bbcode -> ContentType.BBCODE; + case R.id.content_type_misskey_markdown -> ContentType.MISSKEY_MARKDOWN; + default -> null; + }; + GlobalUserPreferences.accountsDefaultContentTypes.put(accountID, contentType); + GlobalUserPreferences.save(); + btn.setText(getContentTypeString(contentType)); + item.setChecked(true); + return true; + } } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java index 837c05d7b..a07cec61d 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/SettingsBaseFragment.java @@ -788,8 +788,11 @@ public abstract class SettingsBaseFragment extends MastodonToolbarFragment imple summary.setText(item.summary); summary.setVisibility(View.VISIBLE); } - - icon.setImageResource(item.icon); + if (item.icon == 0) { + icon.setVisibility(View.GONE); + } else { + icon.setImageResource(item.icon); + } item.buttonConsumer.accept(button); } diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/TimeLineFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/TimeLineFragment.java index 7a9d28be9..ec901026f 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/TimeLineFragment.java +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/settings/TimeLineFragment.java @@ -12,7 +12,7 @@ import java.util.ArrayList; public class TimeLineFragment extends SettingsBaseFragment{ - private SwitchItem showNewPostsButtonItem, compactReblogReplyLineItem; + private SwitchItem showNewPostsItem, compactReblogReplyLineItem; @Override public void addItems(ArrayList items) { items.add(new SwitchItem(R.string.sk_settings_show_replies, R.drawable.ic_fluent_chat_multiple_24_regular, GlobalUserPreferences.showReplies, i->{ @@ -41,15 +41,15 @@ public class TimeLineFragment extends SettingsBaseFragment{ })); items.add(new SwitchItem(R.string.sk_settings_load_new_posts, R.drawable.ic_fluent_arrow_sync_24_regular, GlobalUserPreferences.loadNewPosts, i->{ GlobalUserPreferences.loadNewPosts=i.checked; - showNewPostsButtonItem.enabled = i.checked; + showNewPostsItem.enabled = i.checked; if (!i.checked) { GlobalUserPreferences.showNewPostsButton = false; - showNewPostsButtonItem.checked = false; + showNewPostsItem.checked = false; } - if (list.findViewHolderForAdapterPosition(items.indexOf(showNewPostsButtonItem)) instanceof SwitchViewHolder svh) svh.rebind(); + if (list.findViewHolderForAdapterPosition(items.indexOf(showNewPostsItem)) instanceof SwitchViewHolder svh) svh.rebind(); GlobalUserPreferences.save(); })); - items.add(showNewPostsButtonItem = new SwitchItem(R.string.sk_settings_show_new_posts_button, R.drawable.ic_fluent_arrow_up_24_regular, GlobalUserPreferences.showNewPostsButton, i->{ + items.add(showNewPostsItem = new SwitchItem(R.string.sk_settings_show_new_posts_button, R.drawable.ic_fluent_arrow_up_24_regular, GlobalUserPreferences.showNewPostsButton, i->{ GlobalUserPreferences.showNewPostsButton=i.checked; GlobalUserPreferences.save(); }));