|
|
@@ -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)
|
|
|
+ }
|
|
|
}
|