lmw
2024-08-01 a036ac0c6c8d87f36e68f88f05231c3be23fcc4b
app/src/main/java/com/dollearn/student/MainActivity.kt
@@ -1,14 +1,17 @@
package com.dollearn.student
import android.annotation.SuppressLint
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentPagerAdapter
import cn.sinata.xldutils.gone
import cn.sinata.xldutils.utils.showAllowingStateLoss
import cn.sinata.xldutils.utils.SPUtils
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
@@ -19,7 +22,6 @@
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
@@ -29,6 +31,8 @@
    private val fragments    = arrayListOf<Fragment>()
    var homeData: UserBean? = null //个人信息
    var score = 0
    private var handler: Handler? = null
    var mins = 0
    override fun initClick() {
    }
@@ -42,6 +46,50 @@
        initTab()
        EventBus.getDefault().register(this)
        getData()
        getVoiceUrl()
        getScoreConfig()
    }
    private fun getScoreConfig() {
        HttpManager.onlineDuration().request(this,false) { _, data ->
            SPUtils.instance().put(Const.SCORE_DURATION, (data ?: "0").toInt()).apply()
        }
    }
    private fun startTimer() {
        val duration = SPUtils.instance().getInt(Const.SCORE_DURATION,30)//单位分钟
        if (handler == null)
            handler = object :Handler(Looper.myLooper()!!){
                override fun handleMessage(msg: Message) {
                    super.handleMessage(msg)
                    if (msg.what == 0){
                        mins++
                        Log.e("mmp","当前已听${mins}分钟")
                        if (mins >= duration){
                            HttpManager.giveIntegral().request(this@MainActivity,false,{_,_->
                                mins = 0
                                handler?.sendEmptyMessageDelayed(0,1000*60)
                            }){_,_->
                                handler?.sendEmptyMessageDelayed(0,1000*60)
                            }
                        }else
                            handler?.sendEmptyMessageDelayed(0,1000*60)
                    }
                }
            }
        if (handler?.hasMessages(0) == false){
            mins = 0
            handler?.sendEmptyMessageDelayed(0,1000*60)
        }
    }
    private fun getVoiceUrl() {
        HttpManager.promptVoice().request(this,false){_,data->
            if (!data?.correct.isNullOrEmpty())
                SPUtils.instance().put(Const.RV,data?.correct?:"").apply()
            if (!data?.error.isNullOrEmpty())
                SPUtils.instance().put(Const.EV,data?.error?:"").apply()
        }
    }
    private fun initTab() {
@@ -97,23 +145,22 @@
    @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)
        when (e.code) {
            Const.EventCode.CLICK_GAME_ITEM -> {
                switchHome()
            }
            Const.EventCode.REFRESH_USER -> getData()
            Const.EventCode.SCORE_CHANGED -> getScore()
            Const.EventCode.START_TIMER ->{
                Log.e("mmp","开始学习")
                startTimer()
            }
            Const.EventCode.STOP_TIMER ->{
                Log.e("mmp","结束学习")
                handler?.removeMessages(0)
                mins = 0
            }
        }
    }
    @Subscribe
    fun refreshUser(e:EmptyEvent){
        if (e.code == Const.EventCode.REFRESH_USER)
            getData()
        if (e.code == Const.EventCode.SCORE_CHANGED)
            getScore()
    }
    private fun getData() {
@@ -140,21 +187,12 @@
        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 = ""
        handler?.removeCallbacksAndMessages(null)
        handler = null
    }
}