| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715 |
- package com.yingyangfly.home.activity
- import android.annotation.SuppressLint
- import android.text.TextUtils
- import android.view.MotionEvent
- import android.view.View
- import androidx.recyclerview.widget.GridLayoutManager
- import com.alibaba.android.arouter.facade.annotation.Route
- import com.alibaba.android.arouter.launcher.ARouter
- import com.bumptech.glide.Glide
- import com.tencent.imsdk.v2.V2TIMCallback
- import com.tencent.imsdk.v2.V2TIMManager
- import com.yingyang.home.R
- import com.yingyang.home.databinding.ActivityHomeBinding
- import com.yingyangfly.baselib.bean.GetSaveGameRecordBean
- import com.yingyangfly.baselib.bean.Record
- import com.yingyangfly.baselib.db.VoicePlayerBean
- import com.yingyangfly.baselib.dialog.TaskFragment
- import com.yingyangfly.baselib.dialog.TipsDialog
- import com.yingyangfly.baselib.ext.getEndAnimation
- import com.yingyangfly.baselib.ext.getScaleAnimation
- import com.yingyangfly.baselib.ext.show
- import com.yingyangfly.baselib.ext.toast
- import com.yingyangfly.baselib.guideview.Guide
- import com.yingyangfly.baselib.guideview.GuideBuilder
- import com.yingyangfly.baselib.mvvm.BaseMVVMActivity
- import com.yingyangfly.baselib.player.VoicePlayer
- import com.yingyangfly.baselib.router.RouterUrlCommon
- import com.yingyangfly.baselib.utils.*
- import com.yingyangfly.home.adapter.GameAdapter
- import com.yingyangfly.home.component.ShowFunTimeViewComponent
- import com.yingyangfly.home.component.ShowHealthCounselingViewComponent
- import com.yingyangfly.home.component.ShowPlayPorpoiseViewComponent
- import com.yingyangfly.home.component.ShowProfessionalEvaluationViewComponent
- import com.yingyangfly.home.dialog.DownLoadAppFragment
- import com.yingyangfly.home.utils.AnimUtil
- /**
- * 新版首页
- */
- @Route(path = RouterUrlCommon.home)
- class HomeActivity : BaseMVVMActivity<ActivityHomeBinding, HomeViewModel>(),
- View.OnTouchListener {
- /**
- * 进行中的任务
- */
- private var currentTaskRecord: Record? = null
- private var taskList = mutableListOf<Record>()
- /**
- * 推荐游戏adapter
- */
- private var gameList = mutableListOf<Record>()
- private var isFirstPage = true
- /**
- * 语音合成
- */
- private var voicePlayer: VoicePlayer? = null
- //推荐游戏分页数据
- private var pageList = mutableListOf<Record>()
- private val gameAdapter by lazy { GameAdapter() }
- override fun initViews() {
- voicePlayer = VoicePlayer.getInstance(mContext)
- binding {
- rvGame.layoutManager = GridLayoutManager(this@HomeActivity, 2)
- rvGame.adapter = gameAdapter
- gameAdapter.onGameImageClickListener = {
- if (it.currentLevel == it.totalNum) {
- TipsDialog.TipDialogBuilder()
- .title("恭喜您")
- .content("当前游戏已通关,是否重新开始?")
- .leftBtnText("取消")
- .rightBtnText("确定")
- .leftClick({
- }, true)
- .rightClick({
- jumpWebView(it, true)
- }, true)
- .show(supportFragmentManager)
- } else {
- jumpWebView(it, false)
- }
- }
- }
- }
- @SuppressLint("ClickableViewAccessibility")
- override fun initListener() {
- binding {
- //用户头像
- imageSetting.setOnTouchListener(this@HomeActivity)
- //直播
- layoutLiveBroadcast.setOnTouchListener(this@HomeActivity)
- //使用帮助
- layoutHelp.setOnTouchListener(this@HomeActivity)
- //消息通知
- layoutNotify.setOnTouchListener(this@HomeActivity)
- //服务套餐
- layoutServicePackage.setOnTouchListener(this@HomeActivity)
- //畅玩蓝豚
- tvPlayPorpoise.setOnTouchListener(this@HomeActivity)
- //专业测评
- tvProfessionalEvaluation.setOnTouchListener(this@HomeActivity)
- //健康咨询
- tvHealthCounseling.setOnTouchListener(this@HomeActivity)
- //康复课堂
- tvfunTime.setOnTouchListener(this@HomeActivity)
- //查看
- btnCheck.setOnTouchListener(this@HomeActivity)
- //下一页
- btnNextPage.setOnTouchListener(this@HomeActivity)
- //开始专属训练
- btnTask.setOnTouchListener(this@HomeActivity)
- //开始训练蒙版
- opneBtn.setOnTouchListener(this@HomeActivity)
- }
- }
- override fun initData() {
- initLiveData()
- }
- override fun onResume() {
- super.onResume()
- val stastus = V2TIMManager.getInstance().loginStatus
- if (V2TIMManager.V2TIM_STATUS_LOGINED != stastus) {
- //IM未登录
- getUserSign()
- }
- //获取app升级信息
- selectNewVersion()
- //获取脑力值和训练时长接口
- getCountTrain()
- //获取未读消息数量
- getCountMyMsg()
- binding {
- gameLayout.show(false)
- interstellarLayout.show(false)
- effectLayout.show(true)
- Glide.with(this@HomeActivity)
- .asGif()
- .load(R.drawable.fish)
- .into(fishImage)
- }
- LiveEventBusUtil.send(RxBusCodes.FINISHGAME, "")
- }
- /**
- * 获取腾讯IM密钥
- */
- private fun getUserSign() {
- viewModel.getUserSign(User.getUserId(), fail = {
- }, success = {
- if (TextUtils.isEmpty(it).not()) {
- loginIM(it!!)
- }
- })
- }
- /**
- * 登录腾讯IM
- */
- private fun loginIM(sign: String) {
- runOnUiThread {
- V2TIMManager.getInstance().login(User.getUserId(), sign, object : V2TIMCallback {
- override fun onSuccess() {
- }
- override fun onError(p0: Int, p1: String?) {
- getUserSign()
- }
- })
- }
- }
- /**
- * 获取app升级信息
- */
- private fun selectNewVersion() {
- val versionCode = AppUtil.getVersionCode(mContext)
- viewModel.selectNewVersion(versionCode, fail = {
- launchTask()
- }, success = {
- if (it != null) {
- val downLoadAppFragment = DownLoadAppFragment()
- downLoadAppFragment.setDownloadUrl(mContext, it.versionUrl)
- downLoadAppFragment.show(supportFragmentManager, "downLoadAppFragment")
- } else {
- launchTask()
- }
- })
- }
- /**
- * 获取任务相关接口
- */
- private fun launchTask() {
- //获取任务列表
- getFindMyTask()
- if (TextUtils.equals("0", User.getFirstLogin())) {
- val content =
- "欢迎" + User.getName() + "使用未来蓝豚康复平台!为您提供专业的认知康复支持和训练。帮助您提升认知能力,重建自信。小豚期待与您一同启程!"
- showTaskDialog(content, "")
- } else {
- //获取任务状态弹窗
- getSelectHomePageMsg()
- }
- }
- /**
- * 查询首页任务提醒弹窗是否显示接口
- */
- private fun getSelectHomePageMsg() {
- viewModel.getSelectHomePageMsg("D", fail = {
- it.toast()
- }, success = {
- if (it != null) {
- showTaskDialog(it.msgDesn, it.id)
- }
- })
- }
- /**
- * 获取脑力值和训练时长接口
- */
- private fun getCountTrain() {
- viewModel.getCountTrain(fail = {
- it.toast()
- }, success = {
- if (it != null) {
- binding.data = it
- binding.tvBrainPowerValue.setEnableAnim(true)
- binding.tvBrainPowerValue.setNumberString(it.mentalPower)
- }
- })
- }
- /**
- * 获取未读消息数量
- */
- private fun getCountMyMsg() {
- viewModel.getCountMyMsg(fail = {
- it.toast()
- }, success = {
- if (it != null) {
- val num = it.toInt()
- binding.unReadLayout.show(num > 0)
- }
- })
- }
- /**
- * 查询是否有进行中的任务
- */
- private fun getFindMyTask() {
- viewModel.findMyCurrentTask(fail = {
- it.toast()
- }, success = {
- if (it.isNullOrEmpty().not()) {
- showLayout(false)
- it!![0].let {
- binding.task = it
- if (it.details.isEmpty().not()) {
- val index = it.currentTaskNum - 1
- currentTaskRecord = it.details[index]
- taskList.addAll(it.details)
- }
- if (it.currentTaskNum == it.totalTaskNum) {
- if (TextUtils.equals("0", it.status)) {
- //任务完成
- if (TextUtils.equals(User.getNowDay(), User.getTaskDialogStatus())
- .not()
- ) {
- User.saveTaskDialogStatus(User.getNowDay())
- showTipDialog()
- } else {
- showLayout(true)
- getGameList()
- }
- binding.btnTask.text = "继续专属训练"
- } else {
- binding.btnTask.text = if (it.currentTaskNum == 1) {
- "开始专属训练"
- } else {
- "继续专属训练"
- }
- showLayout(false)
- if (TextUtils.equals(User.getNowDay(), User.getTaskDialogStatus())
- .not()
- ) {
- User.saveTaskDialogStatus(User.getNowDay())
- }
- }
- } else {
- binding.btnTask.text = if (it.currentTaskNum == 1) {
- "开始专属训练"
- } else {
- "继续专属训练"
- }
- if (TextUtils.equals(User.getNowDay(), User.getTaskDialogStatus()).not()) {
- User.saveTaskDialogStatus(User.getNowDay())
- }
- }
- }
- } else {
- showLayout(true)
- getGameList()
- }
- })
- }
- /**
- * 训练完成弹窗页面
- */
- private fun showTipDialog() {
- TipsDialog.TipDialogBuilder()
- .title("恭喜您")
- .content("您完成了今天的专属训练!今天的训练报告已生成,快去查看吧!")
- .leftBtnText("稍后再看")
- .rightBtnText("立即查看")
- .leftClick({
- showLayout(true)
- getGameList()
- }, dimiss = true)
- .rightClick({
- JumpUtil.jumpActivity(RouterUrlCommon.trainData, mContext)
- }, dimiss = true)
- .show(supportFragmentManager)
- }
- /**
- * 欢迎/确定信息弹窗
- */
- private fun showTaskDialog(content: String, id: String) {
- val taskFragment = TaskFragment()
- taskFragment.setTaskDesn(content, id)
- taskFragment.onDialogClickListener = {
- if (id.isEmpty()) {
- if (TextUtils.equals("0", User.getFirstLogin())) {
- //第一次登录弹窗欢迎
- User.saveFirstLogin("1")
- binding.tvPlayPorpoise.post {
- showPlayPorpoiseView()
- }
- }
- } else {
- updateReadMsg(id)
- }
- }
- taskFragment.show(supportFragmentManager, "taskFragment")
- }
- /**
- * 修改未读消息状态
- */
- private fun updateReadMsg(id: String) {
- viewModel.updateReadMsg(id, fail = {
- getFindMyTask()
- }, success = {
- getFindMyTask()
- })
- }
- /**
- * 加载游戏数据
- */
- @SuppressLint("NotifyDataSetChanged")
- private fun getGameList() {
- gameList.clear()
- pageList.clear()
- viewModel.getGameList(fail = {
- it.toast()
- }, success = {
- if (it.isNullOrEmpty().not()) {
- gameList.clear()
- pageList.clear()
- gameList.addAll(it!!)
- binding.btnNextPage.show(it.size > 4)
- if (it.size > 4) {
- binding.btnNextPage.show(true)
- pageList.addAll(it.subList(0, 4))
- } else {
- binding.btnNextPage.show(false)
- pageList.addAll(it)
- }
- } else {
- binding.btnNextPage.show(false)
- }
- gameAdapter.setData(pageList)
- })
- }
- /**
- * 判断展示推荐游戏页面/待完成任务页面
- */
- private fun showLayout(isShow: Boolean) {
- binding {
- allLeftLayout.show(isShow)
- taskLayout.show(isShow.not())
- }
- }
- /**
- * 跳转游戏列表
- */
- private fun jumpWebView(bean: Record, isFull: Boolean) {
- ARouter.getInstance().build(RouterUrlCommon.gameIntroduction)
- .withString("gameCode", bean.gameCode)
- .withString("playClass", "A")
- .withString("videoUrl", bean.gameVideoUrl)
- .withString("gameInbetweenImage", bean.gameInbetweenImage)
- .withString("gameBackgroundImage", bean.gameBackgroundImage)
- .withString("desn", bean.desn)
- .withTransition(R.anim.leftin, R.anim.leftout)
- .navigation(mContext)
- }
- /**
- * 首次登陆添加引导窗
- */
- private fun showPlayPorpoiseView() {
- val builder = GuideBuilder()
- builder.setTargetView(binding.tvPlayPorpoise)
- .setAlpha(150)
- .setHighTargetCorner(20)
- .setHighTargetPadding(3)
- builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
- override fun onShown() {
- }
- override fun onDismiss() {
- //获取任务状态弹窗
- showProfessionalEvaluation()
- }
- })
- builder.addComponent(ShowPlayPorpoiseViewComponent())
- val guide: Guide = builder.createGuide()
- guide.show(this)
- }
- /**
- * 首次登陆添加引导窗
- */
- private fun showProfessionalEvaluation() {
- val builder = GuideBuilder()
- builder.setTargetView(binding.tvProfessionalEvaluation)
- .setAlpha(150)
- .setHighTargetCorner(20)
- .setHighTargetPadding(3)
- builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
- override fun onShown() {
- }
- override fun onDismiss() {
- showHealthCounseling()
- }
- })
- builder.addComponent(ShowProfessionalEvaluationViewComponent())
- val guide: Guide = builder.createGuide()
- guide.show(this)
- }
- /**
- * 首次登陆添加引导窗
- */
- private fun showHealthCounseling() {
- val builder = GuideBuilder()
- builder.setTargetView(binding.tvHealthCounseling)
- .setAlpha(150)
- .setHighTargetCorner(20)
- .setHighTargetPadding(3)
- builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
- override fun onShown() {
- }
- override fun onDismiss() {
- showFunTime()
- }
- })
- builder.addComponent(ShowHealthCounselingViewComponent())
- val guide: Guide = builder.createGuide()
- guide.show(this)
- }
- /**
- * 首次登陆添加引导窗
- */
- private fun showFunTime() {
- val builder = GuideBuilder()
- builder.setTargetView(binding.tvfunTime)
- .setAlpha(150)
- .setHighTargetCorner(20)
- .setHighTargetPadding(3)
- builder.setOnVisibilityChangedListener(object : GuideBuilder.OnVisibilityChangedListener {
- override fun onShown() {
- }
- override fun onDismiss() {
- //获取任务状态弹窗
- getSelectHomePageMsg()
- }
- })
- builder.addComponent(ShowFunTimeViewComponent())
- val guide: Guide = builder.createGuide()
- guide.show(this)
- }
- @SuppressLint("ClickableViewAccessibility")
- override fun onTouch(v: View, event: MotionEvent): Boolean {
- when (event.action) {
- MotionEvent.ACTION_DOWN -> {
- if (v.id == R.id.imageSetting || v.id == R.id.layoutLiveBroadcast || v.id == R.id.layoutHelp ||
- v.id == R.id.layoutNotify || v.id == R.id.layoutServicePackage || v.id == R.id.tvPlayPorpoise ||
- v.id == R.id.tvProfessionalEvaluation || v.id == R.id.tvHealthCounseling || v.id == R.id.tvfunTime ||
- v.id == R.id.btnCheck || v.id == R.id.btnNextPage || v.id == R.id.btnTask || v.id == R.id.opneBtn
- ) {
- v.startAnimation(getScaleAnimation())
- }
- }
- MotionEvent.ACTION_UP -> {
- v.startAnimation(getEndAnimation())
- when (v.id) {
- R.id.imageSetting -> {
- //用户头像
- JumpUtil.jumpActivity(RouterUrlCommon.personalCenter, mContext)
- }
- R.id.layoutLiveBroadcast -> {
- //直播
- JumpUtil.jumpActivity(RouterUrlCommon.liveBroadcastList, mContext)
- }
- R.id.layoutHelp -> {
- //使用帮助
- JumpUtil.jumpActivity(RouterUrlCommon.help, mContext)
- }
- R.id.layoutNotify -> {
- //消息通知
- JumpUtil.jumpActivity(RouterUrlCommon.messageList, mContext)
- }
- R.id.layoutServicePackage -> {
- //服务套餐
- JumpUtil.jumpActivity(RouterUrlCommon.servicePackage, mContext)
- }
- R.id.tvPlayPorpoise -> {
- //畅玩蓝豚
- JumpUtil.jumpActivity(RouterUrlCommon.freeTrain, mContext)
- }
- R.id.tvProfessionalEvaluation -> {
- //专业测评
- JumpUtil.jumpActivity(RouterUrlCommon.evaluation, mContext)
- }
- R.id.tvHealthCounseling -> {
- //健康咨询
- JumpUtil.jumpActivity(RouterUrlCommon.healthConsultation, mContext)
- }
- R.id.tvfunTime -> {
- //康复课堂
- JumpUtil.jumpActivity(RouterUrlCommon.leisureBrain, mContext)
- }
- R.id.btnCheck -> {
- //查看
- JumpUtil.jumpActivity(RouterUrlCommon.trainData, mContext)
- }
- R.id.btnNextPage -> {
- //下一页
- pageList.clear()
- if (isFirstPage) {
- binding.btnNextPage.text = "上一页"
- isFirstPage = false
- pageList.addAll(gameList.subList(4, gameList.size))
- } else {
- binding.btnNextPage.text = "下一页"
- isFirstPage = true
- pageList.addAll(gameList.subList(0, 4))
- }
- gameAdapter.setData(pageList)
- }
- R.id.btnTask -> {
- //开始专属训练
- if (currentTaskRecord != null) {
- gameLoading(currentTaskRecord!!)
- }
- }
- //开始训练蒙版
- R.id.opneBtn -> {
- binding {
- effectLayout.show(false)
- interstellarLayout.show(true)
- opneBtn.post {
- AnimUtil.setTranslateAnimationLeft(
- binding.leftDoorImage,
- binding.gameLayout,
- 2500
- )
- AnimUtil.setTranslateAnimationRight(binding.rightDoorImage, 2500)
- }
- }
- }
- }
- }
- MotionEvent.ACTION_CANCEL -> {
- if (v.id == R.id.imageSetting || v.id == R.id.layoutLiveBroadcast || v.id == R.id.layoutHelp ||
- v.id == R.id.layoutNotify || v.id == R.id.layoutServicePackage || v.id == R.id.tvPlayPorpoise ||
- v.id == R.id.tvProfessionalEvaluation || v.id == R.id.tvHealthCounseling || v.id == R.id.tvfunTime ||
- v.id == R.id.btnCheck || v.id == R.id.btnNextPage || v.id == R.id.btnTask || v.id == R.id.opneBtn
- ) {
- v.startAnimation(getEndAnimation())
- }
- }
- }
- return true
- }
- /**
- * 跳转游戏页面
- */
- private fun gameLoading(bean: Record) {
- ARouter.getInstance().build(RouterUrlCommon.gameIntroduction)
- .withString("gameCode", bean.gameCode)
- .withString("playClass", "A")
- .withString("videoUrl", bean.gameVideoUrl)
- .withString("gameInbetweenImage", bean.gameInbetweenImage)
- .withString("gameBackgroundImage", bean.gameBackgroundImage)
- .withString("desn", bean.desn)
- .withTransition(R.anim.leftin, R.anim.leftout)
- .navigation(mContext)
- }
- private fun initLiveData() {
- //上报游戏进度
- LiveEventBusUtil.observer<GetSaveGameRecordBean>(this, RxBusCodes.REPORTGAMERECORDS) {
- if (it != null) {
- saveData(it)
- }
- }
- //语音合成
- LiveEventBusUtil.observer<String>(this, RxBusCodes.SPEECHSYNTHESIS) {
- if (TextUtils.isEmpty(it).not()) {
- speak(it)
- }
- }
- //结束语音播放
- LiveEventBusUtil.observer<String>(this, RxBusCodes.STOPVOICE) {
- if (voicePlayer != null) {
- if (voicePlayer!!.isPlaying) {
- voicePlayer?.stop()
- }
- }
- }
- }
- /**
- * 保存游戏进度
- */
- private fun saveData(bean: GetSaveGameRecordBean) {
- viewModel.saveGameRecord(bean, fail = {
- }, success = {
- })
- }
- /**
- * 语音合成
- */
- private fun speak(desn: String) {
- if (db != null) {
- val voicePlayerDao = db?.getVoicePlayerDao()
- if (voicePlayerDao != null) {
- val voicePlayerBean = voicePlayerDao.getVoicePlayerBean(desn)
- if (voicePlayerBean != null) {
- if (voicePlayer != null) {
- voicePlayer?.play(voicePlayerBean.url) {
- LiveEventBusUtil.send(RxBusCodes.STARTMUSIC, "")
- }
- }
- } else {
- getVoiceUrl(desn)
- }
- } else {
- getVoiceUrl(desn)
- }
- } else {
- getVoiceUrl(desn)
- }
- }
- /**
- * 获取声音url
- */
- private fun getVoiceUrl(taskDesn: String) {
- viewModel.getVoiceUrl(taskDesn, fail = {
- }, success = {
- if (TextUtils.isEmpty(it).not()) {
- if (db != null) {
- val voicePlayerDao = db?.getVoicePlayerDao()
- if (voicePlayerDao != null) {
- val voicePlayerBean = VoicePlayerBean().apply {
- url = it
- words = taskDesn
- }
- voicePlayerDao.insert(voicePlayerBean)
- }
- }
- if (voicePlayer != null) {
- voicePlayer?.play(it) {
- }
- }
- }
- })
- }
- }
|