Commit d79b500f by 王佳洋

1,音频页面mvp分离

parent 8f1f52f0
package com.yidianling.course.coursePlay.presenter
import android.content.Intent
import android.text.TextUtils
import com.ydl.media.audio.model.Music
import com.ydl.media.view.PlayTypeEnum
import com.ydl.media.view.PlayerFloatHelper
import com.ydl.ydlcommon.data.http.BaseResponse
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.mvp.base.BasePresenter
import com.ydl.ydlcommon.router.IYDLRouterConstant
import com.ydl.ydlcommon.router.YdlCommonRouterManager
import com.ydl.ydlnet.client.observer.CommonObserver
import com.yidianling.common.tools.ToastUtil
import com.yidianling.course.bean.COURSE_AUDIO
import com.yidianling.course.bean.CourseMediaBean
import com.yidianling.course.bean.CourseMediaDetailBean
import com.yidianling.course.coursePlay.model.AudioPlayModelImpl
import org.json.JSONException
import org.json.JSONObject
import java.util.*
class AudioPlayPresenter : BasePresenter<IAudioPlayContract.View, IAudioPlayContract.Model>(),
IAudioPlayContract.Presenter {
private val ROUTER_PARAMS = "routerParam"
private var mFrom: Int = 0
private var mReceiverPlayUrl: String? = null // 外部选中要播放的音/视频文件地址
private var mIsFromFloatView = false // 是否是悬浮窗进入
private var mCourPlayBean: CourseMediaDetailBean? = null
private val mPlayList: ArrayList<CourseMediaBean> = ArrayList() // 播放列表
private var mCurrentPosition = 0
private var mCourseId = 0 // 课程id
override fun createModel(): IAudioPlayContract.Model = AudioPlayModelImpl()
override fun getCoursePlayData(courseId: Int) {
override fun getParams(intent: Intent) {
intent.let {
if (it.hasExtra(ROUTER_PARAMS)) {
//路由传递的入参
val json = it.getStringExtra(ROUTER_PARAMS)
try {
val jsonObject = JSONObject(json)
//课程ID
mCourseId = jsonObject.getInt(IYDLRouterConstant.EXTRA_ID)
} catch (e: JSONException) {
e.printStackTrace()
}
return
}
//正常跳转所传参数
mCourseId = it.getIntExtra("course_id", 0)
mReceiverPlayUrl = it.getStringExtra("coursePlayUrl")
mIsFromFloatView = it.getBooleanExtra("isFromFloatView", false)
if (!TextUtils.isEmpty(mReceiverPlayUrl) && mIsFromFloatView)
mView.setNonWifiTips(false)
try {
if (PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_COURSE
&& mView.audioIsPlaying()
&& TextUtils.isEmpty(mReceiverPlayUrl)
) {
mReceiverPlayUrl = mView.getAudioPath()
}
} catch (e: Exception) {
}
mFrom = it.getIntExtra("from", 0)
if (mCourseId == 0) {
ToastUtil.toastShort("参数错误")
mView.close()
}
}
}
override fun getNewIntent(intent: Intent) {
val courseId = intent.getIntExtra("course_id", 0)
if (0 == courseId) {
ToastUtil.toastShort("参数错误")
mView.close()
} else if (courseId != mCourseId) {
mCourseId = courseId
getCoursePlayData()
}
}
override fun getCoursePlayData() {
mView.showProgressDialog()
mModel.getCoursePlayData(courseId)
mModel.getCoursePlayData(mCourseId)
.compose(RxUtils.applySchedulers())
.subscribe(object : CommonObserver<BaseResponse<CourseMediaDetailBean>>() {
override fun onError(s: String?) {
......@@ -24,16 +94,27 @@ class AudioPlayPresenter : BasePresenter<IAudioPlayContract.View, IAudioPlayCont
override fun onSuccess(resp: BaseResponse<CourseMediaDetailBean>?) {
mView.dismissProgressDialog()
resp?.let {
if (it.code == 200) {
//跟新view显示
if (it.data != null && it.data is CourseMediaDetailBean) {
mView.updateView(it.data as CourseMediaDetailBean)
} else {
resp?.let { response ->
if (response.code == 200) {
response.data?.let { bean ->
mCourPlayBean = bean
setPlayList(bean)
if (!TextUtils.isEmpty(mReceiverPlayUrl)) {
mCurrentPosition = mPlayList.indexOfLast {
TextUtils.equals(mReceiverPlayUrl, it.url) || TextUtils.equals(
mReceiverPlayUrl!!.replace(
"http",
"https"
), it.url
)
}
}
mView.updateView(bean.courseExtra, mCurrentPosition, mPlayList, mFrom)
} ?: let {
ToastUtil.toastShort("解析出错")
}
} else {
ToastUtil.toastShort(it.msg)
ToastUtil.toastShort(response.msg)
}
}
}
......@@ -41,4 +122,91 @@ class AudioPlayPresenter : BasePresenter<IAudioPlayContract.View, IAudioPlayCont
})
}
//设置播放列表数据
private fun setPlayList(bean: CourseMediaDetailBean) {
mPlayList.clear()
if (bean.voiceSample.courseMedia.isNotEmpty()) {
for (voiceSampleData in bean.voiceSample.courseMedia) {
voiceSampleData.isDemo = true
mPlayList.add(voiceSampleData)
}
}
if (bean.voiceWhole.courseMedia.isNotEmpty()) {
for (voiceCourseData in bean.voiceWhole.courseMedia) {
mPlayList.add(voiceCourseData)
}
}
}
override fun convertToMusics(list: List<CourseMediaBean>): ArrayList<Music> {
val musics = ArrayList<Music>()
try {
for (i in list.indices) {
musics.add(Music().apply {
path = list[i].url
coverPath = mCourPlayBean?.courseExtra?.pic
title = list[i].title
artist = list[i].doctorName
})
}
} catch (e: Exception) {
e.printStackTrace()
}
return musics
}
override fun addCourseOrder() {
if (mCourseId == 0) {
ToastUtil.toastShort("请退出页面重试")
return
}
val userInfo = YdlCommonRouterManager.getYdlCommonRoute().getUserInfo()
if (userInfo == null || TextUtils.isEmpty(userInfo.userId)) {
mView.loginByOneKeyLogin()
return
}
mView.commonPayDialog(userInfo, mCourseId.toString())
}
override fun togglePlaying(playPosition: Int) {
if (playPosition >= mPlayList.size || playPosition < 0) {
ToastUtil.toastShort("暂无内容")
} else if (mCurrentPosition != playPosition) {
mCurrentPosition = playPosition
mCourPlayBean?.courseExtra?.let {
if (isCanPlay(it.isBuy)) mView.play(mCurrentPosition)
}
}
}
private fun isCanPlay(isBuy: Boolean): Boolean {
mPlayList.elementAtOrNull(mCurrentPosition)?.let {
if (!it.isDemo && !isBuy) {
if (mView.audioIsPlaying()) mView.audioPausePlay()
mView.buyCourseTipDialog()
} else if (it.mediaType == COURSE_AUDIO) {
return true
}
}
return false
}
override fun share() {
mView.shareDialog(mCourPlayBean?.courseExtra?.shareData)
}
override fun showFloatView() {
if (mView.audioIsPlaying() && PlayerFloatHelper.playingType == PlayTypeEnum.PLAY_TYPE_COURSE) {
mView.getAudioMusic()?.coverPath = mCourPlayBean?.courseExtra?.pic
mView.getAudioMusic()?.artist = mCourPlayBean?.courseExtra?.doctorName
PlayerFloatHelper.playTempData.clear()
val hashMap = HashMap<String, String>()
hashMap["course_id"] = mCourseId.toString()
hashMap["media_type"] = COURSE_AUDIO.toString()
PlayerFloatHelper.playTempData.putAll(hashMap)
}
}
}
\ No newline at end of file
package com.yidianling.course.coursePlay.presenter
import android.content.Intent
import com.ydl.media.audio.model.Music
import com.ydl.ydlcommon.bean.ShareData
import com.ydl.ydlcommon.data.http.BaseResponse
import com.ydl.ydlcommon.mvp.base.IModel
import com.ydl.ydlcommon.mvp.base.IPresenter
import com.ydl.ydlcommon.mvp.base.IView
import com.ydl.ydlcommon.router.YdlUserInfo
import com.yidianling.course.bean.CourseExtraBean
import com.yidianling.course.bean.CourseMediaBean
import com.yidianling.course.bean.CourseMediaDetailBean
import io.reactivex.Observable
import java.util.ArrayList
interface IAudioPlayContract {
......@@ -15,14 +22,49 @@ interface IAudioPlayContract {
fun dismissProgressDialog()
fun updateView(bean: CourseMediaDetailBean)
fun updateView(bean: CourseExtraBean, currentPosition: Int, list: List<CourseMediaBean>, from: Int)
fun setNonWifiTips(show: Boolean)
fun audioIsPlaying(): Boolean
fun audioPausePlay()
fun getAudioPath(): String
fun getAudioMusic(): Music?
fun loginByOneKeyLogin()
fun commonPayDialog(userInfo: YdlUserInfo, courseId: String)
fun buyCourseTipDialog()
fun shareDialog(share: ShareData?)
fun play(playPosition: Int)
fun close()
}
interface Presenter : IPresenter<View> {
fun getCoursePlayData(courseId: Int)
fun getParams(intent: Intent)
fun getNewIntent(intent: Intent)
fun getCoursePlayData()
fun addCourseOrder()
fun togglePlaying(playPosition: Int)
fun share()
fun showFloatView()
fun convertToMusics(list: List<CourseMediaBean>): ArrayList<Music>
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment