SelectConversationActivity.kt 6.09 KB
Newer Older
konghaorui committed
1 2 3 4 5 6 7 8 9 10 11 12
package com.yidianling.tests

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.android.arouter.facade.annotation.Route
import com.ydl.ydl_image.module.GlideApp
import com.ydl.ydl_image.transform.GlideCircleTransform
konghaorui committed
13 14
import com.ydl.ydlcommon.base.BaseActivity
import com.ydl.ydlcommon.router.YdlCommonOut
15 16 17
import com.ydl.ydlcommon.utils.NetworkParamsUtils
import com.ydl.ydlcommon.utils.remind.HttpErrorUtils
import com.ydl.ydlcommon.view.dialog.NormalDialog
konghaorui committed
18
import com.yidianling.common.tools.ToastUtil
konghaorui committed
19
import com.yidianling.im.api.bean.IMRequestCallback
20 21
import com.yidianling.tests.home.param.RecentCmd
import com.yidianling.tests.router.TestsIn
konghaorui committed
22 23
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
24
import kotlinx.android.synthetic.main.tests_activity_select_conversation.*
konghaorui committed
25 26 27 28 29 30 31 32
import java.util.*

/**
 * Created by Wi1ls on 2016/11/10;
 */
@Route(path = "/test/select_conversation")
class SelectConversationActivity : BaseActivity() {
    override fun layoutResId(): Int {
33
        return R.layout.tests_activity_select_conversation
konghaorui committed
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 67 68 69 70 71 72 73 74 75 76 77 78 79
    }

    override fun initDataAndEvent() {
        url = getIntent().getStringExtra("url")
        head = getIntent().getStringExtra("head")
        title = getIntent().getStringExtra("title")
        share_url = getIntent().getStringExtra("share_url")
        id = getIntent().getIntExtra("id", 0)
        flag = getIntent().getIntExtra("flag", 0)

        init()
    }

//    internal var text_empty: TextView? = null

    internal var url: String? = null
    internal var head: String? = null
    internal var title: String? = null
    internal var share_url: String? = null
    internal var id: Int = 0
    internal var flag: Int = 0

    private var myData: MutableList<RecentExpert> = ArrayList<RecentExpert>()

    internal fun init() {
        getData()
    }

    @SuppressLint("WrongConstant")
    private fun handleData() {
            text_empty.visibility = View.INVISIBLE
            select_lv.visibility = View.VISIBLE
            val conversationAdapter = object : BaseAdapter() {
                override fun getCount(): Int {
                    return myData.size
                }

                override fun getItem(position: Int): Any {
                    return myData[position]
                }

                override fun getItemId(position: Int): Long {
                    return position.toLong()
                }

                override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
80
                    val view = LayoutInflater.from(mContext).inflate(R.layout.tests_ui_select_conversation_item, null, false)
konghaorui committed
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
                    val s = view.findViewById<ImageView>(R.id.sdv_head) as ImageView
                    val n = view.findViewById<TextView>(R.id.name) as TextView
                    GlideApp.with(mContext)
                            .load(myData[position].getHead())
                            .transform(GlideCircleTransform(mContext))
                            .centerCrop()
                            .into(s)
                    n.text = myData[position].name
                    view.setOnClickListener {
                        val builder = NormalDialog.Builder(mContext)
                        builder.setTitle("")
                        builder.setMessage("确定将测试结果发送给:" + myData[position].name + "?")
                        builder.setPositiveButton("确定"
                        ) { dialog, which ->
                            TestsIn.sendTestResultMessage(myData[position].uid, "测试结果", title, head, url, id, share_url,
                                    object : IMRequestCallback<Void> {
                                        override fun onSuccess(t: Void?) {
                                            YdlCommonOut.showToast("发送成功")

                                            finish()
                                        }

                                        override fun onFailed(i: Int) {
                                            YdlCommonOut.showToast("发送失败")
                                            finish()
                                        }

                                        override fun onException(throwable: Throwable?) {
                                            YdlCommonOut.showToast("发送失败")
                                            finish()
                                        }
                                    })
                            dialog.dismiss()
                        }
                        builder.setNegativeButton("取消"
                        ) { dialog, _ -> dialog.dismiss() }
                        builder.create().show()
                    }
                    return view
                }
            }
            select_lv.adapter = conversationAdapter
    }

    private fun getData() {
126
        val cmd = RecentCmd()
konghaorui committed
127
        TestRetrofitApi.getTestRetrofitApi().getRecentExpertList(NetworkParamsUtils.getMaps(cmd))
konghaorui committed
128 129 130 131 132 133 134 135 136 137 138 139 140
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .doOnSubscribe({showProgressDialog("")})
                .doAfterTerminate({dismissProgressDialog()})
                .subscribe(
                        {
                            if (it.code == 0) {
                                for (recent in it.data) {
                                    val recentExpert = RecentExpert(""+recent.uid, recent.head, recent.name)
                                    myData.add(recentExpert)
                                }
                                handleData()
                            } else {
konghaorui committed
141
                                ToastUtil.toastShort(this@SelectConversationActivity, it.msg)
konghaorui committed
142 143
                            }
                        },{
konghaorui committed
144
                            HttpErrorUtils.handleError(mContext,it)
konghaorui committed
145 146 147 148 149 150 151 152 153
                        })
    }

    companion object {
        val FLAG_SEND_TESTRESULT = 1
    }


}