2022-04-27 15:20:42 +02:00
|
|
|
package app.fedilab.android.activities;
|
|
|
|
/* Copyright 2022 Thomas Schneider
|
|
|
|
*
|
|
|
|
* This file is a part of Fedilab
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
|
2022-06-21 17:09:34 +02:00
|
|
|
import static app.fedilab.android.BaseMainActivity.currentAccount;
|
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
import android.content.Intent;
|
|
|
|
import android.os.Bundle;
|
2022-04-29 17:12:03 +02:00
|
|
|
import android.os.Handler;
|
|
|
|
import android.os.Looper;
|
2022-04-27 15:20:42 +02:00
|
|
|
import android.view.Menu;
|
|
|
|
import android.view.MenuItem;
|
|
|
|
|
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
import androidx.appcompat.app.ActionBar;
|
2022-11-15 10:51:46 +01:00
|
|
|
import androidx.lifecycle.ViewModelProvider;
|
2022-04-27 15:20:42 +02:00
|
|
|
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.List;
|
|
|
|
|
2022-11-26 16:46:26 +01:00
|
|
|
import app.fedilab.android.BaseMainActivity;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.R;
|
2022-11-26 16:46:26 +01:00
|
|
|
import app.fedilab.android.client.entities.api.Filter;
|
2022-05-24 10:12:04 +02:00
|
|
|
import app.fedilab.android.client.entities.api.Status;
|
|
|
|
import app.fedilab.android.client.entities.app.Pinned;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.client.entities.app.PinnedTimeline;
|
2022-05-24 10:12:04 +02:00
|
|
|
import app.fedilab.android.client.entities.app.StatusDraft;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.client.entities.app.TagTimeline;
|
2022-05-24 10:12:04 +02:00
|
|
|
import app.fedilab.android.client.entities.app.Timeline;
|
2022-04-27 15:20:42 +02:00
|
|
|
import app.fedilab.android.databinding.ActivityHashtagBinding;
|
|
|
|
import app.fedilab.android.exception.DBException;
|
|
|
|
import app.fedilab.android.helper.Helper;
|
|
|
|
import app.fedilab.android.ui.fragment.timeline.FragmentMastodonTimeline;
|
2022-11-26 16:46:26 +01:00
|
|
|
import app.fedilab.android.viewmodel.mastodon.FiltersVM;
|
2022-11-15 10:51:46 +01:00
|
|
|
import app.fedilab.android.viewmodel.mastodon.ReorderVM;
|
|
|
|
import app.fedilab.android.viewmodel.mastodon.TagVM;
|
2022-04-27 15:20:42 +02:00
|
|
|
import es.dmoral.toasty.Toasty;
|
|
|
|
|
|
|
|
|
|
|
|
public class HashTagActivity extends BaseActivity {
|
|
|
|
|
|
|
|
|
|
|
|
public static int position;
|
|
|
|
private String tag;
|
2022-11-15 10:51:46 +01:00
|
|
|
private boolean pinnedTag;
|
|
|
|
private boolean followedTag;
|
2022-11-26 16:46:26 +01:00
|
|
|
private boolean mutedTag;
|
2022-11-15 10:51:46 +01:00
|
|
|
private TagVM tagVM;
|
2022-11-26 16:46:26 +01:00
|
|
|
private Filter fedilabFilter;
|
2022-04-27 15:20:42 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
super.onCreate(savedInstanceState);
|
2022-11-28 10:37:57 +01:00
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
ActivityHashtagBinding binding = ActivityHashtagBinding.inflate(getLayoutInflater());
|
|
|
|
|
|
|
|
setContentView(binding.getRoot());
|
|
|
|
Bundle b = getIntent().getExtras();
|
|
|
|
if (b != null) {
|
|
|
|
tag = b.getString(Helper.ARG_SEARCH_KEYWORD, null);
|
|
|
|
}
|
|
|
|
if (tag == null)
|
|
|
|
finish();
|
2022-11-15 10:51:46 +01:00
|
|
|
pinnedTag = false;
|
|
|
|
followedTag = false;
|
2022-11-26 18:28:00 +01:00
|
|
|
mutedTag = false;
|
2022-04-27 15:20:42 +02:00
|
|
|
setSupportActionBar(binding.toolbar);
|
|
|
|
ActionBar actionBar = getSupportActionBar();
|
|
|
|
//Remove title
|
|
|
|
if (actionBar != null) {
|
|
|
|
actionBar.setDisplayShowTitleEnabled(false);
|
|
|
|
}
|
|
|
|
binding.title.setText(tag);
|
|
|
|
if (getSupportActionBar() != null) {
|
|
|
|
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
|
|
|
|
getSupportActionBar().setDisplayShowHomeEnabled(true);
|
|
|
|
}
|
|
|
|
|
2022-11-15 10:51:46 +01:00
|
|
|
tagVM = new ViewModelProvider(HashTagActivity.this).get(TagVM.class);
|
|
|
|
tagVM.getTag(MainActivity.currentInstance, MainActivity.currentToken, tag).observe(this, returnedTag -> {
|
|
|
|
if (returnedTag != null) {
|
|
|
|
followedTag = returnedTag.following;
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
ReorderVM reorderVM = new ViewModelProvider(HashTagActivity.this).get(ReorderVM.class);
|
|
|
|
reorderVM.getAllPinned().observe(HashTagActivity.this, pinned -> {
|
|
|
|
if (pinned != null) {
|
|
|
|
if (pinned.pinnedTimelines != null) {
|
|
|
|
for (PinnedTimeline pinnedTimeline : pinned.pinnedTimelines) {
|
|
|
|
if (pinnedTimeline.tagTimeline != null) {
|
|
|
|
if (pinnedTimeline.tagTimeline.name.equalsIgnoreCase(tag)) {
|
|
|
|
pinnedTag = true;
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2022-11-26 16:46:26 +01:00
|
|
|
if (MainActivity.filterFetched && MainActivity.mainFilters != null) {
|
|
|
|
for (Filter filter : MainActivity.mainFilters) {
|
|
|
|
if (filter.title.equalsIgnoreCase(Helper.FEDILAB_MUTED_HASHTAGS)) {
|
|
|
|
fedilabFilter = filter;
|
|
|
|
String fetch = tag.startsWith("#") ? tag : "#" + tag;
|
|
|
|
for (Filter.KeywordsAttributes keywordsAttributes : filter.keywords) {
|
|
|
|
if (fetch.equalsIgnoreCase(keywordsAttributes.keyword)) {
|
|
|
|
mutedTag = true;
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mutedTag = false;
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mutedTag = true;
|
|
|
|
}
|
2022-11-15 10:51:46 +01:00
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
Bundle bundle = new Bundle();
|
|
|
|
bundle.putSerializable(Helper.ARG_TIMELINE_TYPE, Timeline.TimeLineEnum.TAG);
|
|
|
|
bundle.putString(Helper.ARG_SEARCH_KEYWORD, tag);
|
|
|
|
Helper.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment_tags, new FragmentMastodonTimeline(), bundle, null, null);
|
|
|
|
binding.compose.setOnClickListener(v -> {
|
|
|
|
Intent intentToot = new Intent(HashTagActivity.this, ComposeActivity.class);
|
|
|
|
StatusDraft statusDraft = new StatusDraft();
|
|
|
|
Status status = new Status();
|
|
|
|
status.text = "#" + tag;
|
|
|
|
List<Status> statuses = new ArrayList<>();
|
|
|
|
statuses.add(status);
|
|
|
|
statusDraft.statusDraftList = statuses;
|
|
|
|
Bundle _b = new Bundle();
|
|
|
|
_b.putSerializable(Helper.ARG_TAG_TIMELINE, statusDraft);
|
|
|
|
intentToot.putExtras(_b);
|
|
|
|
startActivity(intentToot);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean onOptionsItemSelected(MenuItem item) {
|
|
|
|
if (item.getItemId() == android.R.id.home) {
|
|
|
|
finish();
|
|
|
|
return true;
|
|
|
|
} else if (item.getItemId() == R.id.action_add_timeline) {
|
|
|
|
new Thread(() -> {
|
|
|
|
try {
|
2022-06-21 17:09:34 +02:00
|
|
|
Pinned pinned = new Pinned(HashTagActivity.this).getPinned(currentAccount);
|
2022-04-27 15:20:42 +02:00
|
|
|
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) {
|
2022-04-29 17:12:03 +02:00
|
|
|
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);
|
2022-04-27 15:20:42 +02:00
|
|
|
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) {
|
|
|
|
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);
|
2022-11-15 10:51:46 +01:00
|
|
|
pinnedTag = true;
|
|
|
|
invalidateOptionsMenu();
|
2022-04-27 15:20:42 +02:00
|
|
|
} catch (DBException e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}).start();
|
2022-11-15 10:51:46 +01:00
|
|
|
} 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();
|
|
|
|
}
|
|
|
|
});
|
2022-11-26 16:46:26 +01:00
|
|
|
} else if (item.getItemId() == R.id.action_mute) {
|
2022-11-26 18:28:00 +01:00
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
muteTags();
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return super.onOptionsItemSelected(item);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-26 18:28:00 +01:00
|
|
|
private void muteTags() {
|
|
|
|
Filter.FilterParams filterParams = new Filter.FilterParams();
|
|
|
|
filterParams.id = fedilabFilter.id;
|
|
|
|
filterParams.keywords = new ArrayList<>();
|
|
|
|
Filter.KeywordsParams keywordsParams = new Filter.KeywordsParams();
|
|
|
|
keywordsParams.whole_word = true;
|
|
|
|
keywordsParams.keyword = tag.startsWith("#") ? tag : "#" + tag;
|
|
|
|
filterParams.keywords.add(keywordsParams);
|
|
|
|
filterParams.context = fedilabFilter.context;
|
|
|
|
FiltersVM filtersVM = new ViewModelProvider(HashTagActivity.this).get(FiltersVM.class);
|
|
|
|
filtersVM.editFilter(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, filterParams)
|
|
|
|
.observe(HashTagActivity.this, filter -> {
|
|
|
|
mutedTag = true;
|
|
|
|
invalidateOptionsMenu();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-04-27 15:20:42 +02:00
|
|
|
@Override
|
|
|
|
public boolean onCreateOptionsMenu(@NonNull Menu menu) {
|
2022-11-15 10:51:46 +01:00
|
|
|
getMenuInflater().inflate(R.menu.menu_hashtag, menu);
|
|
|
|
MenuItem pin = menu.findItem(R.id.action_add_timeline);
|
|
|
|
MenuItem follow = menu.findItem(R.id.action_follow_tag);
|
2022-11-26 16:46:26 +01:00
|
|
|
MenuItem mute = menu.findItem(R.id.action_mute);
|
2022-11-15 10:51:46 +01:00
|
|
|
if (pinnedTag && pin != null) {
|
|
|
|
pin.setVisible(false);
|
|
|
|
}
|
|
|
|
if (followedTag && follow != null) {
|
|
|
|
follow.setVisible(false);
|
|
|
|
}
|
2022-11-26 16:46:26 +01:00
|
|
|
mute.setVisible(!mutedTag);
|
2022-04-27 15:20:42 +02:00
|
|
|
return super.onCreateOptionsMenu(menu);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|