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

110 lines
3.3 KiB
Kotlin
Raw Normal View History

2025-11-25 10:23:37 +00:00
package com.gamedog.vididin.core.login.login
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-01 11:26:14 +00:00
import com.gamedog.vididin.main.fragments.task.TaskBean
import kotlinx.coroutines.sync.Mutex
import kotlinx.coroutines.sync.withLock
2025-11-25 10:23:37 +00:00
object AccountManager {
2025-12-01 11:26:14 +00:00
private val mutex = Mutex()
2025-11-25 10:23:37 +00:00
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)
}
2025-11-25 10:23:37 +00:00
fun getAccount() : Account? {
return mAccount
}
2025-11-27 06:43:33 +00:00
fun getGold(): Long {
return mAccount?.goldCount ?: 0L
}
fun getCash(): Float {
return mAccount?.cashCount ?: 0F
}
2025-12-04 03:27:27 +00:00
@Synchronized
2025-11-27 06:43:33 +00:00
fun addGold(newGold: Int) {
mAccount?.goldCount += newGold
saveAccountInfo()
2025-11-27 06:43:33 +00:00
NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Gold_Changed, null)
}
2025-12-04 03:27:27 +00:00
@Synchronized
2025-11-27 06:43:33 +00:00
fun addCash(newCash: Float) {
mAccount?.cashCount += newCash
saveAccountInfo()
2025-11-27 06:43:33 +00:00
NotifyMan.instance().sendEvent(VididinEvents.Event_Account_Cash_Changed, null)
}
2025-11-25 10:23:37 +00:00
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()) {
mAccount?.bankInfo = null
} else {
mAccount?.bankInfo = BankInfo(bankAccount=bankAccount)
}
2025-11-27 09:07:30 +00:00
saveAccountInfo()
NotifyMan.instance().sendEvent(
VididinEvents.Event_Account_Bank_Info_Changed, NotifyMan.NotifyData(bankAccount))
}
2025-12-01 11:26:14 +00:00
@Synchronized
fun convertGold2Cash(): Boolean {
try {
val couldCovertCashTotal = mAccount?.goldCount?.div(VidiConst.PER_CASH_COST_GOLD_NUM) ?: 0L
if (couldCovertCashTotal > 0) {
val costGoldNum = couldCovertCashTotal * VidiConst.PER_CASH_COST_GOLD_NUM
mAccount?.goldCount?.let {
if (it > costGoldNum) {
addGold(-1 * costGoldNum.toInt())
addCash(couldCovertCashTotal.toFloat())
AndroidUtil.showToast("Has convert $costGoldNum gold to $couldCovertCashTotal cash.")
return true
}
}
} else {
AndroidUtil.showToast("You don't have enough gold.")
}
} catch (e: Exception) {
e.printStackTrace()
}
return false
}
2025-11-25 10:23:37 +00:00
}