lmw
2023-05-12 f67802a41f9e01444d1115f34ecc6e1beb05fc3b
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
108
109
110
package com.fuban.user.ui.mine
 
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.view.SwipeRefreshRecyclerLayout
import com.fuban.user.R
import com.fuban.user.interfaces.OnDelCallback
import com.fuban.user.network.HttpManager
import com.fuban.user.network.entity.Message
import com.fuban.user.network.requestByF
import com.fuban.user.ui.H5Activity
import com.fuban.user.dialog.TipDialog
import com.fuban.user.ui.mine.adapter.MsgAdapter
import org.jetbrains.anko.support.v4.find
import org.jetbrains.anko.support.v4.startActivity
 
class MsgFragment:BaseFragment(){
    override fun contentViewId() = R.layout.base_recyclerview_layout
 
    private val type by lazy {
        arguments!!.getInt("type", TYPE_MSG)
    }
    private val datas = arrayListOf<Message>()
    private val adapter by lazy {
        MsgAdapter(datas,type,object :OnDelCallback{
            override fun onDel(position: Int) {
                val tipDialog = TipDialog()
                tipDialog.arguments = bundleOf("msg" to "是否确认删除该消息?")
                tipDialog.setCallback(object :TipDialog.OnClickCallback{
                    override fun onOk() {
                        HttpManager.delSystemNotice(datas[position].id).requestByF(this@MsgFragment){_,_->
                            swipeRefreshRecyclerLayout.isRefreshing = true
                            page = 1
                            getData()
                        }
                    }
 
                    override fun onCancel() {
                    }
                })
                tipDialog.show(fragmentManager!!,"del")
            }
        })
    }
    private val swipeRefreshRecyclerLayout by lazy {
        find<SwipeRefreshRecyclerLayout>(R.id.swipeRefreshLayout)
    }
    private var page = 1
 
    override fun onFirstVisibleToUser() {
        swipeRefreshRecyclerLayout.setLayoutManager(LinearLayoutManager(activity))
        swipeRefreshRecyclerLayout.setAdapter(adapter)
        swipeRefreshRecyclerLayout.setOnRefreshListener(object :SwipeRefreshRecyclerLayout.OnRefreshListener{
            override fun onRefresh() {
                page = 1
                getData()
            }
 
            override fun onLoadMore() {
                page++
                getData()
            }
        })
            adapter.setOnItemClickListener { _, position ->
                if (type == TYPE_NOTICE){
                    HttpManager.readSystemNotice(datas[position].id).requestByF(this,showToast = false){_,_->}
                    startActivity<H5Activity>("title" to "公告详情","url" to datas[position].content)
                } else if (datas[position].noticeType == 2)
                    startActivity<MyCouponActivity>()
 
            }
 
        getData()
    }
 
    private fun getData(){
        HttpManager.msgNotice(type,page).requestByF(this,success = {_,data->
            swipeRefreshRecyclerLayout.isRefreshing = false
            data?.let {
                if (page == 1)
                    datas.clear()
                datas.addAll(it)
                if (datas.isEmpty())
                    swipeRefreshRecyclerLayout.setLoadMoreText("暂无数据")
                else if (it.isEmpty())
                    swipeRefreshRecyclerLayout.setLoadMoreText("没有更多")
                adapter.notifyDataSetChanged()
            }
        },error = {_,_->
            swipeRefreshRecyclerLayout.isRefreshing = false
        })
    }
 
    fun clear(){
        datas.clear()
        adapter.notifyDataSetChanged()
        swipeRefreshRecyclerLayout.setLoadMoreText("暂无数据")
    }
 
    companion object{
        const val TYPE_NOTICE = 1 //公告
        const val TYPE_MSG = 2 //系统消息
        fun newInstance(type:Int):MsgFragment {
            val msgFragment = MsgFragment()
            msgFragment.arguments = bundleOf("type" to type)
            return msgFragment
        }
    }
}