lmw
2024-07-16 c303346ae803dc2a89ec0f025192773211861915
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
185
186
package com.dollearn.student.ui.home
 
import android.app.Activity
import android.content.Intent
import android.widget.TextView
import androidx.core.os.bundleOf
import cn.sinata.xldutils.fragment.BaseFragment
import cn.sinata.xldutils.utils.myToast
import cn.sinata.xldutils.utils.showAllowingStateLoss
import com.dollearn.student.R
import com.dollearn.student.dialog.TipDialog
import com.dollearn.student.network.Apis
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.requestByF
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.extention.formatToChinese
import kotlinx.android.synthetic.main.fragment_daily.*
import org.greenrobot.eventbus.EventBus
import org.jetbrains.anko.backgroundResource
import org.jetbrains.anko.support.v4.browse
import org.jetbrains.anko.support.v4.startActivityForResult
import org.jetbrains.anko.support.v4.toast
 
class DailyFragment : BaseFragment() {
    override fun contentViewId() = R.layout.fragment_daily
 
    private val day by lazy {
        arguments?.getInt("day")?:0
    }
    private val season by lazy {
        (requireActivity() as ScheduleActivity).season
    }
    private val week by lazy {
        (requireActivity() as ScheduleActivity).week
    }
    private val current by lazy {
        (requireActivity() as ScheduleActivity).currentDay
    }
 
    override fun onFirstVisibleToUser() {
        getProgress()
        cl_1.clickDelay {
            showDialog("加载题目...")
            HttpManager.listenSelectPicture(season, week, day).requestByF(this,false, {_,data->
                startActivityForResult<ListenActivity>(1,"data" to data,"day" to day,"week" to week,"season" to season)
                EventBus.getDefault().post(EmptyEvent(Const.EventCode.START_TIMER))
            }){_,msg->
                handleError(msg)
            }
        }
        cl_2.clickDelay {
            showDialog("加载题目...")
            HttpManager.pictureSelectVoice(season, week, day).requestByF(this,false, {_,data->
                startActivityForResult<ChooseVoiceActivity>(1,"data" to data,"day" to day,"week" to week,"season" to season)
                EventBus.getDefault().post(EmptyEvent(Const.EventCode.START_TIMER))
 
            }){_,msg->
                handleError(msg)
            }
        }
        cl_3.clickDelay {
            showDialog("加载题目...")
            HttpManager.induceExclude(season, week, day).requestByF(this,false, {_,data->
                startActivityForResult<IncludeActivity>(1,"data" to data,"day" to day,"week" to week,"season" to season)
                EventBus.getDefault().post(EmptyEvent(Const.EventCode.START_TIMER))
 
            }){_,msg->
                handleError(msg)
            }
        }
        cl_4.clickDelay {
            showDialog("加载题目...")
            HttpManager.questionsAndAnswers(season, week, day).requestByF(this,false, {_,data->
                startActivityForResult<QAActivity>(1,"data" to data,"day" to day,"week" to week,"season" to season)
                EventBus.getDefault().post(EmptyEvent(Const.EventCode.START_TIMER))
 
            }){_,msg->
                handleError(msg)
            }
        }
        cl_5.clickDelay {
            showDialog("加载题目...")
            HttpManager.pictureMateVoice(season, week, day).requestByF(this,false,{_,data->
                startActivityForResult<MatchActivity>(1,"data" to data,"day" to day,"week" to week,"season" to season)
                EventBus.getDefault().post(EmptyEvent(Const.EventCode.START_TIMER))
 
            }){_,msg->
                handleError(msg)
            }
        }
    }
 
    private fun handleError(msg:String){
        if (msg.contains("会员")){
            val tipDialog = TipDialog()
            tipDialog.arguments = bundleOf("msg" to msg,"ok" to "成为会员")
            tipDialog.setCallback(object :TipDialog.OnClickCallback{
                override fun onOk() {
                    browse(Apis.SHARE_URL,true)
                }
 
                override fun onCancel() {
                }
            })
            tipDialog.showAllowingStateLoss(childFragmentManager,"vip")
        }else{
            myToast(msg)
        }
    }
 
    private fun getProgress(){
        HttpManager.studySchedule(week,day).requestByF(this){ _, data->
            data?.apply {
                tv_current.text = "当前周目:${week.formatToChinese()}周目"
                tv_progress.text = "${computeSchedule}%"
                tv_total.text = "${totalStudy}小时"
                tv_today.text = "${todayStudy}小时"
                progress_bar.progress = computeSchedule
 
                if (this@DailyFragment.day == current){
                    formatProgress(listen,tv_state)
                    formatProgress(look,tv_state_2)
                    formatProgress(induction,tv_state_3)
                    formatProgress(answer,tv_state_4)
                    formatProgress(pair,tv_state_5)
                }else{
                    formatProgress(100,tv_state)
                    formatProgress(100,tv_state_2)
                    formatProgress(100,tv_state_3)
                    formatProgress(100,tv_state_4)
                    formatProgress(100,tv_state_5)
                }
            }
        }
    }
 
    private fun formatProgress(progress:Int,tv:TextView){
        tv.text =  when(progress){
            -1, 0->{
                tv.backgroundResource = R.drawable.bg_red_9dp
                "未完成"
            }
            100->{
                tv.backgroundResource = R.drawable.bg_blue_9dp
                "已完成"
            }
            else->{
                tv.backgroundResource = R.drawable.bg_pink_9dp
                "剩余:${progress}%"
            }
        }
    }
 
    fun nextSubject(type:Int){
        when(type){
            2->{
                cl_2.callOnClick()
            }
            3->{
                cl_3.callOnClick()
            }
            4->{
                cl_4.callOnClick()
            }
            5->{
                cl_5.callOnClick()
            }
        }
    }
 
    companion object{
        fun newInstance(day:Int):DailyFragment{
            val dailyFragment = DailyFragment()
            dailyFragment.arguments = bundleOf("day" to day)
            return dailyFragment
        }
    }
 
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK)
            getProgress()
    }
}