160 lines
6.1 KiB
Kotlin
160 lines
6.1 KiB
Kotlin
package com.gh.common.util
|
|
|
|
import android.annotation.SuppressLint
|
|
import android.view.Gravity
|
|
import android.view.View
|
|
import android.widget.LinearLayout
|
|
import android.widget.PopupWindow
|
|
import android.widget.TextView
|
|
import com.gh.common.constant.Constants
|
|
import com.gh.gamecenter.BuildConfig
|
|
import com.gh.gamecenter.R
|
|
import com.gh.gamecenter.energy.EnergyCenterActivity
|
|
import com.gh.gamecenter.entity.EnergyTaskCompleteEntity
|
|
import com.gh.gamecenter.manager.UserManager
|
|
import com.gh.gamecenter.retrofit.BiResponse
|
|
import com.gh.gamecenter.retrofit.RetrofitManager
|
|
import com.halo.assistant.HaloApp
|
|
import com.lightgame.utils.AppManager
|
|
import com.lightgame.utils.Utils
|
|
import io.reactivex.android.schedulers.AndroidSchedulers
|
|
import io.reactivex.schedulers.Schedulers
|
|
import okhttp3.MediaType
|
|
import okhttp3.RequestBody
|
|
import org.json.JSONObject
|
|
|
|
/**
|
|
* 上报光能任务辅助类
|
|
*/
|
|
object EnergyTaskHelper {
|
|
|
|
@JvmStatic
|
|
fun postEnergyTask(action: String) {
|
|
postEnergyTask(action, null, null, null)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun postEnergyTask(action: String, id: String) {
|
|
postEnergyTask(action, id, null, null)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun postEnergyTask(action: String, id: String, packageName: String) {
|
|
postEnergyTask(action, id, packageName, null)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun postEnergyTaskForWeb(action: String, url: String) {
|
|
postEnergyTask(action, null, null, url)
|
|
}
|
|
|
|
@JvmStatic
|
|
fun postEnergyTaskForShare(type: String, id: String, url: String) {
|
|
when (type) {
|
|
"游戏详情" -> postEnergyTask("share_game_detail", id)
|
|
|
|
"视频" -> postEnergyTask("share_video", id)
|
|
|
|
"资讯文章" -> postEnergyTask("share_article", id)
|
|
|
|
"问题详情" -> postEnergyTask("share_question", id)
|
|
|
|
"回答详情" -> postEnergyTask("share_answer", id)
|
|
|
|
"文章详情" -> postEnergyTask("share_community_article", id)
|
|
|
|
"工具箱" -> postEnergyTask("share_toolkit", id)
|
|
|
|
"web链接" -> postEnergyTaskForWeb("share_web", url)
|
|
}
|
|
}
|
|
|
|
@SuppressLint("CheckResult")
|
|
@JvmStatic
|
|
fun postEnergyTask(action: String, id: String? = null, packageName: String? = null, url: String? = null) {
|
|
if (BuildConfig.DEBUG || BuildConfig.BUILD_TIME != 0L) {
|
|
val taskParams = JSONObject()
|
|
taskParams.put("action", action)
|
|
|
|
val actionParams = JSONObject()
|
|
if (id != null) actionParams.put("_id", id)
|
|
if (packageName != null) actionParams.put("package", packageName)
|
|
if (url != null) actionParams.put("url", url)
|
|
taskParams.put("action_param", actionParams)
|
|
|
|
debugOnly { Utils.log("EnergyTaskHelper -> $taskParams") }
|
|
|
|
val body = RequestBody.create(MediaType.parse("application/json"), taskParams.toString())
|
|
RetrofitManager.getInstance(HaloApp.getInstance().application)
|
|
.api.postEnergyTask(UserManager.getInstance().userId, body)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(object : BiResponse<List<EnergyTaskCompleteEntity>>() {
|
|
override fun onSuccess(data: List<EnergyTaskCompleteEntity>) {
|
|
data.forEach { showCompletePopup(it) }
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
@SuppressLint("CheckResult")
|
|
@JvmStatic
|
|
fun postInviteCodeTask(code: String, from: String, successCallback: () -> Unit, failCallback: () -> Unit) {
|
|
if (BuildConfig.DEBUG || BuildConfig.BUILD_TIME != 0L) {
|
|
val taskParams = JSONObject()
|
|
taskParams.put("action", "enter_invite_code")
|
|
|
|
val actionParams = JSONObject()
|
|
actionParams.put("code", code)
|
|
actionParams.put("type", "new")
|
|
actionParams.put("from", from)
|
|
taskParams.put("action_param", actionParams)
|
|
|
|
debugOnly { Utils.log("EnergyTaskHelper -> $taskParams") }
|
|
|
|
val body = RequestBody.create(MediaType.parse("application/json"), taskParams.toString())
|
|
RetrofitManager.getInstance(HaloApp.getInstance().application)
|
|
.api.postEnergyTask(UserManager.getInstance().userId, body)
|
|
.subscribeOn(Schedulers.io())
|
|
.observeOn(AndroidSchedulers.mainThread())
|
|
.subscribe(object : BiResponse<List<EnergyTaskCompleteEntity>>() {
|
|
override fun onSuccess(data: List<EnergyTaskCompleteEntity>) {
|
|
SPUtils.setBoolean(Constants.SP_HAS_COMPLETE_INVITE_CODE, true)
|
|
successCallback.invoke()
|
|
data.forEach { showCompletePopup(it) }
|
|
}
|
|
|
|
override fun onFailure(exception: Exception) {
|
|
super.onFailure(exception)
|
|
failCallback.invoke()
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// 完成弹窗
|
|
@JvmStatic
|
|
fun showCompletePopup(entity: EnergyTaskCompleteEntity) {
|
|
val currentActivity = AppManager.getInstance().recentActiveActivity
|
|
currentActivity?.run {
|
|
val contentView = View.inflate(this, R.layout.popup_energy_task, null)
|
|
contentView.run {
|
|
findViewById<TextView>(R.id.taskDesc).text = "恭喜你!完成任务:${entity.name}"
|
|
findViewById<TextView>(R.id.taskEnergy).text = "+${entity.energy}光能"
|
|
isFocusable = true
|
|
isFocusableInTouchMode = true
|
|
setOnClickListener {
|
|
currentActivity.startActivity(EnergyCenterActivity.getIntent(currentActivity))
|
|
}
|
|
}
|
|
val popWindow = PopupWindow(contentView, LinearLayout.LayoutParams.MATCH_PARENT, 88F.dip2px())
|
|
popWindow.showAtLocation(currentActivity.window.decorView, Gravity.TOP, 0, 0)
|
|
|
|
countDownTimer(3) { finish, _ ->
|
|
if (finish && popWindow != null && popWindow.isShowing) {
|
|
popWindow.dismiss()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} |