Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add option to mute hashtag #261

Merged
merged 4 commits into from
Sep 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,29 @@

import org.joinmastodon.android.E;
import org.joinmastodon.android.R;
import org.joinmastodon.android.api.requests.filters.CreateFilter;
import org.joinmastodon.android.api.requests.filters.DeleteFilter;
import org.joinmastodon.android.api.requests.filters.GetFilters;
import org.joinmastodon.android.api.requests.tags.GetHashtag;
import org.joinmastodon.android.api.requests.tags.SetHashtagFollowed;
import org.joinmastodon.android.api.requests.timelines.GetHashtagTimeline;
import org.joinmastodon.android.events.HashtagUpdatedEvent;
import org.joinmastodon.android.fragments.settings.EditFilterFragment;
import org.joinmastodon.android.model.Filter;
import org.joinmastodon.android.model.FilterAction;
import org.joinmastodon.android.model.FilterContext;
import org.joinmastodon.android.model.FilterKeyword;
import org.joinmastodon.android.model.Hashtag;
import org.joinmastodon.android.model.Status;
import org.joinmastodon.android.model.TimelineDefinition;
import org.joinmastodon.android.ui.utils.UiUtils;
import org.joinmastodon.android.utils.StatusFilterPredicate;
import org.parceler.Parcels;

import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import me.grishka.appkit.Nav;
Expand All @@ -41,6 +52,8 @@ public class HashtagTimelineFragment extends PinnableStatusListFragment {
private boolean following;
private boolean localOnly;
private MenuItem followButton;
private MenuItem muteButton;
private Optional<Filter> filter = Optional.empty();

@Override
protected boolean wantsComposeButton() {
Expand Down Expand Up @@ -72,13 +85,18 @@ private void updateFollowingState(boolean newFollowing) {
E.post(new HashtagUpdatedEvent(hashtag, following));
}

private void updateMuteState(boolean newMute) {
muteButton.setTitle(getString(newMute ? R.string.unmute_user : R.string.mute_user, "#" + hashtag));
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.hashtag_timeline, menu);
super.onCreateOptionsMenu(menu, inflater);
followButton = menu.findItem(R.id.follow_hashtag);
updateFollowingState(following);

muteButton = menu.findItem(R.id.mute_hashtag);
updateMuteState(filter.isPresent());
new GetHashtag(hashtag).setCallback(new Callback<>() {
@Override
public void onSuccess(Hashtag hashtag) {
Expand All @@ -92,6 +110,20 @@ public void onError(ErrorResponse error) {
error.showToast(getActivity());
}
}).exec(accountID);

new GetFilters().setCallback(new Callback<>() {
@Override
public void onSuccess(List<Filter> filters) {
if (getActivity() == null) return;
filter=filters.stream().filter(filter->filter.title.equals("#"+hashtag)).findAny();
updateMuteState(filter.isPresent());
}

@Override
public void onError(ErrorResponse error) {
error.showToast(getActivity());
}
}).exec(accountID);
}


Expand All @@ -116,10 +148,47 @@ public void onError(ErrorResponse error) {
}
}).exec(accountID);
return true;
} else if (item.getItemId() == R.id.mute_hashtag) {
showMuteDialog(filter.isPresent());
return true;
}
return false;
}

private void showMuteDialog(boolean mute) {
UiUtils.showConfirmationAlert(getContext(),
mute ? R.string.mo_unmute_hashtag : R.string.mo_mute_hashtag,
mute ? R.string.mo_confirm_to_unmute_hashtag : R.string.mo_confirm_to_mute_hashtag,
mute ? R.string.do_unmute : R.string.do_mute,
mute ? R.drawable.ic_fluent_speaker_2_28_regular : R.drawable.ic_fluent_speaker_off_28_regular,
mute ? this::unmuteHashtag : this::muteHashtag
);
}
private void unmuteHashtag() {
//safe to get, this only called if filter is present
new DeleteFilter(filter.get().id).exec(accountID);
}

private void muteHashtag() {
FilterKeyword hashtagFilter=new FilterKeyword();
hashtagFilter.wholeWord=true;
hashtagFilter.keyword="#"+hashtag;
new CreateFilter("#"+hashtag, EnumSet.of(FilterContext.HOME), FilterAction.HIDE, 0 , List.of(hashtagFilter)).setCallback(new Callback<Filter>(){
@Override
public void onSuccess(Filter result){
filter = Optional.of(result);
updateMuteState(true);
}

@Override
public void onError(ErrorResponse error){
error.showToast(getContext());
}
}).exec(accountID);
}



@Override
protected TimelineDefinition makeTimelineDefinition() {
return TimelineDefinition.ofHashtag(hashtag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ public class EditFilterFragment extends BaseSettingsFragment<Void> implements On
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
filter=Parcels.unwrap(getArguments().getParcelable("filter"));
ArrayList<Parcelable> words=getArguments().getParcelableArrayList("words");
if (words != null) {
words.stream().map(p->(FilterKeyword)Parcels.unwrap(p)).forEach(keywords::add);
}
setTitle(filter==null ? R.string.settings_add_filter : R.string.settings_edit_filter);
onDataLoaded(List.of(
durationItem=new ListItem<>(R.string.settings_filter_duration, 0, this::onDurationClick),
Expand Down
4 changes: 4 additions & 0 deletions mastodon/src/main/res/menu/hashtag_timeline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@
android:icon="@drawable/ic_fluent_person_add_24_regular"
android:showAsAction="always"
android:title="@string/button_follow"/>
<item
android:id="@+id/mute_hashtag"
android:icon="@drawable/ic_fluent_speaker_mute_24_regular"
android:title="@string/mo_mute_hashtag"/>
</menu>
7 changes: 6 additions & 1 deletion mastodon/src/main/res/values/strings_mo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@
<string name="mo_unmute_conversation">Unmute conversation</string>
<string name="mo_confirm_to_mute_conversation">Are you sure you want to mute this conversation?</string>
<string name="mo_confirm_to_unmute_conversation">Are you sure you want to unmute this conversation?</string>

<string name="mo_mute_hashtag">Mute hashtag</string>
<string name="mo_unmute_hashtag">Unmute hashtag</string>
<string name="mo_confirm_to_mute_hashtag">Are you sure you want to mute this hashtag?</string>
<string name="mo_confirm_to_unmute_hashtag">Are you sure you want to unmute this hashtag?</string>

<!-- accessibility labels-->
<string name="mo_poll_option_add">Add new poll option</string>
<string name="mo_fab_compose">Compose</string>
Expand Down Expand Up @@ -106,4 +110,5 @@
<string name="mo_confirm_unfollow">Confirm to unfollow %s</string>
<string name="mo_recent_emoji_cleared">Recent emoji cleared</string>
<string name="mo_show_media_preview">Show media preview in timelines</string>

</resources>