完善 市场界面按钮显示

This commit is contained in:
hyb1996
2019-01-28 10:57:19 +08:00
parent f2c636d599
commit 402a78c354
11 changed files with 223 additions and 33 deletions

View File

@@ -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<Topic> {
return getTopics(CID_SCRIPTS)
}

View File

@@ -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<Category>
@GET("/api/topic/{tid}")
fun getTopic(@Path("tid") cid: Long): Deferred<Topic>
fun getTopic(@Path("tid") pid: Long): Deferred<Topic>
}

View File

@@ -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")

View File

@@ -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{" +

View File

@@ -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)
}
}

View File

@@ -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)
}
}