From 542f053a1fbe48541f497020c58a3c877d16a746 Mon Sep 17 00:00:00 2001 From: HuanCheng65 <609486518@qq.com> Date: Fri, 3 Jun 2022 19:14:19 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=A2=9E=E5=8A=A0=E7=BD=91=E9=A1=B5?= =?UTF-8?q?=E7=89=88=E5=85=B3=E6=B3=A8=E5=90=A7=E5=88=97=E8=A1=A8=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../post/adapters/LikeForumListAdapter.java | 297 ------------------ .../tieba/post/api/HttpConstant.kt | 2 + .../tieba/post/api/interfaces/ITiebaApi.kt | 6 + .../api/interfaces/impls/MixedTiebaApiImpl.kt | 12 + .../tieba/post/api/models/web/ForumHome.kt | 34 ++ .../tieba/post/api/models/web/WebBase.kt | 12 + .../post/api/retrofit/RetrofitTiebaApi.kt | 4 +- .../api/retrofit/interfaces/WebTiebaApi.kt | 17 + 8 files changed, 85 insertions(+), 299 deletions(-) delete mode 100644 app/src/main/java/com/huanchengfly/tieba/post/adapters/LikeForumListAdapter.java create mode 100644 app/src/main/java/com/huanchengfly/tieba/post/api/models/web/ForumHome.kt create mode 100644 app/src/main/java/com/huanchengfly/tieba/post/api/models/web/WebBase.kt diff --git a/app/src/main/java/com/huanchengfly/tieba/post/adapters/LikeForumListAdapter.java b/app/src/main/java/com/huanchengfly/tieba/post/adapters/LikeForumListAdapter.java deleted file mode 100644 index 09759c37..00000000 --- a/app/src/main/java/com/huanchengfly/tieba/post/adapters/LikeForumListAdapter.java +++ /dev/null @@ -1,297 +0,0 @@ -package com.huanchengfly.tieba.post.adapters; - -import android.content.Context; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.ImageView; - -import androidx.annotation.LayoutRes; -import androidx.annotation.NonNull; -import androidx.recyclerview.widget.RecyclerView; - -import com.bumptech.glide.Glide; -import com.huanchengfly.tieba.post.R; -import com.huanchengfly.tieba.post.api.models.ForumRecommend; -import com.huanchengfly.tieba.post.components.MyLinearLayoutManager; -import com.huanchengfly.tieba.post.components.MyViewHolder; -import com.huanchengfly.tieba.post.components.dividers.HorizontalSpacesDecoration; -import com.huanchengfly.tieba.post.interfaces.OnItemClickListener; -import com.huanchengfly.tieba.post.interfaces.OnItemLongClickListener; -import com.huanchengfly.tieba.post.models.database.TopForum; -import com.huanchengfly.tieba.post.utils.DisplayUtil; -import com.huanchengfly.tieba.post.utils.ImageUtil; -import com.huanchengfly.tieba.post.utils.SharedPreferencesUtil; -import com.huanchengfly.tieba.post.utils.ThemeUtil; - -import org.litepal.LitePal; - -import java.lang.ref.WeakReference; -import java.util.ArrayList; -import java.util.List; - -public class LikeForumListAdapter extends RecyclerView.Adapter { - public static final int TYPE_ERROR = -1; - public static final int TYPE_TOP_FORUM_TITLE = 1; - public static final int TYPE_TOP_FORUM = 2; - public static final int TYPE_NORMAL_FORUM_TITLE = 3; - public static final int TYPE_NORMAL_FORUM = 4; - private boolean showTop; - private boolean single; - private WeakReference contextWeakReference; - private List topForums; - private List mLikeForums; - private TopForumsAdapter topForumsAdapter; - private OnItemClickListener onItemClickListener; - private OnItemLongClickListener onItemLongClickListener; - - public LikeForumListAdapter(Context context) { - contextWeakReference = new WeakReference<>(context); - single = SharedPreferencesUtil.get(context, SharedPreferencesUtil.SP_SETTINGS).getBoolean("listSingle", false); - mLikeForums = new ArrayList<>(); - topForums = new ArrayList<>(); - topForumsAdapter = new TopForumsAdapter(context); - onItemClickListener = null; - onItemLongClickListener = null; - showTop = SharedPreferencesUtil.get(context, SharedPreferencesUtil.SP_APP_DATA).getBoolean("show_top_forum", true); - } - - public Context getContext() { - return contextWeakReference.get(); - } - - public void remove(int position) { - if (position >= 0 && position < mLikeForums.size()) { - mLikeForums.remove(position); - notifyDataSetChanged(); - } - } - - public OnItemClickListener getOnItemClickListener() { - return onItemClickListener; - } - - public LikeForumListAdapter setOnItemClickListener(OnItemClickListener onItemClickListener) { - this.onItemClickListener = onItemClickListener; - return this; - } - - public OnItemLongClickListener getOnItemLongClickListener() { - return onItemLongClickListener; - } - - public LikeForumListAdapter setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) { - this.onItemLongClickListener = onItemLongClickListener; - return this; - } - - public boolean isHeader(int position) { - return isForumItemTitle(position) || isTopForumItemTitle(position) || isTopForumItem(position); - } - - @LayoutRes - public int getItemLayoutId(int type) { - switch (type) { - case TYPE_TOP_FORUM_TITLE: - case TYPE_NORMAL_FORUM_TITLE: - return R.layout.layout_forum_list_header; - case TYPE_TOP_FORUM: - return R.layout.item_top_forums; - case TYPE_NORMAL_FORUM: - return R.layout.item_forum_list; - default: - return R.layout.item_empty; - } - } - - public boolean isShowTop() { - return showTop; - } - - public void setShowTop(boolean showTop) { - this.showTop = showTop; - SharedPreferencesUtil.get(getContext(), SharedPreferencesUtil.SP_APP_DATA).edit().putBoolean("show_top_forum", showTop).apply(); - notifyDataSetChanged(); - } - - @NonNull - @Override - public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) { - return new MyViewHolder(LayoutInflater.from(getContext()).inflate(getItemLayoutId(viewType), parent, false)); - } - - public ForumRecommend.LikeForum getItem(int position) { - return mLikeForums.get(position); - } - - public int getSpanCount() { - if (SharedPreferencesUtil.get(getContext(), SharedPreferencesUtil.SP_SETTINGS).getBoolean("listSingle", false)) { - return 1; - } else { - return 2; - } - } - - @Override - public void onBindViewHolder(@NonNull MyViewHolder holder, int position) { - int viewType = getItemViewType(position); - switch (viewType) { - case TYPE_TOP_FORUM_TITLE: - holder.setText(R.id.forum_title_text, R.string.title_top_forum); - ImageView imageView = holder.getView(R.id.forum_title_icon); - imageView.setVisibility(View.VISIBLE); - imageView.setImageResource(isShowTop() ? R.drawable.ic_keyboard_arrow_down : R.drawable.ic_round_keyboard_arrow_right); - holder.setItemOnClickListener(v -> setShowTop(!isShowTop())); - break; - case TYPE_NORMAL_FORUM_TITLE: - holder.setText(R.id.forum_title_text, R.string.forum_list_title); - holder.setVisibility(R.id.forum_title_icon, View.GONE); - break; - case TYPE_TOP_FORUM: - topForumsAdapter.setOnItemClickListener(getOnItemClickListener()); - topForumsAdapter.setOnItemLongClickListener(getOnItemLongClickListener()); - RecyclerView topForumsView = holder.getView(R.id.forum_top_forums); - topForumsView.setLayoutManager(new MyLinearLayoutManager(getContext(), MyLinearLayoutManager.HORIZONTAL, false)); - topForumsView.setAdapter(topForumsAdapter); - topForumsView.setHasFixedSize(false); - for (int i = 0; i < topForumsView.getItemDecorationCount(); i++) { - topForumsView.removeItemDecorationAt(i); - } - int dp16 = DisplayUtil.dp2px(getContext(), 16); - int dp8 = DisplayUtil.dp2px(getContext(), 8); - topForumsView.addItemDecoration(new HorizontalSpacesDecoration(dp8, dp8, dp16, dp16)); - break; - case TYPE_NORMAL_FORUM: - int realPosition = position - getTopForumItemCount() - 1; - int padding = DisplayUtil.dp2px(getContext(), 18); - int dp12 = DisplayUtil.dp2px(getContext(), 12); - if (getSpanCount() == 1) { - holder.itemView.setPaddingRelative(padding, dp12, padding, dp12); - } else if (realPosition % getSpanCount() == 0) { - holder.itemView.setPaddingRelative(padding, dp12, (int) (padding / 1.5), dp12); - } else { - holder.itemView.setPaddingRelative((int) (padding / 1.5), dp12, padding, dp12); - } - if (realPosition >= 0) { - ForumRecommend.LikeForum likeForum = mLikeForums.get(realPosition); - holder.setItemOnClickListener(v -> { - if (getOnItemClickListener() != null) { - getOnItemClickListener().onClick(holder.itemView, likeForum, position, viewType); - } - }); - holder.setItemOnLongClickListener(v -> { - if (getOnItemLongClickListener() != null) { - return getOnItemLongClickListener().onLongClick(holder.itemView, likeForum, position, viewType); - } - return false; - }); - ImageView avatarView = holder.getView(R.id.forum_list_item_avatar); - if (isSingle()) { - avatarView.setVisibility(View.VISIBLE); - ImageUtil.load(avatarView, ImageUtil.LOAD_TYPE_AVATAR, likeForum.getAvatar()); - } else { - avatarView.setVisibility(View.GONE); - Glide.with(getContext()) - .clear(avatarView); - } - ThemeUtil.setChipThemeByLevel(likeForum.getLevelId(), - holder.getView(R.id.forum_list_item_status), - holder.getView(R.id.forum_list_item_level), - holder.getView(R.id.forum_list_item_sign_status)); - holder.setText(R.id.forum_list_item_name, likeForum.getForumName()); - holder.setText(R.id.forum_list_item_level, likeForum.getLevelId()); - holder.setVisibility(R.id.forum_list_item_sign_status, "1".equals(likeForum.isSign()) ? View.VISIBLE : View.GONE); - holder.getView(R.id.forum_list_item_status).setMinimumWidth(DisplayUtil.dp2px(getContext(), "1".equals(likeForum.isSign()) ? 50 : 32)); - } - break; - } - } - - private boolean isTopForumItemTitle(int position) { - return position < getTopForumItemCount() && position == 0; - } - - private boolean isTopForumItem(int position) { - return position < getTopForumItemCount() && position > 0; - } - - private boolean isForumItemTitle(int position) { - return position == getTopForumItemCount(); - } - - private boolean isForumItem(int position) { - return position > getTopForumItemCount(); - } - - @Override - public int getItemViewType(int position) { - if (isTopForumItemTitle(position)) { - return TYPE_TOP_FORUM_TITLE; - } else if (isTopForumItem(position)) { - return TYPE_TOP_FORUM; - } else if (isForumItemTitle(position)) { - return TYPE_NORMAL_FORUM_TITLE; - } else if (isForumItem(position)) { - return TYPE_NORMAL_FORUM; - } - return TYPE_ERROR; - } - - @Override - public int getItemCount() { - return getTopForumItemCount() + getForumItemCount(); - } - - public int getTopForumItemCount() { - if (topForums.size() <= 0) { - return 0; - } - if (!isShowTop()) { - return 1; - } - return 2; - } - - public int getForumItemCount() { - if (mLikeForums.size() <= 0) { - return 0; - } - return mLikeForums.size() + 1; - } - - public void setData(List likeForums) { - if (likeForums == null) likeForums = new ArrayList<>(); - List normal = new ArrayList<>(); - this.topForums = new ArrayList<>(); - List topForums = LitePal.findAll(TopForum.class); - List topIdList = new ArrayList<>(); - List topList = new ArrayList<>(); - for (TopForum topForum : topForums) { - topIdList.add(topForum.getForumId()); - } - for (ForumRecommend.LikeForum likeForum : likeForums) { - if (topIdList.contains(likeForum.getForumId())) { - topList.add(likeForum); - if (SharedPreferencesUtil.get(getContext(), SharedPreferencesUtil.SP_SETTINGS).getBoolean("show_top_forum_in_normal_list", true)) { - normal.add(likeForum); - } - } else { - normal.add(likeForum); - } - } - this.topForums = topList; - this.mLikeForums = normal; - topForumsAdapter.setData(this.topForums); - notifyDataSetChanged(); - } - - public boolean isSingle() { - return single; - } - - public LikeForumListAdapter setSingle(boolean single) { - this.single = single; - notifyDataSetChanged(); - return this; - } -} diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/HttpConstant.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/HttpConstant.kt index c0b72ac9..c3c2e021 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/HttpConstant.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/HttpConstant.kt @@ -37,6 +37,8 @@ object Header { const val ADD_COOKIE = "add_cookie" const val ADD_COOKIE_FALSE = "false" + const val ACCEPT_LANGUAGE = "Accept-Language" + const val ACCEPT_LANGUAGE_VALUE = "zh-CN,zh;q=0.9" const val COOKIE = "cookie" const val HOST = "Host" const val ORIGIN = "Origin" diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt index 5e0d4ec0..9eab57c4 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/ITiebaApi.kt @@ -5,6 +5,7 @@ import com.huanchengfly.tieba.post.api.SearchThreadFilter import com.huanchengfly.tieba.post.api.SearchThreadOrder import com.huanchengfly.tieba.post.api.models.* import com.huanchengfly.tieba.post.api.models.web.ForumBean +import com.huanchengfly.tieba.post.api.models.web.ForumHome import com.huanchengfly.tieba.post.api.models.web.HotMessageListBean import com.huanchengfly.tieba.post.api.retrofit.ApiResult import com.huanchengfly.tieba.post.models.DislikeBean @@ -110,6 +111,11 @@ interface ITiebaApi { subPostId: String? ): Call + fun forumHomeAsync( + sortType: Int, + page: Int = 0 + ): Deferred> + /** * 查看用户关注的吧列表 * diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt index 2e76ead2..617254d4 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/interfaces/impls/MixedTiebaApiImpl.kt @@ -9,6 +9,7 @@ import com.huanchengfly.tieba.post.api.SearchThreadOrder import com.huanchengfly.tieba.post.api.interfaces.ITiebaApi import com.huanchengfly.tieba.post.api.models.* import com.huanchengfly.tieba.post.api.models.web.ForumBean +import com.huanchengfly.tieba.post.api.models.web.ForumHome import com.huanchengfly.tieba.post.api.models.web.HotMessageListBean import com.huanchengfly.tieba.post.api.retrofit.ApiResult import com.huanchengfly.tieba.post.api.retrofit.RetrofitTiebaApi @@ -68,6 +69,17 @@ object MixedTiebaApiImpl : ITiebaApi { ): Call = RetrofitTiebaApi.MINI_TIEBA_API.floor(threadId, page, postId, subPostId) + override fun forumHomeAsync(sortType: Int, page: Int): Deferred> { + return RetrofitTiebaApi.WEB_TIEBA_API.getForumHomeAsync( + sortType, + page, + 20, + "", + "", + AccountUtil.getCookie(BaseApplication.instance) + ) + } + override fun userLikeForum( uid: String, page: Int ): Call { diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/models/web/ForumHome.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/models/web/ForumHome.kt new file mode 100644 index 00000000..9057d649 --- /dev/null +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/models/web/ForumHome.kt @@ -0,0 +1,34 @@ +package com.huanchengfly.tieba.post.api.models.web + +import com.google.gson.annotations.SerializedName + +class ForumHome : WebBase() + +data class ForumHomeData( + @SerializedName("like_forum") + val likeForum: LikeForum +) { + data class LikeForum( + val list: List, + val page: Page + ) { + data class ListItem( + val avatar: String, + @SerializedName("forum_id") + val forumId: Long, + @SerializedName("forum_name") + val forumName: String, + @SerializedName("hot_num") + val hotNum: Long, + @SerializedName("is_brand_forum") + val isBrandForum: Int, + @SerializedName("level_id") + val levelId: Int + ) + + data class Page( + val currentPage: Int, + val totalPage: Int + ) + } +} diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/models/web/WebBase.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/models/web/WebBase.kt new file mode 100644 index 00000000..e98da85e --- /dev/null +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/models/web/WebBase.kt @@ -0,0 +1,12 @@ +package com.huanchengfly.tieba.post.api.models.web + +import com.google.gson.annotations.SerializedName + +open class WebBase { + @SerializedName("errno", alternate = ["no"]) + val errorCode: Int = -1 + + @SerializedName("errmsg", alternate = ["error"]) + val errorMsg: String = "" + val data: Data? = null +} diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/RetrofitTiebaApi.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/RetrofitTiebaApi.kt index a4abb460..3910184d 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/RetrofitTiebaApi.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/RetrofitTiebaApi.kt @@ -1,7 +1,6 @@ package com.huanchengfly.tieba.post.api.retrofit import android.os.Build -import android.webkit.WebSettings import com.huanchengfly.tieba.post.BaseApplication import com.huanchengfly.tieba.post.api.Header import com.huanchengfly.tieba.post.api.Param @@ -66,8 +65,9 @@ object RetrofitTiebaApi { val WEB_TIEBA_API: WebTiebaApi by lazy { createAPI("https://tieba.baidu.com/", CommonHeaderInterceptor( + Header.ACCEPT_LANGUAGE to { Header.ACCEPT_LANGUAGE_VALUE }, Header.HOST to { "tieba.baidu.com" }, - Header.USER_AGENT to { WebSettings.getDefaultUserAgent(BaseApplication.instance) } + Header.USER_AGENT to { "Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.5060.0 Mobile Safari/537.36 Edg/103.0.1264.2" } ), AddCookieInterceptor) } diff --git a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interfaces/WebTiebaApi.kt b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interfaces/WebTiebaApi.kt index 193351c9..25c57c5b 100644 --- a/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interfaces/WebTiebaApi.kt +++ b/app/src/main/java/com/huanchengfly/tieba/post/api/retrofit/interfaces/WebTiebaApi.kt @@ -28,6 +28,23 @@ interface WebTiebaApi { @Url url: String ): Call + @Headers( + "${Header.FORCE_LOGIN}: ${Header.FORCE_LOGIN_TRUE}", + "${Header.REFERER}: https://tieba.baidu.com/index/tbwise/forum?source=index", + "sec-ch-ua: \".Not/A)Brand\";v=\"99\", \"Microsoft Edge\";v=\"103\", \"Chromium\";v=\"103\"", + "sec-ch-ua-mobile: ?1", + "sec-ch-ua-platform: Android" + ) + @GET("/mg/o/getForumHome") + fun getForumHomeAsync( + @Query("st") sortType: Int, + @Query("pn") page: Int, + @Query("rn") pageSize: Int, + @Query("eqid") eqid: String, + @Query("refer") refer: String, + @retrofit2.http.Header("Cookie") cookie: String? + ): Deferred> + @GET("/mo/q/hotMessage/main") fun hotTopicMain( @Query("topic_id") topicId: String,