package com.dollearn.student.ui.home
|
|
import android.os.Handler
|
import android.os.Looper
|
import android.os.Message
|
import android.util.Log
|
import android.view.View
|
import androidx.constraintlayout.motion.widget.MotionLayout
|
import androidx.core.os.bundleOf
|
import cn.sinata.xldutils.fragment.BaseFragment
|
import cn.sinata.xldutils.gone
|
import cn.sinata.xldutils.utils.SPUtils
|
import cn.sinata.xldutils.utils.myToast
|
import cn.sinata.xldutils.visible
|
import com.dollearn.student.R
|
import com.dollearn.student.utils.AudioUtils
|
import com.dollearn.student.utils.Const
|
import com.dollearn.student.utils.event.EmptyEvent
|
import com.dollearn.student.utils.extention.clickDelay
|
import kotlinx.android.synthetic.main.fragment_match.*
|
import org.greenrobot.eventbus.EventBus
|
import org.greenrobot.eventbus.Subscribe
|
import org.jetbrains.anko.imageResource
|
|
|
class MatchFragment : BaseFragment(), AudioUtils.OnAudioStatusUpdateListener {
|
override fun contentViewId() = R.layout.fragment_match
|
|
private val TAG = "Match=======》"
|
|
private var handler:Handler? = null
|
private val PLAY_VOICE = 1
|
private val TO_NEXT = 2
|
|
private val PLAY_ERROR = 3
|
private val PLAY_RIGHT = 4
|
|
private val errorVoice by lazy { SPUtils.instance().getString(Const.EV) }
|
private val rightVoice by lazy { SPUtils.instance().getString(Const.RV) }
|
|
private var rightPlaying = false
|
private var errorPlaying = false
|
|
private val player by lazy { AudioUtils() }
|
private val group by lazy { arguments?.getInt("group")?:0 }
|
private val data by lazy {
|
(requireActivity() as MatchActivity).data
|
}
|
|
private val act by lazy { requireActivity() as MatchActivity }
|
|
private val voiceViews by lazy { arrayListOf(cl_voice_1,cl_voice_2,cl_voice_3,cl_voice_4) }
|
private var playing = false
|
private val voiceList = arrayListOf<String>() //语音列表,随机排序
|
private var voiceIndex = -1 //点击播放的声音序号 0-3取值
|
|
private var nowVoiceView: View? = null
|
private var imgPosition = -1
|
|
private val completedVoice = arrayListOf<String>() //已经选了的音频 飞到图片上了
|
|
var right = true //true进入下一题为全对,false 一旦答错过就置为false,此时进入下一题,本题算做错误
|
var isAutoPlaying = false
|
|
|
|
override fun onFirstVisibleToUser() {
|
player.setOnAudioStatusUpdateListener(this)
|
player.stopPlayMusic()
|
EventBus.getDefault().register(this)
|
|
handler = object : Handler(Looper.getMainLooper()){
|
override fun handleMessage(msg: Message) {
|
super.handleMessage(msg)
|
when(msg.what){
|
PLAY_VOICE->{
|
player.startPlayMusic(requireContext(),voiceList[voiceIndex])
|
}
|
TO_NEXT->{
|
if (isAdded&&!playing){
|
(requireActivity() as MatchActivity).next()
|
}
|
}
|
PLAY_RIGHT->{
|
rightPlaying = true
|
player.startPlayMusic(requireContext(),rightVoice)
|
}
|
PLAY_ERROR->{
|
voiceIndex = -1
|
errorPlaying = true
|
player.startPlayMusic(requireContext(),errorVoice)
|
}
|
}
|
}
|
}
|
data?.apply {
|
val imgs = subjectList[group]
|
iv_1.setImageURI(imgs[0].img)
|
iv_2.setImageURI(imgs[1].img)
|
iv_3.setImageURI(imgs[2].img)
|
iv_4.setImageURI(imgs[3].img)
|
voiceList.addAll(imgs.map { it.correct })
|
voiceList.shuffle()
|
}
|
initClick()
|
}
|
|
private fun initClick() {
|
cover_3.setOnClickListener { }
|
cover_2.setOnClickListener { }
|
cover_1.setOnClickListener { }
|
cover_4.setOnClickListener { }
|
voiceViews.forEachIndexed { index, constraintLayout ->
|
constraintLayout.clickDelay {
|
if (!playing){
|
voiceIndex = index
|
handler?.sendEmptyMessage(PLAY_VOICE)
|
}
|
}
|
}
|
|
cl_1.clickDelay {
|
val voiceIndex = voiceIndex
|
if (playing) //未播放或者正在播放,不可点击
|
return@clickDelay
|
if (voiceIndex == -1){//未播放不可点击
|
myToast("请先听题")
|
return@clickDelay
|
}
|
// if (data!!.subjectList[group][0].completed) //已作答不可点击
|
// return@clickDelay
|
act.totalCount ++
|
if (data!!.subjectList[group][0].correct == voiceList[voiceIndex]){
|
result_1.imageResource = R.mipmap.successs
|
act.rightCount ++
|
//执行飞行动画
|
var start = 0
|
var end = 0
|
when(voiceIndex){
|
0->{
|
start = R.id.start_1
|
end = R.id.end_1_1
|
}
|
1->{
|
start = R.id.start_2
|
end = R.id.end_2_1
|
}
|
2->{
|
start = R.id.start_3
|
end = R.id.end_3_1
|
}
|
3->{
|
start = R.id.start_4
|
end = R.id.end_4_1
|
}
|
}
|
nowVoiceView = voiceViews[voiceIndex]
|
imgPosition = 0
|
motion.setTransitionListener(object :MotionLayout.TransitionListener{
|
override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
|
|
}
|
|
override fun onTransitionChange(
|
p0: MotionLayout?,
|
p1: Int,
|
p2: Int,
|
p3: Float
|
) {
|
}
|
|
override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
|
cl_voice_end_1.visible()
|
when(voiceIndex){
|
0->cover_1.visible()
|
1->cover_2.visible()
|
2->cover_3.visible()
|
3->cover_4.visible()
|
}
|
}
|
|
override fun onTransitionTrigger(
|
p0: MotionLayout?,
|
p1: Int,
|
p2: Boolean,
|
p3: Float
|
) {
|
}
|
})
|
motion.setTransition(start,end)
|
motion.transitionToEnd()
|
handler?.sendEmptyMessage(PLAY_RIGHT)
|
}else{
|
right = false
|
result_1.imageResource = R.mipmap.zhifushibai
|
handler?.sendEmptyMessage(PLAY_ERROR)
|
}
|
showResultAnim(cl_1)
|
}
|
|
cl_2.clickDelay {
|
val voiceIndex = voiceIndex
|
|
if (playing) //未播放或者正在播放,不可点击
|
return@clickDelay
|
if (voiceIndex == -1){//未播放不可点击
|
myToast("请先听题")
|
return@clickDelay
|
}
|
// if (data!!.subjectList[group][1].completed) //已作答不可点击
|
// return@clickDelay
|
act.totalCount ++
|
if (data!!.subjectList[group][1].correct == voiceList[voiceIndex]){
|
result_2.imageResource = R.mipmap.successs
|
act.rightCount ++
|
imgPosition = 1
|
//执行飞行动画
|
var start = 0
|
var end = 0
|
when(voiceIndex){
|
0->{
|
start = R.id.start_1
|
end = R.id.end_1_2
|
}
|
1->{
|
start = R.id.start_2
|
end = R.id.end_2_2
|
}
|
2->{
|
start = R.id.start_3
|
end = R.id.end_3_2
|
}
|
3->{
|
start = R.id.start_4
|
end = R.id.end_4_2
|
}
|
}
|
nowVoiceView = voiceViews[voiceIndex]
|
|
motion.setTransitionListener(object :MotionLayout.TransitionListener{
|
override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
|
|
}
|
|
override fun onTransitionChange(
|
p0: MotionLayout?,
|
p1: Int,
|
p2: Int,
|
p3: Float
|
) {
|
}
|
|
override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
|
cl_voice_end_2.visible()
|
when(voiceIndex){
|
0->cover_1.visible()
|
1->cover_2.visible()
|
2->cover_3.visible()
|
3->cover_4.visible()
|
}
|
}
|
|
override fun onTransitionTrigger(
|
p0: MotionLayout?,
|
p1: Int,
|
p2: Boolean,
|
p3: Float
|
) {
|
}
|
})
|
motion.setTransition(start,end)
|
motion.transitionToEnd()
|
handler?.sendEmptyMessage(PLAY_RIGHT)
|
}else{
|
result_2.imageResource = R.mipmap.zhifushibai
|
handler?.sendEmptyMessage(PLAY_ERROR)
|
}
|
showResultAnim(cl_2)
|
}
|
|
cl_3.clickDelay {
|
val voiceIndex = voiceIndex
|
|
if (playing) //未播放或者正在播放,不可点击
|
return@clickDelay
|
if (voiceIndex == -1){//未播放不可点击
|
myToast("请先听题")
|
return@clickDelay
|
}
|
// if (data!!.subjectList[group][2].completed) //已作答不可点击
|
// return@clickDelay
|
act.totalCount ++
|
if (data!!.subjectList[group][2].correct == voiceList[voiceIndex]){
|
result_3.imageResource = R.mipmap.successs
|
imgPosition = 2
|
act.rightCount ++
|
//执行飞行动画
|
var start = 0
|
var end = 0
|
when(voiceIndex){
|
0->{
|
start = R.id.start_1
|
end = R.id.end_1_3
|
}
|
1->{
|
start = R.id.start_2
|
end = R.id.end_2_3
|
}
|
2->{
|
start = R.id.start_3
|
end = R.id.end_3_3
|
}
|
3->{
|
start = R.id.start_4
|
end = R.id.end_4_3
|
}
|
}
|
nowVoiceView = voiceViews[voiceIndex]
|
|
motion.setTransitionListener(object :MotionLayout.TransitionListener{
|
override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
|
|
}
|
|
override fun onTransitionChange(
|
p0: MotionLayout?,
|
p1: Int,
|
p2: Int,
|
p3: Float
|
) {
|
}
|
|
override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
|
cl_voice_end_3.visible()
|
when(voiceIndex){
|
0->cover_1.visible()
|
1->cover_2.visible()
|
2->cover_3.visible()
|
3->cover_4.visible()
|
}
|
}
|
|
override fun onTransitionTrigger(
|
p0: MotionLayout?,
|
p1: Int,
|
p2: Boolean,
|
p3: Float
|
) {
|
}
|
})
|
motion.setTransition(start,end)
|
motion.transitionToEnd()
|
handler?.sendEmptyMessage(PLAY_RIGHT)
|
}else{
|
result_3.imageResource = R.mipmap.zhifushibai
|
handler?.sendEmptyMessage(PLAY_ERROR)
|
}
|
showResultAnim(cl_3)
|
}
|
|
cl_4.clickDelay {
|
val voiceIndex = voiceIndex
|
if (playing) //未播放或者正在播放,不可点击
|
return@clickDelay
|
if (voiceIndex == -1){//未播放不可点击
|
myToast("请先听题")
|
return@clickDelay
|
}
|
// if (data!!.subjectList[group][3].completed) //已作答不可点击
|
// return@clickDelay
|
act.totalCount ++
|
if (data!!.subjectList[group][3].correct == voiceList[voiceIndex]){
|
result_4.imageResource = R.mipmap.successs
|
imgPosition = 3
|
act.rightCount ++
|
//执行飞行动画
|
var start = 0
|
var end = 0
|
when(voiceIndex){
|
0->{
|
start = R.id.start_1
|
end = R.id.end_1_4
|
}
|
1->{
|
start = R.id.start_2
|
end = R.id.end_2_4
|
}
|
2->{
|
start = R.id.start_3
|
end = R.id.end_3_4
|
}
|
3->{
|
start = R.id.start_4
|
end = R.id.end_4_4
|
}
|
}
|
nowVoiceView = voiceViews[voiceIndex]
|
|
motion.setTransitionListener(object :MotionLayout.TransitionListener{
|
override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
|
|
}
|
|
override fun onTransitionChange(
|
p0: MotionLayout?,
|
p1: Int,
|
p2: Int,
|
p3: Float
|
) {
|
}
|
|
override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
|
cl_voice_end_4.visible()
|
when(voiceIndex){
|
0->cover_1.visible()
|
1->cover_2.visible()
|
2->cover_3.visible()
|
3->cover_4.visible()
|
}
|
}
|
|
override fun onTransitionTrigger(
|
p0: MotionLayout?,
|
p1: Int,
|
p2: Boolean,
|
p3: Float
|
) {
|
}
|
})
|
motion.setTransition(start,end)
|
motion.transitionToEnd()
|
handler?.sendEmptyMessage(PLAY_RIGHT)
|
}else{
|
result_4.imageResource = R.mipmap.zhifushibai
|
handler?.sendEmptyMessage(PLAY_ERROR)
|
}
|
showResultAnim(cl_4)
|
}
|
|
cl_voice_end_1.setOnClickListener {
|
voiceIndex = 4
|
player.startPlayMusic(requireContext(),data!!.subjectList[group][0].correct)
|
}
|
cl_voice_end_2.setOnClickListener {
|
voiceIndex = 5
|
player.startPlayMusic(requireContext(),data!!.subjectList[group][1].correct)
|
|
}
|
cl_voice_end_3.setOnClickListener {
|
voiceIndex = 6
|
player.startPlayMusic(requireContext(),data!!.subjectList[group][2].correct)
|
|
}
|
cl_voice_end_4.setOnClickListener {
|
voiceIndex = 7
|
player.startPlayMusic(requireContext(),data!!.subjectList[group][3].correct)
|
|
}
|
}
|
|
private fun showResultAnim(m:MotionLayout){
|
m.transitionToEnd()
|
m.setTransitionListener(object :MotionLayout.TransitionListener{
|
override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
|
}
|
|
override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
|
}
|
|
override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {
|
m.progress = 0f
|
tv_name.text =""
|
}
|
|
override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
|
}
|
})
|
}
|
|
@Subscribe
|
fun onEvent(e: EmptyEvent){
|
if (e.code == Const.EventCode.RECOVERD){
|
if (act.fragments[act.vp.currentItem] == this){ //自动播放
|
isAutoPlaying = true
|
cl_voice_1.callOnClick()
|
}
|
}
|
}
|
|
/**
|
* 回到上一题,恢复状态
|
*/
|
fun recover(){
|
Log.e(TAG,"回到上一题,恢复答题前的状态")
|
right = true
|
motion.progress = 0f
|
data!!.subjectList[group].forEach {
|
it.completed = false
|
it.listend = false
|
}
|
cover_1.gone()
|
cover_2.gone()
|
cover_3.gone()
|
cover_4.gone()
|
tv_name.text =""
|
cl_voice_end_1.gone()
|
cl_voice_end_2.gone()
|
cl_voice_end_3.gone()
|
cl_voice_end_4.gone()
|
if (act.fragments[act.vp.currentItem] == this&&act.recoverd) { //自动播放
|
isAutoPlaying = true
|
cl_voice_1.callOnClick()
|
}
|
}
|
|
companion object{
|
/**
|
* @param group 题组号
|
* @param index 组内序号
|
*/
|
fun getInstance(group:Int):MatchFragment{
|
val listenFragment = MatchFragment()
|
listenFragment.arguments = bundleOf("group" to group)
|
return listenFragment
|
}
|
}
|
|
override fun onUpdate(db: Double, time: Long) {
|
}
|
|
override fun onStop(filePath: String?) {
|
}
|
|
override fun onStartPlay() {
|
playing = true
|
if (errorPlaying||rightPlaying)
|
return
|
val list = data!!.subjectList[group]
|
if (voiceIndex in 0..3)
|
tv_name.text = list[list.map { it.correct }.indexOf(voiceList[voiceIndex])].name
|
when(voiceIndex){
|
0->{
|
iv1_1.gone()
|
iv2_1.gone()
|
iv_playing_1.visible()
|
iv_playing_1.visible()
|
}
|
1->{
|
iv1_2.gone()
|
iv2_2.gone()
|
iv_playing_2.visible()
|
}
|
2->{
|
iv1_3.gone()
|
iv2_3.gone()
|
iv_playing_3.visible()
|
|
}
|
3->{
|
iv1_4.gone()
|
iv2_4.gone()
|
iv_playing_4.visible()
|
}
|
|
4->{
|
iv1_1_end.gone()
|
iv2_1_end.gone()
|
iv_playing_1_end.visible()
|
}
|
5->{
|
iv1_2_end.gone()
|
iv2_2_end.gone()
|
iv_playing_2_end.visible()
|
}
|
6->{
|
iv1_3_end.gone()
|
iv2_3_end.gone()
|
iv_playing_3_end.visible()
|
}
|
7->{
|
iv1_4_end.gone()
|
iv2_4_end.gone()
|
iv_playing_4_end.visible()
|
}
|
|
}
|
}
|
|
override fun onFinishPlay() {
|
playing = false
|
when(voiceIndex){
|
0->{
|
iv1_1.visible()
|
iv2_1.visible()
|
iv_playing_1.gone()
|
if (nowVoiceView!=null && data!!.subjectList[group][imgPosition].completed){
|
voiceIndex = -1
|
nowVoiceView = null
|
}
|
}
|
1->{
|
iv1_2.visible()
|
iv2_2.visible()
|
iv_playing_2.gone()
|
if (nowVoiceView!=null && data!!.subjectList[group][imgPosition].completed){
|
voiceIndex = -1
|
nowVoiceView = null
|
}
|
}
|
2->{
|
iv1_3.visible()
|
iv2_3.visible()
|
iv_playing_3.gone()
|
if (nowVoiceView!=null && data!!.subjectList[group][imgPosition].completed){
|
voiceIndex = -1
|
nowVoiceView = null
|
}
|
}
|
3->{
|
iv1_4.visible()
|
iv2_4.visible()
|
iv_playing_4.gone()
|
if (nowVoiceView!=null && data!!.subjectList[group][imgPosition].completed){
|
voiceIndex = -1
|
nowVoiceView = null
|
}
|
}
|
|
4->{
|
iv1_1_end.visible()
|
iv2_1_end.visible()
|
iv_playing_1_end.gone()
|
voiceIndex = -1
|
}
|
5->{
|
iv1_2_end.visible()
|
iv2_2_end.visible()
|
iv_playing_2_end.gone()
|
voiceIndex = -1
|
}
|
6->{
|
iv1_3_end.visible()
|
iv2_3_end.visible()
|
iv_playing_3_end.gone()
|
voiceIndex = -1
|
}
|
7->{
|
iv1_4_end.visible()
|
iv2_4_end.visible()
|
iv_playing_4_end.gone()
|
voiceIndex = -1
|
}
|
}
|
if (data!!.subjectList[group].filter { it.completed }.size == 4){ //全部答对
|
handler?.sendEmptyMessage(TO_NEXT)
|
}
|
|
if (errorPlaying)
|
errorPlaying = false
|
if (rightPlaying){
|
rightPlaying = false
|
nowVoiceView?.callOnClick()
|
data!!.subjectList[group][imgPosition].completed = true
|
}
|
}
|
|
|
override fun onPause() {
|
super.onPause()
|
player.stopPlayMusic()
|
}
|
|
override fun onDestroy() {
|
super.onDestroy()
|
handler?.removeCallbacksAndMessages(null)
|
EventBus.getDefault().unregister(this)
|
|
}
|
}
|