CourseTopicActivity.kt 5.14 KB
Newer Older
严久程 committed
1 2
package com.yidianling.course.courseNew

严久程 committed
3
import android.annotation.SuppressLint
严久程 committed
4 5 6 7 8 9 10 11 12 13 14 15
import android.content.Context
import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.ydl.ydl_image.module.GlideApp
严久程 committed
16 17 18
import com.ydl.ydlcommon.actions.share.ShareUtils
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.data.http.RxUtils
严久程 committed
19 20 21
import com.yidianling.common.tools.LogUtil
import com.yidianling.common.tools.RxNetTool
import com.yidianling.course.R
严久程 committed
22
import com.yidianling.course.net.CourseRetrofitUtils
严久程 committed
23
import com.yidianling.course.bean.Course
严久程 committed
24 25 26 27
import com.yidianling.course.model.TopicCourseBean
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.activity_course_topic.*
严久程 committed
28
import kotlinx.android.synthetic.main.course_layout_title_bar.*
严久程 committed
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

/**
 * 课程专题页面
 */
@Route(path = "/course/special")
class CourseTopicActivity : BaseActivity(), View.OnClickListener, SwipeRefreshLayout.OnRefreshListener {

    private var opicCourseBean: TopicCourseBean? = null
    private var adapter: CourseTopicAdapter? = null
    private var datas: List<Course>? = null
    private var specialId: Int? = 0
    private var headView: ImageView? = null
    private var footerView: View? = null


    companion object {
        fun start(context: Context, id: String) {
            val i = Intent(context, CourseTopicActivity::class.java)
            i.putExtra("special_id", id)
            context.startActivity(i)
        }
    }

    override fun layoutResId(): Int {
        return R.layout.activity_course_topic
    }

    override fun initDataAndEvent() {
        tv_title.text = "课程专题"
        iv_back.setOnClickListener(this)
        course_topic_rcv.layoutManager = LinearLayoutManager(this)
        datas = ArrayList()
        adapter = CourseTopicAdapter(this, datas!!)
        course_topic_rcv.adapter = adapter
        if (!TextUtils.isEmpty(intent.getStringExtra("special_id"))) {
            specialId = intent.getStringExtra("special_id").toInt()
        }
        LogUtil.i("special id: $specialId")
严久程 committed
67
        swl.setColorSchemeResources(R.color.main_theme)
严久程 committed
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        swl.setOnRefreshListener(this)
        tv_left_menu.setOnClickListener {
            //分享
            share()
        }
        initHeadView()
        initFooterView()
        loadData()
    }

    override fun onRefresh() {
        swl.isRefreshing = true
        loadData()
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_back -> finish()
        }
    }

    fun initHeadView() {
        headView = LayoutInflater.from(this).inflate(R.layout.item_course_topic_image, course_topic_rcv, false) as ImageView
        adapter?.addHeaderView(headView)
    }

    fun initFooterView() {
严久程 committed
95
        footerView = LayoutInflater.from(this).inflate(R.layout.item_course_topic_text, course_topic_rcv, false)
严久程 committed
96 97 98 99 100 101 102 103 104
        adapter?.addFooterView(footerView)
    }

    private fun share() {
        if (null != opicCourseBean && null != opicCourseBean!!.share) {
            ShareUtils.share(CourseTopicActivity@ this, opicCourseBean!!.share!!.title, opicCourseBean!!.share!!.share_url, opicCourseBean!!.share!!.desc, opicCourseBean!!.share!!.cover)
        }
    }

严久程 committed
105
    @SuppressLint("CheckResult")
严久程 committed
106 107 108 109 110 111 112 113 114
    private fun loadData() {
        if (!RxNetTool.isConnected(CourseTopicActivity@ this)) {
            swl.isRefreshing = false
            course_topic_rcv.visibility = View.GONE
            v_no_network.visibility = View.VISIBLE
            return
        }
        course_topic_rcv.visibility = View.VISIBLE
        v_no_network.visibility = View.GONE
严久程 committed
115
        CourseRetrofitUtils.getCourseTopic(specialId.toString())
严久程 committed
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
                .subscribeOn(Schedulers.io())
                .compose(RxUtils.resultJavaData())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    LogUtil.i("resp: $resp")
                    opicCourseBean = resp
                    course_topic_rcv.visibility = View.VISIBLE
                    adapter?.setDatas(resp.courses)
                    GlideApp.with(CourseTopicActivity@ this).load(resp.pic).into(headView)
                    if (resp.explain.isNullOrEmpty()) {
                        footerView?.findViewById<LinearLayout>(R.id.ll_title)!!.visibility = View.GONE
                    } else {
                        footerView?.findViewById<LinearLayout>(R.id.ll_title)!!.visibility = View.VISIBLE
                        footerView?.findViewById<TextView>(R.id.tv_content)!!.text = resp.explain!!
                    }
                    adapter?.notifyDataSetChanged()
                    swl.isRefreshing = false
                }, { throwable ->
                    LogUtil.e(throwable.toString())
                    course_topic_rcv.visibility = View.GONE
                    swl.isRefreshing = false
                })

    }


}