浏览代码

1.优化代码

王鹏鹏 2 年之前
父节点
当前提交
ad1e95e7dc

+ 34 - 2
baselib/schemas/com.yingyangfly.baselib.db.AppDataBase/1.json

@@ -2,7 +2,7 @@
   "formatVersion": 1,
   "database": {
     "version": 1,
-    "identityHash": "5c1566a338ca6c5d6398be75acd7150c",
+    "identityHash": "a8e4dc429cedceb34dc39bb5c7e4ee60",
     "entities": [
       {
         "tableName": "Questions",
@@ -339,12 +339,44 @@
         },
         "indices": [],
         "foreignKeys": []
+      },
+      {
+        "tableName": "GameSound",
+        "createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`id` INTEGER NOT NULL, `voiceUrl` TEXT, `voiceType` TEXT, PRIMARY KEY(`id`))",
+        "fields": [
+          {
+            "fieldPath": "id",
+            "columnName": "id",
+            "affinity": "INTEGER",
+            "notNull": true
+          },
+          {
+            "fieldPath": "voiceUrl",
+            "columnName": "voiceUrl",
+            "affinity": "TEXT",
+            "notNull": false
+          },
+          {
+            "fieldPath": "voiceType",
+            "columnName": "voiceType",
+            "affinity": "TEXT",
+            "notNull": false
+          }
+        ],
+        "primaryKey": {
+          "columnNames": [
+            "id"
+          ],
+          "autoGenerate": false
+        },
+        "indices": [],
+        "foreignKeys": []
       }
     ],
     "views": [],
     "setupQueries": [
       "CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
-      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '5c1566a338ca6c5d6398be75acd7150c')"
+      "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'a8e4dc429cedceb34dc39bb5c7e4ee60')"
     ]
   }
 }

+ 1 - 1
baselib/src/main/java/com/yingyangfly/baselib/base/BaseActivity.kt

@@ -95,7 +95,7 @@ abstract class BaseActivity<DB : ViewDataBinding> : AppCompatActivity(),
         bindingBase.llytContent.addView(binding.root)
         setContentView(ViewTool.inflateLayoutPixels(this, bindingBase.root, 1194, 834))
         requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE // 竖屏
-        db = AppDataBase.getInstance(mContext)
+        db = AppDataBase.getInstance(mContext.applicationContext)
         if (db != null) {
             questionsDao = db?.getQuestionsDao()
             doctorDao = db?.getDoctorsDao()

+ 1 - 1
baselib/src/main/java/com/yingyangfly/baselib/base/BaseFragment.kt

@@ -70,7 +70,7 @@ abstract class BaseFragment<DB : ViewDataBinding> : Fragment(), OnRefreshLoadMor
         )
         binding.root.layoutParams = params
         bindingBase.llytContent.addView(ViewTool.inflateFragmentPixels(activity, binding.root,1194, 834))
-        db = AppDataBase.getInstance(mContext)
+        db = AppDataBase.getInstance(mContext.applicationContext)
         if (db != null) {
             questionsDao = db?.getQuestionsDao()
         }

+ 7 - 2
baselib/src/main/java/com/yingyangfly/baselib/db/AppDataBase.kt

@@ -5,7 +5,10 @@ import androidx.room.Database
 import androidx.room.Room
 import androidx.room.RoomDatabase
 
-@Database(entities = [QuestionsBean::class, DoctorBean::class, VoicePlayerBean::class], version = 1)
+@Database(
+    entities = [QuestionsBean::class, DoctorBean::class, VoicePlayerBean::class, GameSoundBean::class],
+    version = 1
+)
 abstract class AppDataBase : RoomDatabase() {
 
     abstract fun getQuestionsDao(): QuestionsDao
@@ -14,6 +17,8 @@ abstract class AppDataBase : RoomDatabase() {
 
     abstract fun getVoicePlayerDao(): VoicePlayerDao
 
+    abstract fun getGameSoundDao(): GameSoundDao
+
     companion object {
         @Volatile
         private var sInstance: AppDataBase? = null
@@ -33,7 +38,7 @@ abstract class AppDataBase : RoomDatabase() {
 
         private fun createInstance(context: Context): AppDataBase {
             return Room.databaseBuilder(
-                context.applicationContext,
+                context,
                 AppDataBase::class.java,
                 DATA_BASE_NAME
             ).allowMainThreadQueries()

+ 22 - 0
baselib/src/main/java/com/yingyangfly/baselib/db/GameSoundBean.java

@@ -19,5 +19,27 @@ public class GameSoundBean {
     private String voiceUrl;//待合成词语
     private String voiceType;//合成连接
 
+    public int getId() {
+        return id;
+    }
 
+    public void setId(int id) {
+        this.id = id;
+    }
+
+    public String getVoiceUrl() {
+        return voiceUrl;
+    }
+
+    public void setVoiceUrl(String voiceUrl) {
+        this.voiceUrl = voiceUrl;
+    }
+
+    public String getVoiceType() {
+        return voiceType;
+    }
+
+    public void setVoiceType(String voiceType) {
+        this.voiceType = voiceType;
+    }
 }

+ 26 - 0
baselib/src/main/java/com/yingyangfly/baselib/db/GameSoundDao.kt

@@ -0,0 +1,26 @@
+package com.yingyangfly.baselib.db
+
+import androidx.room.*
+
+@Dao
+interface GameSoundDao : BaseDao<GameSoundBean> {
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    fun insert(element: GameSoundBean)
+
+    @Insert(onConflict = OnConflictStrategy.REPLACE)
+    override fun insertAll(list: MutableList<GameSoundBean>)
+
+    @Query("select * from GameSound")
+    fun getAllGameSoundBean(): MutableList<GameSoundBean>
+
+    @Query("select * from GameSound where voiceType = :voiceType")
+    fun getGameSound(voiceType: String): GameSoundBean
+
+    @Query("delete from GameSound")
+    fun deleteAll()
+
+    @Update
+    override fun update(element: GameSoundBean)
+
+}

+ 37 - 31
baselib/src/main/java/com/yingyangfly/baselib/dialog/TaskFragment.kt

@@ -3,6 +3,7 @@ package com.yingyangfly.baselib.dialog
 import android.annotation.SuppressLint
 import android.content.Context
 import android.os.Bundle
+import android.text.TextUtils
 import android.view.LayoutInflater
 import android.view.MotionEvent
 import android.view.View
@@ -17,6 +18,7 @@ import com.gyf.immersionbar.ktx.immersionBar
 import com.yingyangfly.baselib.R
 import com.yingyangfly.baselib.db.AppDataBase
 import com.yingyangfly.baselib.db.VoicePlayerBean
+import com.yingyangfly.baselib.db.VoicePlayerDao
 import com.yingyangfly.baselib.ext.setOnSingleClickListener
 import com.yingyangfly.baselib.ext.toast
 import com.yingyangfly.baselib.net.BaseObserver
@@ -34,13 +36,16 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
     private var taskDesn = ""
     private var fishImage: AppCompatImageView? = null
     private var tvContent: AppCompatTextView? = null
-    private var confirmBtan: AppCompatButton? = null
+    private var confirmBtn: AppCompatButton? = null
     private var id = ""
+    private var content: Context? = null
 
     /**
      * 语音合成
      */
     private var voicePlayer: VoicePlayer? = null
+    private var db: AppDataBase? = null
+    private var dao: VoicePlayerDao? = null
 
     var onDialogClickListener: ((bean: String) -> Unit)? = null
 
@@ -76,7 +81,12 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
         val rootView = ViewTool.inflateFragmentPixels(
             activity, R.layout.fragment_task, container, 1194, 834
         )
-        voicePlayer = VoicePlayer.getInstance(activity)
+        content = activity
+        voicePlayer = VoicePlayer.getInstance(content)
+        db = AppDataBase.getInstance(content!!.applicationContext)
+        if (db != null) {
+            dao = db?.getVoicePlayerDao()
+        }
         findId(rootView)
         init()
         return rootView
@@ -85,8 +95,8 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
     private fun findId(rootView: View) {
         fishImage = rootView.findViewById(R.id.fishImage)
         tvContent = rootView.findViewById(R.id.tvContent)
-        confirmBtan = rootView.findViewById(R.id.confirmBtan)
-        confirmBtan?.setOnSingleClickListener {
+        confirmBtn = rootView.findViewById(R.id.confirmBtn)
+        confirmBtn?.setOnSingleClickListener {
             onDialogClickListener?.invoke(id)
             dismiss()
         }
@@ -106,17 +116,11 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
      */
     private fun speak(taskDesn: String) {
         if (voicePlayer != null && voicePlayer?.isPlaying == false) {
-            val db = AppDataBase.getInstance(requireActivity())
-            if (db != null) {
-                val voicePlayerDao = db.getVoicePlayerDao()
-                if (voicePlayerDao != null) {
-                    val voicePlayerBean = voicePlayerDao.getVoicePlayerBean(taskDesn)
-                    if (voicePlayerBean != null && voicePlayer != null) {
-                        voicePlayer?.play(voicePlayerBean.url) {
-
-                        }
-                    } else {
-                        getWordsUrl(taskDesn)
+            if (dao != null) {
+                val voicePlayerBean = dao?.getVoicePlayerBean(taskDesn)
+                if (voicePlayerBean != null && voicePlayer != null) {
+                    voicePlayer?.play(voicePlayerBean.url) {
+
                     }
                 } else {
                     getWordsUrl(taskDesn)
@@ -134,24 +138,11 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
         BaselibServiceFactory.getService()
             .getVoiceUrl(word)
             .subscribeOn(Schedulers.io())
-            .compose(MyRxScheduler.ioMain(requireActivity(), false))
+            .compose(MyRxScheduler.ioMain(content!!, false))
             .subscribe(object : BaseObserver<String>() {
                 override fun onSuccess(t: String?) {
-                    t?.let {
-                        val db = AppDataBase.getInstance(requireActivity())
-                        if (db != null) {
-                            val voicePlayerDao = db.getVoicePlayerDao()
-                            if (voicePlayerDao != null) {
-                                val voicePlayerBean = VoicePlayerBean().apply {
-                                    url = it
-                                    words = taskDesn
-                                }
-                                voicePlayerDao.insert(voicePlayerBean)
-                            }
-                        }
-                        voicePlayer?.play(it) {
-
-                        }
+                    if (TextUtils.isEmpty(t).not()) {
+                        setData(t!!)
                     }
                 }
 
@@ -178,4 +169,19 @@ class TaskFragment : DialogFragment(), View.OnTouchListener {
         }
         super.dismiss()
     }
+
+    private fun setData(t: String) {
+        confirmBtn!!.postDelayed({
+            if (dao != null) {
+                val voicePlayerBean = VoicePlayerBean().apply {
+                    url = t
+                    words = taskDesn
+                }
+                dao?.insert(voicePlayerBean)
+            }
+            voicePlayer?.play(t) {
+
+            }
+        }, 300)
+    }
 }

+ 1 - 1
baselib/src/main/res/layout/fragment_task.xml

@@ -37,7 +37,7 @@
             android:textSize="@dimen/divider_28px" />
 
         <androidx.appcompat.widget.AppCompatButton
-            android:id="@+id/confirmBtan"
+            android:id="@+id/confirmBtn"
             android:layout_width="@dimen/divider_204px"
             android:layout_height="@dimen/divider_64px"
             android:layout_marginTop="@dimen/divider_25px"

+ 1 - 1
webview/src/main/java/com/yingyangfly/webview/BridgeWebActivity.kt

@@ -48,7 +48,7 @@ class BridgeWebActivity : AppCompatActivity() {
             hideBar(BarHide.FLAG_HIDE_BAR)
             navigationBarColor(com.yingyangfly.baselib.R.color.transparent)
         }
-        db = AppDataBase.getInstance(this)
+        db = AppDataBase.getInstance(this.applicationContext)
         if (db != null) {
             dao = db?.getVoicePlayerDao()
         }

+ 1 - 1
webview/src/main/java/com/yingyangfly/webview/TencentWebviewActivity.kt

@@ -38,7 +38,7 @@ class TencentWebviewActivity : AppCompatActivity() {
         }
         setContentView(R.layout.activity_tencent_webview)
         requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
-        db = AppDataBase.getInstance(this)
+        db = AppDataBase.getInstance(this.applicationContext)
         if (db != null) {
             dao = db?.getVoicePlayerDao()
         }