package com.igo.user.ui.trip
|
|
import android.Manifest
|
import android.animation.ObjectAnimator
|
import android.app.Activity
|
import android.content.Intent
|
import android.content.pm.PackageManager
|
import android.content.res.Resources
|
import android.graphics.Bitmap
|
import android.graphics.Color
|
import android.net.Uri
|
import android.util.Log
|
import android.view.View
|
import android.widget.CheckBox
|
import android.widget.CheckedTextView
|
import android.widget.ImageView
|
import android.widget.LinearLayout
|
import android.widget.TextView
|
import androidx.constraintlayout.widget.ConstraintLayout
|
import androidx.core.app.ActivityCompat
|
import androidx.core.os.bundleOf
|
import androidx.core.widget.NestedScrollView
|
import androidx.lifecycle.lifecycleScope
|
import cn.sinata.rxnetty.LogHelper
|
import cn.sinata.xldutils.callPhone
|
import cn.sinata.xldutils.defaultScheduler
|
import cn.sinata.xldutils.fragment.BaseFragment
|
import cn.sinata.xldutils.gone
|
import cn.sinata.xldutils.utils.LanguageUtil
|
import cn.sinata.xldutils.utils.SpanBuilder
|
import cn.sinata.xldutils.utils.myToast
|
import cn.sinata.xldutils.utils.optBoolean
|
import cn.sinata.xldutils.utils.optInt
|
import cn.sinata.xldutils.utils.showAllowingStateLoss
|
import cn.sinata.xldutils.utils.toTime
|
import cn.sinata.xldutils.visible
|
import com.facebook.drawee.view.SimpleDraweeView
|
import com.flyco.tablayout.listener.CustomTabEntity
|
import com.flyco.tablayout.listener.OnTabSelectListener
|
import com.google.android.gms.maps.CameraUpdateFactory
|
import com.google.android.gms.maps.GoogleMap
|
import com.google.android.gms.maps.SupportMapFragment
|
import com.google.android.gms.maps.model.BitmapDescriptorFactory
|
import com.google.android.gms.maps.model.LatLng
|
import com.google.android.gms.maps.model.LatLngBounds
|
import com.google.android.gms.maps.model.Marker
|
import com.google.android.gms.maps.model.MarkerOptions
|
import com.google.android.gms.maps.model.Polyline
|
import com.google.android.gms.maps.model.PolylineOptions
|
import com.google.android.material.bottomsheet.BottomSheetBehavior
|
import com.google.gson.JsonObject
|
import com.google.maps.android.PolyUtil
|
import com.igo.user.IGoApplication
|
import com.igo.user.R
|
import com.igo.user.dialog.ChooseReserveTimeDialog
|
import com.igo.user.dialog.SetRedPackageDialog
|
import com.igo.user.dialog.TipDialog
|
import com.igo.user.network.HttpManager
|
import com.igo.user.network.entity.GeoBean
|
import com.igo.user.network.entity.RouteBean
|
import com.igo.user.network.requestByF
|
import com.igo.user.ui.H5Activity
|
import com.igo.user.ui.HomeActivity.Companion.latHome
|
import com.igo.user.ui.HomeActivity.Companion.lonHome
|
import com.igo.user.ui.MainActivity
|
import com.igo.user.ui.mine.EditEmergencyActivity
|
import com.igo.user.ui.mine.SetEmergencyActivity
|
import com.igo.user.utils.Const
|
import com.igo.user.utils.ImageUtils
|
import com.igo.user.utils.ScreenUtil
|
import com.igo.user.utils.google.AMapKit
|
import com.igo.user.views.CommonTabLayout
|
import com.igo.user.views.textbanner.TextBannerView
|
import com.tbruyelle.rxpermissions2.RxPermissions
|
import io.reactivex.subscribers.DisposableSubscriber
|
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.launch
|
import org.jetbrains.anko.backgroundResource
|
import org.jetbrains.anko.find
|
import org.jetbrains.anko.imageResource
|
import org.jetbrains.anko.sdk27.coroutines.onClick
|
import org.jetbrains.anko.support.v4.dip
|
import org.jetbrains.anko.support.v4.find
|
import org.jetbrains.anko.support.v4.startActivity
|
import org.jetbrains.anko.support.v4.startActivityForResult
|
import org.jetbrains.anko.support.v4.toast
|
import androidx.core.view.isVisible
|
import androidx.core.net.toUri
|
import androidx.core.graphics.toColorInt
|
import com.igo.user.extension.extClearInvalidZero
|
|
class MapTripFragment : BaseFragment() {
|
override fun contentViewId() = R.layout.fragment_map_trip
|
|
var aMap: GoogleMap? = null
|
|
var callback: (Boolean.() -> Unit)? = null
|
|
|
private val behavior by lazy {
|
BottomSheetBehavior.from(bottom)
|
}
|
private val mainActivity by lazy {
|
activity as MainActivity
|
}
|
var type = Const.OrderType.TYPE_SPECIAL //默认出租车
|
private val titles by lazy {
|
arrayOf(
|
getString(R.string.现在),
|
getString(R.string.预约),
|
getString(R.string.代叫)
|
)
|
}
|
private var startByChoose = false //true 通过选择出发地移动地图,false 通过手势拖动地图
|
private var startByLocal = false //true 通过复位按钮移动地图,false 通过手势拖动地图
|
|
//出发经纬度
|
private var startLon = 0.0
|
private var startLat = 0.0
|
private var startName = ""
|
|
//到达经纬度
|
private var endLon = 0.0
|
private var endLat = 0.0
|
private var endName = ""
|
private var orderType = 1 //订单类型(1=普通,2=预约)
|
private var substitute = 0 //是否是代下单(0:否,1:是)
|
private var tipMoney = 0 //小费金额
|
private var time = "" //预约时间
|
private var timeParam = "" //预约时间请求参数
|
private var name = "" //乘车人
|
private var phone: String? = null //乘车人电话
|
|
private var carTypeId = 0 //专车车型id
|
private var carTypeIndex = -1 //选中的位置
|
|
private var isEnterEmergencyContact = true //false:不强制填写紧急联系人
|
private val bottom by lazy { find<NestedScrollView>(R.id.bottom) }
|
private val tv_110 by lazy { find<TextView>(R.id.tv_110) }
|
private val ll_call by lazy { find<View>(R.id.ll_call) }
|
private val iv_back by lazy { find<ImageView>(R.id.iv_back) }
|
private val iv_location by lazy { find<ImageView>(R.id.iv_location) }
|
private val iv_location2 by lazy { find<ImageView>(R.id.iv_location2) }
|
private val tv_service by lazy { find<TextView>(R.id.tv_service) }
|
private val tv_start by lazy { find<TextView>(R.id.tv_start) }
|
private val tv_end by lazy { find<TextView>(R.id.tv_end) }
|
private val tv_time by lazy { find<TextView>(R.id.tv_time) }
|
private val tv_option by lazy { find<TextView>(R.id.tv_option) }
|
private val tv_time_special by lazy { find<TextView>(R.id.tv_time_special) }
|
private val tab_type by lazy { find<CommonTabLayout>(R.id.tab_type) }
|
private val tv_passenger by lazy { find<TextView>(R.id.tv_passenger) }
|
private val tv_passenger_special by lazy { find<TextView>(R.id.tv_passenger_special) }
|
private val tv_retry by lazy { find<TextView>(R.id.tv_retry) }
|
private val tv_tips by lazy { find<TextView>(R.id.tv_tips) }
|
private val tv_action_special by lazy { find<TextView>(R.id.tv_action_special) }
|
private val cb_use_coupon_special by lazy { find<CheckBox>(R.id.cb_use_coupon_special) }
|
private val tv_action_taxi by lazy { find<TextView>(R.id.tv_action_taxi) }
|
private val cb_use_coupon_taxi by lazy { find<CheckBox>(R.id.cb_use_coupon_taxi) }
|
private val tv_set_emergency by lazy { find<TextView>(R.id.tv_set_emergency) }
|
private val tab_confirm by lazy { find<CommonTabLayout>(R.id.tab_confirm) }
|
private val tv_msg by lazy { find<TextView>(R.id.tv_msg) }
|
private val tv_empty by lazy { find<TextView>(R.id.tv_empty) }
|
private val tv_error by lazy { find<TextView>(R.id.tv_error) }
|
private val iv_center by lazy { find<ImageView>(R.id.iv_center) }
|
private val vertical_1 by lazy { find<View>(R.id.vertical_1) }
|
private val textBanner by lazy { find<TextBannerView<String>>(R.id.textBanner) }
|
private val textBanner2 by lazy { find<TextBannerView<String>>(R.id.textBanner2) }
|
private val ll_ads by lazy { find<LinearLayout>(R.id.ll_ads) }
|
private val ll_type by lazy { find<LinearLayout>(R.id.ll_type) }
|
private val cl_bottom by lazy { find<ConstraintLayout>(R.id.cl_bottom) }
|
private val iv_banner2 by lazy { find<ImageView>(R.id.iv_banner2) }
|
private val vertical_1_special by lazy { find<View>(R.id.vertical_1_special) }
|
private val cl_special by lazy { find<View>(R.id.cl_special) }
|
private val cl_taxi by lazy { find<View>(R.id.cl_taxi) }
|
|
//是否循环刷新
|
private var hadLoopRefresh = false
|
override fun onFirstVisibleToUser() {
|
val language = LanguageUtil.getLanguage()
|
tv_110.setCompoundDrawablesRelativeWithIntrinsicBounds(
|
0,
|
if (language == Const.Language.CHINESE) R.mipmap.icon_110 else R.mipmap.icon_191,
|
0,
|
0
|
)
|
time = getString(R.string.选择时间)
|
timeParam = getString(R.string.现在)
|
name = getString(R.string.选乘车人)
|
getNotice()
|
getEmergencyEnable()
|
getBottomAds()
|
initTab()
|
initTab2()
|
initClick()
|
behavior.addBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() {
|
override fun onSlide(bottomSheet: View, slideOffset: Float) {
|
Log.e(Const.Tag, "slideOffset:$slideOffset")
|
if (slideOffset > 0.71f && slideOffset < 0.8) {
|
val fl = 0.8f - slideOffset
|
mainActivity.updateTipAlpha(fl / 0.09f)
|
} else if (slideOffset <= 0.71)
|
mainActivity.updateTipAlpha(1f)
|
else
|
mainActivity.updateTipAlpha(0f)
|
if (slideOffset > 0.03) {
|
ll_call.gone()
|
} else
|
ll_call.visible()
|
if (slideOffset < 0.9)
|
mainActivity.updateTitleVisibility(false, false)
|
else if (slideOffset > 0.92) {
|
mainActivity.updateTitleVisibility(true, false)
|
iv_back.imageResource = R.mipmap.close_bottom
|
}
|
}
|
|
override fun onStateChanged(bottomSheet: View, newState: Int) {
|
Log.e(Const.Tag, "newState:$newState")
|
if (newState == BottomSheetBehavior.STATE_EXPANDED) {
|
// cl_option.gone()
|
// bottom.smoothScrollTo(0,ll_ads.top)
|
} else if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
|
// cl_option.visible()
|
}
|
}
|
})
|
|
//用于处理当虚拟导航栏存在时,BottomSheetBehavior窥视高度不对的问题
|
val virtualBarHeigh = ScreenUtil().getVirtualBarHeigh(activity)
|
Log.e("虚拟导航栏高度", "$virtualBarHeigh")
|
val navigationBarHeight = getNavigationBarHeight()
|
Log.e("导航栏高度", "$navigationBarHeight")
|
if (virtualBarHeigh >= getNavigationBarHeight()) //如果虚拟导航栏高度大于或等于导航栏高度,说明虚拟导航栏在显示,需要减去导航栏的高度
|
behavior.setPeekHeight(dip(240) - getNavigationBarHeight(), true)
|
else
|
behavior.setPeekHeight(dip(240), true)
|
enableMyLocation()
|
loopRefreshDriverNum()
|
}
|
|
private fun getNavigationBarHeight(): Int {
|
val resources: Resources = requireActivity().resources
|
val resourceId: Int = resources.getIdentifier("navigation_bar_height", "dimen", "android")
|
val height: Int = resources.getDimensionPixelSize(resourceId)
|
return height
|
}
|
|
private fun initClick() {
|
//点击归位
|
iv_location.setOnClickListener {
|
val currentLocation = IGoApplication.getCurrentLocation()
|
startByLocal = true
|
aMap?.animateCamera(CameraUpdateFactory.newLatLngZoom(currentLocation, 14f))
|
}
|
iv_location2.setOnClickListener {
|
aMap?.animateCamera(
|
CameraUpdateFactory.newLatLngZoom(
|
LatLng(startLat, startLon),
|
14f
|
)
|
)
|
}
|
tv_110.setOnClickListener {
|
callPhone(mainActivity.phoneEmergency)
|
}
|
tv_service.setOnClickListener {
|
callPhone(IGoApplication.phoneService)
|
}
|
tv_start.setOnClickListener {
|
startActivityForResult<ChoosePositionActivity>(1)
|
if (behavior.state == BottomSheetBehavior.STATE_EXPANDED)
|
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
|
}
|
tv_end.setOnClickListener {
|
startActivityForResult<ChoosePositionActivity>(2, "isStart" to false)
|
if (behavior.state == BottomSheetBehavior.STATE_EXPANDED)
|
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
|
}
|
tv_time.setOnClickListener {
|
showTimeDialog()
|
}
|
tv_option.setOnClickListener {
|
if (tab_type.currentTab == 1) {
|
showTimeDialog()
|
} else {//代叫
|
startActivityForResult<OrderOtherActivity>(3)
|
}
|
}
|
tv_time_special.onClick {
|
showTimeDialog()
|
}
|
|
tv_passenger.setOnClickListener {
|
startActivityForResult<OrderOtherActivity>(3)
|
}
|
|
tv_passenger_special.onClick {
|
startActivityForResult<OrderOtherActivity>(3)
|
}
|
|
tv_retry.onClick {
|
getCarType()
|
}
|
|
tv_tips.setOnClickListener {
|
val setRedPackageDialog = SetRedPackageDialog()
|
setRedPackageDialog.setCallback(object : SetRedPackageDialog.Callback {
|
override fun onOk(int: Int) {
|
tipMoney = int
|
tv_tips.text = String.format("小费%d元", tipMoney)
|
}
|
})
|
setRedPackageDialog.show(childFragmentManager, "red")
|
}
|
|
iv_back.setOnClickListener {
|
if (behavior.state == BottomSheetBehavior.STATE_EXPANDED) {
|
behavior.state = BottomSheetBehavior.STATE_COLLAPSED
|
bottom.smoothScrollTo(0, 0)
|
} else {
|
back()
|
}
|
}
|
|
tv_set_emergency.onClick {
|
//setEmergency()
|
}
|
|
|
tv_action_taxi.setOnClickListener {
|
makeOrder()
|
}
|
|
tv_action_special.setOnClickListener {
|
if (carTypeIndex == -1)
|
myToast("请选择车型")
|
else
|
makeOrder()
|
}
|
}
|
|
private fun makeOrder() {
|
if (startName.isEmpty()) {
|
myToast(getString(R.string.正在获取起点位置信息))
|
return
|
}
|
if (mainActivity.checkLogin() && mainActivity.checkPhone()) {
|
/*if (IGoApplication.emergency.isEmpty()&&isEnterEmergencyContact) {
|
myToast(getString(R.string.请设置紧急联系人))
|
startActivity<SetEmergencyActivity>(
|
"name" to "no",
|
"phone" to "",
|
"myPhone" to mainActivity.userInfo!!.phone
|
)
|
return
|
}*/
|
orderType = if (type == Const.OrderType.TYPE_TAXI) { //出租车预约判断多一个tab选择为1的条件
|
if (tab_confirm.currentTab == 1 && (time != getString(R.string.选择时间) && time != getString(
|
R.string.现在
|
))
|
) 2 else 1
|
} else {//专车预约只需要判断是否选择时间
|
if ((time != getString(R.string.选择时间) && time != getString(R.string.现在))) 2 else 1
|
}
|
substitute = if (type == Const.OrderType.TYPE_TAXI) { //出租车代叫判断tab选择为2
|
if (tab_confirm.currentTab == 2) 1 else 0
|
} else {//专车代叫只判断是否选择姓名
|
if (name != getString(R.string.选乘车人)) 1 else 0
|
}
|
if (chooseindex == 2 && (name.isEmpty() || phone.isNullOrEmpty())) {
|
myToast(getString(R.string.请选择乘车人))
|
return
|
}
|
tv_action_taxi.isClickable = false
|
tv_action_special.isClickable = false
|
showDialog()
|
val currentLocation = IGoApplication.getCurrentLocation()
|
HttpManager.addTaxiOrder(
|
endName,
|
endLat,
|
endLon,
|
1,
|
orderType,
|
currentLocation.latitude,
|
currentLocation.longitude,
|
startName,
|
startLat,
|
startLon,
|
substitute,
|
tipMoney,
|
if (orderType == 1) System.currentTimeMillis().toTime("yyyy-MM-dd HH:mm")
|
else timeParam,
|
null,
|
if (substitute == 0) null else name,
|
if (substitute == 0) null else phone,
|
carTypeId, 1, type,
|
(if (type == Const.OrderType.TYPE_TAXI) cb_use_coupon_taxi else cb_use_coupon_special).isChecked
|
).requestByF(this, success = { _, data ->
|
data?.run {
|
tv_action_taxi.isClickable = true
|
tv_action_special.isClickable = true
|
if (optInt("state") == 7) {
|
val tipDialog = TipDialog()
|
tipDialog.arguments = bundleOf(
|
"msg" to getString(R.string.有订单未支付),
|
"ok" to getString(R.string.立即支付),
|
"cancel" to getString(R.string.取消)
|
)
|
tipDialog.setCallback(object : TipDialog.OnClickCallback {
|
override fun onOk() {
|
startActivity<OrderDetailActivity>(
|
"type" to type,
|
"id" to optInt("id")
|
)
|
}
|
|
override fun onCancel() {
|
}
|
})
|
tipDialog.show(childFragmentManager, "unpay")
|
} else {
|
LogHelper.logRecord("您有订单进行中orderId:${optInt("id")} orderType:${type}")
|
startActivity<TripActivity>("type" to type, "id" to optInt("id"))
|
back()
|
}
|
}
|
}, error = { _, _ ->
|
tv_action_taxi.isClickable = true
|
tv_action_special.isClickable = true
|
})
|
}
|
}
|
|
private fun setEmergency() {
|
if (mainActivity.checkLogin()) {
|
if (mainActivity.userInfo?.emergencyContactNumber.isNullOrEmpty())
|
startActivity<SetEmergencyActivity>(
|
"name" to "",
|
"phone" to "", "myPhone" to mainActivity.userInfo?.phone
|
)
|
else
|
startActivity<EditEmergencyActivity>(
|
"name" to mainActivity.userInfo?.emergencyContact,
|
"phone" to mainActivity.userInfo?.emergencyContactNumber,
|
"myPhone" to mainActivity.userInfo?.phone
|
)
|
}
|
}
|
|
private fun showTimeDialog() {
|
val chooseReserveTimeDialog = ChooseReserveTimeDialog()
|
chooseReserveTimeDialog.setCallback(object : ChooseReserveTimeDialog.Callback {
|
override fun onOk(s: String, formatS: String) {
|
tv_time.text = s
|
tv_option.text = s
|
tv_time_special.text = s
|
time = s
|
timeParam = formatS
|
}
|
})
|
chooseReserveTimeDialog.show(childFragmentManager, "time")
|
}
|
|
|
private var locCount = 0L //定位次数
|
private fun initMap() {
|
val fragment = childFragmentManager.findFragmentById(R.id.mMapView) as SupportMapFragment
|
AMapKit.initMap(fragment) { it ->
|
val requireActivity = requireActivity()
|
if (ActivityCompat.checkSelfPermission(
|
requireActivity,
|
Manifest.permission.ACCESS_FINE_LOCATION
|
) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(
|
requireActivity,
|
Manifest.permission.ACCESS_COARSE_LOCATION
|
) != PackageManager.PERMISSION_GRANTED
|
) {
|
return@initMap
|
}
|
aMap = it.apply {
|
isMyLocationEnabled = true
|
uiSettings.isMyLocationButtonEnabled = false
|
setOnMyLocationChangeListener {
|
Log.e(Const.Tag, "出租车:lat:" + it.latitude + ",lon:" + it.longitude)
|
IGoApplication.setLocation(it.latitude, it.longitude)
|
// IGoApplication.address = it.address
|
// IGoApplication.cityCode = it.extras.getString("adcode")?:""
|
if (locCount < 2) {
|
if (mainActivity.lat != 0.0)
|
moveCamera(
|
CameraUpdateFactory.newLatLngZoom(
|
LatLng(
|
mainActivity.lat,
|
mainActivity.lon
|
), 18f
|
)
|
)
|
else
|
moveCamera(
|
CameraUpdateFactory.newLatLngZoom(
|
LatLng(
|
it.latitude,
|
it.longitude
|
), 18f
|
)
|
)
|
}
|
locCount++
|
}
|
setOnCameraMoveStartedListener {
|
if (startByLocal)
|
tv_msg.text = getString(R.string.local_ing)
|
else if (!startByChoose)
|
tv_msg.text = getString(R.string.move_tip)
|
}
|
setOnCameraIdleListener {
|
if (!startByChoose && iv_center.isVisible) {
|
startLat = cameraPosition.target.latitude
|
startLon = cameraPosition.target.longitude
|
getPoi(startLat, startLon)
|
}
|
startByChoose = false
|
startByLocal = false
|
val objectAnimator =
|
ObjectAnimator.ofFloat(iv_center, "translationY", 0f, -30f, 0f)
|
.setDuration(600)
|
val objectAnimator1 =
|
ObjectAnimator.ofFloat(tv_msg, "translationY", 0f, -30f, 0f)
|
.setDuration(600)
|
objectAnimator.start()
|
objectAnimator1.start()
|
if (endLat == 0.0)
|
getDriverNum()
|
tv_msg.text = getString(R.string.在这里上车)
|
}
|
moveCamera(CameraUpdateFactory.newLatLng(IGoApplication.getCurrentLocation()))
|
}
|
}
|
}
|
|
|
/**
|
* Enables the My Location layer if the fine location permission has been granted.
|
*/
|
private fun enableMyLocation() {
|
val subscribe = RxPermissions(requireActivity()).request(
|
Manifest.permission.ACCESS_FINE_LOCATION,
|
Manifest.permission.ACCESS_COARSE_LOCATION
|
).subscribe {
|
if (it)
|
initMap()
|
else {
|
myToast(getString(R.string.没有权限))
|
}
|
}
|
}
|
|
var chooseindex = 0
|
|
private fun initTab() {
|
val list = arrayListOf<CustomTabEntity>()
|
list.addAll(titles.map {
|
object : CustomTabEntity {
|
override fun getTabUnselectedIcon() = 0
|
|
override fun getTabSelectedIcon() = 0
|
|
override fun getTabTitle() = it
|
}
|
})
|
tab_type.setTabData(list)
|
tab_type.currentTab = 0
|
tab_type.setOnTabSelectListener(object : OnTabSelectListener {
|
override fun onTabSelect(position: Int) {
|
chooseindex = position
|
Log.e("onTabSelect: ", "当前选择${position}")
|
tv_option.visibility = if (position == 0) View.GONE else View.VISIBLE
|
tv_option.text = if (position == 1) time else name
|
|
tv_passenger_special.text = name
|
tv_time_special.text =
|
if (position == 1) getString(R.string.选择时间) else getString(R.string.现在)
|
}
|
|
override fun onTabReselect(position: Int) {
|
}
|
})
|
}
|
|
private fun initTab2() {
|
val list = arrayListOf<CustomTabEntity>()
|
list.addAll(titles.map {
|
object : CustomTabEntity {
|
override fun getTabUnselectedIcon() = 0
|
|
override fun getTabSelectedIcon() = 0
|
|
override fun getTabTitle() = it
|
}
|
})
|
tab_confirm.setTabData(list)
|
tab_confirm.currentTab = 0
|
tab_confirm.setOnTabSelectListener(object : OnTabSelectListener {
|
override fun onTabSelect(position: Int) {
|
if (position == 1) {
|
tv_time.visible()
|
vertical_1.visible()
|
} else {
|
tv_time.gone()
|
vertical_1.gone()
|
}
|
}
|
|
override fun onTabReselect(position: Int) {
|
}
|
})
|
}
|
|
fun changeType(type: Int) {
|
this.type = type
|
getDriverNum()
|
checkBack()
|
}
|
|
private var noCarShowing = false
|
fun getDriverNum() {
|
getDriverNum { _, data ->
|
val i = data?.optInt("number") ?: 0
|
val t = getString(R.string.当前附近有n辆车可用, i)
|
val stringBuilder = SpanBuilder(t).bold(
|
t.indexOf(i.toString()),
|
t.indexOf(i.toString()) + i.toString().length
|
)
|
.color(
|
requireContext(),
|
t.indexOf(i.toString()),
|
t.indexOf(i.toString()) + i.toString().length,
|
cn.sinata.xldutils.R.color.black
|
).build()
|
activity?.takeIf {
|
!it.isFinishing && !it.isDestroyed
|
}?.also {
|
mainActivity.updateTipText(stringBuilder)
|
}
|
if (i == 0 && !noCarShowing) {
|
noCarShowing = true
|
val tipDialog = TipDialog()
|
tipDialog.arguments =
|
bundleOf("msg" to getString(R.string.no_car), "isAlert" to true)
|
tipDialog.showAllowingStateLoss(childFragmentManager, "noCar")
|
tipDialog.setDismissCallback(object : TipDialog.OnDismiss {
|
override fun onDismiss() {
|
noCarShowing = false
|
}
|
})
|
}
|
}
|
}
|
|
//循环刷新司机数量
|
private fun loopRefreshDriverNum() {
|
if (hadLoopRefresh) {
|
return
|
}
|
hadLoopRefresh = true
|
viewLifecycleOwner.lifecycleScope.launch {
|
while (true) {
|
delay(5000)
|
getDriverNum { _, data ->
|
val i = data?.optInt("number") ?: 0
|
val t = getString(R.string.当前附近有n辆车可用, i)
|
val stringBuilder = SpanBuilder(t).bold(
|
t.indexOf(i.toString()),
|
t.indexOf(i.toString()) + i.toString().length
|
)
|
.color(
|
requireContext(),
|
t.indexOf(i.toString()),
|
t.indexOf(i.toString()) + i.toString().length,
|
cn.sinata.xldutils.R.color.black
|
).build()
|
activity?.takeIf {
|
!it.isFinishing && !it.isDestroyed
|
}?.also {
|
mainActivity.updateTipText(stringBuilder)
|
}
|
}
|
}
|
}
|
}
|
|
private fun getDriverNum(success: (msg: String?, t: JsonObject?) -> Unit) {
|
if (activity == null)
|
return
|
if (!mainActivity.isTripping && startLat != 0.0)
|
HttpManager.getDriverNum(startLat, startLon, type).requestByF(
|
fragment = this,
|
success = success
|
)
|
}
|
|
private fun getEmergencyEnable() {
|
HttpManager.isEnterEmergencyContact().requestByF(this, false) { _, data ->
|
isEnterEmergencyContact = data?.optBoolean("isEnterEmergencyContact", true) ?: true
|
Log.e("FMLog", "是否强制填写紧急联系人:$isEnterEmergencyContact")
|
}
|
}
|
|
private fun getNotice() {
|
HttpManager.queryNotices(1).requestByF(this) { _, data ->
|
data?.let {
|
val list = arrayListOf<String>()
|
list.addAll(it.map { it.content })
|
textBanner.setDatas(list)
|
textBanner2.setDatas(list)
|
}
|
}
|
}
|
|
/**
|
* 底部广告
|
*/
|
private fun getBottomAds() {
|
if (!mainActivity.isOpen) {
|
return
|
}
|
if (mainActivity.lat != 0.0 && mainActivity.lon != 0.0) {
|
getBottomAds(mainActivity.lat, mainActivity.lon)
|
} else {
|
initLocation(mainActivity) {
|
getBottomAds(it.latitude, it.longitude)
|
}
|
}
|
}
|
|
/**
|
* 底部广告
|
*/
|
private fun getBottomAds(latitude: Double, longitude: Double) {
|
HttpManager.getAds(2, latitude.toString(), longitude.toString())
|
.requestByF(this) { _, data ->
|
ll_ads.removeAllViews()
|
data?.forEach {
|
val view = layoutInflater.inflate(R.layout.item_img_ads, null)
|
val ivImg = view.find<ImageView>(R.id.iv_img)
|
ImageUtils.load(
|
ivImg,
|
it.imgUrl,
|
cn.sinata.xldutils.R.color.page_bg,
|
cn.sinata.xldutils.R.color.page_bg,
|
6
|
)
|
ivImg.setOnClickListener { _ ->
|
if (it.isJump == 1) { //要跳转
|
if (it.jumpType == "2")
|
startActivity<H5Activity>(
|
"title" to it.name,
|
"url" to it.content
|
)
|
else {
|
val intent = Intent()
|
intent.action = "android.intent.action.VIEW"
|
intent.data = it.jumpUrl.toUri()
|
startActivity(intent)
|
}
|
}
|
}
|
ll_ads.addView(view)
|
}
|
}
|
}
|
|
|
/**
|
*查询专车类型和价格
|
*/
|
private fun getCarType() {
|
showDialog()
|
HttpManager.queryServerCarModel("$startLon,$startLat", "$endLon,$endLat", type)
|
.requestByF(this, success = { _, data ->
|
data?.let { list ->
|
if (list.isEmpty()) {
|
tv_empty.visible()
|
tv_action_special.isEnabled = false
|
} else {
|
tv_action_special.isEnabled = true
|
tv_error.gone()
|
tv_retry.gone()
|
list.forEachIndexed { index, it ->
|
val view =
|
layoutInflater.inflate(R.layout.item_car_price, ll_type, false)
|
val tv_name = view.find<CheckedTextView>(R.id.tv_name)
|
val tv_price = view.find<CheckedTextView>(R.id.tv_price)
|
val iv_car = view.find<SimpleDraweeView>(R.id.iv_car)
|
val ll_car = view.find<View>(R.id.ll_car)
|
tv_name.text = it.name
|
tv_price.text =
|
getString(R.string.预估价格, it.amount).replace(",", ".").extClearInvalidZero()
|
iv_car.setImageURI(it.img)
|
if (index == 0) { //默认选择第一个
|
carTypeId = it.id
|
carTypeIndex = 0
|
tv_name.isChecked = true
|
tv_price.isChecked = true
|
ll_car.elevation = dip(3).toFloat()
|
ll_car.backgroundResource = R.drawable.bg_white_6dp
|
}
|
view.onClick { _ ->
|
showCarPriceNote(it.fareTypeNote)
|
if (index != carTypeIndex) {
|
tv_name.isChecked = true
|
tv_price.isChecked = true
|
ll_car.elevation = dip(3).toFloat()
|
ll_car.backgroundResource = R.drawable.bg_white_6dp
|
if (carTypeIndex != -1)
|
ll_type.getChildAt(carTypeIndex).apply {
|
val tv_name = find<CheckedTextView>(R.id.tv_name)
|
val tv_price = find<CheckedTextView>(R.id.tv_price)
|
val ll_car = find<View>(R.id.ll_car)
|
tv_name.isChecked = false
|
tv_price.isChecked = false
|
ll_car.elevation = 0f
|
ll_car.backgroundResource = 0
|
}
|
carTypeIndex = index
|
carTypeId = it.id
|
}
|
}
|
ll_type.addView(view)
|
}
|
}
|
}
|
}) { _, _ ->
|
tv_error.visible()
|
tv_retry.visible()
|
tv_action_special.isEnabled = false
|
}
|
}
|
|
/**
|
* 运费说明
|
* 点击车型图片即弹窗说明
|
*/
|
private fun showCarPriceNote(msg: String) {
|
val tipDialog = TipDialog()
|
tipDialog.arguments = bundleOf("msg" to msg, "isAlert" to true, "noTitle" to true)
|
tipDialog.showAllowingStateLoss(childFragmentManager, "note")
|
}
|
|
/**
|
* 获取poi信息
|
*/
|
private var poiSearchDisposable: DisposableSubscriber<GeoBean>? = null
|
|
//
|
fun getPoi(lat: Double?, lng: Double?, call: (String.() -> Unit)? = null) {
|
if (lat == null || lng == null) {
|
return
|
}
|
//如果上一次还没处理完,取消订阅
|
if (poiSearchDisposable != null && !poiSearchDisposable!!.isDisposed) {
|
poiSearchDisposable?.dispose()
|
}
|
poiSearchDisposable = object : DisposableSubscriber<GeoBean>() {
|
override fun onComplete() {
|
}
|
|
override fun onNext(t: GeoBean?) {
|
if (view == null) {
|
return
|
}
|
val address = if (t != null && t.status == "OK") {
|
t.results.firstOrNull()?.formatted_address
|
} else {
|
null
|
}
|
startName = address ?: ""
|
tv_start.text = address
|
call?.invoke(startName)
|
mainActivity.startName = startName
|
mainActivity.startLat = startLat
|
mainActivity.startLon = startLon
|
}
|
|
override fun onError(t: Throwable?) {
|
if (view == null) {
|
return
|
}
|
if (isAdded) {
|
startName = ""
|
tv_start.text = getString(R.string.您从哪里上车)
|
}
|
}
|
}
|
HttpManager.geoCode(lat, lng).defaultScheduler().subscribe(poiSearchDisposable)
|
}
|
|
override fun onDestroyView() {
|
if (poiSearchDisposable?.isDisposed == false) {
|
poiSearchDisposable?.dispose()
|
}
|
if (routeSearchDisposable?.isDisposed == false) {
|
routeSearchDisposable?.dispose()
|
}
|
super.onDestroyView()
|
}
|
|
/**
|
* 获取路线信息
|
*/
|
private var routeSearchDisposable: DisposableSubscriber<RouteBean>? = null
|
private var drivingRouteOverlay: Polyline? = null
|
private fun searchRoute() {
|
//如果上一次还没处理完,取消订阅
|
if (routeSearchDisposable != null && !routeSearchDisposable!!.isDisposed) {
|
routeSearchDisposable?.dispose()
|
}
|
routeSearchDisposable = object : DisposableSubscriber<RouteBean>() {
|
override fun onComplete() {
|
}
|
|
override fun onNext(t: RouteBean?) {
|
if (view == null) {
|
return
|
}
|
if (t?.status == "OK") {
|
drivingRouteOverlay?.remove()
|
val decode = PolyUtil.decode(t.routes.firstOrNull()?.overview_polyline?.points)
|
val geodesic = PolylineOptions()
|
.add(LatLng(startLat, startLon))
|
.width(15f)
|
.color("#00C47A".toColorInt())
|
.geodesic(true)
|
geodesic.addAll(decode)
|
geodesic.add(LatLng(endLat, endLon))
|
drivingRouteOverlay = aMap?.addPolyline(geodesic)
|
val newLatLngBounds = CameraUpdateFactory.newLatLngBounds(
|
LatLngBounds(
|
LatLng(
|
t.routes.firstOrNull()?.bounds?.southwest?.lat ?: 0.0,
|
t.routes.firstOrNull()?.bounds?.southwest?.lng ?: 0.0
|
),
|
LatLng(
|
t.routes.firstOrNull()?.bounds?.northeast?.lat ?: 0.0,
|
t.routes.firstOrNull()?.bounds?.northeast?.lng ?: 0.0
|
)
|
), 240
|
)
|
aMap?.animateCamera(newLatLngBounds)
|
setMarker(
|
LatLng(endLat, endLon),
|
getString(
|
R.string.大约行驶分钟,
|
t.routes.firstOrNull()?.legs?.firstOrNull()?.duration?.text
|
)
|
)
|
}
|
}
|
|
override fun onError(t: Throwable?) {
|
toast(getString(R.string.路径规划失败))
|
}
|
}
|
HttpManager.directions(startLat, startLon, endLat, endLon).defaultScheduler()
|
.subscribe(routeSearchDisposable)
|
}
|
|
/**确认订单*/
|
private fun showConfirmOrder() {
|
if (endLat != 0.0 && endLon != 0.0) {
|
iv_center.gone()
|
tv_msg.gone()
|
searchRoute()
|
bottom.gone()
|
cl_bottom.visible()
|
iv_location2.visible()
|
textBanner2.visible()
|
iv_banner2.visible()
|
tab_confirm.currentTab = tab_type.currentTab
|
tv_time.visibility = if (tab_confirm.currentTab == 1) View.VISIBLE else View.GONE
|
tv_time_special.visibility = if (tab_type.currentTab == 1) View.VISIBLE else View.GONE
|
vertical_1_special.visibility = tv_time_special.visibility
|
vertical_1.visibility = tv_time.visibility
|
mainActivity.updateTitleVisibility(false, true)
|
iv_back.imageResource = cn.sinata.xldutils.R.mipmap.back
|
setMarker(LatLng(startLat, startLon))
|
when (type) { //根据订单类型渲染
|
Const.OrderType.TYPE_SPECIAL -> {
|
// mainActivity.updateTipAlpha(0f)
|
cl_special.visible()
|
if (chooseindex == 2) {
|
tv_passenger_special.visible()
|
} else {
|
tv_passenger_special.gone()
|
}
|
callback?.invoke(false)
|
getCarType()
|
}
|
|
Const.OrderType.TYPE_TAXI -> {
|
cl_taxi.visible()
|
}
|
}
|
}
|
}
|
|
/**检查返回状态*/
|
fun checkBack(): Boolean {
|
return try {
|
if (cl_bottom.isVisible) { //先返回到上一步
|
back()
|
false
|
} else
|
true
|
} catch (e: Exception) {
|
true
|
}
|
}
|
|
/**返回上一步*/
|
fun back() {
|
cl_bottom.gone()
|
bottom.visible()
|
iv_location2.gone()
|
iv_banner2.gone()
|
textBanner2.gone()
|
iv_center.visible()
|
mainActivity.updateTitleVisibility(false, false)
|
mainActivity.updateTipAlpha(1f)
|
drivingRouteOverlay?.remove()
|
startMarker?.remove()
|
endMarker?.remove()
|
endLat = 0.0
|
endLon = 0.0
|
time = getString(R.string.选择时间)
|
phone = ""
|
name = getString(R.string.选乘车人)
|
startByLocal = true
|
aMap?.animateCamera(
|
CameraUpdateFactory.newLatLngZoom(IGoApplication.getCurrentLocation(), 18f)
|
)
|
mainActivity.updateTipAlpha(1f)
|
|
//出租车重置
|
cl_taxi.gone()
|
tipMoney = 0
|
tv_time.text = getString(R.string.预约时间)
|
tv_passenger.text = "更换乘车人"
|
tv_tips.text = "小费"
|
tab_type.currentTab = 0
|
|
//专车重置
|
cl_special.gone()
|
callback?.invoke(true)
|
tv_time_special.text = getString(R.string.现在)
|
tv_passenger_special.text = getString(R.string.选乘车人)
|
carTypeIndex = -1
|
carTypeId = 0
|
ll_type.removeAllViews()
|
tv_option.gone()
|
tv_msg.visible()
|
tv_empty.gone()
|
tv_error.gone()
|
tv_retry.gone()
|
tv_action_special.isEnabled = true
|
}
|
|
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
super.onActivityResult(requestCode, resultCode, data)
|
if (resultCode == Activity.RESULT_OK && data != null) {
|
tv_passenger_special.gone()
|
when (requestCode) {
|
1 -> {
|
startLat = data.getDoubleExtra("lat", 0.0)
|
startLon = data.getDoubleExtra("lon", 0.0)
|
startByChoose = true
|
aMap?.animateCamera(CameraUpdateFactory.newLatLng(LatLng(startLat, startLon)))
|
startName = data.getStringExtra("name") ?: ""
|
tv_start.text = startName
|
showConfirmOrder()
|
}
|
|
2 -> {
|
endLat = data.getDoubleExtra("lat", 0.0)
|
endLon = data.getDoubleExtra("lon", 0.0)
|
endName = data.getStringExtra("name") ?: ""
|
showConfirmOrder()
|
}
|
|
3 -> {
|
name = data.getStringExtra("name") ?: ""
|
phone = data.getStringExtra("phone") ?: ""
|
tv_option.text = name
|
Log.e("选择的: ", name)
|
tv_passenger.text = name
|
if (chooseindex == 2) tv_passenger_special.visible() else tv_passenger_special.gone()
|
tv_passenger_special.text = name
|
|
}
|
}
|
}
|
}
|
|
private var startMarker: Marker? = null
|
private var endMarker: Marker? = null
|
private fun setMarker(latLon: LatLng, content: String = "") {
|
val options = MarkerOptions()
|
options.position(latLon)
|
val language = LanguageUtil.getLanguage()
|
if (content.isNotEmpty()) {
|
val ic = when (language) {
|
Const.Language.ENGLISH -> R.mipmap.end_point_e
|
Const.Language.CHINESE -> R.mipmap.end_point
|
else -> R.mipmap.end_point_f
|
}
|
val view = layoutInflater.inflate(R.layout.marker_with_text, null, false)
|
val contentView: TextView = view.findViewById(R.id.tv_content)
|
contentView.text = content
|
(view.findViewById<ImageView>(R.id.iv)!!).imageResource = ic
|
options.icon(BitmapDescriptorFactory.fromBitmap(convertViewToBitmap(view)))
|
startMarker = aMap?.addMarker(options)
|
} else {
|
val ic = when (language) {
|
Const.Language.ENGLISH -> R.mipmap.starting_point_e
|
Const.Language.CHINESE -> R.mipmap.starting_point
|
else -> R.mipmap.starting_point_f
|
}
|
options.icon(BitmapDescriptorFactory.fromResource(ic))
|
endMarker = aMap?.addMarker(options)
|
}
|
}
|
|
/**
|
* 吧iew转化为bitmap
|
*
|
* @param view
|
* @return
|
*/
|
fun convertViewToBitmap(view: View): Bitmap {
|
view.isDrawingCacheEnabled = true
|
view.measure(
|
View.MeasureSpec.makeMeasureSpec(
|
0,
|
View.MeasureSpec.UNSPECIFIED
|
),
|
View.MeasureSpec.makeMeasureSpec(
|
0,
|
View.MeasureSpec.UNSPECIFIED
|
)
|
)
|
view.layout(0, 0, view.measuredWidth, view.measuredHeight)
|
view.buildDrawingCache()
|
val mCacheBitmap = view.drawingCache
|
return Bitmap.createBitmap(mCacheBitmap)
|
}
|
|
//初始化定位
|
private fun initLocation(activity: Activity, block: (LatLng) -> Unit) {
|
if (!IGoApplication.hadInitLocation()) {
|
AMapKit.initLocation(activity, success = {
|
if (view == null) {
|
return@initLocation
|
}
|
val latLng = it.latLng
|
if (latLng == null) {
|
LogHelper.logRecord("定位失败")
|
return@initLocation
|
}
|
latHome = latLng.latitude
|
lonHome = latLng.longitude
|
LogHelper.logRecord("定位成功:latitude=${latLng.latitude} longitude=${latLng.longitude}")
|
IGoApplication.setLocation(latLng)
|
block.invoke(latLng)
|
}) {
|
LogHelper.logRecord("定位失败")
|
}
|
return
|
}
|
block.invoke(IGoApplication.getCurrentLocation())
|
}
|
}
|