Fix issue #615 - add reverse option for tags + Change label descriptions

This commit is contained in:
Thomas 2022-12-12 11:57:41 +01:00
parent 83ffc82851
commit 1b643bcb1d
12 changed files with 262 additions and 94 deletions

View File

@ -26,6 +26,7 @@ import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AlertDialog;
import androidx.lifecycle.ViewModelProvider;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
@ -56,11 +57,14 @@ public class HashTagActivity extends BaseActivity {
public static int position;
private String tag;
private boolean pinnedTag;
private boolean followedTag;
private boolean mutedTag;
private Boolean pinnedTag;
private Boolean followedTag;
private Boolean mutedTag;
private TagVM tagVM;
private Filter fedilabFilter;
private Filter.KeywordsAttributes keyword;
private PinnedTimeline pinnedTimeline;
private Pinned pinned;
@Override
protected void onCreate(Bundle savedInstanceState) {
@ -75,9 +79,9 @@ public class HashTagActivity extends BaseActivity {
}
if (tag == null)
finish();
pinnedTag = false;
followedTag = false;
mutedTag = false;
pinnedTag = null;
followedTag = null;
mutedTag = null;
setSupportActionBar(binding.toolbar);
ActionBar actionBar = getSupportActionBar();
//Remove title
@ -100,19 +104,24 @@ public class HashTagActivity extends BaseActivity {
ReorderVM reorderVM = new ViewModelProvider(HashTagActivity.this).get(ReorderVM.class);
reorderVM.getAllPinned().observe(HashTagActivity.this, pinned -> {
if (pinned != null) {
this.pinned = pinned;
pinnedTag = false;
if (pinned.pinnedTimelines != null) {
for (PinnedTimeline pinnedTimeline : pinned.pinnedTimelines) {
if (pinnedTimeline.tagTimeline != null) {
if (pinnedTimeline.tagTimeline.name.equalsIgnoreCase(tag)) {
this.pinnedTimeline = pinnedTimeline;
pinnedTag = true;
invalidateOptionsMenu();
break;
}
}
}
invalidateOptionsMenu();
}
}
});
if (MainActivity.filterFetched && MainActivity.mainFilters != null) {
mutedTag = false;
for (Filter filter : MainActivity.mainFilters) {
if (filter.title.equalsIgnoreCase(Helper.FEDILAB_MUTED_HASHTAGS)) {
fedilabFilter = filter;
@ -120,17 +129,14 @@ public class HashTagActivity extends BaseActivity {
for (Filter.KeywordsAttributes keywordsAttributes : filter.keywords) {
if (fetch.equalsIgnoreCase(keywordsAttributes.keyword)) {
mutedTag = true;
keyword = keywordsAttributes;
invalidateOptionsMenu();
break;
}
}
mutedTag = false;
invalidateOptionsMenu();
break;
}
}
} else {
mutedTag = true;
invalidateOptionsMenu();
}
Bundle bundle = new Bundle();
@ -158,89 +164,125 @@ public class HashTagActivity extends BaseActivity {
finish();
return true;
} else if (item.getItemId() == R.id.action_add_timeline) {
new Thread(() -> {
try {
Pinned pinned = new Pinned(HashTagActivity.this).getPinned(currentAccount);
boolean canBeAdded = true;
boolean update = true;
if (pinned == null) {
pinned = new Pinned();
pinned.pinnedTimelines = new ArrayList<>();
update = false;
} else {
for (PinnedTimeline pinnedTimeline : pinned.pinnedTimelines) {
if (pinnedTimeline.type == Timeline.TimeLineEnum.TAG) {
if (pinnedTimeline.tagTimeline.name.compareTo(tag.trim()) == 0) {
canBeAdded = false;
}
}
}
}
if (!canBeAdded) {
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> Toasty.warning(HashTagActivity.this, getString(R.string.tags_already_stored), Toasty.LENGTH_SHORT).show();
mainHandler.post(myRunnable);
return;
}
PinnedTimeline pinnedTimeline = new PinnedTimeline();
pinnedTimeline.type = Timeline.TimeLineEnum.TAG;
pinnedTimeline.position = pinned.pinnedTimelines.size();
pinnedTimeline.displayed = true;
TagTimeline tagTimeline = new TagTimeline();
tagTimeline.name = tag.trim();
tagTimeline.isNSFW = false;
tagTimeline.isART = false;
pinnedTimeline.tagTimeline = tagTimeline;
pinned.pinnedTimelines.add(pinnedTimeline);
if (update) {
if (pinnedTag) {
AlertDialog.Builder unpinConfirm = new AlertDialog.Builder(HashTagActivity.this, Helper.dialogStyle());
unpinConfirm.setMessage(getString(R.string.unpin_timeline_description));
unpinConfirm.setNegativeButton(R.string.cancel, (dialog, which) -> dialog.dismiss());
unpinConfirm.setPositiveButton(R.string.yes, (dialog, which) -> {
pinned.pinnedTimelines.remove(pinnedTimeline);
try {
new Pinned(HashTagActivity.this).updatePinned(pinned);
} else {
new Pinned(HashTagActivity.this).insertPinned(pinned);
} catch (DBException e) {
e.printStackTrace();
}
pinnedTag = false;
invalidateOptionsMenu();
Bundle b = new Bundle();
b.putBoolean(Helper.RECEIVE_REDRAW_TOPBAR, true);
Intent intentBD = new Intent(Helper.BROADCAST_DATA);
intentBD.putExtras(b);
LocalBroadcastManager.getInstance(HashTagActivity.this).sendBroadcast(intentBD);
pinnedTag = true;
invalidateOptionsMenu();
} catch (DBException e) {
e.printStackTrace();
}
}).start();
dialog.dismiss();
});
unpinConfirm.show();
} else {
new Thread(() -> {
try {
Pinned pinned = new Pinned(HashTagActivity.this).getPinned(currentAccount);
boolean canBeAdded = true;
boolean update = true;
if (pinned == null) {
pinned = new Pinned();
pinned.pinnedTimelines = new ArrayList<>();
update = false;
} else {
for (PinnedTimeline pinnedTimeline : pinned.pinnedTimelines) {
if (pinnedTimeline.type == Timeline.TimeLineEnum.TAG) {
if (pinnedTimeline.tagTimeline.name.compareTo(tag.trim()) == 0) {
canBeAdded = false;
}
}
}
}
if (!canBeAdded) {
Handler mainHandler = new Handler(Looper.getMainLooper());
Runnable myRunnable = () -> Toasty.warning(HashTagActivity.this, getString(R.string.tags_already_stored), Toasty.LENGTH_SHORT).show();
mainHandler.post(myRunnable);
return;
}
pinnedTimeline = new PinnedTimeline();
pinnedTimeline.type = Timeline.TimeLineEnum.TAG;
pinnedTimeline.position = pinned.pinnedTimelines.size();
pinnedTimeline.displayed = true;
TagTimeline tagTimeline = new TagTimeline();
tagTimeline.name = tag.trim();
tagTimeline.isNSFW = false;
tagTimeline.isART = false;
pinnedTimeline.tagTimeline = tagTimeline;
pinned.pinnedTimelines.add(pinnedTimeline);
if (update) {
new Pinned(HashTagActivity.this).updatePinned(pinned);
} else {
new Pinned(HashTagActivity.this).insertPinned(pinned);
}
Bundle b = new Bundle();
b.putBoolean(Helper.RECEIVE_REDRAW_TOPBAR, true);
Intent intentBD = new Intent(Helper.BROADCAST_DATA);
intentBD.putExtras(b);
LocalBroadcastManager.getInstance(HashTagActivity.this).sendBroadcast(intentBD);
pinnedTag = true;
invalidateOptionsMenu();
} catch (DBException e) {
e.printStackTrace();
}
}).start();
}
} else if (item.getItemId() == R.id.action_follow_tag) {
tagVM.follow(MainActivity.currentInstance, MainActivity.currentToken, tag).observe(this, returnedTag -> {
if (returnedTag != null) {
followedTag = returnedTag.following;
invalidateOptionsMenu();
}
});
if (!followedTag) {
tagVM.follow(MainActivity.currentInstance, MainActivity.currentToken, tag).observe(this, returnedTag -> {
if (returnedTag != null) {
followedTag = returnedTag.following;
invalidateOptionsMenu();
}
});
} else {
tagVM.unfollow(MainActivity.currentInstance, MainActivity.currentToken, tag).observe(this, returnedTag -> {
if (returnedTag != null) {
followedTag = returnedTag.following;
invalidateOptionsMenu();
}
});
}
} else if (item.getItemId() == R.id.action_mute) {
if (MainActivity.mainFilters == null || fedilabFilter == null) {
MainActivity.mainFilters = new ArrayList<>();
Filter.FilterParams filterParams = new Filter.FilterParams();
filterParams.title = Helper.FEDILAB_MUTED_HASHTAGS;
filterParams.filter_action = "hide";
filterParams.context = new ArrayList<>();
filterParams.context.add("home");
filterParams.context.add("public");
filterParams.context.add("thread");
filterParams.context.add("account");
String finalTag = tag;
FiltersVM filtersVM = new ViewModelProvider(HashTagActivity.this).get(FiltersVM.class);
filtersVM.addFilter(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, filterParams)
.observe(HashTagActivity.this, filter -> {
if (filter != null) {
MainActivity.mainFilters.add(filter);
mutedTag = false;
fedilabFilter = filter;
muteTags();
invalidateOptionsMenu();
}
});
if (!mutedTag) {
if (MainActivity.mainFilters == null || fedilabFilter == null) {
MainActivity.mainFilters = new ArrayList<>();
Filter.FilterParams filterParams = new Filter.FilterParams();
filterParams.title = Helper.FEDILAB_MUTED_HASHTAGS;
filterParams.filter_action = "hide";
filterParams.context = new ArrayList<>();
filterParams.context.add("home");
filterParams.context.add("public");
filterParams.context.add("thread");
filterParams.context.add("account");
FiltersVM filtersVM = new ViewModelProvider(HashTagActivity.this).get(FiltersVM.class);
filtersVM.addFilter(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, filterParams)
.observe(HashTagActivity.this, filter -> {
if (filter != null) {
MainActivity.mainFilters.add(filter);
mutedTag = false;
fedilabFilter = filter;
muteTags();
invalidateOptionsMenu();
}
});
} else {
muteTags();
}
} else {
muteTags();
unmuteTags();
}
}
@ -249,6 +291,24 @@ public class HashTagActivity extends BaseActivity {
}
private void unmuteTags() {
String search = tag.startsWith("#") ? tag : "#" + tag;
for (Filter.KeywordsAttributes keywordsAttributes : fedilabFilter.keywords) {
if (search.equalsIgnoreCase(keywordsAttributes.keyword)) {
keyword = keywordsAttributes;
break;
}
}
if (keyword != null && keyword.id != null) {
FiltersVM filtersVM = new ViewModelProvider(HashTagActivity.this).get(FiltersVM.class);
filtersVM.removeKeyword(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, keyword.id);
fedilabFilter.keywords.remove(keyword);
mutedTag = false;
invalidateOptionsMenu();
}
}
private void muteTags() {
Filter.FilterParams filterParams = new Filter.FilterParams();
filterParams.id = fedilabFilter.id;
@ -261,6 +321,7 @@ public class HashTagActivity extends BaseActivity {
FiltersVM filtersVM = new ViewModelProvider(HashTagActivity.this).get(FiltersVM.class);
filtersVM.editFilter(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, filterParams)
.observe(HashTagActivity.this, filter -> {
fedilabFilter = filter;
mutedTag = true;
invalidateOptionsMenu();
});
@ -272,13 +333,42 @@ public class HashTagActivity extends BaseActivity {
MenuItem pin = menu.findItem(R.id.action_add_timeline);
MenuItem follow = menu.findItem(R.id.action_follow_tag);
MenuItem mute = menu.findItem(R.id.action_mute);
if (pinnedTag && pin != null) {
if (pinnedTag != null) {
pin.setVisible(true);
if (pinnedTag) {
pin.setIcon(R.drawable.tag_pin_off);
pin.setTitle(getString(R.string.unpin_tag));
} else {
pin.setTitle(getString(R.string.unpin_tag));
pin.setIcon(R.drawable.tag_pin);
}
} else {
pin.setVisible(false);
}
if (followedTag && follow != null) {
if (followedTag != null) {
follow.setVisible(true);
if (followedTag) {
follow.setTitle(getString(R.string.unfollow_tag));
follow.setIcon(R.drawable.tag_unfollow);
} else {
follow.setTitle(getString(R.string.follow_tag));
follow.setIcon(R.drawable.tag_follow);
}
} else {
follow.setVisible(false);
}
mute.setVisible(!mutedTag);
if (mutedTag != null) {
mute.setVisible(true);
if (mutedTag) {
mute.setTitle(getString(R.string.unmute_tag_action));
mute.setIcon(R.drawable.tag_unmuted);
} else {
mute.setTitle(getString(R.string.mute_tag_action));
mute.setIcon(R.drawable.tag_muted);
}
} else {
mute.setVisible(false);
}
return super.onCreateOptionsMenu(menu);
}

View File

@ -96,7 +96,7 @@ public interface MastodonFiltersService {
);
//Remove a keyword for a filter
@DELETE("filter_keywords/{id}")
@DELETE("filters/keywords/{id}")
Call<Void> removeKeywordFilter(
@Header("Authorization") String token,
@Path("id") String id

View File

@ -361,7 +361,6 @@ public class FragmentMastodonTimeline extends Fragment implements StatusAdapter.
if (timelineType != null) {
slug = timelineType != Timeline.TimeLineEnum.ART ? timelineType.getValue() + (ident != null ? "|" + ident : "") : Timeline.TimeLineEnum.TAG.getValue() + (ident != null ? "|" + ident : "");
}
LocalBroadcastManager.getInstance(requireActivity()).registerReceiver(receive_action, new IntentFilter(Helper.RECEIVE_STATUS_ACTION));
binding = FragmentPaginationBinding.inflate(inflater, container, false);
return binding.getRoot();

View File

@ -212,4 +212,24 @@ public class FiltersVM extends AndroidViewModel {
}).start();
}
/**
* Remove a filter
*
* @param id ID of the filter
*/
public void removeKeyword(@NonNull String instance, String token, @NonNull String id) {
MastodonFiltersService mastodonAccountsService = initV2(instance);
new Thread(() -> {
Call<Void> removeFilterCall = mastodonAccountsService.removeKeywordFilter(token, id);
if (removeFilterCall != null) {
try {
removeFilterCall.execute();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}
}

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M21.41,11.58L12.41,2.58C12.04,2.21 11.53,2 11,2H4A2,2 0,0 0,2 4V11C2,11.53 2.21,12.04 2.59,12.41L3,12.81C3.9,12.27 4.94,12 6,12A6,6 0,0 1,12 18C12,19.06 11.72,20.09 11.18,21L11.58,21.4C11.95,21.78 12.47,22 13,22C13.53,22 14.04,21.79 14.41,21.41L21.41,14.41C21.79,14.04 22,13.53 22,13C22,12.47 21.79,11.96 21.41,11.58M5.5,7A1.5,1.5 0,0 1,4 5.5A1.5,1.5 0,0 1,5.5 4A1.5,1.5 0,0 1,7 5.5A1.5,1.5 0,0 1,5.5 7M10,19H7V22H5V19H2V17H5V14H7V17H10V19Z" />
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M5.64,3.64L21.36,19.36L19.95,20.78L16,16.83V20L11,15H7V9H8.17L4.22,5.05L5.64,3.64M16,4V11.17L12.41,7.58L16,4Z" />
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M16,12V4H17V2H7V4H8V12L6,14V16H11.2V22H12.8V16H18V14L16,12Z" />
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M2,5.27L3.28,4L20,20.72L18.73,22L12.8,16.07V22H11.2V16H6V14L8,12V11.27L2,5.27M16,12L18,14V16H17.82L8,6.18V4H7V2H17V4H16V12Z" />
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M21.41,11.58L12.41,2.58C12.04,2.21 11.53,2 11,2H4A2,2 0,0 0,2 4V11C2,11.53 2.21,12.04 2.59,12.41L3,12.81C3.9,12.27 4.94,12 6,12A6,6 0,0 1,12 18C12,19.06 11.72,20.09 11.18,21L11.58,21.4C11.95,21.78 12.47,22 13,22C13.53,22 14.04,21.79 14.41,21.41L21.41,14.41C21.79,14.04 22,13.53 22,13C22,12.47 21.79,11.96 21.41,11.58M5.5,7A1.5,1.5 0,0 1,4 5.5A1.5,1.5 0,0 1,5.5 4A1.5,1.5 0,0 1,7 5.5A1.5,1.5 0,0 1,5.5 7M8.12,21.54L6,19.41L3.88,21.54L2.46,20.12L4.59,18L2.46,15.88L3.87,14.47L6,16.59L8.12,14.47L9.53,15.88L7.41,18L9.53,20.12L8.12,21.54Z" />
</vector>

View File

@ -0,0 +1,9 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#FF000000"
android:pathData="M4,9V15H8L13,20V4L8,9H4M16.55,2.47L15.5,3.53L17.93,6L15,9L17.93,12L15,15L17.93,18L15.5,20.47L16.55,21.53L20,18L17.07,15L20,12L17.07,9L20,6L16.55,2.47Z" />
</vector>

View File

@ -3,17 +3,17 @@
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/action_follow_tag"
android:icon="@drawable/ic_baseline_post_add_24"
android:icon="@drawable/tag_follow"
android:title="@string/follow_tag"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_add_timeline"
android:icon="@drawable/ic_baseline_add_24"
android:title="@string/add_instances"
android:icon="@drawable/tag_pin"
android:title="@string/pin_tag"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_mute"
android:icon="@drawable/ic_baseline_volume_mute_24"
android:title="@string/mute_tag"
android:icon="@drawable/tag_muted"
android:title="@string/mute_tag_action"
app:showAsAction="ifRoom" />
</menu>

View File

@ -2052,4 +2052,9 @@
<string name="toast_try_later">Please, try again later.</string>
<string name="toast_on_your_instance">The conversation started on your instance!</string>
<string name="toast_error_fetch_message">The app didn\'t find the remote message.</string>
<string name="mute_tag_action">Mute tag</string>
<string name="unmute_tag_action">Unmute tag</string>
<string name="pin_tag">Pin tag</string>
<string name="unpin_tag">Unpin tag</string>
<string name="unfollow_tag">Unfollow tag</string>
</resources>