package com.aiosman.ravenow import android.content.Context import android.content.Intent import android.icu.util.Calendar import android.icu.util.TimeZone import android.util.Log import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import com.aiosman.ravenow.data.AccountService import com.aiosman.ravenow.data.AccountServiceImpl import com.aiosman.ravenow.data.DictService import com.aiosman.ravenow.data.DictServiceImpl 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.follower.FollowingListViewModel import com.aiosman.ravenow.ui.index.IndexViewModel import com.aiosman.ravenow.ui.index.tabs.message.MessageListViewModel import com.aiosman.ravenow.ui.index.tabs.moment.tabs.expolre.MomentExploreViewModel 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.like.LikeNoticeViewModel import com.aiosman.ravenow.utils.Utils import com.tencent.imsdk.v2.V2TIMCallback import com.tencent.imsdk.v2.V2TIMLogListener import com.tencent.imsdk.v2.V2TIMManager import com.tencent.imsdk.v2.V2TIMSDKConfig import com.tencent.imsdk.v2.V2TIMUserFullInfo import kotlinx.coroutines.CoroutineScope import kotlin.coroutines.suspendCoroutine object AppState { var UserId: Int? = null var profile: AccountProfileEntity? = null var darkMode by mutableStateOf(false) var appTheme by mutableStateOf(LightThemeColors()) var googleClientId: String? = null var enableGoogleLogin: Boolean = false var enableChat = false suspend fun initWithAccount(scope: CoroutineScope, context: Context) { 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 var profileResult = accountService.getMyAccountProfile() profile = profileResult // 获取当前用户资料 // 注册 JPush Messaging.registerDevice(scope, context) initChat(context) } 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() // 注册 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() loginToTrtc(sign.userId, sign.sig) updateTrtcUserProfile() // 登录成功后,启动TrtcService context.startService( Intent(context, TrtcService::class.java) ) enableChat = true } catch (e: Exception) { e.printStackTrace() enableChat = false } } 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"))) } override fun onSuccess() { continuation.resumeWith(Result.success(true)) } }) } } 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)) } }) } } fun switchTheme() { darkMode = !darkMode appTheme = if (darkMode) { DarkThemeColors() } else { LightThemeColors() } AppStore.saveDarkMode(darkMode) } fun ReloadAppState(context: Context) { // 重置动态列表页面 TimelineMomentViewModel.ResetModel() MomentExploreViewModel.ResetModel() // 重置我的页面 MyProfileViewModel.ResetModel() // 重置发现页面 DiscoverViewModel.ResetModel() // 重置搜索页面 SearchViewModel.ResetModel() // 重置消息页面 MessageListViewModel.ResetModel() // 重置点赞通知页面 LikeNoticeViewModel.ResetModel() // 重置收藏页面 FavouriteListViewModel.ResetModel() // 重置收藏通知页面 FavouriteNoticeViewModel.ResetModel() // 重置粉丝通知页面 FollowerNoticeViewModel.ResetModel() // 重置关注通知页面 IndexViewModel.ResetModel() UserId = null // 关闭 TrtcService val trtcService = Intent( context, TrtcService::class.java ) context.stopService(trtcService) } }