Commit cdbfb17d authored by Thomas's avatar Thomas
Browse files

Follow Hashtags

parent 170dbbd0
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -251,6 +251,11 @@
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/action_lists"
            android:theme="@style/AppThemeBar" />
        <activity
            android:name=".activities.FollowedTagActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
            android:label="@string/followed_tags"
            android:theme="@style/AppThemeBar" />
        <activity
            android:name=".activities.SettingsActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
+4 −0
Original line number Diff line number Diff line
@@ -103,6 +103,7 @@ import app.fedilab.android.activities.ContextActivity;
import app.fedilab.android.activities.DraftActivity;
import app.fedilab.android.activities.FilterActivity;
import app.fedilab.android.activities.FollowRequestActivity;
import app.fedilab.android.activities.FollowedTagActivity;
import app.fedilab.android.activities.InstanceActivity;
import app.fedilab.android.activities.InstanceHealthActivity;
import app.fedilab.android.activities.LoginActivity;
@@ -359,6 +360,9 @@ public abstract class BaseMainActivity extends BaseActivity implements NetworkSt
            } else if (id == R.id.nav_list) {
                Intent intent = new Intent(this, MastodonListActivity.class);
                startActivity(intent);
            } else if (id == R.id.nav_followed_tags) {
                Intent intent = new Intent(this, FollowedTagActivity.class);
                startActivity(intent);
            } else if (id == R.id.nav_settings) {
                Intent intent = new Intent(this, SettingsActivity.class);
                startActivity(intent);
+202 −0
Original line number Diff line number Diff line
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>. */


import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.text.InputFilter;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.core.content.ContextCompat;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;

import java.util.ArrayList;

import app.fedilab.android.BaseMainActivity;
import app.fedilab.android.R;
import app.fedilab.android.client.entities.api.Tag;
import app.fedilab.android.client.entities.app.Timeline;
import app.fedilab.android.databinding.ActivityFollowedTagsBinding;
import app.fedilab.android.databinding.PopupAddFollowedTagtBinding;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.ThemeHelper;
import app.fedilab.android.ui.drawer.FollowedTagAdapter;
import app.fedilab.android.ui.fragment.timeline.FragmentMastodonTimeline;
import app.fedilab.android.viewmodel.mastodon.TagVM;
import es.dmoral.toasty.Toasty;


public class FollowedTagActivity extends BaseActivity implements FollowedTagAdapter.ActionOnTag {


    private ActivityFollowedTagsBinding binding;
    private boolean canGoBack;
    private TagVM tagVM;
    private Tag tag;
    private ArrayList<Tag> tagList;
    private FollowedTagAdapter followedTagAdapter;
    private FragmentMastodonTimeline fragmentMastodonTimeline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeHelper.applyThemeBar(this);
        binding = ActivityFollowedTagsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        canGoBack = false;
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setDisplayShowHomeEnabled(true);
            getSupportActionBar().setBackgroundDrawable(new ColorDrawable(ContextCompat.getColor(this, R.color.cyanea_primary)));
        }
        tagVM = new ViewModelProvider(FollowedTagActivity.this).get(TagVM.class);
        tagVM.followedTags(BaseMainActivity.currentInstance, BaseMainActivity.currentToken)
                .observe(FollowedTagActivity.this, tags -> {
                    if (tags != null && tags.tags != null && tags.tags.size() > 0) {
                        tagList = new ArrayList<>(tags.tags);
                        followedTagAdapter = new FollowedTagAdapter(tagList);
                        followedTagAdapter.actionOnTag = this;
                        binding.notContent.setVisibility(View.GONE);
                        binding.recyclerView.setAdapter(followedTagAdapter);
                        binding.recyclerView.setLayoutManager(new LinearLayoutManager(FollowedTagActivity.this));
                    } else {
                        binding.notContent.setVisibility(View.VISIBLE);
                    }
                });
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        } else if (item.getItemId() == R.id.action_unfollow && tag != null) {
            AlertDialog.Builder alt_bld = new AlertDialog.Builder(FollowedTagActivity.this, Helper.dialogStyle());
            alt_bld.setTitle(R.string.action_unfollow_tag);
            alt_bld.setMessage(R.string.action_unfollow_tag_confirm);
            alt_bld.setPositiveButton(R.string.unfollow, (dialog, id) -> {
                tagVM.unfollow(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, tag.name);
                int position = 0;
                for (Tag tagTmp : tagList) {
                    if (tagTmp.name.equalsIgnoreCase(tag.name)) {
                        break;
                    }
                    position++;
                }
                tagList.remove(position);
                followedTagAdapter.notifyItemRemoved(position);
                ThemeHelper.slideViewsToRight(binding.fragmentContainer, binding.recyclerView, () -> {
                    canGoBack = false;
                    if (fragmentMastodonTimeline != null) {
                        fragmentMastodonTimeline.onDestroyView();
                    }
                    invalidateOptionsMenu();
                    setTitle(R.string.action_lists);
                });
                if (tagList.size() == 0) {
                    binding.notContent.setVisibility(View.VISIBLE);
                } else {
                    binding.notContent.setVisibility(View.GONE);
                }
            });
            alt_bld.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
            AlertDialog alert = alt_bld.create();
            alert.show();
        } else if (item.getItemId() == R.id.action_follow_tag) {
            AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(FollowedTagActivity.this, Helper.dialogStyle());
            PopupAddFollowedTagtBinding popupAddFollowedTagtBinding = PopupAddFollowedTagtBinding.inflate(getLayoutInflater());
            dialogBuilder.setView(popupAddFollowedTagtBinding.getRoot());
            popupAddFollowedTagtBinding.addTag.setFilters(new InputFilter[]{new InputFilter.LengthFilter(255)});
            dialogBuilder.setPositiveButton(R.string.validate, (dialog, id) -> {
                if (popupAddFollowedTagtBinding.addTag.getText() != null && popupAddFollowedTagtBinding.addTag.getText().toString().trim().length() > 0) {
                    tagVM.follow(BaseMainActivity.currentInstance, BaseMainActivity.currentToken, popupAddFollowedTagtBinding.addTag.getText().toString().trim())
                            .observe(FollowedTagActivity.this, newTag -> {
                                if (tagList == null) {
                                    tagList = new ArrayList<>();
                                }
                                if (newTag != null && followedTagAdapter != null) {
                                    tagList.add(0, newTag);
                                    followedTagAdapter.notifyItemInserted(0);
                                } else {
                                    Toasty.error(FollowedTagActivity.this, getString(R.string.toast_error), Toasty.LENGTH_LONG).show();
                                }
                            });
                    dialog.dismiss();
                } else {
                    popupAddFollowedTagtBinding.addTag.setError(getString(R.string.not_valid_tag_name));
                }

            });
            dialogBuilder.setNegativeButton(R.string.cancel, (dialog, id) -> dialog.dismiss());
            dialogBuilder.create().show();
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onCreateOptionsMenu(@NonNull Menu menu) {
        if (!canGoBack) {
            getMenuInflater().inflate(R.menu.menu_main_followed_tag, menu);
        } else {
            getMenuInflater().inflate(R.menu.menu_followed_tag, menu);
        }
        return true;
    }

    @Override
    public void onBackPressed() {
        if (canGoBack) {
            canGoBack = false;
            ThemeHelper.slideViewsToRight(binding.fragmentContainer, binding.recyclerView, () -> {
                if (fragmentMastodonTimeline != null) {
                    fragmentMastodonTimeline.onDestroyView();
                }
            });
            setTitle(R.string.action_lists);
            invalidateOptionsMenu();
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public void click(Tag tag) {
        this.tag = tag;
        canGoBack = true;
        ThemeHelper.slideViewsToLeft(binding.recyclerView, binding.fragmentContainer, () -> {
            fragmentMastodonTimeline = new FragmentMastodonTimeline();
            Bundle bundle = new Bundle();
            bundle.putSerializable(Helper.ARG_SEARCH_KEYWORD, tag.name);
            bundle.putSerializable(Helper.ARG_TIMELINE_TYPE, Timeline.TimeLineEnum.TAG);
            setTitle(tag.name);
            fragmentMastodonTimeline.setArguments(bundle);
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction =
                    fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.fragment_container, fragmentMastodonTimeline);
            fragmentTransaction.commit();
            invalidateOptionsMenu();
        });
    }
}
+49 −2
Original line number Diff line number Diff line
@@ -28,6 +28,7 @@ import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.core.content.ContextCompat;
import androidx.lifecycle.ViewModelProvider;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import java.util.ArrayList;
@@ -45,6 +46,8 @@ import app.fedilab.android.exception.DBException;
import app.fedilab.android.helper.Helper;
import app.fedilab.android.helper.ThemeHelper;
import app.fedilab.android.ui.fragment.timeline.FragmentMastodonTimeline;
import app.fedilab.android.viewmodel.mastodon.ReorderVM;
import app.fedilab.android.viewmodel.mastodon.TagVM;
import es.dmoral.toasty.Toasty;


@@ -53,6 +56,9 @@ public class HashTagActivity extends BaseActivity {

    public static int position;
    private String tag;
    private boolean pinnedTag;
    private boolean followedTag;
    private TagVM tagVM;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
@@ -67,7 +73,8 @@ public class HashTagActivity extends BaseActivity {
        }
        if (tag == null)
            finish();

        pinnedTag = false;
        followedTag = false;
        setSupportActionBar(binding.toolbar);
        ActionBar actionBar = getSupportActionBar();
        //Remove title
@@ -81,6 +88,29 @@ public class HashTagActivity extends BaseActivity {
            getSupportActionBar().setDisplayShowHomeEnabled(true);
        }

        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();
                            }
                        }
                    }
                }
            }
        });

        Bundle bundle = new Bundle();
        bundle.putSerializable(Helper.ARG_TIMELINE_TYPE, Timeline.TimeLineEnum.TAG);
        bundle.putString(Helper.ARG_SEARCH_KEYWORD, tag);
@@ -151,10 +181,19 @@ public class HashTagActivity extends BaseActivity {
                    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();
                }
            });
        }

        return super.onOptionsItemSelected(item);
@@ -163,7 +202,15 @@ public class HashTagActivity extends BaseActivity {

    @Override
    public boolean onCreateOptionsMenu(@NonNull Menu menu) {
        getMenuInflater().inflate(R.menu.menu_reorder, menu);
        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);
        if (pinnedTag && pin != null) {
            pin.setVisible(false);
        }
        if (followedTag && follow != null) {
            follow.setVisible(false);
        }
        return super.onCreateOptionsMenu(menu);
    }

+8 −5
Original line number Diff line number Diff line
@@ -23,18 +23,21 @@ import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.Path;
import retrofit2.http.Query;

public interface MastodonTagService {

    //Get followed tags
    @GET("followed_tags")
    Call<List<Tag>> getFollowedTags(
            @Header("Authorization") String token
    );


    //Get followed tags
    @GET("tags/{name}")
    Call<Tag> getTag(
            @Header("Authorization") String token,
            @Query("max_id") String max_id,
            @Query("since_id") String since_id,
            @Query("min_id") String min_id,
            @Query("limit") int limit
            @Path("name") String name
    );

    //Follow tag
Loading