|
@@ -3,30 +3,194 @@ package com.quansu.heifengwuliu.vmodel
|
|
|
|
|
|
import android.app.Activity
|
|
|
import android.content.Intent
|
|
|
+import android.text.TextUtils
|
|
|
+import android.util.Log
|
|
|
+import androidx.databinding.Bindable
|
|
|
+import androidx.lifecycle.MutableLiveData
|
|
|
+import com.quansu.heifengwuliu.BR
|
|
|
+import com.quansu.heifengwuliu.activity.AddressAddActivity
|
|
|
+import com.quansu.heifengwuliu.activity.AreaSelectActivity
|
|
|
+import com.quansu.heifengwuliu.config.MIntentAction
|
|
|
+import com.quansu.heifengwuliu.model.AddressDetailsBean
|
|
|
import com.quansu.heifengwuliu.utils.net.NetEngine
|
|
|
+import com.yanzhenjie.permission.AndPermission
|
|
|
+import com.yanzhenjie.permission.runtime.Permission
|
|
|
import com.ysnows.base.base.BRepository
|
|
|
import com.ysnows.base.base.BViewModel
|
|
|
+import com.ysnows.base.ccretrofit.CCRetrofit
|
|
|
+import com.ysnows.base.route.IGaode
|
|
|
+import com.ysnows.base.utils.UiSwitch
|
|
|
|
|
|
open class AddressAddVModel : BViewModel<BRepository>() {
|
|
|
|
|
|
+ var checked: MutableLiveData<Int> = MutableLiveData()
|
|
|
+ var cityname: MutableLiveData<String> = MutableLiveData()
|
|
|
|
|
|
- fun setAdd( addr_id:String,lat:String,lng:String,province:String,
|
|
|
- city:String,area:String,address:String,
|
|
|
- name:String,mobile:String,type:String,
|
|
|
- is_default:String){
|
|
|
- repository().lreq(NetEngine.service.addressEdit(addr_id,lat,lng,province,city,area,address,
|
|
|
- name, mobile,type,is_default))
|
|
|
+ var lat = ""
|
|
|
+ var lng = ""
|
|
|
+ var province = ""
|
|
|
+ var city = ""
|
|
|
+ var area = ""
|
|
|
+ var type = "0"//默认发货
|
|
|
+ var addr_id="0"////默认添加
|
|
|
+
|
|
|
+ init {
|
|
|
+ checked.value = 1//默认选中
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setMapValue(lat_data: String, lng_data: String, position: String) {
|
|
|
+ lat = lat_data
|
|
|
+ lng = lng_data
|
|
|
+ address = position
|
|
|
+ }
|
|
|
+
|
|
|
+ fun setCityValue(province_code: String, city_code: String, area_code: String, name: String) {
|
|
|
+ province = province_code
|
|
|
+ city = city_code
|
|
|
+ area = area_code
|
|
|
+ cityname.value = name
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ @Bindable
|
|
|
+ open var name: String? = null
|
|
|
+ set(value) {
|
|
|
+ field = value
|
|
|
+ notifyPropertyChanged(BR.name)
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bindable
|
|
|
+ open var phone: String? = null
|
|
|
+ set(value) {
|
|
|
+ field = value
|
|
|
+ notifyPropertyChanged(BR.phone)
|
|
|
+ }
|
|
|
+
|
|
|
+ @Bindable
|
|
|
+ open var address: String? = null
|
|
|
+ set(value) {
|
|
|
+ field = value
|
|
|
+ notifyPropertyChanged(BR.address)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ fun choseMap() {
|
|
|
+ //地图选点
|
|
|
+ AndPermission.with(repository().context)
|
|
|
+ .runtime()
|
|
|
+ .permission(Permission.ACCESS_COARSE_LOCATION,
|
|
|
+ Permission.ACCESS_FINE_LOCATION,
|
|
|
+ Permission.WRITE_EXTERNAL_STORAGE,
|
|
|
+ Permission.READ_EXTERNAL_STORAGE,
|
|
|
+ Permission.READ_PHONE_STATE
|
|
|
+ )
|
|
|
+ .onGranted { data ->
|
|
|
+ CCRetrofit.instance()
|
|
|
+ .with(repository().context)
|
|
|
+ .create(IGaode::class.java)
|
|
|
+ .selectLocation()
|
|
|
+ .subscribe()
|
|
|
+
|
|
|
+ }
|
|
|
+ .start()
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ fun choseCity() {
|
|
|
+ //选择省市区
|
|
|
+ UiSwitch.singleRes(repository().context as AddressAddActivity, AreaSelectActivity::class.java,
|
|
|
+ MIntentAction.REQUEST_CODE_TWO)
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ fun setSave() {
|
|
|
+ //保存数据
|
|
|
+ Log.e("-shy-", "name=: " +name)
|
|
|
+ Log.e("-shy-", "phone=: " +phone)
|
|
|
+
|
|
|
+
|
|
|
+ if (name.isNullOrEmpty()) {
|
|
|
+ toast("请输入联系人")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (phone.isNullOrEmpty()) {
|
|
|
+
|
|
|
+ toast("请输入手机号码")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (TextUtils.isEmpty(province)) {
|
|
|
+ toast("请选择地区")
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if (address.isNullOrEmpty()) {
|
|
|
+ toast("请输入详细地址")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ repository().lreq(NetEngine.service.addressEdit(addr_id, lat, lng, province, city, area, address,
|
|
|
+ name, phone, type, checked.value.toString()))
|
|
|
.doOnNext() { res ->
|
|
|
if (res.ok(true)) {
|
|
|
val intent = Intent()
|
|
|
- var con= repository().context as Activity
|
|
|
+ var con = repository().context as Activity
|
|
|
con.setResult(Activity.RESULT_OK, intent)
|
|
|
con.finish()
|
|
|
}
|
|
|
}
|
|
|
.subscribe()
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ fun choseDefault() {
|
|
|
+ //选择默认
|
|
|
+ if (checked.value == 1) checked.value = 0 else checked.value = 1
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ //获取地址详情
|
|
|
+ fun getAddressInfo(addr_id: String) {
|
|
|
+
|
|
|
+ this.addr_id=addr_id
|
|
|
+ repository().lreq(NetEngine.service.getAddress(addr_id))
|
|
|
+ .doOnNext() {
|
|
|
+ if (it.ok()) {
|
|
|
+ var info=it.data() as AddressDetailsBean
|
|
|
+ //设置值
|
|
|
+ name=info.name
|
|
|
+ phone=info.mobile
|
|
|
+ address=info.address
|
|
|
+ lat=info.lat
|
|
|
+ lng=info.lng
|
|
|
+ province=info.province
|
|
|
+ city=info.city
|
|
|
+ area=info.area
|
|
|
+ type=info.type
|
|
|
+ checked.value=info.is_default
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
+ fun setAddressDel(addr_id: String) {
|
|
|
+ //删除
|
|
|
+ repository().lreq(NetEngine.service.setAddressDel(addr_id))
|
|
|
+ .doOnNext() {
|
|
|
+ if (it.ok()) {
|
|
|
+ val intent = Intent()
|
|
|
+ var con = repository().context as Activity
|
|
|
+ con.setResult(Activity.RESULT_OK, intent)
|
|
|
+ con.finish()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
|
|
|
}
|