Files
rider-pro-android-app/app/src/main/java/com/aiosman/ravenow/AppState.kt

232 lines
8.2 KiB
Kotlin
Raw Normal View History

2024-11-17 20:07:42 +08:00
package com.aiosman.ravenow
2024-09-23 23:47:16 +08:00
import android.content.Context
2024-10-11 16:51:51 +08:00
import android.content.Intent
2024-09-23 23:47:16 +08:00
import android.icu.util.Calendar
import android.icu.util.TimeZone
import android.util.Log
2024-10-26 19:05:52 +08:00
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
2024-11-17 20:07:42 +08:00
import com.aiosman.ravenow.data.AccountService
import com.aiosman.ravenow.data.AccountServiceImpl
2024-12-06 10:57:31 +08:00
import com.aiosman.ravenow.data.DictService
import com.aiosman.ravenow.data.DictServiceImpl
2024-11-17 20:07:42 +08:00
import com.aiosman.ravenow.entity.AccountProfileEntity
import com.aiosman.ravenow.ui.favourite.FavouriteListViewModel
import com.aiosman.ravenow.ui.favourite.FavouriteNoticeViewModel
import com.aiosman.ravenow.ui.follower.FollowerNoticeViewModel
import com.aiosman.ravenow.ui.index.IndexViewModel
import com.aiosman.ravenow.ui.index.tabs.message.MessageListViewModel
2025-08-14 19:04:20 +08:00
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.dynamic.DynamicViewModel
2025-08-14 15:39:21 +08:00
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.expolre.Explore
2025-07-23 19:07:29 +08:00
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.hot.HotMomentViewModel
2024-11-17 20:07:42 +08:00
import com.aiosman.ravenow.ui.index.tabs.moment.tabs.timeline.TimelineMomentViewModel
import com.aiosman.ravenow.ui.index.tabs.profile.MyProfileViewModel
import com.aiosman.ravenow.ui.index.tabs.search.DiscoverViewModel
import com.aiosman.ravenow.ui.index.tabs.search.SearchViewModel
import com.aiosman.ravenow.ui.index.tabs.ai.AgentViewModel
import com.aiosman.ravenow.ui.index.tabs.ai.tabs.mine.MineAgentViewModel
2024-11-17 20:07:42 +08:00
import com.aiosman.ravenow.ui.like.LikeNoticeViewModel
import com.aiosman.ravenow.utils.Utils
2024-09-23 23:47:16 +08:00
import com.tencent.imsdk.v2.V2TIMCallback
import com.tencent.imsdk.v2.V2TIMLogListener
import com.tencent.imsdk.v2.V2TIMManager
import com.tencent.imsdk.v2.V2TIMSDKConfig
2024-09-27 21:11:48 +08:00
import com.tencent.imsdk.v2.V2TIMUserFullInfo
2024-09-23 23:47:16 +08:00
import kotlinx.coroutines.CoroutineScope
2024-09-27 21:11:48 +08:00
import kotlin.coroutines.suspendCoroutine
2024-09-20 21:40:57 +08:00
object AppState {
var UserId: Int? = null
2024-12-01 21:27:39 +08:00
var profile: AccountProfileEntity? = null
2024-12-07 17:14:45 +08:00
var darkMode by mutableStateOf(false)
2024-10-26 19:05:52 +08:00
var appTheme by mutableStateOf<AppThemeData>(LightThemeColors())
2024-12-01 21:27:39 +08:00
var googleClientId: String? = null
var enableGoogleLogin: Boolean = false
2024-12-06 10:57:31 +08:00
var enableChat = false
2024-09-23 23:47:16 +08:00
suspend fun initWithAccount(scope: CoroutineScope, context: Context) {
Enhance AI Agent Profile Interaction This commit introduces several enhancements to how AI agent profiles are displayed and interacted with: **Profile Display:** - **AI Account Distinction:** Profile pages now differentiate between regular user accounts and AI agent accounts. - AI agent profiles will not display the "Agents" tab in their profile. - The profile header height is adjusted for AI accounts. - **Navigation Parameter:** An `isAiAccount` boolean parameter is added to the `AccountProfile` navigation route to indicate if the profile being viewed belongs to an AI. **Interaction & Navigation:** - **Avatar Click Navigation:** - Clicking an AI agent's avatar in various lists (Hot Agents, My Agents, User Agents Row, User Agents List) now navigates to the agent's dedicated profile page. - When navigating to an agent's profile from an agent list, `isAiAccount` is set to `true`. - **Chat Initiation:** Clicking the chat button on AI agent cards in the "Agent" tab (both Hot and My Agents) now correctly initiates a chat with the respective AI. - **ViewModel Updates:** - `AgentViewModel`, `MineAgentViewModel`, and `HotAgentViewModel` now include a `goToProfile` function to handle navigation to agent profiles, correctly passing the `isAiAccount` flag. **Code Refinements:** - Click handlers for agent avatars and chat buttons are now wrapped with `DebounceUtils.simpleDebounceClick` to prevent multiple rapid clicks. - The `UserContentPageIndicator` now conditionally hides the "Agent" tab based on the `isAiAccount` status. - `UserAgentsRow` and `UserAgentsList` now accept an `onAvatarClick` callback for navigating to agent profiles. - `AgentItem` (used in `UserAgentsRow`) and `UserAgentCard` (used in `UserAgentsList`) now handle avatar clicks. - The general `Agent` composable (used in `AiPostComposable`) now also supports an `onAvatarClick` callback.
2025-09-01 14:17:44 +08:00
// 如果是游客模式,使用简化的初始化流程
if (AppStore.isGuest) {
initWithGuestAccount()
return
}
2024-09-23 23:47:16 +08:00
val accountService: AccountService = AccountServiceImpl()
// 获取用户认证信息
val resp = accountService.getMyAccount()
// 更新必要的用户信息
val calendar: Calendar = Calendar.getInstance()
val tz: TimeZone = calendar.timeZone
val offsetInMillis: Int = tz.rawOffset
accountService.updateUserExtra(
Utils.getCurrentLanguage(),
// 时区偏移量单位是秒
offsetInMillis / 1000,
tz.displayName
)
// 设置当前登录用户 ID
UserId = resp.id
2024-10-24 15:00:11 +08:00
var profileResult = accountService.getMyAccountProfile()
profile = profileResult
// 获取当前用户资料
2024-09-23 23:47:16 +08:00
// 注册 JPush
2024-10-12 10:07:18 +08:00
Messaging.registerDevice(scope, context)
2024-12-06 10:57:31 +08:00
initChat(context)
}
Enhance AI Agent Profile Interaction This commit introduces several enhancements to how AI agent profiles are displayed and interacted with: **Profile Display:** - **AI Account Distinction:** Profile pages now differentiate between regular user accounts and AI agent accounts. - AI agent profiles will not display the "Agents" tab in their profile. - The profile header height is adjusted for AI accounts. - **Navigation Parameter:** An `isAiAccount` boolean parameter is added to the `AccountProfile` navigation route to indicate if the profile being viewed belongs to an AI. **Interaction & Navigation:** - **Avatar Click Navigation:** - Clicking an AI agent's avatar in various lists (Hot Agents, My Agents, User Agents Row, User Agents List) now navigates to the agent's dedicated profile page. - When navigating to an agent's profile from an agent list, `isAiAccount` is set to `true`. - **Chat Initiation:** Clicking the chat button on AI agent cards in the "Agent" tab (both Hot and My Agents) now correctly initiates a chat with the respective AI. - **ViewModel Updates:** - `AgentViewModel`, `MineAgentViewModel`, and `HotAgentViewModel` now include a `goToProfile` function to handle navigation to agent profiles, correctly passing the `isAiAccount` flag. **Code Refinements:** - Click handlers for agent avatars and chat buttons are now wrapped with `DebounceUtils.simpleDebounceClick` to prevent multiple rapid clicks. - The `UserContentPageIndicator` now conditionally hides the "Agent" tab based on the `isAiAccount` status. - `UserAgentsRow` and `UserAgentsList` now accept an `onAvatarClick` callback for navigating to agent profiles. - `AgentItem` (used in `UserAgentsRow`) and `UserAgentCard` (used in `UserAgentsList`) now handle avatar clicks. - The general `Agent` composable (used in `AiPostComposable`) now also supports an `onAvatarClick` callback.
2025-09-01 14:17:44 +08:00
/**
* 游客模式的简化初始化
*/
private fun initWithGuestAccount() {
// 游客模式下不初始化推送和TRTC
// 设置默认的用户信息
UserId = 0
profile = null
enableChat = false
Log.d("AppState", "Guest mode initialized without push notifications and TRTC")
}
2024-12-06 10:57:31 +08:00
private suspend fun initChat(context: Context){
val dictService :DictService = DictServiceImpl()
val enableItem = dictService.getDictByKey(ConstVars.DICT_KEY_ENABLE_TRTC)
val isEnableTrtc = enableItem.value as? Boolean
if (isEnableTrtc != true) {
enableChat = false
return
}
val accountService: AccountService = AccountServiceImpl()
2024-09-23 23:47:16 +08:00
// 注册 Trtc
val config = V2TIMSDKConfig()
config.logLevel = V2TIMSDKConfig.V2TIM_LOG_INFO
config.logListener = object : V2TIMLogListener() {
override fun onLog(logLevel: Int, logContent: String) {
Log.d("V2TIMLogListener", logContent)
}
}
val appConfig = accountService.getAppConfig()
V2TIMManager.getInstance().initSDK(context, appConfig.trtcAppId, config)
try {
val sign = accountService.getMyTrtcSign()
2024-09-27 21:11:48 +08:00
loginToTrtc(sign.userId, sign.sig)
updateTrtcUserProfile()
2024-10-11 16:51:51 +08:00
// 登录成功后启动TrtcService
context.startService(
Intent(context, TrtcService::class.java)
)
2024-12-06 10:57:31 +08:00
enableChat = true
2024-09-27 21:11:48 +08:00
} catch (e: Exception) {
2024-10-11 16:51:51 +08:00
e.printStackTrace()
2024-12-06 10:57:31 +08:00
enableChat = false
2024-09-27 21:11:48 +08:00
}
}
suspend fun loginToTrtc(userId: String, userSig: String): Boolean {
return suspendCoroutine { continuation ->
V2TIMManager.getInstance().login(userId, userSig, object : V2TIMCallback {
override fun onError(code: Int, desc: String?) {
continuation.resumeWith(Result.failure(Exception("Login failed: $code, $desc")))
2024-09-23 23:47:16 +08:00
}
2024-09-27 21:11:48 +08:00
override fun onSuccess() {
continuation.resumeWith(Result.success(true))
}
})
2024-09-23 23:47:16 +08:00
}
}
2024-09-27 21:11:48 +08:00
suspend fun updateTrtcUserProfile() {
val accountService: AccountService = AccountServiceImpl()
val profile = accountService.getMyAccountProfile()
val info = V2TIMUserFullInfo()
info.setNickname(profile.nickName)
info.faceUrl = profile.rawAvatar
2024-09-27 21:11:48 +08:00
info.selfSignature = profile.bio
return suspendCoroutine { continuation ->
V2TIMManager.getInstance().setSelfInfo(info, object : V2TIMCallback {
override fun onError(code: Int, desc: String?) {
continuation.resumeWith(Result.failure(Exception("Update user profile failed: $code, $desc")))
}
override fun onSuccess() {
continuation.resumeWith(Result.success(Unit))
}
})
}
}
2024-09-23 23:47:16 +08:00
2024-10-26 19:05:52 +08:00
fun switchTheme() {
darkMode = !darkMode
appTheme = if (darkMode) {
DarkThemeColors()
} else {
LightThemeColors()
}
AppStore.saveDarkMode(darkMode)
}
Enhance AI Agent Profile Interaction This commit introduces several enhancements to how AI agent profiles are displayed and interacted with: **Profile Display:** - **AI Account Distinction:** Profile pages now differentiate between regular user accounts and AI agent accounts. - AI agent profiles will not display the "Agents" tab in their profile. - The profile header height is adjusted for AI accounts. - **Navigation Parameter:** An `isAiAccount` boolean parameter is added to the `AccountProfile` navigation route to indicate if the profile being viewed belongs to an AI. **Interaction & Navigation:** - **Avatar Click Navigation:** - Clicking an AI agent's avatar in various lists (Hot Agents, My Agents, User Agents Row, User Agents List) now navigates to the agent's dedicated profile page. - When navigating to an agent's profile from an agent list, `isAiAccount` is set to `true`. - **Chat Initiation:** Clicking the chat button on AI agent cards in the "Agent" tab (both Hot and My Agents) now correctly initiates a chat with the respective AI. - **ViewModel Updates:** - `AgentViewModel`, `MineAgentViewModel`, and `HotAgentViewModel` now include a `goToProfile` function to handle navigation to agent profiles, correctly passing the `isAiAccount` flag. **Code Refinements:** - Click handlers for agent avatars and chat buttons are now wrapped with `DebounceUtils.simpleDebounceClick` to prevent multiple rapid clicks. - The `UserContentPageIndicator` now conditionally hides the "Agent" tab based on the `isAiAccount` status. - `UserAgentsRow` and `UserAgentsList` now accept an `onAvatarClick` callback for navigating to agent profiles. - `AgentItem` (used in `UserAgentsRow`) and `UserAgentCard` (used in `UserAgentsList`) now handle avatar clicks. - The general `Agent` composable (used in `AiPostComposable`) now also supports an `onAvatarClick` callback.
2025-09-01 14:17:44 +08:00
/**
* 检查是否是游客模式并且是否需要登录
* @return true 如果是游客模式
*/
fun isGuestMode(): Boolean {
return AppStore.isGuest
}
/**
* 检查游客模式并提示登录
* @param onGuestMode 当是游客模式时的回调
* @return true 如果是游客模式
*/
fun checkGuestModeAndPromptLogin(onGuestMode: (() -> Unit)? = null): Boolean {
if (AppStore.isGuest) {
onGuestMode?.invoke()
return true
}
return false
}
2024-10-11 16:51:51 +08:00
fun ReloadAppState(context: Context) {
2024-09-20 21:40:57 +08:00
// 重置动态列表页面
2024-10-25 22:01:58 +08:00
TimelineMomentViewModel.ResetModel()
2025-08-14 19:04:20 +08:00
DynamicViewModel.ResetModel()
HotMomentViewModel.resetModel()
2025-07-23 19:07:29 +08:00
2024-09-20 21:40:57 +08:00
// 重置我的页面
MyProfileViewModel.ResetModel()
// 重置发现页面
DiscoverViewModel.ResetModel()
// 重置搜索页面
SearchViewModel.ResetModel()
// 重置消息页面
MessageListViewModel.ResetModel()
// 重置点赞通知页面
LikeNoticeViewModel.ResetModel()
// 重置收藏页面
FavouriteListViewModel.ResetModel()
// 重置收藏通知页面
FavouriteNoticeViewModel.ResetModel()
// 重置粉丝通知页面
FollowerNoticeViewModel.ResetModel()
// 重置关注通知页面
IndexViewModel.ResetModel()
// 重置AI Agent相关页面
AgentViewModel.ResetModel()
MineAgentViewModel.ResetModel()
2024-09-20 21:40:57 +08:00
UserId = null
2024-10-11 16:51:51 +08:00
Enhance AI Agent Profile Interaction This commit introduces several enhancements to how AI agent profiles are displayed and interacted with: **Profile Display:** - **AI Account Distinction:** Profile pages now differentiate between regular user accounts and AI agent accounts. - AI agent profiles will not display the "Agents" tab in their profile. - The profile header height is adjusted for AI accounts. - **Navigation Parameter:** An `isAiAccount` boolean parameter is added to the `AccountProfile` navigation route to indicate if the profile being viewed belongs to an AI. **Interaction & Navigation:** - **Avatar Click Navigation:** - Clicking an AI agent's avatar in various lists (Hot Agents, My Agents, User Agents Row, User Agents List) now navigates to the agent's dedicated profile page. - When navigating to an agent's profile from an agent list, `isAiAccount` is set to `true`. - **Chat Initiation:** Clicking the chat button on AI agent cards in the "Agent" tab (both Hot and My Agents) now correctly initiates a chat with the respective AI. - **ViewModel Updates:** - `AgentViewModel`, `MineAgentViewModel`, and `HotAgentViewModel` now include a `goToProfile` function to handle navigation to agent profiles, correctly passing the `isAiAccount` flag. **Code Refinements:** - Click handlers for agent avatars and chat buttons are now wrapped with `DebounceUtils.simpleDebounceClick` to prevent multiple rapid clicks. - The `UserContentPageIndicator` now conditionally hides the "Agent" tab based on the `isAiAccount` status. - `UserAgentsRow` and `UserAgentsList` now accept an `onAvatarClick` callback for navigating to agent profiles. - `AgentItem` (used in `UserAgentsRow`) and `UserAgentCard` (used in `UserAgentsList`) now handle avatar clicks. - The general `Agent` composable (used in `AiPostComposable`) now also supports an `onAvatarClick` callback.
2025-09-01 14:17:44 +08:00
// 清除游客状态
AppStore.isGuest = false
2024-10-11 16:51:51 +08:00
// 关闭 TrtcService
val trtcService = Intent(
context,
TrtcService::class.java
)
context.stopService(trtcService)
2024-09-20 21:40:57 +08:00
}
}