lmw
2024-06-17 f571288a24fcf10143dcc8015ffbbf38dbc0c614
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
package com.dollearn.student
 
import android.annotation.SuppressLint
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.showAllowingStateLoss
import com.flyco.tablayout.listener.CustomTabEntity
import com.flyco.tablayout.listener.OnTabSelectListener
import com.github.zackratos.ultimatebar.UltimateBar
import com.dollearn.student.dialog.CompetitionTipDialog
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.UserBean
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.ui.home.HomeFragment
import com.dollearn.student.ui.shop.ShopFragment
import com.dollearn.student.ui.mine.MineFragment
import com.dollearn.student.utils.Const
import com.dollearn.student.utils.event.EmptyEvent
import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.fragment_shop.*
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
 
class MainActivity : TransparentStatusBarActivity(), OnTabSelectListener{
    override fun setContentView() = R.layout.activity_main
 
    private val fragments    = arrayListOf<Fragment>()
    var homeData: UserBean? = null //个人信息
    var score = 0
 
    override fun initClick() {
    }
 
    override fun initView() {
        titleBar.gone()
        bg_title.gone()
        UltimateBar.with(this@MainActivity)
            .statusDark(true)
            .create().immersionBar() //沉浸状态栏
        initTab()
        EventBus.getDefault().register(this)
        getData()
    }
 
    private fun initTab() {
        val titles = arrayListOf("首页","商城","我的")
        val iconChecked = arrayListOf(
            R.mipmap.home_selected,
            R.mipmap.shangcheng_selected,
            R.mipmap.mine_selected
        )
        val iconUnchecked = arrayListOf(
            R.mipmap.home,
            R.mipmap.shangcheng,
            R.mipmap.mine
        )
        fragments.add(HomeFragment())
        fragments.add(ShopFragment())
        fragments.add(MineFragment())
        view_pager.offscreenPageLimit = fragments.size
        view_pager.adapter = @SuppressLint("WrongConstant")
        object : FragmentPagerAdapter(supportFragmentManager,0) {
            override fun getItem(p0: Int): Fragment {
                return fragments[p0]
            }
 
            override fun getCount(): Int {
                return fragments.size
            }
        }
        val tabs = arrayListOf<CustomTabEntity>()
        (0 until fragments.size).forEach {
            tabs.add(object : CustomTabEntity {
                override fun getTabUnselectedIcon() = iconUnchecked[it]
 
                override fun getTabSelectedIcon() = iconChecked[it]
 
                override fun getTabTitle() = titles[it]
            })
        }
        tab_bar.setTabData(tabs)
        tab_bar.setOnTabSelectListener(this)
    }
 
    fun switchHome(){
        tab_bar.currentTab = 0
        onTabSelect(0)
    }
    override fun onTabSelect(position: Int) {
        view_pager.currentItem = position
    }
 
    override fun onTabReselect(position: Int) {
    }
 
    @Subscribe
    fun onSwitch(e:EmptyEvent){
        if (e.code == Const.EventCode.SWITCH_HOME){
            switchHome()
        }else if(e.code == Const.EventCode.CHANGE_WELFARE){
            tab_bar.currentTab = 2
            onTabSelect(2)
        }else if(e.code == Const.EventCode.CHANGE_EXPLORE){
            tab_bar.currentTab = 3
            onTabSelect(3)
        }
    }
 
    @Subscribe
    fun refreshUser(e:EmptyEvent){
        if (e.code == Const.EventCode.REFRESH_USER)
            getData()
        if (e.code == Const.EventCode.SCORE_CHANGED)
            getScore()
    }
 
    private fun getData() {
        HttpManager.userDetails().request(this){_,data->
            homeData = data
            EventBus.getDefault().post(EmptyEvent(Const.EventCode.REFRESH_USER_INFO))
            if (score == 0){
                score = homeData?.user?.integral?:0
                EventBus.getDefault().post(EmptyEvent(Const.EventCode.REFRESH_SCORE))
            }
        }
    }
 
    private fun getScore() {
        HttpManager.getIntegralStudy().request(this){_,data->
            score = data?:0
            homeData?.user?.integral = score
            EventBus.getDefault().post(EmptyEvent(Const.EventCode.REFRESH_SCORE))
            EventBus.getDefault().post(EmptyEvent(Const.EventCode.REFRESH_USER_INFO))
        }
    }
 
    override fun onResume() {
        super.onResume()
        if (tab_bar.currentTab == 1)
            EventBus.getDefault().post(EmptyEvent(Const.EventCode.CHANGE_COURSDATA))
        checkCompetition()
    }
 
    private fun checkCompetition() {
        HttpManager.getCompletedWorldCupTips().request(this,false){_,data->
            if (data?:0!=0){
                val competitionTipDialog = CompetitionTipDialog()
                competitionTipDialog.showAllowingStateLoss(supportFragmentManager,"ct")
            }
        }
    }
 
    override fun onDestroy() {
        super.onDestroy()
        EventBus.getDefault().unregister(this)
        WeparkApplication.storeId = ""
    }
}