lmw
2023-06-07 f9dd2cdac746d308d5c4bcfdbea389ab67a66b12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package com.kuanzhai.user.ui.menu
 
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.clickDelay
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.view.SwipeRefreshRecyclerLayout
import com.kuanzhai.user.OkApplication
import com.kuanzhai.user.R
import com.kuanzhai.user.dialog.CommentDialog
import com.kuanzhai.user.interfaces.StringCallback
import com.kuanzhai.user.network.HttpManager
import com.kuanzhai.user.network.entity.CommentBean
import com.kuanzhai.user.network.requestByF
import com.kuanzhai.user.ui.menu.adapter.CommentAdapter
import kotlinx.android.synthetic.main.fragment_comment.*
 
class CommentFragment:BaseFragment() {
    override fun contentViewId() = R.layout.fragment_comment
 
    private val id by lazy { arguments?.getLong("orderId")?:0L }
    private var commentId = 0L
    private val type by lazy { arguments?.getInt("type")?:0 }
    private val isReply by lazy { arguments?.getBoolean("isReply")?:false }
 
    private var page = 1
 
    private val list = arrayListOf<CommentBean>()
    private val adapter = CommentAdapter(list)
 
    override fun onFirstVisibleToUser() {
        commentId = arguments?.getLong("commentId")?:0L
        lv_comment.setLayoutManager(LinearLayoutManager(requireContext()))
        lv_comment.setAdapter(adapter)
        lv_comment.setOnRefreshListener(object :SwipeRefreshRecyclerLayout.OnRefreshListener{
            override fun onRefresh() {
                page = 1
                getData()
            }
 
            override fun onLoadMore() {
                page++
                getData()
            }
        })
        iv_avatar.setImageURI(OkApplication.avatar)
        tv_comment.clickDelay {
            val commentDialog = CommentDialog()
            commentDialog.arguments = bundleOf("id" to id,"type" to type)
            commentDialog.setCallback(object :StringCallback{
                override fun onRlt(rlt: String) {
                    page = 1
                    getData()
                }
            })
            commentDialog.show(childFragmentManager,"comment")
        }
        adapter.callback = object :CommentAdapter.ReplyCallback{
            override fun onComment(position: Int) {
                val commentDialog = CommentDialog()
                commentDialog.arguments = bundleOf("id" to id,"type" to type,"commentId" to list[position].id,"name" to list[position].userName
                    ,"replyUserId" to list[position].userId)
                commentDialog.setCallback(object :StringCallback{
                    override fun onRlt(rlt: String) {
                        page = 1
                        getData()
                    }
                })
                commentDialog.show(childFragmentManager,"comment")
            }
 
            override fun onReply(position: Int, subPosition: Int) {
                val commentDialog = CommentDialog()
                commentDialog.arguments = bundleOf("id" to id,"type" to type,"commentId" to list[position].replyCommentList[subPosition].id,
                    "name" to list[position].replyCommentList[subPosition].userName,"replyUserId" to list[position].replyCommentList[subPosition].userId)
                commentDialog.setCallback(object :StringCallback{
                    override fun onRlt(rlt: String) {
                        page = 1
                        getData()
                    }
                })
                commentDialog.show(childFragmentManager,"comment")
            }
        }
        getData()
    }
 
    private fun getData(){
        HttpManager.getCommentList(id,type,page).requestByF(this,success = {_,data->
            lv_comment.isRefreshing = false
            if (page == 1)
                list.clear()
            list.addAll(data?: emptyList())
            adapter.notifyDataSetChanged()
            if (commentId!=0L){ //首次进入跳转到对应评论
                val indexOf = data?.map { it.id }?.indexOf(commentId)?:0
                lv_comment.mRecyclerView.scrollToPosition(indexOf)
                (lv_comment.mRecyclerView.layoutManager as LinearLayoutManager).scrollToPositionWithOffset(indexOf,0)
                if (isReply)
                    adapter.showReplyList.add(commentId)
                commentId = 0L
            }
        }){_,_->
            lv_comment.isRefreshing = false
        }
    }
}