[2.13.0-arch-opt] code opt

This commit is contained in:
zhongchao
2023-03-03 15:02:29 +08:00
parent d30cbb2bda
commit 835a547ad8
10 changed files with 141 additions and 178 deletions

View File

@@ -1,26 +1,35 @@
package com.mogo.eagle.core.function.msgbox
import android.annotation.SuppressLint
import android.content.Context
import android.os.Looper
import com.mogo.eagle.core.data.deva.report.ReportEntity
import com.mogo.eagle.core.data.enums.DataSourceType
import com.mogo.eagle.core.data.msgbox.*
import com.mogo.eagle.core.data.deva.report.ReportEntity
import com.mogo.eagle.core.function.call.msgbox.CallerMsgBoxListenerManager
import com.mogo.eagle.core.function.msgbox.db.MsgBoxDb
import com.mogo.eagle.core.function.msgbox.db.MsgBoxInfo
import com.mogo.eagle.core.utilcode.kotlin.lifeCycleScope
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger
import com.mogo.eagle.core.utilcode.mogo.logger.CallerLogger.e
import com.mogo.eagle.core.utilcode.util.GsonUtils
import com.mogo.eagle.core.utilcode.util.ProcessUtils
import com.mogo.eagle.core.utilcode.util.SPUtils
import com.mogo.eagle.core.utilcode.util.Utils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.io.File
import java.text.SimpleDateFormat
import java.util.*
object DataManager {
// private val msgBoxMap: EnumMap<MsgBoxType, MutableList<MsgBoxBean>> = EnumMap(MsgBoxType::class.java)
const val TAG = "DataManager"
// 消失时间5000ms
const val DISMISS_TIME = 5000L
@@ -148,6 +157,10 @@ object DataManager {
* 从本地数据库中查询数据
*/
fun queryAllMessages(context: Context) {
if (!ProcessUtils.isMainProcess(context)) {
return
}
clearMessageBoxTable(context)
scope.launch {
initCache()
try {
@@ -158,6 +171,38 @@ object DataManager {
}
}
@SuppressLint("SimpleDateFormat")
private fun clearMessageBoxTable(context: Context) {
Thread {
val lastLaunchTimeStr = SPUtils.getInstance().getString("last_launch", "")
val format = SimpleDateFormat("yyyy-MM-dd")
val currDate = Date(System.currentTimeMillis())
val currTimeStr = format.format(currDate)
try {
if (lastLaunchTimeStr != null && lastLaunchTimeStr.isNotEmpty()) {
val isSameDay = currTimeStr == lastLaunchTimeStr
// 超过一天需要清除消息盒子中的数据并把时间戳存入SP
if (!isSameDay) {
val file: File = context.getDatabasePath(MsgBoxDb.INTERNAL_DB_NAME)
if (file != null && file.exists()) {
context.deleteDatabase(MsgBoxDb.INTERNAL_DB_NAME)
}
SPUtils.getInstance().put("last_launch", currTimeStr)
}
} else {
// 首次使用App或中途仅删除sp文件
val file: File = context.getDatabasePath(MsgBoxDb.INTERNAL_DB_NAME)
if (file != null && file.exists()) {
context.deleteDatabase(MsgBoxDb.INTERNAL_DB_NAME)
}
SPUtils.getInstance().put("last_launch", currTimeStr)
}
} catch (e: Exception) {
e(TAG, e.message)
}
}.start()
}
private fun initCache() {
if (cacheNotifyList.isNotEmpty()) {
cacheNotifyList.clear()
@@ -170,86 +215,87 @@ object DataManager {
}
}
private suspend fun getCacheMessages(context: Context): List<MsgBoxBean> = withContext(Dispatchers.IO) {
delay(2000)
return@withContext MsgBoxDb.getDb(context)
.monitorDao()
.getAllCachedMessages()
.map { msgInfo ->
val json = msgInfo.bean2Json
when (msgInfo.obj2JsonType) {
MsgBoxType.V2X.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.V2X,
GsonUtils.fromJson(json, V2XMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheNotifyList.add(this@apply)
private suspend fun getCacheMessages(context: Context): List<MsgBoxBean> =
withContext(Dispatchers.IO) {
delay(2000)
return@withContext MsgBoxDb.getDb(context)
.monitorDao()
.getAllCachedMessages()
.map { msgInfo ->
val json = msgInfo.bean2Json
when (msgInfo.obj2JsonType) {
MsgBoxType.V2X.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.V2X,
GsonUtils.fromJson(json, V2XMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheNotifyList.add(this@apply)
}
}
}
}
MsgBoxType.OBU.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.OBU,
GsonUtils.fromJson(json, V2XMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheNotifyList.add(this@apply)
MsgBoxType.OBU.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.OBU,
GsonUtils.fromJson(json, V2XMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheNotifyList.add(this@apply)
}
}
}
}
MsgBoxType.OPERATION.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.OPERATION,
GsonUtils.fromJson(json, OperationMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheNotifyList.add(this@apply)
MsgBoxType.OPERATION.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.OPERATION,
GsonUtils.fromJson(json, OperationMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheNotifyList.add(this@apply)
}
}
}
}
MsgBoxType.REPORT.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.REPORT,
GsonUtils.fromJson(json, ReportEntity::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheSysInfoList.add(this@apply)
MsgBoxType.REPORT.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.REPORT,
GsonUtils.fromJson(json, ReportEntity::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheSysInfoList.add(this@apply)
}
}
}
}
MsgBoxType.RECORD.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.RECORD,
GsonUtils.fromJson(json, RecordBagMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheRecordList.add(this@apply)
MsgBoxType.RECORD.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.RECORD,
GsonUtils.fromJson(json, RecordBagMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheRecordList.add(this@apply)
}
}
}
}
MsgBoxType.NOTICE.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.NOTICE,
GsonUtils.fromJson(json, NoticeFrCloudMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheNotifyList.add(this@apply)
MsgBoxType.NOTICE.ordinal -> {
return@map MsgBoxBean(
MsgBoxType.NOTICE,
GsonUtils.fromJson(json, NoticeFrCloudMsg::class.java)
).apply {
this.timestamp = msgInfo.timeStamp
withContext(Dispatchers.Main) {
cacheNotifyList.add(this@apply)
}
}
}
}
else -> {
return@map MsgBoxBean(MsgBoxType.V2X, V2XMsg())
else -> {
return@map MsgBoxBean(MsgBoxType.V2X, V2XMsg())
}
}
}
}
}
}
/**
* 存储到本地数据库
@@ -298,7 +344,8 @@ object DataManager {
fun delMsgBoxBean(context: Context, msgBoxBean: MsgBoxBean) {
scope.launch {
withContext(Dispatchers.Default) {
val msgBoxInfo = MsgBoxInfo(msgBoxBean.bean2Json, msgBoxBean.type.ordinal, msgBoxBean.timestamp)
val msgBoxInfo =
MsgBoxInfo(msgBoxBean.bean2Json, msgBoxBean.type.ordinal, msgBoxBean.timestamp)
MsgBoxDb.getDb(context)
.monitorDao()
.deleteMsg(msgBoxInfo)