From 402a78c354fef7bebd34ea62b31d16d505e7ce66 Mon Sep 17 00:00:00 2001 From: hyb1996 <946994919@qq.com> Date: Mon, 28 Jan 2019 10:57:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=20=E5=B8=82=E5=9C=BA?= =?UTF-8?q?=E7=95=8C=E9=9D=A2=E6=8C=89=E9=92=AE=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/autojs/autojs/network/TopicService.kt | 7 ++ .../org/autojs/autojs/network/api/TopicApi.kt | 4 +- .../autojs/network/entity/topic/AppInfo.java | 2 + .../autojs/network/entity/topic/Topic.java | 17 +++ .../autojs/autojs/ui/main/market/ImageText.kt | 58 ++++++++++ .../autojs/ui/main/market/MarketFragment.kt | 106 ++++++++++++++---- app/src/main/res/layout/image_text.xml | 17 +++ app/src/main/res/layout/item_topic.xml | 31 +++-- app/src/main/res/values/attrs.xml | 6 + app/src/main/res/values/colors.xml | 3 +- app/src/main/res/values/strings.xml | 5 + 11 files changed, 223 insertions(+), 33 deletions(-) create mode 100644 app/src/main/java/org/autojs/autojs/ui/main/market/ImageText.kt create mode 100644 app/src/main/res/layout/image_text.xml diff --git a/app/src/main/java/org/autojs/autojs/network/TopicService.kt b/app/src/main/java/org/autojs/autojs/network/TopicService.kt index ea01b1f4..b5f960d6 100644 --- a/app/src/main/java/org/autojs/autojs/network/TopicService.kt +++ b/app/src/main/java/org/autojs/autojs/network/TopicService.kt @@ -1,6 +1,7 @@ package org.autojs.autojs.network import org.autojs.autojs.network.api.TopicApi +import org.autojs.autojs.network.entity.topic.Post import org.autojs.autojs.network.entity.topic.Topic object TopicService { @@ -16,6 +17,12 @@ object TopicService { } } + suspend fun getMainPost(topic: Topic): Post { + val fullTopic = mTopicApi.getTopic(topic.tid.toLong()).await() + topic.mainPost = fullTopic.posts.first { post -> post.pid == topic.mainPid } + return topic.mainPost + } + suspend fun getScriptsTopics(): List { return getTopics(CID_SCRIPTS) } diff --git a/app/src/main/java/org/autojs/autojs/network/api/TopicApi.kt b/app/src/main/java/org/autojs/autojs/network/api/TopicApi.kt index a98a1b75..78784854 100644 --- a/app/src/main/java/org/autojs/autojs/network/api/TopicApi.kt +++ b/app/src/main/java/org/autojs/autojs/network/api/TopicApi.kt @@ -2,6 +2,7 @@ package org.autojs.autojs.network.api import kotlinx.coroutines.Deferred import org.autojs.autojs.network.entity.topic.Category +import org.autojs.autojs.network.entity.topic.Post import org.autojs.autojs.network.entity.topic.Topic import retrofit2.http.GET import retrofit2.http.Path @@ -12,6 +13,7 @@ interface TopicApi { fun getCategory(@Path("cid") cid: Long): Deferred @GET("/api/topic/{tid}") - fun getTopic(@Path("tid") cid: Long): Deferred + fun getTopic(@Path("tid") pid: Long): Deferred + } \ No newline at end of file diff --git a/app/src/main/java/org/autojs/autojs/network/entity/topic/AppInfo.java b/app/src/main/java/org/autojs/autojs/network/entity/topic/AppInfo.java index 30ad856c..64f6957c 100644 --- a/app/src/main/java/org/autojs/autojs/network/entity/topic/AppInfo.java +++ b/app/src/main/java/org/autojs/autojs/network/entity/topic/AppInfo.java @@ -8,6 +8,8 @@ import com.google.gson.annotations.SerializedName; @SuppressWarnings("unused") public class AppInfo { + public static final String PERMISSION_ROOT = "root"; + @SerializedName("details") private String mDetails; @SerializedName("email") diff --git a/app/src/main/java/org/autojs/autojs/network/entity/topic/Topic.java b/app/src/main/java/org/autojs/autojs/network/entity/topic/Topic.java index 49b78df6..d4588708 100644 --- a/app/src/main/java/org/autojs/autojs/network/entity/topic/Topic.java +++ b/app/src/main/java/org/autojs/autojs/network/entity/topic/Topic.java @@ -80,6 +80,7 @@ public class Topic { private Post mTeaser; private AppInfo mAppInfo; + private Post mMainPost; public Category getCategory() { return mCategory; @@ -339,6 +340,22 @@ public class Topic { } } + public Post getTeaser() { + return mTeaser; + } + + public void setTeaser(Post teaser) { + mTeaser = teaser; + } + + public Post getMainPost() { + return mMainPost; + } + + public void setMainPost(Post mainPost) { + mMainPost = mainPost; + } + @Override public String toString() { return "Topic{" + diff --git a/app/src/main/java/org/autojs/autojs/ui/main/market/ImageText.kt b/app/src/main/java/org/autojs/autojs/ui/main/market/ImageText.kt new file mode 100644 index 00000000..62ee83fe --- /dev/null +++ b/app/src/main/java/org/autojs/autojs/ui/main/market/ImageText.kt @@ -0,0 +1,58 @@ +package org.autojs.autojs.ui.main.market + +import android.content.Context +import android.util.AttributeSet +import android.view.Gravity +import android.view.View +import android.widget.LinearLayout +import kotlinx.android.synthetic.main.image_text.view.* +import org.autojs.autojs.R + +class ImageText : LinearLayout { + + var text: CharSequence? + get() = textView.text + set(value) { + textView.text = value + } + + constructor(context: Context?) : super(context) { + init(null) + } + + constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) { + init(attrs) + } + + constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) { + init(attrs) + } + + private fun init(attrs: AttributeSet?) { + View.inflate(context, R.layout.image_text, this) + gravity = Gravity.CENTER + orientation = HORIZONTAL + if (attrs == null) { + return + } + val a = context.obtainStyledAttributes(attrs, R.styleable.ImageText) + a.getString(R.styleable.ImageText_text)?.let { + textView.text = it + } + val iconResId = a.getResourceId(R.styleable.ImageText_src, 0) + if (iconResId != 0) { + imageView.setImageResource(iconResId) + } + val imageWidth = a.getDimensionPixelSize(R.styleable.ImageText_image_width, 0) + if (imageWidth != 0) { + imageView.layoutParams.width = imageWidth + } + a.recycle() + } + + fun setColor(color: Int) { + textView.setTextColor(color) + imageView.setColorFilter(color) + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/autojs/autojs/ui/main/market/MarketFragment.kt b/app/src/main/java/org/autojs/autojs/ui/main/market/MarketFragment.kt index fdde2062..0e137d6c 100644 --- a/app/src/main/java/org/autojs/autojs/ui/main/market/MarketFragment.kt +++ b/app/src/main/java/org/autojs/autojs/ui/main/market/MarketFragment.kt @@ -6,6 +6,7 @@ import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView +import androidx.core.content.ContextCompat import androidx.recyclerview.widget.RecyclerView import com.google.android.material.floatingactionbutton.FloatingActionButton import com.stardust.autojs.workground.WrapContentLinearLayoutManager @@ -15,6 +16,8 @@ import kotlinx.coroutines.GlobalScope import kotlinx.coroutines.launch import org.autojs.autojs.R import org.autojs.autojs.network.TopicService +import org.autojs.autojs.network.entity.topic.AppInfo +import org.autojs.autojs.network.entity.topic.Post import org.autojs.autojs.network.entity.topic.Topic import org.autojs.autojs.ui.main.ViewPagerFragment import org.autojs.autojs.ui.widget.AvatarView @@ -59,11 +62,89 @@ class MarketFragment : ViewPagerFragment(0) { inner class TopicViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) { - val rootView: TextView = itemView.findViewById(R.id.root) - val titleView: TextView = itemView.findViewById(R.id.title) - val avatarView: AvatarView = itemView.findViewById(R.id.avatar) - val usernameView: TextView = itemView.findViewById(R.id.username) - val dateView: TextView = itemView.findViewById(R.id.date) + private lateinit var topic: Topic + + private val rootView: TextView = itemView.findViewById(R.id.root) + private val titleView: TextView = itemView.findViewById(R.id.title) + private val avatarView: AvatarView = itemView.findViewById(R.id.avatar) + private val usernameView: TextView = itemView.findViewById(R.id.username) + private val dateView: TextView = itemView.findViewById(R.id.date) + private val upvoteView: ImageText = itemView.findViewById(R.id.upvote) + private val downvoteView: ImageText = itemView.findViewById(R.id.downvote) + private val downloadView: ImageText = itemView.findViewById(R.id.download) + private val starView: ImageText = itemView.findViewById(R.id.star) + + init { + upvoteView.setOnClickListener { + + } + downvoteView.setOnClickListener { + + } + starView.setOnClickListener { + + } + downloadView.setOnClickListener { + + } + } + + fun bind(topic: Topic) { + this.topic = topic + rootView.setText(if (topic.appInfo.permissions.contains(AppInfo.PERMISSION_ROOT)) { + R.string.text_root + } else { + R.string.text_no_root + }) + titleView.text = topic.title + avatarView.setUser(topic.user) + usernameView.text = topic.user.username + dateView.text = DateTimeFormat.mediumDateTime().print(topic.timestamp.toLong()) + if (topic.mainPost != null) { + bindMainPost(topic.mainPost) + } else { + fetchMainPost(topic) + } + } + + private fun fetchMainPost(topic: Topic) { + GlobalScope.launch(Dispatchers.Main) { + try { + val mainPost = TopicService.getMainPost(topic) + if (topic === this@TopicViewHolder.topic) { + bindMainPost(mainPost) + } + } catch (e: Exception) { + e.printStackTrace() + } + } + } + + private fun bindMainPost(mainPost: Post) { + context?.let { context -> + upvoteView.text = if (mainPost.upvotes == 0L) { + context.getString(R.string.text_upvote) + } else { + mainPost.upvotes.toString() + } + upvoteView.setColor(if (mainPost.upvoted) { + ContextCompat.getColor(context, R.color.market_button_selected) + } else { + ContextCompat.getColor(context, R.color.market_button_unselected) + }) + downvoteView.text = if (mainPost.downvotes == 0L) { + context.getString(R.string.text_downvote) + } else { + mainPost.downvotes.toString() + } + downvoteView.setColor(if (mainPost.downvoted) { + ContextCompat.getColor(context, R.color.market_button_selected) + } else { + ContextCompat.getColor(context, R.color.market_button_unselected) + }) + } + + } } @@ -77,21 +158,8 @@ class MarketFragment : ViewPagerFragment(0) { } override fun onBindViewHolder(holder: TopicViewHolder, position: Int) { - val topic = mTopics[position] - holder.run { - val root = topic.appInfo.permissions.contains("root") - rootView.text = if (root) { - "Root" - } else { - "免Root" - } - titleView.text = topic.title - avatarView.setUser(topic.user) - usernameView.text = topic.user.username - - dateView.text = DateTimeFormat.mediumDateTime().print(topic.timestamp.toLong()) - } + holder.bind(topic) } } diff --git a/app/src/main/res/layout/image_text.xml b/app/src/main/res/layout/image_text.xml new file mode 100644 index 00000000..8a7b5512 --- /dev/null +++ b/app/src/main/res/layout/image_text.xml @@ -0,0 +1,17 @@ + + + + + + + + \ No newline at end of file diff --git a/app/src/main/res/layout/item_topic.xml b/app/src/main/res/layout/item_topic.xml index c5723433..104da64a 100644 --- a/app/src/main/res/layout/item_topic.xml +++ b/app/src/main/res/layout/item_topic.xml @@ -93,37 +93,44 @@ android:paddingTop="3dp" android:paddingBottom="3dp"> - + android:background="?selectableItemBackgroundBorderless" + android:tint="#939393" + app:image_width="30dp" + app:src="@drawable/download" + app:text="@string/text_download"/> - + android:background="?selectableItemBackgroundBorderless" + app:image_width="30dp" + app:src="@drawable/upvote"/> - + android:background="?selectableItemBackgroundBorderless" + app:image_width="30dp" + app:src="@drawable/downvote"/> - + android:background="?selectableItemBackgroundBorderless" + app:image_width="30dp" + app:src="@drawable/star" + app:text="@string/text_star"/> diff --git a/app/src/main/res/values/attrs.xml b/app/src/main/res/values/attrs.xml index 7b777c35..513f7a63 100644 --- a/app/src/main/res/values/attrs.xml +++ b/app/src/main/res/values/attrs.xml @@ -35,4 +35,10 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 47b14279..5c16ed8f 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -26,7 +26,8 @@ #77e0e0e0 #cc000000 #dfc0c0c0 - + #e14123 + #939393 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 99eb1d8c..477d2266 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -400,6 +400,11 @@ 查看使用统计权限 通过\"查看使用统计\"权限可以获取本设备过去使用应用的情况,从而使获取当前应用(currentPackage)时更准确。 打开方式 + Root + 免Root + 收藏 + + 描述至少要 %d 个字. 描述至少要 %d 个字.