lmw
2024-07-16 935a87b3578806ca37fee37f03da8c419a3896ce
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
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
package com.dollearn.student.ui.shop
 
import androidx.core.os.bundleOf
import cn.sinata.xldutils.utils.isValidPhone
import cn.sinata.xldutils.utils.myToast
import com.dollearn.student.R
import com.dollearn.student.dialog.ChooseCityDialog
import com.dollearn.student.network.HttpManager
import com.dollearn.student.network.entity.Province
import com.dollearn.student.network.request
import com.dollearn.student.ui.TransparentStatusBarActivity
import com.dollearn.student.utils.extention.clickDelay
import kotlinx.android.synthetic.main.activity_add_address.*
 
class AddAddressActivity : TransparentStatusBarActivity() {
    override fun setContentView() = R.layout.activity_add_address
 
    private val id by lazy { intent.getStringExtra("id") }
    private var city = ""
    private var cityCode = ""
    private var province = ""
    private var provinceCode = ""
 
    private val cityDialog by lazy {
        val chooseCityDialog = ChooseCityDialog()
        chooseCityDialog.callback = object :ChooseCityDialog.CityCallback{
            override fun onCitySelected(
                pCode: String,
                pName: String,
                cCode: String,
                cName: String
            ) {
                province = pName
                city = cName
                provinceCode = pCode
                cityCode = cCode
                tv_city.text = province+city
            }
        }
        chooseCityDialog
    }
 
    private val list = arrayListOf<Province>()
 
    override fun initClick() {
        tv_city.clickDelay {
            if (list.isNotEmpty()){
                cityDialog.show(supportFragmentManager,"city")
            }
        }
 
        tv_action.setOnClickListener {
            val name = et_name.text.toString()
            if (name.isNullOrEmpty()){
                myToast("请填写收件人姓名")
                return@setOnClickListener
            }
            val phone = et_phone.text.toString()
            if (!phone.isValidPhone()){
                myToast("请填写正确的收件人电话")
                return@setOnClickListener
            }
            if (cityCode.isEmpty()){
                myToast("请选择所在城市")
                return@setOnClickListener
            }
            val address = et_address.text.toString()
            if (address.isNullOrEmpty()){
                myToast("请填写详细地址")
                return@setOnClickListener
            }
            tv_action.isEnabled = false
            HttpManager.addressSaveOrUpdate(address,cityCode,city,province,provinceCode,if (cb_default.isChecked) 1 else 0,name, phone, id).request(this){_,_->
                myToast(if (id.isNullOrEmpty()) "添加成功" else "修改成功")
                setResult(RESULT_OK)
                finish()
            }
        }
    }
 
    override fun initView() {
        getCity()
        if (!id.isNullOrEmpty()){
            HttpManager.getAddressByIdStudy(id!!).request(this){_,data->
                data?.apply {
                    et_name.setText(recipient)
                    et_phone.setText(recipientPhone)
                    this@AddAddressActivity.province = province
                    this@AddAddressActivity.city = city
                    this@AddAddressActivity.provinceCode = provinceCode
                    this@AddAddressActivity.cityCode = cityCode
                    tv_city.text = province+city
                    et_address.setText(address)
                    cb_default.isChecked = isDefault == 1
                }
            }
        }
    }
 
    private fun getCity() {
        HttpManager.addressTree().request(this){_,data->
            list.clear()
            list.addAll((data?: arrayListOf()).filter { !it.children.isNullOrEmpty() })
            cityDialog.arguments = bundleOf("list" to list)
        }
    }
 
}