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

165 lines
6.1 KiB
Kotlin
Raw Normal View History

package com.aiosman.riderpro
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-10-24 15:00:11 +08:00
import com.aiosman.riderpro.data.AccountProfile
2024-09-23 23:47:16 +08:00
import com.aiosman.riderpro.data.AccountService
import com.aiosman.riderpro.data.AccountServiceImpl
2024-10-24 15:00:11 +08:00
import com.aiosman.riderpro.entity.AccountProfileEntity
2024-09-20 21:40:57 +08:00
import com.aiosman.riderpro.ui.favourite.FavouriteListViewModel
import com.aiosman.riderpro.ui.favourite.FavouriteNoticeViewModel
import com.aiosman.riderpro.ui.follower.FollowerNoticeViewModel
import com.aiosman.riderpro.ui.follower.FollowingListViewModel
import com.aiosman.riderpro.ui.index.IndexViewModel
import com.aiosman.riderpro.ui.index.tabs.message.MessageListViewModel
import com.aiosman.riderpro.ui.index.tabs.moment.MomentViewModel
2024-10-25 22:01:58 +08:00
import com.aiosman.riderpro.ui.index.tabs.moment.tabs.timeline.TimelineMomentViewModel
2024-09-20 21:40:57 +08:00
import com.aiosman.riderpro.ui.index.tabs.profile.MyProfileViewModel
import com.aiosman.riderpro.ui.index.tabs.search.DiscoverViewModel
import com.aiosman.riderpro.ui.index.tabs.search.SearchViewModel
import com.aiosman.riderpro.ui.like.LikeNoticeViewModel
2024-09-23 23:47:16 +08:00
import com.aiosman.riderpro.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
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-10-24 15:00:11 +08:00
var profile :AccountProfileEntity? = null
2024-10-24 23:53:51 +08:00
var darkMode = false
2024-10-26 19:05:52 +08:00
var appTheme by mutableStateOf<AppThemeData>(LightThemeColors())
2024-09-23 23:47:16 +08:00
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
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-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-09-27 21:11:48 +08:00
} catch (e: Exception) {
2024-10-11 16:51:51 +08:00
e.printStackTrace()
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.avatar
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)
}
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()
2024-09-20 21:40:57 +08:00
// 重置我的页面
MyProfileViewModel.ResetModel()
// 重置发现页面
DiscoverViewModel.ResetModel()
// 重置搜索页面
SearchViewModel.ResetModel()
// 重置消息页面
MessageListViewModel.ResetModel()
// 重置点赞通知页面
LikeNoticeViewModel.ResetModel()
// 重置收藏页面
FavouriteListViewModel.ResetModel()
// 重置收藏通知页面
FavouriteNoticeViewModel.ResetModel()
// 重置粉丝通知页面
FollowerNoticeViewModel.ResetModel()
// 重置关注列表页面
FollowingListViewModel.ResetModel()
// 重置关注通知页面
IndexViewModel.ResetModel()
UserId = null
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
}
}