ChooseAccountActivity.kt 6.13 KB
Newer Older
ydl committed
1 2 3 4 5 6 7 8 9 10
package com.yidianling.user.mine

import android.annotation.SuppressLint
import android.app.Activity
import android.app.Dialog
import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.widget.AdapterView
import com.ydl.ydlcommon.base.BaseActivity
11
import com.ydl.ydlcommon.bean.StatusBarOptions
ydl committed
12 13 14 15 16 17
import com.ydl.ydlcommon.data.http.RxUtils
import com.ydl.ydlcommon.data.http.ThrowableConsumer
import com.ydl.ydlcommon.utils.TransferCacheUtils
import com.ydl.ydlcommon.view.ListNoCancelDialog
import com.yidianling.common.tools.ToastUtil
import com.yidianling.user.R
18 19 20
import com.yidianling.user.mine.WithDrawActivity.Companion.INTENT_ACCOUNT_DATA
import com.yidianling.user.mine.adapter.AccountListAdapter
import com.yidianling.user.mine.bean.AccountBean
ydl committed
21 22 23 24 25 26 27 28 29 30 31 32 33
import com.yidianling.user.mine.bean.DefaultAccountCmd
import com.yidianling.user.mine.data.AppDataManager
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.functions.Consumer
import kotlinx.android.synthetic.main.user_mine_activity_choose_account.*

/**
 * 选择提现账户页面
 */
class ChooseAccountActivity : BaseActivity() {
    private var adapter: AccountListAdapter? = null
    private var dataLists: MutableList<AccountBean>? = null

34 35 36 37
    override fun getStatusViewOptions(): StatusBarOptions {
        return StatusBarOptions(true, true)
    }

ydl committed
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 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 108 109 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    override fun layoutResId(): Int {
        return R.layout.user_mine_activity_choose_account
    }

    override fun initDataAndEvent() {
        title_bar.setOnLeftTextClick { _, _ -> onBackPressed() }
        dataLists = TransferCacheUtils.getTransferData(INTENT_LIST) as MutableList<AccountBean>
        TransferCacheUtils.removeTransferData(INTENT_LIST)
        adapter = AccountListAdapter(this, AccountListAdapter.OnChooseItemListener {

            val intent = Intent()
            TransferCacheUtils.putTransferData(INTENT_ACCOUNT_DATA, it)
            setResult(Activity.RESULT_OK, intent)
            finish()
        })
        btn_add.setOnClickListener {
            AddAccountActivity.startForResult(this, ADD_ACCOUNT_REQUEST_CODE)
        }
        adapter!!.setList(dataLists)
        lv_content.adapter = adapter
        lv_content.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            adapter!!.checkChoose(position)
        }
        lv_content.onItemLongClickListener = AdapterView.OnItemLongClickListener { parent, view, position, id ->
            val list = mutableListOf<String>()
            list.add("编辑")
            list.add("删除")
            val builder = ListNoCancelDialog.Builder(this, list, 0)
            builder.SetOnItemClickLister(object : ListNoCancelDialog.Builder.OnItemClickLister {
                override fun onItemClick(dialog: Dialog?, view: View?, index: Int) {
                    val name = list[index]
                    when (name) {
                        "编辑" -> {
                            EditAccountActivity.startForResult(this@ChooseAccountActivity, dataLists!![position], INTENT_DATA_REQUEST_CODE)
                            dialog!!.dismiss()
                        }
                        "删除" -> {
                            deleteAccount(dataLists!![position])
                            dialog!!.dismiss()
                        }
                    }
                }

                override fun onItemLongClick(dialog: Dialog?, view: View?, position: Int) {
                }

            })
            if (!ChooseAccountActivity@this.isFinishing){
                builder.create().show()
            }
            true
        }
    }

    /**
     * 删除帐号
     */
    @SuppressLint("CheckResult")
    private fun deleteAccount(bean: AccountBean) {
        showProgressDialog()
        val parms = DefaultAccountCmd(bean.id)
        AppDataManager.getHttp().deleteAccount(parms)
                .compose(RxUtils.resultData())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(Consumer {
                    dismissProgressDialog()
                    ToastUtil.toastShort("帐号删除成功")
                    adapter!!.deleteAccount(bean)
                }, object : ThrowableConsumer() {
                    override fun accept(msg: String) {
                        dismissProgressDialog()
                        ToastUtil.toastShort(msg)
                    }
                })
    }


    override fun onBackPressed() {
        val intent = Intent()
        TransferCacheUtils.putTransferData(INTENT_ACCOUNT_DATA, adapter!!.data)
        setResult(Activity.RESULT_OK, intent)
        super.onBackPressed()
    }


    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }

        if (requestCode == INTENT_DATA_REQUEST_CODE) {//编辑帐号页面返回
            val accountBean: AccountBean = TransferCacheUtils.getTransferData(INTENT_DATA) as AccountBean
            dataLists!!.forEach {
                if (TextUtils.equals(it.id, accountBean.id)) {
                    it.account = accountBean.account
                    it.cashName = accountBean.cashName
                }
            }
            adapter!!.notifyDataSetChanged()
            TransferCacheUtils.removeTransferData(INTENT_DATA)
        }

        if (requestCode == ADD_ACCOUNT_REQUEST_CODE) {//添加帐号页面返回
            val bean: AccountBean = TransferCacheUtils.getTransferData(ADD_ACCOUNT_REQUEST_CODE) as AccountBean
            dataLists!!.add(1, bean)
            adapter!!.notifyDataSetChanged()
        }
    }

    companion object {
        const val INTENT_LIST = "intent_list"
        const val INTENT_DATA = "intent_data"
        const val INTENT_DATA_REQUEST_CODE = 1024
        const val ADD_ACCOUNT_REQUEST_CODE = 1025
        fun startForResult(activity: Activity, list: MutableList<AccountBean>, code: Int) {
            val intent = Intent(activity, ChooseAccountActivity::class.java)
            TransferCacheUtils.putTransferData(INTENT_LIST, list)
            activity.startActivityForResult(intent, code)
        }
    }
}