package com.kuanzhai.user.ui.mine
|
|
import androidx.core.os.bundleOf
|
import androidx.recyclerview.widget.LinearLayoutManager
|
import cn.sinata.xldutils.fragment.BaseFragment
|
import cn.sinata.xldutils.gone
|
import cn.sinata.xldutils.view.SwipeRefreshRecyclerLayout
|
import cn.sinata.xldutils.visible
|
import com.kuanzhai.user.KuanzhaiApplication
|
import com.kuanzhai.user.R
|
import com.kuanzhai.user.interfaces.OnDelCallback
|
import com.kuanzhai.user.network.HttpManager
|
import com.kuanzhai.user.network.requestByF
|
import com.kuanzhai.user.network.entity.BusinessCoupon
|
import com.kuanzhai.user.ui.mine.adapter.BusinessCouponAdapter
|
import kotlinx.android.synthetic.main.fragment_recycler_with_empty.*
|
import org.jetbrains.anko.support.v4.find
|
import org.jetbrains.anko.support.v4.startActivity
|
|
class BusinessCouponFragment : BaseFragment() {
|
private val type by lazy {
|
arguments?.getInt("type", TYPE_USEFUL)?: TYPE_USEFUL
|
}
|
private val swipeRefreshLayout by lazy {
|
find<SwipeRefreshRecyclerLayout>(R.id.swipeRefreshLayout)
|
}
|
private val datas = arrayListOf<BusinessCoupon>()
|
private val adapter by lazy {
|
BusinessCouponAdapter(datas,type,object : OnDelCallback {
|
override fun onDel(position: Int) {
|
startActivity<CouponCheckListActivity>("id" to datas[position].id,"activityId" to datas[position].activityId)
|
}
|
})
|
}
|
private var page = 1
|
override fun contentViewId() = R.layout.fragment_recycler_with_empty
|
|
override fun onFirstVisibleToUser() {
|
swipeRefreshLayout.setLayoutManager(LinearLayoutManager(activity))
|
swipeRefreshLayout.setAdapter(adapter)
|
swipeRefreshLayout.setOnRefreshListener(object :SwipeRefreshRecyclerLayout.OnRefreshListener{
|
override fun onRefresh() {
|
page = 1
|
getData()
|
}
|
|
override fun onLoadMore() {
|
page++
|
getData()
|
}
|
})
|
swipeRefreshLayout.isRefreshing = true
|
tv_empty.text = "您还没有优惠券信息\n您可以和平台联系,建立合作哦~\n平台电话:${KuanzhaiApplication.phoneService}"
|
getData()
|
}
|
|
fun refresh(){
|
swipeRefreshLayout.isRefreshing = true
|
page = 1
|
getData()
|
}
|
|
private fun getData(){
|
HttpManager.getMerchantCoupon(type, page).requestByF(this,success = {_,data->
|
swipeRefreshLayout.isRefreshing = false
|
data?.let {
|
if (page == 1)
|
datas.clear()
|
datas.addAll(it)
|
tv_empty.gone()
|
if (datas.isEmpty()){
|
swipeRefreshLayout.setLoadMoreText("")
|
tv_empty.visible()
|
} else if (it.isEmpty())
|
swipeRefreshLayout.setLoadMoreText("没有更多")
|
adapter.notifyDataSetChanged()
|
}
|
},error = {_,_->
|
swipeRefreshLayout.isRefreshing = false
|
})
|
}
|
|
companion object{
|
const val TYPE_USEFUL = 1
|
const val TYPE_DISABLE = 2
|
fun newInstance(type:Int):BusinessCouponFragment{
|
val couponFragment = BusinessCouponFragment()
|
couponFragment.arguments = bundleOf("type" to type)
|
return couponFragment
|
}
|
}
|
}
|