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

176 lines
5.7 KiB
Kotlin
Raw Normal View History

2025-11-25 10:23:37 +00:00
package com.gamedog.vididin.core.login.login
import com.ama.core.architecture.BaseApp
2025-12-01 11:26:14 +00:00
import com.ama.core.architecture.util.AndroidUtil
2025-11-25 10:23:37 +00:00
import com.ama.core.architecture.util.DateUtil
import com.ama.core.architecture.util.DeviceUtil
import com.ama.core.architecture.util.SpUtil
2025-11-27 06:43:33 +00:00
import com.ama.core.architecture.util.eventbus.NotifyMan
2025-12-01 11:26:14 +00:00
import com.gamedog.vididin.VidiConst
2025-11-27 06:43:33 +00:00
import com.gamedog.vididin.VididinEvents
2025-11-25 10:23:37 +00:00
import com.gamedog.vididin.beans.Account
import com.gamedog.vididin.beans.BankInfo
2025-12-24 08:31:38 +00:00
import com.gamedog.vididin.beans.RECORD_CASH_PLUS_GOLD_CONVERT
2025-12-24 09:12:21 +00:00
import com.gamedog.vididin.beans.RECORD_GOLD_MINUS_CONVERT_2_CASH
2025-12-24 08:31:38 +00:00
import com.gamedog.vididin.beans.RecordCash
2025-12-24 09:12:21 +00:00
import com.gamedog.vididin.beans.RecordGold
2025-12-24 08:31:38 +00:00
import com.gamedog.vididin.manager.RecordsManager
import com.remax.notification.controller.NotificationTriggerController
import com.remax.notification.service.NotificationKeepAliveService
2025-11-25 10:23:37 +00:00
object AccountManager {
2025-12-10 03:22:07 +00:00
init {
NotifyMan.instance().register(object: NotifyMan.ICallback(true) {
override fun onEvent(data: NotifyMan.NotifyData<*>?) {
when (data?.mEventType) {
2025-12-10 09:35:49 +00:00
VididinEvents.EVENT_AD_WATCHED_FOR_ZEROBUY_EARN_DIAMOND -> {
adjustDiamond(VidiConst.DIAMOND_NUM_FOR_ONE_AD)
2025-12-10 03:22:07 +00:00
}
}
}
2025-12-10 09:35:49 +00:00
}, VididinEvents.EVENT_AD_WATCHED_FOR_ZEROBUY_EARN_DIAMOND)
2025-12-10 03:22:07 +00:00
}
private val mAccount: Account by lazy {
2025-11-25 10:23:37 +00:00
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)
}
//------------------------- 3个数值相关属性 增减 start --------------------------//
fun getAccount() : Account {
2025-11-25 10:23:37 +00:00
return mAccount
}
2025-11-27 06:43:33 +00:00
fun getGold(): Long {
return mAccount.goldCount
}
2025-12-24 09:12:21 +00:00
fun adjustGold(adjustNum: Long, recordBean: RecordGold): Boolean {
if (adjustNum < 0L && Math.abs(adjustNum) > getGold()) {
return false
}
mAccount.goldCount += adjustNum
saveAccountInfo()
NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Gold_Changed, null)
2025-12-24 09:12:21 +00:00
RecordsManager.instance().appendGoldRecord(recordBean)
updateResidentNotification()
return true
2025-11-27 06:43:33 +00:00
}
private fun updateResidentNotification() {
NotificationKeepAliveService.updateNotification(BaseApp.appContext())
}
fun getCash(): Double {
return mAccount.cashCount
2025-11-27 06:43:33 +00:00
}
2025-12-04 03:27:27 +00:00
@Synchronized
fun adjustCash(adjustNum: Double, recordBean: RecordCash? = null): Boolean {
2025-12-24 10:34:10 +00:00
if (adjustNum < 0L && Math.abs(adjustNum) > getCash()) {
return false
}
mAccount.cashCount += adjustNum
saveAccountInfo()
NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Cash_Changed, null)
2025-12-24 08:31:38 +00:00
recordBean?.let {
RecordsManager.instance().appendCashRecord(recordBean)
}
updateResidentNotification()
return true
2025-11-27 06:43:33 +00:00
}
fun getDiamond(): Int {
return mAccount.diamondCount
2025-12-10 03:22:07 +00:00
}
2025-12-04 03:27:27 +00:00
@Synchronized
fun adjustDiamond(adjustNum: Int): Boolean {
2025-12-24 10:34:10 +00:00
if (adjustNum < 0L && Math.abs(adjustNum) > getDiamond()) {
return false
2025-12-15 09:30:16 +00:00
}
mAccount.diamondCount += adjustNum
saveAccountInfo()
NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Diamond_Changed, null)
return true
2025-11-27 06:43:33 +00:00
}
2025-11-25 10:23:37 +00:00
//------------------------- 3个数值相关属性 增减 End --------------------------//
fun getBankInfo(): BankInfo? {
return mAccount?.bankInfo
}
fun hasValidBankInfo(): Boolean {
val backInfo = getBankInfo()
return !backInfo?.bankName.isNullOrEmpty() && !backInfo.bankAccount.isNullOrEmpty()
}
2025-11-25 10:23:37 +00:00
2025-11-27 09:11:56 +00:00
fun saveBankAccount(bankAccount: String?) {
if (bankAccount.isNullOrEmpty()) {
2025-12-26 09:49:39 +00:00
mAccount.bankInfo = null
2025-11-27 09:11:56 +00:00
} else {
2025-12-26 09:49:39 +00:00
mAccount.bankInfo = BankInfo(bankAccount=bankAccount)
2025-11-27 09:11:56 +00:00
}
2025-11-27 09:07:30 +00:00
saveAccountInfo()
NotifyMan.instance().sendEvent(
2025-12-26 09:49:39 +00:00
VididinEvents.EVENT_BANK_INFO_CHANGED, NotifyMan.NotifyData(bankAccount))
2025-11-27 09:07:30 +00:00
}
2025-12-01 11:26:14 +00:00
@Synchronized
fun convertGold2Cash(): Boolean {
try {
val couldCovertCashTotal = getGold().div(VidiConst.PER_01CASH_COST_GOLD_NUM) ?: 0
2025-12-01 11:26:14 +00:00
if (couldCovertCashTotal > 0) {
2025-12-16 10:08:11 +00:00
val costGoldNum = couldCovertCashTotal * VidiConst.PER_01CASH_COST_GOLD_NUM
if (getGold() >= costGoldNum) {
2025-12-24 09:12:21 +00:00
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()))
AndroidUtil.showToast("Has convert $costGoldNum gold to $couldCovertCashTotal cash.")
return true
2025-12-01 11:26:14 +00:00
}
} else {
AndroidUtil.showToast("You don't have enough gold.")
}
} catch (e: Exception) {
e.printStackTrace()
}
return false
}
2025-12-08 07:12:53 +00:00
fun saveUserIdInfo(userId: Int) {
mAccount?.let {
it.userId = userId
saveAccountInfo()
}
}
2025-12-15 05:44:00 +00:00
fun isBankAccountExist(): Boolean {
return !mAccount?.bankInfo?.bankAccount.isNullOrEmpty()
}
2025-12-09 03:53:33 +00:00
2025-11-25 10:23:37 +00:00
}