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