lmw
2024-06-18 1f45a54dc8e149548d3a61d1228741627aa4f23e
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package com.dollearn.student.ui.worldcup
 
import android.os.Bundle
import android.view.View
import android.widget.TextView
import androidx.core.os.bundleOf
import androidx.fragment.app.Fragment
import androidx.viewpager.widget.ViewPager
import cn.sinata.xldutils.activity.BaseActivity
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.SpanBuilder
import cn.sinata.xldutils.utils.showAllowingStateLoss
import cn.sinata.xldutils.utils.toTime
import cn.sinata.xldutils.visible
import com.github.zackratos.ultimatebar.UltimateBar
import com.dollearn.student.R
import com.dollearn.student.dialog.CheckQrCodeDialog
import com.dollearn.student.dialog.SwitchStudentDialog
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.Student
import com.dollearn.student.network.entity.WorldRank
import com.dollearn.student.network.request
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.event.EmptyEvent
import com.dollearn.student.utils.extention.clickDelay
import com.dollearn.student.utils.getQRcodeImg
import kotlinx.android.synthetic.main.activity_my_world_cup.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.find
import org.jetbrains.anko.textColorResource
 
class MyWorldActivity : BaseActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my_world_cup)
        initView()
        initClick()
    }
 
    val rbCity by lazy {
        find<TextView>(R.id.rb_city)
    }
    val rbCountry by lazy {
        find<TextView>(R.id.rb_country)
    }
 
    private val index by lazy { intent.getIntExtra("index",0) }
    var stu:Student? = null
    var cityName = ""
 
    var mineRank: WorldRank? = null //我的排名
 
 
    fun initClick() {
        iv_code.clickDelay {
            if (stu == null)
                return@clickDelay
            val dialog = CheckQrCodeDialog()
            val content = "{\"id\":${stu?.id},\"isStudent\":${stu?.isStudent}}"
            dialog.arguments = bundleOf("qrinfo" to content,"title" to stu?.name)
            dialog.showAllowingStateLoss(supportFragmentManager, "checkqr")
        }
        tv_change.clickDelay {
            SwitchStudentDialog.show(supportFragmentManager,stu,object :SwitchStudentDialog.ResultCallback{
                override fun checked(list: List<Student>) {
                    if (!list.isNullOrEmpty()){
                        stu = list.firstOrNull()
                        stu?.apply {
                            iv_avatar.setImageURI(avatar)
                            tv_name.text = name
                            val content = "{\"id\":${id},\"isStudent\":${isStudent}}"
                            iv_code.setImageBitmap(getQRcodeImg(this@MyWorldActivity, content))
                            getRank()
                        }
                    }
                }
            })
        }
    }
 
    fun initView() {
        titleBar.titleView.text = title
        titleBar.titleView.textColorResource = R.color.white
        titleBar.leftView.setCompoundDrawablesWithIntrinsicBounds(R.mipmap.back_white,0,0,0)
        UltimateBar.with(this)
            .statusDark(false)
            .create().transparentBar() //沉浸状态栏
        tv_year.text = "您的${System.currentTimeMillis().toTime("yyyy")}年场次排名"
        rb_city.isSelected = true
        getUser()
    }
 
    private fun initTab(){
        val frags = arrayListOf<Fragment>()
        frags.add(MyWorldFragment.newInstance(MyWorldFragment.TYPE_DOING))
        frags.add(MyWorldFragment.newInstance(MyWorldFragment.TYPE_UN_START))
        frags.add(WorldRecordFragment())
        frags.add(WorldRankFragment())
        tab_bar.setViewPager(view_pager, arrayOf("进行中","未开始","比赛记录","排名"),this,frags)
        view_pager.currentItem = index
        view_pager.addOnPageChangeListener(object :ViewPager.OnPageChangeListener{
            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {
 
            }
 
            override fun onPageSelected(position: Int) {
                rg_region.visibility = if (position == 3) View.VISIBLE else View.GONE
                if (position == 3) showMineRank() else bg.gone()
            }
 
            override fun onPageScrollStateChanged(state: Int) {
 
            }
        })
    }
 
    fun showMineRank(){
        bg.visibility = if (mineRank!=null){
            when(mineRank!!.rank){
                1->{
                    tv_rank.gone()
                    iv_rank.visible()
                    iv_rank.setImageResource(R.mipmap.goldmedal)
                }
                2->{
                    tv_rank.gone()
                    iv_rank.visible()
                    iv_rank.setImageResource(R.mipmap.silvermedal)
                }
                3->{
                    tv_rank.gone()
                    iv_rank.visible()
                    iv_rank.setImageResource(R.mipmap.bronzemedal)
                }
                else->{
                    tv_rank.visible()
                    iv_rank.gone()
                    tv_rank.text = mineRank!!.rank.toString()
                }
            }
            iv_mine_avatar.setImageURI(mineRank!!.avatar)
            tv_mine_name.text = mineRank!!.name
            tv_count.text = mineRank!!.totalSession.toString()
            tv_rate.text = "%.2f".format(mineRank!!.winRate)+"%"
            View.VISIBLE
        }else View.GONE
 
    }
 
    private fun getUser() {
        HttpManager.getParticipant().request(this){ _, data->
            data?.firstOrNull()?.apply {
                stu = this
                initTab()
                iv_avatar.setImageURI(avatar)
                tv_name.text = name
                val content = "{\"id\":${id},\"isStudent\":${isStudent}}"
                iv_code.setImageBitmap(getQRcodeImg(this@MyWorldActivity, content))
                getRank()
            }
        }
    }
 
    private fun getRank(){
        HttpManager.getEntrantRank(stu?.id?:"",stu?.isStudent?:0).request(this){_,data->
            data?.apply {
                this@MyWorldActivity.cityName = cityName
                rb_city.text = cityName
                tv_rank_country.text = nationalRank.toString()
                tv_rank_city.text = cityRank.toString()
                tv_city.text = cityName+"排名"
                tv_win.text = win
                tv_lose.text = lose
                val s = "%.2f".format(winRate)+"%"
                tv_win_rate.text = SpanBuilder(s).size(s.length-1,s.length,15).build()
            }
        }
    }
 
}