CourseChapterActivity.kt 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package com.dlc.exam.ui.learn
  2. import android.annotation.SuppressLint
  3. import android.content.Intent
  4. import android.os.Bundle
  5. import android.view.LayoutInflater
  6. import android.view.View
  7. import androidx.recyclerview.widget.LinearLayoutManager
  8. import com.blankj.utilcode.util.LogUtils
  9. import com.chad.library.adapter.base.BaseDelegateMultiAdapter
  10. import com.chad.library.adapter.base.delegate.BaseMultiTypeDelegate
  11. import com.chad.library.adapter.base.viewholder.BaseViewHolder
  12. import com.dlc.exam.ExamApp
  13. import com.dlc.exam.R
  14. import com.dlc.exam.databinding.ActivityCourseChapterBinding
  15. import com.dlc.exam.ui.MainActivity
  16. import com.dlc.exam.ui.common.BaseCountDownActivity
  17. import com.dlc.exam.ui.learn.test.ClassTestActivity
  18. import com.dlc.exam.ui.learn.test.ClassTestRecordListActivity
  19. import com.dlc.exam.ui.me.PersonalCenterActivity
  20. import com.dlc.exam.ui.widget.TitleBar
  21. import com.google.gson.Gson
  22. import com.rc.httpcore.HttpClient
  23. import com.rc.httpcore.HttpConfig
  24. import com.rc.httpcore.client.ApiRepository
  25. import com.rc.httpcore.vo.response.ExamCourseVo
  26. /**
  27. * 安全学习-章节
  28. *
  29. * @author ReiChin_
  30. */
  31. class CourseChapterActivity : BaseCountDownActivity<ActivityCourseChapterBinding>(),
  32. TitleBar.TitleListener {
  33. override fun createViewBinding() =
  34. ActivityCourseChapterBinding.inflate(LayoutInflater.from(this))
  35. private val mChapterAdapter by lazy { ChapterAdapter() }
  36. private var mCourseId: String = ""
  37. private var mRecord: Boolean = false
  38. private var mVideoDraggable: String? = null
  39. override fun initViews(savedInstanceState: Bundle?) {
  40. super.initViews(savedInstanceState)
  41. viewBinding.titleBar.setTitleListener(this)
  42. viewBinding.titleBar.setUsername(ExamApp.sLearnLoginVo?.userName)
  43. mRecord = intent.getBooleanExtra("record", false)
  44. mChapterAdapter.record = mRecord
  45. mChapterAdapter.addChildClickViewIds(R.id.startLearn, R.id.classTest, R.id.classTestRecord)
  46. mChapterAdapter.setOnItemChildClickListener { _, view, position ->
  47. val item = mChapterAdapter.data[position]
  48. when (view.id) {
  49. R.id.startLearn -> {
  50. // 开始学习
  51. // dispatchStartLearn(item.data.learnStatus, position)
  52. dispatchStartLearn(item.data)
  53. }
  54. R.id.classTest -> {
  55. // 课后考核课后考核
  56. dispatchClassTest(item.data.id)
  57. }
  58. R.id.classTestRecord -> {
  59. // 考核记录
  60. dispatchClassTestRecord(item.data.id)
  61. }
  62. }
  63. }
  64. viewBinding.chapterContent.layoutManager = LinearLayoutManager(this)
  65. viewBinding.chapterContent.adapter = mChapterAdapter
  66. }
  67. private fun dispatchClassTestRecord(chapterId: String?) {
  68. val intent = Intent(this, ClassTestRecordListActivity::class.java)
  69. intent.putExtra("chapterId", chapterId)
  70. startActivity(intent)
  71. }
  72. private fun dispatchClassTest(chapterId: String?) {
  73. val intent = Intent(this, ClassTestActivity::class.java)
  74. intent.putExtra("chapterId", chapterId)
  75. startActivityForResult(intent, 100)
  76. }
  77. private fun dispatchStartLearn(courseVo: ExamCourseVo.ChapterVo) {
  78. val chapterBean = LearnChapterBean().apply {
  79. chapterId = courseVo.id
  80. courseId = courseVo.courseId
  81. type = courseVo.type
  82. chapterData = courseVo.chapterData
  83. duration = courseVo.duration
  84. title = courseVo.title
  85. assessStatus = courseVo.showClassTest()
  86. }
  87. val intent = Intent()
  88. intent.putExtra("Chapter", chapterBean)
  89. intent.putExtra("relearn", "1" == courseVo.learnStatus)
  90. // 视频处理
  91. if (chapterBean.type == "2") {
  92. intent.setClass(this, LearnDetailExoActivity::class.java)
  93. }
  94. // WebView处理
  95. else {
  96. intent.setClass(this, LearnDetailLibOfficeActivity::class.java)
  97. }
  98. startActivityForResult(intent, 100)
  99. }
  100. private fun dispatchStartLearn(learnStatus: String?, position: Int) {
  101. // val intent = Intent(this, FullScreenActivity::class.java)
  102. // startActivityForResult(intent, 100)
  103. val intent = Intent(this, LearnDetailActivity::class.java)
  104. intent.putExtra("nextChapter", nextChapterList(position))
  105. intent.putExtra("relearn", "1" == learnStatus)
  106. intent.putExtra("videoDraggable", "0" != mVideoDraggable)
  107. startActivityForResult(intent, 100)
  108. }
  109. private fun nextChapterList(position: Int): ArrayList<LearnChapterBean> {
  110. val learnChapterList = ArrayList<LearnChapterBean>()
  111. val size = mChapterAdapter.data.size
  112. for (index in position until size) {
  113. val item = mChapterAdapter.data[index]
  114. if (item.itemType == ChapterAdapter.PLAN_SECTION
  115. && (index == position || "1" != item.data.learnStatus)
  116. ) {
  117. val chapterBean = LearnChapterBean().apply {
  118. chapterId = item.data.id
  119. courseId = item.data.courseId
  120. type = item.data.type
  121. chapterData = item.data.chapterData
  122. duration = item.data.duration
  123. title = item.data.title
  124. assessStatus = item.data.showClassTest()
  125. }
  126. learnChapterList.add(chapterBean)
  127. }
  128. }
  129. return learnChapterList
  130. }
  131. override fun initData() {
  132. super.initData()
  133. mCourseId = intent.getStringExtra("courseId") ?: ""
  134. queryCourseDetail()
  135. }
  136. private fun queryCourseDetail() {
  137. showLoading("加载中...")
  138. val disposable = ApiRepository.examCourseDetail(mCourseId)
  139. .subscribe({
  140. dismissLoading()
  141. updateViews(it)
  142. }, { throwable ->
  143. dismissLoading()
  144. throwable.printStackTrace()
  145. showNetError(throwable)
  146. })
  147. addDisposable(disposable)
  148. }
  149. @SuppressLint("SetTextI18n")
  150. private fun updateViews(data: ExamCourseVo?) {
  151. LogUtils.json(data)
  152. data?.let {
  153. mVideoDraggable = it.videoDraggable
  154. if (0L != it.learnDurations) {
  155. // 已学习时长
  156. viewBinding.learnDuration.text = "已学习时长:${it.learnDurationStr ?: ""}"
  157. }
  158. // 章节名称
  159. viewBinding.chapterName.text = data.allCateTitle
  160. // 学习时长
  161. viewBinding.time.text = "学习时长:${it.durationsStr ?: ""}"
  162. // 有效期
  163. viewBinding.termValidity.text =
  164. "有效期:${if ("0" == it.ceType) "永久" else it.ceTime ?: ""}"
  165. // 已有1250人学习
  166. viewBinding.learnPersonCount.text = "已有${it.learns ?: "0"}人学习"
  167. val chapterData = generateChapterData(it.chapterList)
  168. mChapterAdapter.setNewInstance(chapterData)
  169. if (chapterData.isNullOrEmpty()) {
  170. mChapterAdapter.setEmptyView(com.rc.core.R.layout.view_list_empty)
  171. }
  172. }
  173. }
  174. private fun generateChapterData(data: List<ExamCourseVo.ChapterVo>?): MutableList<ChapterInfo>? {
  175. if (data.isNullOrEmpty()) return null
  176. val result = mutableListOf<ChapterInfo>()
  177. data.forEachIndexed { index, pItem ->
  178. result.add(ChapterInfo(ChapterAdapter.PLAN_CHAPTER, pItem, index))
  179. if (!pItem.children.isNullOrEmpty()) {
  180. pItem.children.mapTo(result, {
  181. ChapterInfo(ChapterAdapter.PLAN_SECTION, it)
  182. })
  183. }
  184. }
  185. return result
  186. }
  187. override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  188. if (100 == requestCode) {
  189. if (RESULT_OK == resultCode) {
  190. queryCourseDetail()
  191. }
  192. }
  193. super.onActivityResult(requestCode, resultCode, data)
  194. }
  195. override fun onHomeViewClicked(view: View) {
  196. startActivity(Intent(this, MainActivity::class.java))
  197. }
  198. override fun onLogoutViewClicked(view: View) {
  199. callLogoutApi {
  200. showToast("已退出登录")
  201. ExamApp.sLearnLoginVo = null
  202. HttpClient.token = null
  203. startActivity(Intent(this, MainActivity::class.java))
  204. }
  205. }
  206. override fun onBackViewClicked(view: View) {
  207. finish()
  208. }
  209. override fun onUserViewClicked(view: View) {
  210. startActivity(Intent(this, PersonalCenterActivity::class.java))
  211. }
  212. }
  213. data class ChapterInfo(val itemType: Int, val data: ExamCourseVo.ChapterVo, val pIndex: Int = -1)
  214. private class ChapterAdapter : BaseDelegateMultiAdapter<ChapterInfo, BaseViewHolder>() {
  215. var record: Boolean = false
  216. companion object {
  217. const val PLAN_CHAPTER = 1
  218. const val PLAN_SECTION = 2
  219. }
  220. init {
  221. setMultiTypeDelegate(object : BaseMultiTypeDelegate<ChapterInfo>() {
  222. override fun getItemType(data: List<ChapterInfo>, position: Int) =
  223. data[position].itemType
  224. })
  225. getMultiTypeDelegate()!!
  226. .addItemType(PLAN_CHAPTER, R.layout.item_chapter)
  227. .addItemType(PLAN_SECTION, R.layout.item_section)
  228. }
  229. override fun convert(holder: BaseViewHolder, item: ChapterInfo) {
  230. when (holder.itemViewType) {
  231. PLAN_CHAPTER -> convertChapter(holder, item)
  232. PLAN_SECTION -> convertSection(holder, item)
  233. }
  234. }
  235. private fun convertChapter(holder: BaseViewHolder, item: ChapterInfo) {
  236. holder.setText(R.id.chapterPosition, "${item.pIndex + 1}")
  237. .setText(R.id.chapterName, item.data.title)
  238. }
  239. private fun convertSection(holder: BaseViewHolder, item: ChapterInfo) {
  240. holder.setText(R.id.sectionName, item.data.title)
  241. .setText(R.id.videoDuration, "时长:${item.data.durationStr ?: ""}")
  242. // 学习状态 0 学习中,2 未学习,3 待考核,1 已完成
  243. val learnStatus = item.data.formatLearnStatus()
  244. holder.setGone(R.id.learnCompleted, 2 == learnStatus)
  245. learnStatusImg(learnStatus)?.let { holder.setImageResource(R.id.learnCompleted, it) }
  246. if (record) {
  247. holder.setGone(R.id.startLearn, true)
  248. .setGone(R.id.classTest, true)
  249. .setGone(R.id.learnDuration, "1" != item.data.learnStatus)
  250. .setText(R.id.learnDuration, "已学习时长:${item.data.durationStr ?: ""}")
  251. .setGone(R.id.classTestRecord, !item.data.showClassTestRecord())
  252. } else {
  253. holder.setGone(R.id.startLearn, false)
  254. .setText(
  255. R.id.startLearn,
  256. if ("1" == item.data.learnStatus) "重新学习" else "开始学习"
  257. )
  258. .setGone(R.id.classTest, !item.data.showClassTest())
  259. .setGone(R.id.learnDuration, true)
  260. .setGone(R.id.classTestRecord, true)
  261. }
  262. }
  263. /**
  264. * 学习状态imageview src
  265. *
  266. * @param learnStatus 学习状态 0 学习中,2 未学习,3 待考核,1 已完成
  267. */
  268. private fun learnStatusImg(learnStatus: Int): Int? {
  269. return when (learnStatus) {
  270. 0 -> R.mipmap.icon_xxzyz
  271. 1 -> R.mipmap.icon_ywcyz
  272. 3 -> R.mipmap.icon_dkhyz
  273. else -> null
  274. }
  275. }
  276. }