HomeActivity.kt 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. package com.yingyangfly.home.activity
  2. import android.annotation.SuppressLint
  3. import android.text.TextUtils
  4. import android.view.MotionEvent
  5. import android.view.View
  6. import androidx.recyclerview.widget.GridLayoutManager
  7. import com.alibaba.android.arouter.facade.annotation.Route
  8. import com.alibaba.android.arouter.launcher.ARouter
  9. import com.bumptech.glide.Glide
  10. import com.tencent.imsdk.v2.V2TIMCallback
  11. import com.tencent.imsdk.v2.V2TIMManager
  12. import com.yingyang.home.R
  13. import com.yingyang.home.databinding.ActivityHomeBinding
  14. import com.yingyangfly.baselib.bean.GetSaveGameRecordBean
  15. import com.yingyangfly.baselib.bean.Record
  16. import com.yingyangfly.baselib.db.VoicePlayerBean
  17. import com.yingyangfly.baselib.dialog.TaskFragment
  18. import com.yingyangfly.baselib.dialog.TipsDialog
  19. import com.yingyangfly.baselib.ext.getEndAnimation
  20. import com.yingyangfly.baselib.ext.getScaleAnimation
  21. import com.yingyangfly.baselib.ext.show
  22. import com.yingyangfly.baselib.ext.toast
  23. import com.yingyangfly.baselib.guideview.Guide
  24. import com.yingyangfly.baselib.guideview.GuideBuilder
  25. import com.yingyangfly.baselib.mvvm.BaseMVVMActivity
  26. import com.yingyangfly.baselib.player.VoicePlayer
  27. import com.yingyangfly.baselib.router.RouterUrlCommon
  28. import com.yingyangfly.baselib.utils.*
  29. import com.yingyangfly.home.adapter.GameAdapter
  30. import com.yingyangfly.home.component.ShowFunTimeViewComponent
  31. import com.yingyangfly.home.component.ShowHealthCounselingViewComponent
  32. import com.yingyangfly.home.component.ShowPlayPorpoiseViewComponent
  33. import com.yingyangfly.home.component.ShowProfessionalEvaluationViewComponent
  34. import com.yingyangfly.home.dialog.DownLoadAppFragment
  35. import com.yingyangfly.home.utils.AnimUtil
  36. /**
  37. * 新版首页
  38. */
  39. @Route(path = RouterUrlCommon.home)
  40. class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
  41. View.OnTouchListener {
  42. /**
  43. * 进行中的任务
  44. */
  45. private var currentTaskRecord: Record? = null
  46. private var taskList = mutableListOf<Record>()
  47. /**
  48. * 推荐游戏adapter
  49. */
  50. private var gameList = mutableListOf<Record>()
  51. private var isFirstPage = true
  52. /**
  53. * 语音合成
  54. */
  55. private var voicePlayer: VoicePlayer? = null
  56. //推荐游戏分页数据
  57. private var pageList = mutableListOf<Record>()
  58. private val gameAdapter by lazy { GameAdapter() }
  59. override fun initViews() {
  60. voicePlayer = VoicePlayer.getInstance(mContext)
  61. binding {
  62. rvGame.layoutManager = GridLayoutManager(this@HomeActivity, 2)
  63. rvGame.adapter = gameAdapter
  64. gameAdapter.onGameImageClickListener = {
  65. if (it.currentLevel == it.totalNum) {
  66. TipsDialog.TipDialogBuilder()
  67. .title("恭喜您")
  68. .content("当前游戏已通关,是否重新开始?")
  69. .leftBtnText("取消")
  70. .rightBtnText("确定")
  71. .leftClick({
  72. }, true)
  73. .rightClick({
  74. jumpWebView(it, true)
  75. }, true)
  76. .show(supportFragmentManager)
  77. } else {
  78. jumpWebView(it, false)
  79. }
  80. }
  81. }
  82. }
  83. @SuppressLint("ClickableViewAccessibility")
  84. override fun initListener() {
  85. binding {
  86. //用户头像
  87. imageSetting.setOnTouchListener(this@HomeActivity)
  88. //直播
  89. layoutLiveBroadcast.setOnTouchListener(this@HomeActivity)
  90. //使用帮助
  91. layoutHelp.setOnTouchListener(this@HomeActivity)
  92. //消息通知
  93. layoutNotify.setOnTouchListener(this@HomeActivity)
  94. //服务套餐
  95. layoutServicePackage.setOnTouchListener(this@HomeActivity)
  96. //畅玩蓝豚
  97. tvPlayPorpoise.setOnTouchListener(this@HomeActivity)
  98. //专业测评
  99. tvProfessionalEvaluation.setOnTouchListener(this@HomeActivity)
  100. //健康咨询
  101. tvHealthCounseling.setOnTouchListener(this@HomeActivity)
  102. //康复课堂
  103. tvfunTime.setOnTouchListener(this@HomeActivity)
  104. //查看
  105. btnCheck.setOnTouchListener(this@HomeActivity)
  106. //下一页
  107. btnNextPage.setOnTouchListener(this@HomeActivity)
  108. //开始专属训练
  109. btnTask.setOnTouchListener(this@HomeActivity)
  110. //开始训练蒙版
  111. opneBtn.setOnTouchListener(this@HomeActivity)
  112. }
  113. }
  114. override fun initData() {
  115. initLiveData()
  116. }
  117. override fun onResume() {
  118. super.onResume()
  119. val stastus = V2TIMManager.getInstance().loginStatus
  120. if (V2TIMManager.V2TIM_STATUS_LOGINED != stastus) {
  121. //IM未登录
  122. getUserSign()
  123. }
  124. //获取app升级信息
  125. selectNewVersion()
  126. //获取脑力值和训练时长接口
  127. getCountTrain()
  128. //获取未读消息数量
  129. getCountMyMsg()
  130. binding {
  131. gameLayout.show(false)
  132. interstellarLayout.show(false)
  133. effectLayout.show(true)
  134. Glide.with(this@HomeActivity)
  135. .asGif()
  136. .load(R.drawable.fish)
  137. .into(fishImage)
  138. }
  139. LiveEventBusUtil.send(RxBusCodes.FINISHGAME, "")
  140. }
  141. /**
  142. * 获取腾讯IM密钥
  143. */
  144. private fun getUserSign() {
  145. viewModel.getUserSign(User.getUserId(), fail = {
  146. }, success = {
  147. if (TextUtils.isEmpty(it).not()) {
  148. loginIM(it!!)
  149. }
  150. })
  151. }
  152. /**
  153. * 登录腾讯IM
  154. */
  155. private fun loginIM(sign: String) {
  156. runOnUiThread {
  157. V2TIMManager.getInstance().login(User.getUserId(), sign, object : V2TIMCallback {
  158. override fun onSuccess() {
  159. }
  160. override fun onError(p0: Int, p1: String?) {
  161. getUserSign()
  162. }
  163. })
  164. }
  165. }
  166. /**
  167. * 获取app升级信息
  168. */
  169. private fun selectNewVersion() {
  170. val versionCode = AppUtil.getVersionCode(mContext)
  171. viewModel.selectNewVersion(versionCode, fail = {
  172. launchTask()
  173. }, success = {
  174. if (it != null) {
  175. val downLoadAppFragment = DownLoadAppFragment()
  176. downLoadAppFragment.setDownloadUrl(mContext, it.versionUrl)
  177. downLoadAppFragment.show(supportFragmentManager, "downLoadAppFragment")
  178. } else {
  179. launchTask()
  180. }
  181. })
  182. }
  183. /**
  184. * 获取任务相关接口
  185. */
  186. private fun launchTask() {
  187. //获取任务列表
  188. getFindMyTask()
  189. if (TextUtils.equals("0", User.getFirstLogin())) {
  190. val content =
  191. "欢迎" + User.getName() + "使用未来蓝豚康复平台!为您提供专业的认知康复支持和训练。帮助您提升认知能力,重建自信。小豚期待与您一同启程!"
  192. showTaskDialog(content, "")
  193. } else {
  194. //获取任务状态弹窗
  195. getSelectHomePageMsg()
  196. }
  197. }
  198. /**
  199. * 查询首页任务提醒弹窗是否显示接口
  200. */
  201. private fun getSelectHomePageMsg() {
  202. viewModel.getSelectHomePageMsg("D", fail = {
  203. it.toast()
  204. }, success = {
  205. if (it != null) {
  206. showTaskDialog(it.msgDesn, it.id)
  207. }
  208. })
  209. }
  210. /**
  211. * 获取脑力值和训练时长接口
  212. */
  213. private fun getCountTrain() {
  214. viewModel.getCountTrain(fail = {
  215. it.toast()
  216. }, success = {
  217. if (it != null) {
  218. binding.data = it
  219. binding.tvBrainPowerValue.setEnableAnim(true)
  220. binding.tvBrainPowerValue.setNumberString(it.mentalPower)
  221. }
  222. })
  223. }
  224. /**
  225. * 获取未读消息数量
  226. */
  227. private fun getCountMyMsg() {
  228. viewModel.getCountMyMsg(fail = {
  229. it.toast()
  230. }, success = {
  231. if (it != null) {
  232. val num = it.toInt()
  233. binding.unReadLayout.show(num > 0)
  234. }
  235. })
  236. }
  237. /**
  238. * 查询是否有进行中的任务
  239. */
  240. private fun getFindMyTask() {
  241. viewModel.findMyCurrentTask(fail = {
  242. it.toast()
  243. }, success = {
  244. if (it.isNullOrEmpty().not()) {
  245. showLayout(false)
  246. it!![0].let {
  247. binding.task = it
  248. if (it.details.isEmpty().not()) {
  249. val index = it.currentTaskNum - 1
  250. currentTaskRecord = it.details[index]
  251. taskList.addAll(it.details)
  252. }
  253. if (it.currentTaskNum == it.totalTaskNum) {
  254. if (TextUtils.equals("0", it.status)) {
  255. //任务完成
  256. if (TextUtils.equals(User.getNowDay(), User.getTaskDialogStatus())
  257. .not()
  258. ) {
  259. User.saveTaskDialogStatus(User.getNowDay())
  260. showTipDialog()
  261. } else {
  262. showLayout(true)
  263. getGameList()
  264. }
  265. binding.btnTask.text = "继续专属训练"
  266. } else {
  267. binding.btnTask.text = if (it.currentTaskNum == 1) {
  268. "开始专属训练"
  269. } else {
  270. "继续专属训练"
  271. }
  272. showLayout(false)
  273. if (TextUtils.equals(User.getNowDay(), User.getTaskDialogStatus())
  274. .not()
  275. ) {
  276. User.saveTaskDialogStatus(User.getNowDay())
  277. }
  278. }
  279. } else {
  280. binding.btnTask.text = if (it.currentTaskNum == 1) {
  281. "开始专属训练"
  282. } else {
  283. "继续专属训练"
  284. }
  285. if (TextUtils.equals(User.getNowDay(), User.getTaskDialogStatus()).not()) {
  286. User.saveTaskDialogStatus(User.getNowDay())
  287. }
  288. }
  289. }
  290. } else {
  291. showLayout(true)
  292. getGameList()
  293. }
  294. })
  295. }
  296. /**
  297. * 训练完成弹窗页面
  298. */
  299. private fun showTipDialog() {
  300. TipsDialog.TipDialogBuilder()
  301. .title("恭喜您")
  302. .content("您完成了今天的专属训练!今天的训练报告已生成,快去查看吧!")
  303. .leftBtnText("稍后再看")
  304. .rightBtnText("立即查看")
  305. .leftClick({
  306. showLayout(true)
  307. getGameList()
  308. }, dimiss = true)
  309. .rightClick({
  310. JumpUtil.jumpActivity(RouterUrlCommon.trainData, mContext)
  311. }, dimiss = true)
  312. .show(supportFragmentManager)
  313. }
  314. /**
  315. * 欢迎/确定信息弹窗
  316. */
  317. private fun showTaskDialog(content: String, id: String) {
  318. val taskFragment = TaskFragment()
  319. taskFragment.setTaskDesn(content, id)
  320. taskFragment.onDialogClickListener = {
  321. if (id.isEmpty()) {
  322. if (TextUtils.equals("0", User.getFirstLogin())) {
  323. //第一次登录弹窗欢迎
  324. User.saveFirstLogin("1")
  325. binding.tvPlayPorpoise.post {
  326. showPlayPorpoiseView()
  327. }
  328. }
  329. } else {
  330. updateReadMsg(id)
  331. }
  332. }
  333. taskFragment.show(supportFragmentManager, "taskFragment")
  334. }
  335. /**
  336. * 修改未读消息状态
  337. */
  338. private fun updateReadMsg(id: String) {
  339. viewModel.updateReadMsg(id, fail = {
  340. getFindMyTask()
  341. }, success = {
  342. getFindMyTask()
  343. })
  344. }
  345. /**
  346. * 加载游戏数据
  347. */
  348. @SuppressLint("NotifyDataSetChanged")
  349. private fun getGameList() {
  350. gameList.clear()
  351. pageList.clear()
  352. viewModel.getGameList(fail = {
  353. it.toast()
  354. }, success = {
  355. if (it.isNullOrEmpty().not()) {
  356. gameList.clear()
  357. pageList.clear()
  358. gameList.addAll(it!!)
  359. binding.btnNextPage.show(it.size > 4)
  360. if (it.size > 4) {
  361. binding.btnNextPage.show(true)
  362. pageList.addAll(it.subList(0, 4))
  363. } else {
  364. binding.btnNextPage.show(false)
  365. pageList.addAll(it)
  366. }
  367. } else {
  368. binding.btnNextPage.show(false)
  369. }
  370. gameAdapter.setData(pageList)
  371. })
  372. }
  373. /**
  374. * 判断展示推荐游戏页面/待完成任务页面
  375. */
  376. private fun showLayout(isShow: Boolean) {
  377. binding {
  378. allLeftLayout.show(isShow)
  379. taskLayout.show(isShow.not())
  380. }
  381. }
  382. /**
  383. * 跳转游戏列表
  384. */
  385. private fun jumpWebView(bean: Record, isFull: Boolean) {
  386. ARouter.getInstance().build(RouterUrlCommon.gameIntroduction)
  387. .withString("gameCode", bean.gameCode)
  388. .withString("playClass", "A")
  389. .withString("videoUrl", bean.gameVideoUrl)
  390. .withString("gameInbetweenImage", bean.gameInbetweenImage)
  391. .withString("gameBackgroundImage", bean.gameBackgroundImage)
  392. .withString("desn", bean.desn)
  393. .withTransition(R.anim.leftin, R.anim.leftout)
  394. .navigation(mContext)
  395. }
  396. /**
  397. * 首次登陆添加引导窗
  398. */
  399. private fun showPlayPorpoiseView() {
  400. val builder = GuideBuilder()
  401. builder.setTargetView(binding.tvPlayPorpoise)
  402. .setAlpha(150)
  403. .setHighTargetCorner(20)
  404. .setHighTargetPadding(3)
  405. builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
  406. override fun onShown() {
  407. }
  408. override fun onDismiss() {
  409. //获取任务状态弹窗
  410. showProfessionalEvaluation()
  411. }
  412. })
  413. builder.addComponent(ShowPlayPorpoiseViewComponent())
  414. val guide: Guide = builder.createGuide()
  415. guide.show(this)
  416. }
  417. /**
  418. * 首次登陆添加引导窗
  419. */
  420. private fun showProfessionalEvaluation() {
  421. val builder = GuideBuilder()
  422. builder.setTargetView(binding.tvProfessionalEvaluation)
  423. .setAlpha(150)
  424. .setHighTargetCorner(20)
  425. .setHighTargetPadding(3)
  426. builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
  427. override fun onShown() {
  428. }
  429. override fun onDismiss() {
  430. showHealthCounseling()
  431. }
  432. })
  433. builder.addComponent(ShowProfessionalEvaluationViewComponent())
  434. val guide: Guide = builder.createGuide()
  435. guide.show(this)
  436. }
  437. /**
  438. * 首次登陆添加引导窗
  439. */
  440. private fun showHealthCounseling() {
  441. val builder = GuideBuilder()
  442. builder.setTargetView(binding.tvHealthCounseling)
  443. .setAlpha(150)
  444. .setHighTargetCorner(20)
  445. .setHighTargetPadding(3)
  446. builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
  447. override fun onShown() {
  448. }
  449. override fun onDismiss() {
  450. showFunTime()
  451. }
  452. })
  453. builder.addComponent(ShowHealthCounselingViewComponent())
  454. val guide: Guide = builder.createGuide()
  455. guide.show(this)
  456. }
  457. /**
  458. * 首次登陆添加引导窗
  459. */
  460. private fun showFunTime() {
  461. val builder = GuideBuilder()
  462. builder.setTargetView(binding.tvfunTime)
  463. .setAlpha(150)
  464. .setHighTargetCorner(20)
  465. .setHighTargetPadding(3)
  466. builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
  467. override fun onShown() {
  468. }
  469. override fun onDismiss() {
  470. //获取任务状态弹窗
  471. getSelectHomePageMsg()
  472. }
  473. })
  474. builder.addComponent(ShowFunTimeViewComponent())
  475. val guide: Guide = builder.createGuide()
  476. guide.show(this)
  477. }
  478. @SuppressLint("ClickableViewAccessibility")
  479. override fun onTouch(v: View, event: MotionEvent): Boolean {
  480. when (event.action) {
  481. MotionEvent.ACTION_DOWN -> {
  482. if (v.id == R.id.imageSetting || v.id == R.id.layoutLiveBroadcast || v.id == R.id.layoutHelp ||
  483. v.id == R.id.layoutNotify || v.id == R.id.layoutServicePackage || v.id == R.id.tvPlayPorpoise ||
  484. v.id == R.id.tvProfessionalEvaluation || v.id == R.id.tvHealthCounseling || v.id == R.id.tvfunTime ||
  485. v.id == R.id.btnCheck || v.id == R.id.btnNextPage || v.id == R.id.btnTask || v.id == R.id.opneBtn
  486. ) {
  487. v.startAnimation(getScaleAnimation())
  488. }
  489. }
  490. MotionEvent.ACTION_UP -> {
  491. v.startAnimation(getEndAnimation())
  492. when (v.id) {
  493. R.id.imageSetting -> {
  494. //用户头像
  495. JumpUtil.jumpActivity(RouterUrlCommon.personalCenter, mContext)
  496. }
  497. R.id.layoutLiveBroadcast -> {
  498. //直播
  499. JumpUtil.jumpActivity(RouterUrlCommon.liveBroadcastList, mContext)
  500. }
  501. R.id.layoutHelp -> {
  502. //使用帮助
  503. JumpUtil.jumpActivity(RouterUrlCommon.help, mContext)
  504. }
  505. R.id.layoutNotify -> {
  506. //消息通知
  507. JumpUtil.jumpActivity(RouterUrlCommon.messageList, mContext)
  508. }
  509. R.id.layoutServicePackage -> {
  510. //服务套餐
  511. JumpUtil.jumpActivity(RouterUrlCommon.servicePackage, mContext)
  512. }
  513. R.id.tvPlayPorpoise -> {
  514. //畅玩蓝豚
  515. JumpUtil.jumpActivity(RouterUrlCommon.freeTrain, mContext)
  516. }
  517. R.id.tvProfessionalEvaluation -> {
  518. //专业测评
  519. JumpUtil.jumpActivity(RouterUrlCommon.evaluation, mContext)
  520. }
  521. R.id.tvHealthCounseling -> {
  522. //健康咨询
  523. JumpUtil.jumpActivity(RouterUrlCommon.healthConsultation, mContext)
  524. }
  525. R.id.tvfunTime -> {
  526. //康复课堂
  527. JumpUtil.jumpActivity(RouterUrlCommon.leisureBrain, mContext)
  528. }
  529. R.id.btnCheck -> {
  530. //查看
  531. JumpUtil.jumpActivity(RouterUrlCommon.trainData, mContext)
  532. }
  533. R.id.btnNextPage -> {
  534. //下一页
  535. pageList.clear()
  536. if (isFirstPage) {
  537. binding.btnNextPage.text = "上一页"
  538. isFirstPage = false
  539. pageList.addAll(gameList.subList(4, gameList.size))
  540. } else {
  541. binding.btnNextPage.text = "下一页"
  542. isFirstPage = true
  543. pageList.addAll(gameList.subList(0, 4))
  544. }
  545. gameAdapter.setData(pageList)
  546. }
  547. R.id.btnTask -> {
  548. //开始专属训练
  549. if (currentTaskRecord != null) {
  550. gameLoading(currentTaskRecord!!)
  551. }
  552. }
  553. //开始训练蒙版
  554. R.id.opneBtn -> {
  555. binding {
  556. effectLayout.show(false)
  557. interstellarLayout.show(true)
  558. opneBtn.post {
  559. AnimUtil.setTranslateAnimationLeft(
  560. binding.leftDoorImage,
  561. binding.gameLayout,
  562. 2500
  563. )
  564. AnimUtil.setTranslateAnimationRight(binding.rightDoorImage, 2500)
  565. }
  566. }
  567. }
  568. }
  569. }
  570. MotionEvent.ACTION_CANCEL -> {
  571. if (v.id == R.id.imageSetting || v.id == R.id.layoutLiveBroadcast || v.id == R.id.layoutHelp ||
  572. v.id == R.id.layoutNotify || v.id == R.id.layoutServicePackage || v.id == R.id.tvPlayPorpoise ||
  573. v.id == R.id.tvProfessionalEvaluation || v.id == R.id.tvHealthCounseling || v.id == R.id.tvfunTime ||
  574. v.id == R.id.btnCheck || v.id == R.id.btnNextPage || v.id == R.id.btnTask || v.id == R.id.opneBtn
  575. ) {
  576. v.startAnimation(getEndAnimation())
  577. }
  578. }
  579. }
  580. return true
  581. }
  582. /**
  583. * 跳转游戏页面
  584. */
  585. private fun gameLoading(bean: Record) {
  586. ARouter.getInstance().build(RouterUrlCommon.gameIntroduction)
  587. .withString("gameCode", bean.gameCode)
  588. .withString("playClass", "A")
  589. .withString("videoUrl", bean.gameVideoUrl)
  590. .withString("gameInbetweenImage", bean.gameInbetweenImage)
  591. .withString("gameBackgroundImage", bean.gameBackgroundImage)
  592. .withString("desn", bean.desn)
  593. .withTransition(R.anim.leftin, R.anim.leftout)
  594. .navigation(mContext)
  595. }
  596. private fun initLiveData() {
  597. //上报游戏进度
  598. LiveEventBusUtil.observer<GetSaveGameRecordBean>(this, RxBusCodes.REPORTGAMERECORDS) {
  599. if (it != null) {
  600. saveData(it)
  601. }
  602. }
  603. //语音合成
  604. LiveEventBusUtil.observer<String>(this, RxBusCodes.SPEECHSYNTHESIS) {
  605. if (TextUtils.isEmpty(it).not()) {
  606. speak(it)
  607. }
  608. }
  609. //结束语音播放
  610. LiveEventBusUtil.observer<String>(this, RxBusCodes.STOPVOICE) {
  611. if (voicePlayer != null) {
  612. if (voicePlayer!!.isPlaying) {
  613. voicePlayer?.stop()
  614. }
  615. }
  616. }
  617. }
  618. /**
  619. * 保存游戏进度
  620. */
  621. private fun saveData(bean: GetSaveGameRecordBean) {
  622. viewModel.saveGameRecord(bean, fail = {
  623. }, success = {
  624. })
  625. }
  626. /**
  627. * 语音合成
  628. */
  629. private fun speak(desn: String) {
  630. if (db != null) {
  631. val voicePlayerDao = db?.getVoicePlayerDao()
  632. if (voicePlayerDao != null) {
  633. val voicePlayerBean = voicePlayerDao.getVoicePlayerBean(desn)
  634. if (voicePlayerBean != null) {
  635. if (voicePlayer != null) {
  636. voicePlayer?.play(voicePlayerBean.url) {
  637. LiveEventBusUtil.send(RxBusCodes.STARTMUSIC, "")
  638. }
  639. }
  640. } else {
  641. getVoiceUrl(desn)
  642. }
  643. } else {
  644. getVoiceUrl(desn)
  645. }
  646. } else {
  647. getVoiceUrl(desn)
  648. }
  649. }
  650. /**
  651. * 获取声音url
  652. */
  653. private fun getVoiceUrl(taskDesn: String) {
  654. viewModel.getVoiceUrl(taskDesn, fail = {
  655. }, success = {
  656. if (TextUtils.isEmpty(it).not()) {
  657. if (db != null) {
  658. val voicePlayerDao = db?.getVoicePlayerDao()
  659. if (voicePlayerDao != null) {
  660. val voicePlayerBean = VoicePlayerBean().apply {
  661. url = it
  662. words = taskDesn
  663. }
  664. voicePlayerDao.insert(voicePlayerBean)
  665. }
  666. }
  667. if (voicePlayer != null) {
  668. voicePlayer?.play(it) {
  669. }
  670. }
  671. }
  672. })
  673. }
  674. }