From 68f88c29d3b6a0dc6da8fc5c9864c68156808760 Mon Sep 17 00:00:00 2001 From: LucasGGamerM Date: Sat, 18 Feb 2023 17:49:04 -0300 Subject: [PATCH] Initial try to integrate the custom local timelines into the custom tabs system --- .../CustomLocalTimelineFragment.java | 88 +++++++++++++++++++ .../android/model/CustomLocalTimeline.java | 26 ++++++ .../android/model/TimelineDefinition.java | 20 ++++- 3 files changed, 132 insertions(+), 2 deletions(-) create mode 100644 mastodon/src/main/java/org/joinmastodon/android/fragments/CustomLocalTimelineFragment.java create mode 100644 mastodon/src/main/java/org/joinmastodon/android/model/CustomLocalTimeline.java diff --git a/mastodon/src/main/java/org/joinmastodon/android/fragments/CustomLocalTimelineFragment.java b/mastodon/src/main/java/org/joinmastodon/android/fragments/CustomLocalTimelineFragment.java new file mode 100644 index 000000000..866606504 --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/fragments/CustomLocalTimelineFragment.java @@ -0,0 +1,88 @@ +package org.joinmastodon.android.fragments; + +import android.app.Activity; +import android.os.Bundle; +import android.view.HapticFeedbackConstants; +import android.view.Menu; +import android.view.MenuInflater; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.widget.ImageButton; +import android.widget.Toast; + +import org.joinmastodon.android.E; +import org.joinmastodon.android.R; +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.api.requests.timelines.GetPublicTimeline; +import org.joinmastodon.android.events.HashtagUpdatedEvent; +import org.joinmastodon.android.model.Filter; +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 java.util.List; +import java.util.stream.Collectors; + +import me.grishka.appkit.Nav; +import me.grishka.appkit.api.Callback; +import me.grishka.appkit.api.ErrorResponse; +import me.grishka.appkit.api.SimpleCallback; +import me.grishka.appkit.utils.V; + +public class CustomLocalTimelineFragment extends PinnableStatusListFragment { +// private String name; + private String domain; + + private String maxID; + @Override + protected boolean withComposeButton() { + return false; + } + + @Override + public void onAttach(Activity activity){ + super.onAttach(activity); + domain=getArguments().getString("domain"); + updateTitle(getArguments().getString("domain")); + + setHasOptionsMenu(true); + } + + private void updateTitle(String domain) { + this.domain = domain; + setTitle(domain); + } + + @Override + protected TimelineDefinition makeTimelineDefinition() { + return TimelineDefinition.ofCustomLocalTimeline(domain); + } + + @Override + protected void doLoadData(int offset, int count){ + currentRequest=new GetPublicTimeline(true, false, refreshing ? null : maxID, count) + .setCallback(new SimpleCallback<>(this){ + @Override + public void onSuccess(List result){ + if(!result.isEmpty()) + maxID=result.get(result.size()-1).id; + if (getActivity() == null) return; + result=result.stream().filter(new StatusFilterPredicate(accountID, Filter.FilterContext.PUBLIC)).collect(Collectors.toList()); + onDataLoaded(result, !result.isEmpty()); + } + }) + .execNoAuth("fosstodon.org"); + } + + @Override + protected void onShown(){ + super.onShown(); + if(!getArguments().getBoolean("noAutoLoad") && !loaded && !dataLoading) + loadData(); + } +} diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/CustomLocalTimeline.java b/mastodon/src/main/java/org/joinmastodon/android/model/CustomLocalTimeline.java new file mode 100644 index 000000000..80520db82 --- /dev/null +++ b/mastodon/src/main/java/org/joinmastodon/android/model/CustomLocalTimeline.java @@ -0,0 +1,26 @@ +package org.joinmastodon.android.model; + +import androidx.annotation.NonNull; + +import com.google.gson.annotations.SerializedName; + +import org.joinmastodon.android.api.RequiredField; +import org.parceler.Parcel; + +@Parcel +public class CustomLocalTimeline extends BaseModel { + @RequiredField + public String domain; + @RequiredField + public String title; + + @NonNull + @Override + public String toString() { + return "CustomLocalTimeline{" + + "domain='" + domain + '\'' + + ", title='" + title + + '}'; + } + +} diff --git a/mastodon/src/main/java/org/joinmastodon/android/model/TimelineDefinition.java b/mastodon/src/main/java/org/joinmastodon/android/model/TimelineDefinition.java index fd7c3346a..ba832cd80 100644 --- a/mastodon/src/main/java/org/joinmastodon/android/model/TimelineDefinition.java +++ b/mastodon/src/main/java/org/joinmastodon/android/model/TimelineDefinition.java @@ -10,6 +10,7 @@ import androidx.annotation.StringRes; import org.joinmastodon.android.BuildConfig; import org.joinmastodon.android.R; +import org.joinmastodon.android.fragments.CustomLocalTimelineFragment; import org.joinmastodon.android.fragments.HashtagTimelineFragment; import org.joinmastodon.android.fragments.HomeTimelineFragment; import org.joinmastodon.android.fragments.ListTimelineFragment; @@ -29,6 +30,7 @@ public class TimelineDefinition { private @Nullable String listTitle; private @Nullable String hashtagName; + private @Nullable String customLocalTimelineDomain; public static TimelineDefinition ofList(String listId, String listTitle) { TimelineDefinition def = new TimelineDefinition(TimelineType.LIST); @@ -51,6 +53,18 @@ public class TimelineDefinition { return ofHashtag(hashtag.name); } + public static TimelineDefinition ofCustomLocalTimeline(String domain) { + TimelineDefinition def = new TimelineDefinition(TimelineType.CUSTOM_LOCAL_TIMELINE); + def.customLocalTimelineDomain = domain; + return def; + } + + public static TimelineDefinition ofCustomLocalTimeline(CustomLocalTimeline customLocalTimeline) { + return ofCustomLocalTimeline(customLocalTimeline.domain); + } + + + @SuppressWarnings("unused") public TimelineDefinition() {} @@ -78,13 +92,14 @@ public class TimelineDefinition { case POST_NOTIFICATIONS -> ctx.getString(R.string.sk_timeline_posts); case LIST -> listTitle; case HASHTAG -> hashtagName; + case CUSTOM_LOCAL_TIMELINE -> customLocalTimelineDomain; }; } public Icon getDefaultIcon() { return switch (type) { case HOME -> Icon.HOME; - case LOCAL -> Icon.LOCAL; + case LOCAL, CUSTOM_LOCAL_TIMELINE -> Icon.LOCAL; case FEDERATED -> Icon.FEDERATED; case POST_NOTIFICATIONS -> Icon.POST_NOTIFICATIONS; case LIST -> Icon.LIST; @@ -100,6 +115,7 @@ public class TimelineDefinition { case LIST -> new ListTimelineFragment(); case HASHTAG -> new HashtagTimelineFragment(); case POST_NOTIFICATIONS -> new NotificationsListFragment(); + case CUSTOM_LOCAL_TIMELINE -> new CustomLocalTimelineFragment(); }; } @@ -156,7 +172,7 @@ public class TimelineDefinition { return args; } - public enum TimelineType { HOME, LOCAL, FEDERATED, POST_NOTIFICATIONS, LIST, HASHTAG } + public enum TimelineType { HOME, LOCAL, FEDERATED, POST_NOTIFICATIONS, LIST, HASHTAG, CUSTOM_LOCAL_TIMELINE} public enum Icon { HEART(R.drawable.ic_fluent_heart_24_regular, R.string.sk_icon_heart),