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

236 lines
8.6 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.account.AccountEditViewModel
2024-11-17 20:07:42 +08:00
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 kotlinx.coroutines.CoroutineScope
2024-09-27 21:11:48 +08:00
import kotlin.coroutines.suspendCoroutine
2025-09-08 12:10:38 +08:00
import com.aiosman.ravenow.im.OpenIMManager
import io.openim.android.sdk.OpenIMClient
import io.openim.android.sdk.models.InitConfig
2024-09-27 21:11:48 +08:00
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
2025-09-09 19:05:07 +08:00
try {
var profileResult = accountService.getMyAccountProfile()
profile = profileResult
} catch (e:Exception) {
Log.e("AppState", "getMyAccountProfile Error:"+ e.message )
}
2024-10-24 15:00:11 +08:00
// 获取当前用户资料
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
2025-09-08 12:10:38 +08:00
val initConfig = InitConfig(
"https://im.ravenow.ai/api",//SDK api地址
2025-09-09 17:53:52 +08:00
"wss://im.ravenow.ai/msg_gateway",//SDK WebSocket地址
2025-09-08 12:10:38 +08:00
OpenIMManager.getStorageDir(context),//SDK数据库存储目录
)
2025-09-09 17:53:52 +08:00
// initConfig.isLogStandardOutput = true;
// initConfig.logLevel = 6
2025-09-08 12:10:38 +08:00
// 使用 OpenIMManager 初始化 SDK
OpenIMManager.initSDK(context, initConfig)
2024-09-23 23:47:16 +08:00
try {
2025-09-08 16:02:46 +08:00
if (profile?.chatToken != null && profile?.trtcUserId != null) {
loginToOpenIM(profile!!.trtcUserId, profile!!.chatToken!!)
}
2025-09-08 12:10:38 +08:00
context.startService(Intent(context, OpenIMService::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
}
}
2025-09-08 12:10:38 +08:00
suspend fun loginToOpenIM(userId: String, imToken: String): Boolean {
2024-09-27 21:11:48 +08:00
return suspendCoroutine { continuation ->
2025-09-08 12:10:38 +08:00
OpenIMClient.getInstance().login(object : io.openim.android.sdk.listener.OnBase<String> {
override fun onError(code: Int, error: String?) {
Log.e("AppState", "OpenIM 登录失败: code=$code, error=$error")
continuation.resumeWith(Result.failure(Exception("OpenIM Login failed: $code, $error")))
2024-09-23 23:47:16 +08:00
}
2025-09-08 12:10:38 +08:00
override fun onSuccess(data: String?) {
Log.d("AppState", "OpenIM 登录成功: $data")
//其他api调用必须保证登录回调成功后操作
2024-09-27 21:11:48 +08:00
continuation.resumeWith(Result.success(true))
}
2025-09-08 12:10:38 +08:00
}, userId, imToken)
2024-09-23 23:47:16 +08:00
}
}
2025-09-08 12:10:38 +08:00
// suspend fun updateTrtcUserProfile() {
// val accountService: AccountService = AccountServiceImpl()
// val profile = accountService.getMyAccountProfile()
// val info = V2TIMUserFullInfo()
// info.setNickname(profile.nickName)
// info.faceUrl = profile.rawAvatar
// 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()
// 重置编辑资料页面 - 暂时注释掉看是否是这里导致的问题
// AccountEditViewModel.ResetModel()
2024-09-20 21:40:57 +08:00
// 重置发现页面
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
2025-09-08 12:10:38 +08:00
context.stopService(Intent(context, OpenIMService::class.java))
2024-09-20 21:40:57 +08:00
}
2025-09-08 12:10:38 +08:00
}