Files
assistant-android/app/src/main/java/com/gh/common/util/ReservationHelper.kt

180 lines
7.4 KiB
Kotlin

package com.gh.common.util
import android.annotation.SuppressLint
import android.content.Context
import com.gh.gamecenter.common.constant.Constants
import com.gh.common.repository.ReservationRepository
import com.gh.gamecenter.R
import com.gh.gamecenter.WebActivity
import com.gh.gamecenter.common.callback.CancelListener
import com.gh.gamecenter.common.callback.ConfirmListener
import com.gh.gamecenter.common.entity.WechatConfigEntity
import com.gh.gamecenter.core.utils.*
import com.gh.gamecenter.feature.entity.GameEntity
import com.gh.gamecenter.common.retrofit.BiResponse
import com.gh.gamecenter.common.utils.*
import com.gh.gamecenter.retrofit.RetrofitManager
import com.halo.assistant.HaloApp
import com.lightgame.utils.Utils
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import okhttp3.ResponseBody
object ReservationHelper {
@JvmStatic
fun deleteReservation(game: GameEntity, refreshCallback: EmptyCallback) {
deleteOrCancelReservation(game, true, refreshCallback)
}
@JvmStatic
fun cancelReservation(game: GameEntity, refreshCallback: EmptyCallback) {
deleteOrCancelReservation(game, false, refreshCallback)
}
@SuppressLint("CheckResult")
private fun deleteOrCancelReservation(
game: GameEntity,
deleteReservation: Boolean,
refreshCallback: EmptyCallback
) {
val retrofit = RetrofitManager.getInstance()
val requestMap = hashMapOf<String, String>()
requestMap["game_id"] = game.id
val single = if (deleteReservation) {
retrofit.api
.deleteGameReservation(requestMap.createRequestBody())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
} else {
retrofit.api
.cancelGameReservation(requestMap.createRequestBody())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
}
single.subscribe(object : BiResponse<ResponseBody>() {
override fun onSuccess(data: ResponseBody) {
ReservationRepository.removeReservationFromMemoryAndRefresh(game.id)
refreshCallback.onCallback()
}
override fun onFailure(exception: Exception) {
Utils.toast(HaloApp.getInstance().application, exception.message)
exception.printStackTrace()
}
})
}
@JvmStatic
@SuppressLint("CheckResult")
fun reserve(context: Context, game: GameEntity?, sourceEntrance: String = "其他", callback: EmptyCallback) {
val requestMap = hashMapOf<String, String>()
requestMap["game_id"] = game?.id ?: ""
RetrofitManager.getInstance().api
.createNewGameReservation(requestMap.createRequestBody())
.compose(singleToMain())
.subscribe(object : BiResponse<ResponseBody>() {
override fun onSuccess(data: ResponseBody) {
SensorsBridge.trackEvent(
"AppointmentGameResult",
"game_name",
game?.name ?: "",
"game_id",
game?.id ?: "",
"game_type",
game?.categoryChinese ?: "",
"result",
"成功",
"source_entrance",
sourceEntrance
)
ReservationRepository.addReservationToMemoryAndRefresh(game?.id ?: "")
callback.onCallback()
val wechatConfig = SPUtils.getString(Constants.SP_WECHAT_CONFIG).toObject<WechatConfigEntity>()
wechatConfig?.run {
NewLogUtils.logReserveGameSuccess(wechatConfig)
if (bind && follow && notice) {
NewLogUtils.logReserveWechatSuccessPopShow()
DialogUtils.showReserveOrVoteSuccessDialog(context, true)
} else {
NewLogUtils.logReserveWechatRemindPopShow(wechatConfig)
SensorsBridge.trackEvent("AppointmenWechatRemindDialogShow")
DialogUtils.showReserveOrVoteSuccess2WechatBindDialog(context, true, {
NewLogUtils.logReserveWechatRemindPopClick(wechatConfig, "开启微信提醒")
SensorsBridge.trackEvent("AppointmenWechatRemindDialogClick")
context.startActivity(WebActivity.getBindWechatIntent(context))
SensorsBridge.trackEvent(
"AppointmenWechatRemindConfigPageShow",
"source_entrance",
"设置微信提醒弹窗"
)
}, object : CancelListener {
override fun onCancel() {
NewLogUtils.logReserveWechatRemindPopClick(wechatConfig, "关闭弹窗")
SensorsBridge.trackEvent("AppointmenWechatRemindDialogClick")
}
})
}
}
}
override fun onFailure(exception: Exception) {
SensorsBridge.trackEvent(
"AppointmentGameResult",
"game_name",
game?.name ?: "",
"game_id",
game?.id ?: "",
"game_type",
game?.categoryChinese ?: "",
"result",
"失败",
"source_entrance",
sourceEntrance
)
ToastUtils.showToast(exception.message ?: "")
}
})
}
@JvmStatic
fun showDeleteReservationDialog(context: Context, emptyCallback: EmptyCallback) {
DialogUtils.showCancelOrDeleteReservationDialog(
context,
"删除预约",
"游戏已上线,你可以删除此预约记录,确定删除吗?",
"确定删除",
"暂不删除", object : ConfirmListener {
override fun onConfirm() {
emptyCallback.onCallback()
}
}, null
)
}
@JvmStatic
fun showCancelReservationDialog(context: Context, emptyCallback: EmptyCallback) {
showCancelReservationDialog(context, emptyCallback, null)
}
@JvmStatic
fun showCancelReservationDialog(context: Context, emptyCallback: EmptyCallback, cancelListener: CancelListener?) {
DialogHelper.showDialog(context, "取消预约",
"取消之后你将无法收到游戏上线的通知,确定取消预约吗?",
"确定取消",
"暂不取消", confirmClickCallback = {
emptyCallback.onCallback()
}, cancelClickCallback = {
cancelListener?.onCancel()
}, uiModificationCallback = {
it.confirmTv.setTextColor(R.color.secondary_red.toColor(context))
}, extraConfig = DialogHelper.Config(centerContent = true, centerTitle = true)
)
}
}