FeedBackActivity.kt 12.5 KB
Newer Older
ydl committed
1 2 3 4 5 6 7 8
package com.yidianling.user.mine

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.os.Parcelable
YKai committed
9 10
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
ydl committed
11 12 13 14 15 16
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
17 18
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.listener.OnResultCallbackListener
19 20
import com.ydl.ydlcommon.actions.imagepicker.BrowsePicturesActivity
import com.ydl.ydlcommon.actions.imagepicker.ImageCompress
21
import com.ydl.ydlcommon.actions.imagepicker.YDLImagePicker
ydl committed
22 23
import com.ydl.ydlcommon.adapter.custom.PublishImageAdapter
import com.ydl.ydlcommon.base.BaseActivity
24
import com.ydl.ydlcommon.bean.StatusBarOptions
ydl committed
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
import com.ydl.ydlcommon.data.PlatformDataManager
import com.ydl.ydlcommon.ui.ParcelableImage
import com.ydl.ydlcommon.utils.Utils
import com.ydl.ydlcommon.utils.remind.HttpErrorUtils
import com.ydl.ydlcommon.view.dialog.CommonDialog
import com.yidianling.common.tools.LogUtil
import com.yidianling.common.tools.ToastUtil
import com.yidianling.user.R
import com.yidianling.user.UserHelper
import com.yidianling.user.mine.data.AppDataManager
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.user_mine_activity_feed_back.*
import java.io.File
import kotlin.properties.Delegates
YKai committed
41 42 43 44 45
import android.os.Build




ydl committed
46 47 48 49 50 51 52 53


/**
 * 意见反馈
 * Created by softrice on 15/12/1.
 */
@Route(path = "/mine/FeedBackActivity")
class FeedBackActivity : BaseActivity(), View.OnClickListener {
54
    private val PUBLISH_PIC_BROW = 21009
ydl committed
55 56 57 58 59 60 61 62

    private var adapter: PublishImageAdapter? = null
    private var imags: ArrayList<ParcelableImage>? = ArrayList()
    private var phone = UserHelper.getUserInfo()?.userInfo?.phone?:""
    private var isPhoneNull = true
    private var imgFiles: MutableList<File>? = null

    internal var work_time = if (PlatformDataManager.getRam().getGlobalInfo() == null) "早8:30-凌晨2:00" else PlatformDataManager.getRam().getGlobalInfo()?.info?.work_time
63
    internal var tel = if (PlatformDataManager.getRam().getGlobalInfo() == null) "400-765-1010" else PlatformDataManager.getRam().getGlobalInfo()?.info?.tel
64 65 66
    override fun getStatusViewOptions(): StatusBarOptions {
        return StatusBarOptions(true, true)
    }
ydl committed
67 68 69 70 71 72 73 74
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.user_mine_activity_feed_back)
        init()
    }

    internal fun init() {
        adapter = PublishImageAdapter(imags, this)
YKai committed
75 76 77 78
        feedback_img_rcv!!.layoutManager = GridLayoutManager(
            this,
            3
        ) as RecyclerView.LayoutManager?
ydl committed
79 80 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
        feedback_img_rcv!!.adapter = adapter
        if (!TextUtils.isEmpty(phone)) {
            isPhoneNull = false
            et_input_phone!!.setText(phone!!)
            et_input_phone!!.setSelection(et_input_phone!!.text.length)
            et_input_phone!!.clearFocus()
        }
        et_input_phone!!.onFocusChangeListener = View.OnFocusChangeListener { _, _ ->
            if (et_input_phone!!.text.toString().contains("*")) {
                et_input_phone!!.setText("")
            }
        }
        et_input_phone!!.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
                LogUtil.d("s: $s start: $start count: $count after: $after")

            }

            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
                LogUtil.d("s: $s start: $start before: $before count: $count")


            }

            override fun afterTextChanged(s: Editable) {
                phone = s.toString()
                LogUtil.d("final edit phone: $phone")
            }
        })
108 109
        tb_title!!.setLeftTextColor(resources.getColor(R.color.platform_main_theme))
        tb_title!!.setRightTextColor(resources.getColor(R.color.platform_main_theme))
ydl committed
110 111 112 113 114 115 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 143 144
        tv_commit.setOnClickListener {
            if (TextUtils.isEmpty(et_input_phone!!.text.toString())) {
                ToastUtil.toastShort("请输入联系手机,以便我们及时反馈")
                return@setOnClickListener
            }
            if (TextUtils.isEmpty(et_feed_back!!.text.toString())) {
                ToastUtil.toastShort("请输入内容哦")
                return@setOnClickListener
            }
            if (Utils.isFastClick()) {
                //防止连击
                return@setOnClickListener
            }
            if (imags != null && imags!!.size > 0) {
                dealFile(imags)
            } else {
                upLoadFeedBack()
            }
        }
        setUpEvent()


//        var listener = KeyboardChangeListener(this)
//        listener.setKeyBoardListener { isShow, _ ->
//            if (!isShow) {
//                et_input_phone.clearFocus()
//            }
//        }
    }

    private fun setUpEvent() {
        adapter!!.setOnItemClickLister(object : PublishImageAdapter.OnItemClickLister {
            override fun onItemClick(view: View, position: Int) {
                if (imags!!.size > 0) {
                    try {
145 146 147 148 149
                        val intent = Intent()
                        intent.setClass(
                            this@FeedBackActivity,
                            BrowsePicturesActivity::class.java
                        )
ydl committed
150 151 152 153 154
                        intent.putExtra("browse_type", "preview")
                        intent.putExtra("position", position)
                        val bd = Bundle()
                        bd.putParcelableArrayList("allTrendImages", imags as ArrayList<out Parcelable>?)
                        intent.putExtra("allTrendImages_bd", bd)
155
                        startActivityForResult(intent, PUBLISH_PIC_BROW)
ydl committed
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
                    }catch (e: Exception) {}
                }

            }

            override fun onItemLongClick(view: View, position: Int) {

            }
        })
        adapter!!.setOnDeleteClickLister(object : PublishImageAdapter.OnDeleteClickLister {
            override fun onDeleteClick(view: View, position: Int) {
                deleteImg(position)
            }

            override fun onDeleteLongClick(view: View, position: Int) {

            }
        })
        adapter!!.setOnAddClickLister(object : PublishImageAdapter.OnAddClickLister {
            override fun OnAddClick(view: View, position: Int) {
                if (imags!!.size < 3) {
                    showSelector()
                } else {
                    if ("default" == imags!![2].image_url) {
                        showSelector()
                    } else {
                        Toast.makeText(this@FeedBackActivity, "最多选择9张照片", Toast.LENGTH_SHORT).show()
                    }
                }
            }

            override fun OnAddLongClick(view: View, position: Int) {

            }
        })
        iv_add_imag.setOnClickListener(this)
        tv_phone.setOnClickListener(this)
    }

    /**
     * 删除照片
     */
    private fun deleteImg(position: Int) {
        if (imags!!.size > 0) {
            imags!!.removeAt(position)
            adapter!!.updateDate(imags)
        }
        if (imags!!.size <= 0) {
            iv_add_imag!!.visibility = View.VISIBLE
            feedback_img_rcv!!.visibility = View.GONE
        }
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.iv_add_imag -> showSelector()
            R.id.tv_phone -> CommonDialog(this@FeedBackActivity)
                    .setMessage("欢迎致电壹点灵客服热线\n$tel\n服务时间:$work_time")
                    .setLeftOnclick("取消") { }.setRightClick("拨打") {
                        val phoneIntent = Intent(Intent.ACTION_DIAL,
                                Uri.parse("tel:$tel"))
                        this@FeedBackActivity.startActivity(phoneIntent)
                    }.show()
        }
    }

    private fun showSelector() {
223 224 225
        YDLImagePicker.startPicker(this as BaseActivity, object : OnResultCallbackListener {
            override fun onResult(list: List<LocalMedia>) {
                if (list.isEmpty()) {
ydl committed
226 227
                    return
                }
YKai committed
228 229 230 231 232 233 234 235
                if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) {
                    for (i in list.indices) {
                        addImag(list[i].androidQToPath)
                    }
                } else {
                    for (i in list.indices) {
                        addImag(list[i].path)
                    }
ydl committed
236 237
                }
            }
238 239 240 241

            override fun onCancel() {

            }
ydl committed
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
        })
    }

    /**
     * 添加照片
     */
    private fun addImag(pic_path: String) {
        LogUtil.d("PATH: $pic_path")
        val trendImage = ParcelableImage(pic_path)
        if (imags!!.size >= 1) {
            ToastUtil.toastShort("最多上传一张图片")
        } else {
            imags!!.add(trendImage)
        }
        adapter!!.updateDate(imags)
        if (feedback_img_rcv!!.visibility != View.VISIBLE) {
            feedback_img_rcv!!.visibility = View.VISIBLE
        }
        if (iv_add_imag!!.visibility == View.VISIBLE) {
            iv_add_imag!!.visibility = View.GONE
        }
    }


    @SuppressLint("WrongConstant")
    private fun dealFile(publishTrendImgs: MutableList<ParcelableImage>?) {
        imgFiles = ArrayList()
        if (publishTrendImgs!!.size > 1 && publishTrendImgs.size <= 3) {
            publishTrendImgs.removeAt(publishTrendImgs.size - 1)
        }
        Observable.fromIterable(publishTrendImgs)
                .subscribeOn(Schedulers.io())
                .map { parcelableImage ->
                    LogUtil.d("url: " + parcelableImage.image_url)
                    DealFile2(parcelableImage.image_url, 1500 / publishTrendImgs.size)
                }
                .subscribe { file ->
                    if (file != null) {
                        LogUtil.d("xyutest", "图片压缩后大小: ==>" + file.length() + " file path: " + file.absolutePath)
                        imgFiles!!.add(file)
                        if (imgFiles!!.size == publishTrendImgs.size) {
                            upLoadFeedBack()
                        }
                    } else {
                        runOnUiThread { Toast.makeText(this@FeedBackActivity, "有图片已被删除", Toast.LENGTH_SHORT).show() }
                    }
                }

    }

    @Synchronized
    private fun DealFile2(path: String, size: Int): File {
        var deal_file: File by Delegates.notNull()
        try {
296
            deal_file = ImageCompress.scal(this@FeedBackActivity, path, size)
ydl committed
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
        }catch (e:Exception){

        }
        try {
            Thread.sleep(100)
        } catch (e: InterruptedException) {
            e.printStackTrace()
        }

        return deal_file
    }

    private fun upLoadFeedBack() {
        showProgressDialog("")
        val cmd = FeedBackParam(et_feed_back!!.text.toString(), phone)
        var files: Array<File>? = emptyArray()
        if (imgFiles != null && imgFiles!!.size > 0) {
            files = Array(imgFiles!!.size) { i: Int -> imgFiles!![i] }
//            for (i in imgFiles!!.indices) {
//                files[i] = imgFiles!![i]
//            }
        }
        AppDataManager.getHttp().feedBack(cmd, files)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe({ resp ->
                    dismissProgressDialog()
                    if (resp.code === 0) {
                        startActivity(Intent(this@FeedBackActivity, FeedBackSuccessActivity::class.java))
                        finish()
                    } else {
                        ToastUtil.toastShort(resp.msg)
                    }
                }, { throwable ->
                    HttpErrorUtils.handleError(this@FeedBackActivity, throwable)
                    dismissProgressDialog()
                })
    }

    override fun layoutResId(): Int {
        return 0
    }

    override fun initDataAndEvent() {

    }

    companion object {

        private val PICK_IMAGE_COUNT = 9
        private val PORTRAIT_IMAGE_WIDTH = 720
        val JPG = ".jpg"

        fun newIntent(activity: Activity): Intent {
            return Intent(activity, FeedBackActivity::class.java)
        }
    }
}