lmw
2024-06-16 03172fc2d9a7717f4a9d8de1c5eca3158a550b30
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
package com.dollearn.student.ui.home
 
import android.app.Activity
import android.content.Intent
import androidx.core.os.bundleOf
import androidx.recyclerview.widget.LinearLayoutManager
import cn.sinata.xldutils.fragment.BaseFragment
import com.dollearn.student.R
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.Match
import com.dollearn.student.network.requestByF
import com.dollearn.student.ui.home.adapter.MatchAdapter
import kotlinx.android.synthetic.main.layout_common_list.*
import org.jetbrains.anko.support.v4.startActivityForResult
 
class MyMatchFragment:BaseFragment() {
    override fun contentViewId() = R.layout.layout_common_list
 
    private val type by lazy { arguments?.getInt("type", TYPE_ALL)?: TYPE_ALL }
    private var page = 1
    private val list = arrayListOf<Match>()
    private val adapter  = MatchAdapter(list,true)
 
    override fun onFirstVisibleToUser() {
        rv_list.layoutManager = LinearLayoutManager(requireContext())
        rv_list.adapter = adapter
        refreshLayout.setOnRefreshListener {
            page = 1
            getData()
        }
        refreshLayout.setOnLoadMoreListener {
            page++
            getData()
        }
        getData()
        adapter.setOnItemClickListener { view, position ->
            startActivityForResult<MyMatchDetailActivity>(1,"id" to list[position].id)
        }
    }
 
    private fun getData(){
        HttpManager.queryMyCompetitionList(type, page).requestByF(this,success = {_,data->
            if (page == 1)
                list.clear()
            list.addAll(data?: arrayListOf())
            adapter.notifyDataSetChanged()
            if (list.isEmpty())
                refreshLayout.finishRefreshWithNoMoreData()
            else if (data.isNullOrEmpty())
                refreshLayout.finishLoadMoreWithNoMoreData()
            else if (page == 1)
                refreshLayout.finishRefresh()
            else
                refreshLayout.finishLoadMore()
        }){_,_->
            if (page == 1)
                refreshLayout.finishRefresh(false)
            else
                refreshLayout.finishLoadMore(false)
            page--
        }
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == Activity.RESULT_OK)
            refreshLayout.autoRefresh()
    }
 
    companion object{
        const val TYPE_ALL = 0
        const val TYPE_DOING = 2
        const val TYPE_FINISHED = 3
        const val TYPE_UN_START = 1
        const val TYPE_CANCELED = 4
        fun newInstance(type:Int):MyMatchFragment{
            val myMatchFragment = MyMatchFragment()
            myMatchFragment.arguments = bundleOf("type" to type)
            return myMatchFragment
        }
    }
}