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
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.Coupon
import com.fuban.user.network.requestByF
import com.fuban.user.dialog.TipDialog
import com.fuban.user.ui.mine.adapter.MyCouponAdapter
import org.jetbrains.anko.support.v4.find
 
class CouponFragment : 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<Coupon>()
    private val adapter by lazy {
        MyCouponAdapter(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.delMyCoupon(datas[position].id).requestByF(this@CouponFragment){_,_->
                            refresh()
                        }
                    }
 
                    override fun onCancel() {
                    }
                })
                tipDialog.show(fragmentManager!!,"del")
            }
        })
    }
    private var page = 1
    override fun contentViewId() = R.layout.base_recyclerview_layout
 
    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
        getData()
    }
 
    fun refresh(){
        swipeRefreshLayout.isRefreshing = true
        page = 1
        getData()
    }
 
    private fun getData(){
        HttpManager.queryMyCoupons(page,type).requestByF(this,success = {_,data->
            swipeRefreshLayout.isRefreshing = false
            data?.let {
                if (page == 1)
                    datas.clear()
                datas.addAll(it)
                if (datas.isEmpty())
                    swipeRefreshLayout.setLoadMoreText("暂无优惠券")
                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):CouponFragment{
            val couponFragment = CouponFragment()
            couponFragment.arguments = bundleOf("type" to type)
            return couponFragment
        }
    }
}