From 481723ce3c05d74fec53b8567b9c79d77bdcc155 Mon Sep 17 00:00:00 2001
From: 罗明文 <125975490@qq.com>
Date: 星期三, 19 六月 2024 02:27:53 +0800
Subject: [PATCH] save

---
 app/src/main/java/com/dollearn/student/ui/home/ResultActivity.kt      |    8 ++
 app/src/main/java/com/dollearn/student/network/entity/MemoryBean.kt   |    6 
 app/src/main/java/com/dollearn/student/ui/home/adapter/CardAdapter.kt |   71 +++++++++++++++++
 app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt          |   27 +++++-
 app/src/main/java/com/dollearn/student/ui/home/MemoryActivity.kt      |  103 ++++++++++++++++++++++---
 .idea/runConfigurations.xml                                           |   10 ++
 app/src/main/res/layout/activity_memory.xml                           |    6 
 7 files changed, 207 insertions(+), 24 deletions(-)

diff --git a/.idea/runConfigurations.xml b/.idea/runConfigurations.xml
new file mode 100644
index 0000000..797acea
--- /dev/null
+++ b/.idea/runConfigurations.xml
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project version="4">
+  <component name="RunConfigurationProducerService">
+    <option name="ignoredProducers">
+      <set>
+        <option value="com.android.tools.idea.compose.preview.runconfiguration.ComposePreviewRunConfigurationProducer" />
+      </set>
+    </option>
+  </component>
+</project>
\ No newline at end of file
diff --git a/app/src/main/java/com/dollearn/student/network/entity/MemoryBean.kt b/app/src/main/java/com/dollearn/student/network/entity/MemoryBean.kt
index 1cb1022..cc9d840 100644
--- a/app/src/main/java/com/dollearn/student/network/entity/MemoryBean.kt
+++ b/app/src/main/java/com/dollearn/student/network/entity/MemoryBean.kt
@@ -14,15 +14,15 @@
 data class MemoryData(
     val answerCount: Int,
     val answerIntegral: Int,
-    val answerRate: String,
+    val answerRate: String?,
     val answerTime: Int,
     val count: Int,
     val createBy: String,
     val createTime: String,
     val disabled: Boolean,
-    val id: Int,
+    val id: String,
     val integral: String,
-    val rate: String,
+    val rate: String?,
     val studyId: Int,
     val time: String,
     val updateBy: String,
diff --git a/app/src/main/java/com/dollearn/student/ui/home/MemoryActivity.kt b/app/src/main/java/com/dollearn/student/ui/home/MemoryActivity.kt
index 6ab0321..7fd64c2 100644
--- a/app/src/main/java/com/dollearn/student/ui/home/MemoryActivity.kt
+++ b/app/src/main/java/com/dollearn/student/ui/home/MemoryActivity.kt
@@ -1,8 +1,9 @@
 package com.dollearn.student.ui.home
 
-import android.os.CountDownTimer
+import android.os.Handler
 import androidx.core.os.bundleOf
 import androidx.recyclerview.widget.GridLayoutManager
+import cn.sinata.xldutils.utils.showAllowingStateLoss
 import com.dollearn.student.R
 import com.dollearn.student.dialog.TipDialog
 import com.dollearn.student.network.entity.Card
@@ -19,32 +20,110 @@
     }
     private val list = arrayListOf<Card>()
     private val adapter = CardAdapter(list)
-    private val TIME = 600*1000L
-    private var timer:CountDownTimer? = null
+
+    private var lastTime = 600 //剩余秒
+
+    private var totalCount = 0
+    private var rightCount = 0
+
+    private var handler: Handler? = null
+    var time = 0 //学习秒数
+
+
+    private val PLAY_VOICE = 1
+    private val STUDY_TIME = 2
+
+    private val player by lazy { com.dollearn.student.utils.AudioUtils() }
+
+    private var index = 0
+
 
     override fun initClick() {
-
+        tv_exit.setOnClickListener {
+            if (tv_exit.text == "提交"){
+                //todo 分数问题
+                ResultActivity.startResult(this,0,0,0,7,totalCount,rightCount,100,time,data!!.data.id)
+                finish()
+            }else
+                finish()
+        }
     }
 
     override fun initView() {
+        handler = object : Handler(android.os.Looper.getMainLooper()){
+            override fun handleMessage(msg: android.os.Message) {
+                super.handleMessage(msg)
+                when(msg.what){
+                    STUDY_TIME->{
+                        time++
+                        lastTime --
+                        tv_timer.text = "${lastTime}S"
+                        if (lastTime == 0){
+                            val tipDialog = TipDialog()
+                            tipDialog.arguments = bundleOf("msg" to "答题时间已结束,停止作答!","isAlert" to true,"ok" to "查看成绩")
+                            tipDialog.setDismissCallback(object :TipDialog.OnDismiss{
+                                override fun onDismiss() {
+                                    ResultActivity.startResult(this@MemoryActivity,0,0,0,7,totalCount,rightCount
+                                        ,100,time,data!!.data.id)
+                                }
+                            })
+                            tipDialog.showAllowingStateLoss(supportFragmentManager,"finish")
+                        }else
+                            sendEmptyMessageDelayed(STUDY_TIME,1000)
+                    }
+                    PLAY_VOICE->{
+                        player.startPlayMusic(this@MemoryActivity,list[index].url)
+                    }
+                }
+            }
+        }
+
+        handler?.sendEmptyMessageDelayed(STUDY_TIME,1000)
+
         rv_list.layoutManager = GridLayoutManager(this,5)
         rv_list.adapter = adapter
         list.addAll(data.photoList.map { Card(it.id,1,it.photo) })
         list.addAll(data.voiceList.map { Card(it.id,2,it.voice) })
         list.shuffle()
         adapter.notifyDataSetChanged()
+    }
 
-        timer = object :CountDownTimer(TIME,1000){
-            override fun onTick(millisUntilFinished: Long) {
+    /**
+     * 翻开卡片
+     */
+    fun flyover(position:Int){
+        val card = list[position]
+        if (card.type == 2){
+            index = position
+            handler?.sendEmptyMessage(PLAY_VOICE)
+        }
 
+        if (adapter.position1!=-1&&adapter.position2!=-1){ //已经翻开2张卡片,比较结果是否匹配
+            totalCount++
+            val card1 = list[adapter.position1]
+            val card2 = list[adapter.position2]
+            if (card1.type!=card2.type&&card1.id == card2.id){//匹配成功
+                adapter.releaseHolder()
+                rightCount++
+                tv_timer.postDelayed({
+                    if (list.filter { !it.isBack }.size == list.size){ //全部答对
+                        tv_exit.text = "提交"
+                        handler?.removeMessages(STUDY_TIME) //停止计时
+                    }
+                },500)
+            }else{
+                tv_timer.postDelayed({
+                    adapter.bothToBack()
+                },500)
             }
-
-            override fun onFinish() {
-                val tipDialog = TipDialog()
-                tipDialog.arguments = bundleOf("msg" to "答题时间已结束,停止作答!")
-            }
-
         }
     }
 
+
+    override fun onDestroy() {
+        super.onDestroy()
+        handler?.removeMessages(PLAY_VOICE)
+        handler?.removeMessages(STUDY_TIME)
+        handler = null
+    }
 }
diff --git a/app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt b/app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt
index c6f0d38..187f0a4 100644
--- a/app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt
+++ b/app/src/main/java/com/dollearn/student/ui/home/QAFragment.kt
@@ -86,6 +86,8 @@
         showVoiceUi()
 
         rl_check_1.clickDelay {
+            if (playing)
+                return@clickDelay
             if (list[questionIndex].completed) //已作答
                 return@clickDelay
             act.totalCount++
@@ -94,6 +96,7 @@
                 Log.e(TAG,"选择正确")
                 transAnimation(cl_answer_1,voiceViews[questionIndex])
                 list[questionIndex].completed = true
+                cl_answer_1.callOnClick()
             }else{
                 dot_1.visible()
                 iv_error_1.visible()
@@ -104,6 +107,8 @@
             }
         }
         rl_check_2.clickDelay {
+            if (playing)
+                return@clickDelay
             if (list[questionIndex].completed) //已作答
                 return@clickDelay
             act.totalCount++
@@ -112,6 +117,7 @@
                 Log.e(TAG,"选择正确")
                 transAnimation(cl_answer_2,voiceViews[questionIndex])
                 list[questionIndex].completed = true
+                cl_answer_2.callOnClick()
             }else{
                 dot_2.visible()
                 iv_error_2.visible()
@@ -122,6 +128,8 @@
             }
         }
         rl_check_3.clickDelay {
+            if (playing)
+                return@clickDelay
             if (list[questionIndex].completed) //已作答
                 return@clickDelay
             act.totalCount++
@@ -130,6 +138,7 @@
                 Log.e(TAG,"选择正确")
                 transAnimation(cl_answer_3,voiceViews[questionIndex])
                 list[questionIndex].completed = true
+                cl_answer_3.callOnClick()
             }else{
                 dot_3.visible()
                 iv_error_3.visible()
@@ -183,18 +192,25 @@
         cl_answer_1.visible()
         cl_answer_2.visible()
         cl_answer_3.visible()
+        answerList.clear()
         answerList.add(list[position].correct)
         answerList.addAll(list[position].error?.split(",")?: arrayListOf())
         answerList.shuffle()
         cl_answer_1.setOnClickListener {
+            if (playing)
+                return@setOnClickListener
             voiceIndex = 4
             handler?.sendEmptyMessage(PLAY_VOICE)
         }
         cl_answer_2.setOnClickListener {
+            if (playing)
+                return@setOnClickListener
             voiceIndex = 5
             handler?.sendEmptyMessage(PLAY_VOICE)
         }
         cl_answer_3.setOnClickListener {
+            if (playing)
+                return@setOnClickListener
             voiceIndex = 6
             handler?.sendEmptyMessage(PLAY_VOICE)
         }
@@ -260,17 +276,17 @@
         v1Views[voiceIndex].gone()
         v2Views[voiceIndex].gone()
         playingViews[voiceIndex].visible()
-        if (voiceIndex == 0&&!list[1].completed){
+        if (voiceIndex == 0&&list[0].isQuestion == 1&&!list[1].completed){
             showAnswerView(1)
         }
-        if (voiceIndex == 1&&!list[0].completed){
+        if (voiceIndex == 1&&list[1].isQuestion == 1&&!list[0].completed){
             showAnswerView(0)
 
         }
-        if (voiceIndex == 2&&!list[3].completed){
+        if (voiceIndex == 2&&list[2].isQuestion == 1&&!list[3].completed){
             showAnswerView(3)
         }
-        if (voiceIndex == 3&&!list[2].completed){
+        if (voiceIndex == 3&&list[3].isQuestion == 1&&!list[2].completed){
             showAnswerView(2)
         }
     }
@@ -280,5 +296,8 @@
         v1Views[voiceIndex].visible()
         v2Views[voiceIndex].visible()
         playingViews[voiceIndex].gone()
+        if (list.filter { it.completed }.size == 2){ //全部作答,3秒后下一题
+            handler?.sendEmptyMessageDelayed(TO_NEXT,3000)
+        }
     }
 }
\ No newline at end of file
diff --git a/app/src/main/java/com/dollearn/student/ui/home/ResultActivity.kt b/app/src/main/java/com/dollearn/student/ui/home/ResultActivity.kt
index d3db8d0..283da05 100644
--- a/app/src/main/java/com/dollearn/student/ui/home/ResultActivity.kt
+++ b/app/src/main/java/com/dollearn/student/ui/home/ResultActivity.kt
@@ -62,6 +62,14 @@
                 HttpManager.gameAchievement(rate,difficulty,id?:"","超级听力",time).request(this){_,data->
                 }
             }
+            7->{
+                title = "超级记忆"
+                tv_back.visible()
+                tv_last.gone()
+                tv_exit.gone()
+                HttpManager.gameAchievement(rate,difficulty,id?:"","超级记忆",time).request(this){_,data->
+                }
+            }
             8->{
                 tv_back.visible()
                 tv_last.gone()
diff --git a/app/src/main/java/com/dollearn/student/ui/home/adapter/CardAdapter.kt b/app/src/main/java/com/dollearn/student/ui/home/adapter/CardAdapter.kt
index 2400b9d..f4ee1c0 100644
--- a/app/src/main/java/com/dollearn/student/ui/home/adapter/CardAdapter.kt
+++ b/app/src/main/java/com/dollearn/student/ui/home/adapter/CardAdapter.kt
@@ -7,17 +7,53 @@
 import cn.sinata.xldutils.visible
 import com.dollearn.student.R
 import com.dollearn.student.network.entity.Card
+import com.dollearn.student.ui.home.MemoryActivity
 import com.dollearn.student.utils.ViewAnimUtils
+import com.dollearn.student.utils.extention.clickDelay
+import com.facebook.drawee.view.SimpleDraweeView
 import kotlinx.android.synthetic.main.fragment_q_a.*
 import java.util.ArrayList
 
 class CardAdapter(list: ArrayList<Card>):HFRecyclerAdapter<Card>(list, R.layout.item_card) {
+    private val act by lazy { context as MemoryActivity }
+
+    //第一次翻开的
+    var position1 = -1
+    private var p1View:View? = null
+    private var f1View:View? = null
+    private var b1View:View? = null
+
+    //第2次翻开的
+    var position2 = -1
+    private var p2View:View? = null
+    private var f2View:View? = null
+    private var b2View:View? = null
+
     override fun onBind(holder: ViewHolder, position: Int, data: Card) {
         val frame = holder.bind<View>(R.id.frame)
         val back = holder.bind<View>(R.id.back)
         val front = holder.bind<View>(R.id.front)
-        frame.setOnClickListener {
-            flip(position,frame,back, front)
+        val rlVoice = holder.bind<View>(R.id.rl_voice)
+        val ivImg = holder.bind<SimpleDraweeView>(R.id.iv_img)
+
+        if(data.type == 1){
+            rlVoice.gone()
+            ivImg.visible()
+            ivImg.setImageURI(data.url)
+        }else{
+            rlVoice.visible()
+            ivImg.gone()
+        }
+
+        frame.clickDelay {
+            if(data.isBack){
+                if(position1 == -1)
+                    position1 = position
+                else if(position2 == -1)
+                    position2 = position
+                flip(position,frame,back, front)
+                act.flyover(position)
+            }
         }
         if (data.isBack){
             back.visible()
@@ -37,6 +73,37 @@
         parent.postDelayed({
             mData[position].isBack = direction != -1
             switchViewVisibility(back, front) },300)
+        if(p1View == null){
+            p1View = parent
+            b1View = back
+            f1View = front
+        }else if(p2View == null){
+            p2View = parent
+            b2View = back
+            f2View = front
+        }
+    }
+
+    /**
+     * 答案不匹配,2张卡片都翻回去
+     */
+    fun bothToBack(){
+        if (p1View!=null&&b1View!=null&&f1View!=null)
+            flip(position1,p1View!!,b1View!!,f1View!!)
+        if (p2View!=null&&b2View!=null&&f2View!=null)
+            flip(position2,p2View!!,b2View!!,f2View!!)
+        releaseHolder()
+    }
+
+    fun releaseHolder(){
+        position1 = -1
+        position2 = -1
+        p1View = null
+        b1View = null
+        f1View = null
+        p2View = null
+        b2View = null
+        f2View = null
     }
 
     private fun switchViewVisibility(back:View,front:View) {
diff --git a/app/src/main/res/layout/activity_memory.xml b/app/src/main/res/layout/activity_memory.xml
index 4399351..be8c376 100644
--- a/app/src/main/res/layout/activity_memory.xml
+++ b/app/src/main/res/layout/activity_memory.xml
@@ -13,15 +13,15 @@
         android:layout_marginStart="40dp"
         android:textSize="14sp"
         android:textColor="@color/textColor"
-        app:layout_constraintTop_toTopOf="@id/timer"
-        app:layout_constraintBottom_toBottomOf="@id/timer"
+        app:layout_constraintTop_toTopOf="@id/tv_timer"
+        app:layout_constraintBottom_toBottomOf="@id/tv_timer"
         android:textStyle="bold"
         android:text="答题剩余时间:"/>
 
     <TextView
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
-        android:id="@+id/timer"
+        android:id="@+id/tv_timer"
         app:layout_constraintStart_toEndOf="@id/tv_1"
         android:textSize="36sp"
         android:textColor="@color/colorRed"

--
Gitblit v1.7.1