VidiDin-Android/app/src/main/java/com/gamedog/vididin/login/AccountManager.kt

196 lines
6.2 KiB
Kotlin

package com.gamedog.vididin.core.login.login
import com.ama.core.architecture.BaseApp
import com.ama.core.architecture.util.AndroidUtil
import com.ama.core.architecture.util.DateUtil
import com.ama.core.architecture.util.DeviceUtil
import com.ama.core.architecture.util.ResUtil
import com.ama.core.architecture.util.SpUtil
import com.ama.core.architecture.util.eventbus.NotifyMan
import com.gamedog.vididin.VidiConst
import com.gamedog.vididin.VididinEvents
import com.gamedog.vididin.beans.Account
import com.gamedog.vididin.beans.BankInfo
import com.gamedog.vididin.beans.RECORD_CASH_PLUS_GOLD_CONVERT
import com.gamedog.vididin.beans.RECORD_GOLD_MINUS_CONVERT_2_CASH
import com.gamedog.vididin.beans.RecordCash
import com.gamedog.vididin.beans.RecordGold
import com.gamedog.vididin.manager.RecordsManager
import com.remax.notification.service.NotificationKeepAliveService
import com.viddin.videos.free.R
object AccountManager {
init {
NotifyMan.instance().register(object: NotifyMan.ICallback(true) {
override fun onEvent(data: NotifyMan.NotifyData<*>?) {
when (data?.mEventType) {
VididinEvents.EVENT_AD_WATCHED_FOR_ZEROBUY_EARN_DIAMOND -> {
adjustDiamond(VidiConst.DIAMOND_NUM_FOR_ONE_AD)
}
}
}
}, VididinEvents.EVENT_AD_WATCHED_FOR_ZEROBUY_EARN_DIAMOND)
}
private val mAccount: Account by lazy {
var account = SpUtil.instance().getObject<Account>(SpUtil.KEY_ACCOUNT)
if (account == null) {
val deviceUUId = DeviceUtil.generateDeviceId()
account = Account(accountId = "user_$deviceUUId", deviceUUId=deviceUUId, createdAt = DateUtil.getCurTimeMs())
account.let {
SpUtil.instance().putObject(SpUtil.KEY_ACCOUNT, account)
}
}
account
}
private fun saveAccountInfo() {
SpUtil.instance().putObject(SpUtil.KEY_ACCOUNT, mAccount)
}
fun updateUserName(userName: String) {
mAccount.userName = userName
saveAccountInfo()
}
//------------------------- 3个数值相关属性 增减 start --------------------------//
fun getAccount() : Account {
return mAccount
}
fun getGold(): Long {
return mAccount.goldCount
}
fun getGoldRVTimes(): Int {
return mAccount.rvTimesGold
}
fun adjustGold(adjustNum: Long, recordBean: RecordGold, isEarnByRv: Boolean = false): Boolean {
if (adjustNum < 0L && Math.abs(adjustNum) > getGold()) {
return false
}
mAccount.goldCount += adjustNum
if (isEarnByRv) {
mAccount.rvTimesGold++
}
saveAccountInfo()
NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Gold_Changed, null)
RecordsManager.instance().appendGoldRecord(recordBean)
updateResidentNotification()
return true
}
private fun updateResidentNotification() {
NotificationKeepAliveService.updateNotification(BaseApp.appContext())
}
fun getCash(): Double {
return mAccount.cashCount
}
fun getCashRVTimes(): Int {
return mAccount.rvTimesCash
}
@Synchronized
fun adjustCash(adjustNum: Double, recordBean: RecordCash? = null, isEarnByRv: Boolean = false): Boolean {
if (adjustNum < 0L && Math.abs(adjustNum) > getCash()) {
return false
}
mAccount.cashCount += adjustNum
if (isEarnByRv) {
mAccount.rvTimesCash++
}
saveAccountInfo()
NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Cash_Changed, null)
recordBean?.let {
RecordsManager.instance().appendCashRecord(recordBean)
}
updateResidentNotification()
return true
}
fun getDiamond(): Int {
return mAccount.diamondCount
}
@Synchronized
fun adjustDiamond(adjustNum: Int): Boolean {
if (adjustNum < 0L && Math.abs(adjustNum) > getDiamond()) {
return false
}
mAccount.diamondCount += adjustNum
saveAccountInfo()
NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Diamond_Changed, null)
return true
}
//------------------------- 3个数值相关属性 增减 End --------------------------//
fun getBankInfo(): BankInfo? {
return mAccount?.bankInfo
}
fun hasValidBankInfo(): Boolean {
val backInfo = getBankInfo()
return !backInfo?.bankName.isNullOrEmpty() && !backInfo.bankAccount.isNullOrEmpty()
}
fun saveBankAccount(bankAccount: String?) {
if (bankAccount.isNullOrEmpty()) {
mAccount.bankInfo = null
} else {
mAccount.bankInfo = BankInfo(bankAccount=bankAccount)
}
saveAccountInfo()
NotifyMan.instance().sendEvent(
VididinEvents.EVENT_BANK_INFO_CHANGED, NotifyMan.NotifyData(bankAccount))
}
@Synchronized
fun convertGold2Cash(): Boolean {
try {
val couldCovertCashTotal = getGold().div(VidiConst.PER_01CASH_COST_GOLD_NUM) ?: 0
if (couldCovertCashTotal > 0) {
val costGoldNum = couldCovertCashTotal * VidiConst.PER_01CASH_COST_GOLD_NUM
if (getGold() >= costGoldNum) {
adjustGold(-1L * costGoldNum.toInt(), RecordGold(RECORD_GOLD_MINUS_CONVERT_2_CASH, -1L * costGoldNum.toInt()))
val adjustCashNum = couldCovertCashTotal * 0.1
adjustCash(adjustCashNum, RecordCash(RECORD_CASH_PLUS_GOLD_CONVERT, adjustCashNum.toDouble()), true)
AndroidUtil.showCustomToast(String.format(ResUtil.getString(R.string.has_claim_box_cash_hint), adjustCashNum))
return true
}
} else {
AndroidUtil.showToast(ResUtil.getString(R.string.not_enough_gold))
}
} catch (e: Exception) {
e.printStackTrace()
}
return false
}
fun saveUserIdInfo(userId: Int) {
mAccount?.let {
it.userId = userId
saveAccountInfo()
}
}
fun isBankAccountExist(): Boolean {
return !mAccount?.bankInfo?.bankAccount.isNullOrEmpty()
}
}