package com.driver.igotrip.netUtls
|
|
import android.content.Context
|
import android.os.CountDownTimer
|
import android.os.Handler
|
import android.os.Looper
|
import android.view.LayoutInflater
|
import android.view.View
|
import cn.sinata.xldutils.activitys.BaseActivity
|
import com.driver.igotrip.base.MyBaseActivity
|
import com.driver.igotrip.base.MyBaseFragment
|
import com.driver.igotrip.utils.Cache.CacheKey
|
import com.driver.igotrip.utils.DES
|
|
fun delayTo(time: Long, clickAction: () -> Unit) {
|
Handler(Looper.getMainLooper()).postDelayed({
|
clickAction()
|
}, time)
|
}
|
|
fun MyBaseActivity.callNet(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit
|
) {
|
callNet(true, api, map, clickAction)
|
}
|
|
fun BaseActivity.callNet(
|
showDialog: Boolean,
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit
|
) {
|
callNet(showDialog, api, map, clickAction) { _, _ -> }
|
}
|
|
fun BaseActivity.callNet(
|
showDialog: Boolean,
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
clickActionError: (code: Int, errorString: String?) -> Unit
|
) {
|
callNet(false, showDialog, api, map, clickAction, clickActionError)
|
}
|
|
fun BaseActivity.callNet(
|
isUseAllTrustService: Boolean,
|
showDialog: Boolean,
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
clickActionError: (code: Int, errorString: String?) -> Unit
|
) {
|
if (showDialog)
|
showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
val sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(isUseAllTrustService, api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
if (showDialog)
|
dismissDialog()
|
clickAction(responseString)
|
}
|
|
override fun onError(code: Int, msg: String?, resposeString: String?) {
|
super.onError(code, msg, resposeString)
|
clickActionError(code, msg)
|
}
|
})
|
}
|
|
fun MyBaseFragment.callNet(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit
|
) {
|
this.showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
dismissDialog()
|
clickAction(responseString)
|
}
|
})
|
}
|
|
|
fun getAppId(): String {
|
var appid = CacheKey.getKeyStr("appid")
|
if (appid.isEmpty()) {
|
return "0"
|
} else {
|
return appid
|
}
|
}
|
|
fun getUserId(): Int {
|
var user = CacheKey.getKeyStr("userId")
|
if (user.isNullOrEmpty()) {
|
user = "-1"
|
}
|
return user.toInt()
|
}
|
|
fun getToken(): String {
|
return CacheKey.getKeyStr("token")
|
}
|
|
fun MyBaseActivity.callNet(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
clickActionError: (code: Int, msg: String?) -> Unit
|
) {
|
this.showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
val sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
dismissDialog()
|
clickAction(responseString)
|
}
|
|
override fun onError(code: Int, msg: String?, resposeString: String?) {
|
super.onError(code, msg, resposeString)
|
clickActionError(code, msg)
|
}
|
})
|
|
}
|
|
|
fun MyBaseActivity.callNetDriver(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
clickActionError: (errorString: String?) -> Unit
|
) {
|
this.showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
dismissDialog()
|
clickAction(responseString)
|
}
|
|
override fun onError(code: Int, msg: String?, resposeString: String?) {
|
clickActionError(resposeString)
|
}
|
})
|
|
}
|
|
|
fun MyBaseFragment.callNet(
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
clickActionError: (errorString: String?) -> Unit
|
) {
|
this.showDialog()
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(this) {
|
override fun success(responseString: String?) {
|
dismissDialog()
|
clickAction(responseString)
|
}
|
|
override fun onError(code: Int, msg: String?, resposeString: String?) {
|
super.onError(code, msg, resposeString)
|
clickActionError(msg)
|
}
|
})
|
|
}
|
|
fun callNet(
|
context: Context,
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit
|
) {
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(context) {
|
override fun success(responseString: String?) {
|
clickAction(responseString)
|
}
|
})
|
}
|
|
fun callNet1(
|
context: Context,
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
errorAction: (code: Int, msg: String?) -> Unit, showError: Boolean = true
|
) {
|
if (api.startsWith("api")) {
|
map["appid"] = getAppId()
|
var sign = DES.getSign(map)
|
map["sign"] = sign
|
}
|
HttpManager.getInstance().post(api, map, object : MyObserver(context) {
|
override fun success(responseString: String?) {
|
clickAction(responseString)
|
}
|
|
override fun onError(code: Int, msg: String?, resposeString: String?) {
|
if (showError)
|
super.onError(code, msg, resposeString)
|
errorAction(code, msg)
|
}
|
})
|
}
|
|
|
fun callNetGoogle(
|
context: Context,
|
api: String,
|
map: HashMap<String?, Any?>,
|
clickAction: (responseString: String?) -> Unit,
|
errorAction: (e: Throwable) -> Unit
|
) {
|
HttpManager.getInstance().post(api, map, object : MyObserver(context) {
|
override fun success(responseString: String?) {
|
clickAction(responseString)
|
}
|
|
override fun onError(e: Throwable) {
|
errorAction(e)
|
}
|
|
override fun onError(code: Int, msg: String?, resposeString: String?) {
|
}
|
})
|
}
|
|
fun getMapByAny(): HashMap<String?, Any?> {
|
return HashMap()
|
}
|
|
|
fun createView(layout: Int, context: Context): View {
|
return LayoutInflater.from(context).inflate(layout, null)
|
}
|
|
fun timeOver(click: (mill: Int) -> Unit): CountDownTimer {
|
val timer by lazy {
|
object : CountDownTimer(60000, 1000) {
|
override fun onFinish() {
|
click(0)
|
}
|
|
override fun onTick(millisUntilFinished: Long) {
|
click((millisUntilFinished / 1000).toInt())
|
}
|
}
|
}
|
timer.start()
|
return timer
|
}
|
|
|
fun timeOver(timeDown: Long, click: (mill: Int) -> Unit): CountDownTimer {
|
val timer by lazy {
|
object : CountDownTimer(timeDown, 1000) {
|
override fun onFinish() {
|
click(0)
|
}
|
|
override fun onTick(millisUntilFinished: Long) {
|
click((millisUntilFinished / 1000).toInt())
|
}
|
}
|
}
|
timer.start()
|
return timer
|
}
|
|
|
fun timeStart(click: (mill: Int) -> Unit): CountDownTimer {
|
val timer by lazy {
|
object : CountDownTimer(100000, 1000) {
|
override fun onFinish() {
|
click(0)
|
}
|
|
override fun onTick(millisUntilFinished: Long) {
|
var num = ((100000 - millisUntilFinished.toInt()) / 1000)
|
click(num)
|
}
|
}
|
}
|
timer.start()
|
return timer
|
}
|