动态主题切换

This commit is contained in:
2024-10-26 19:05:52 +08:00
parent 01fb092e83
commit 2012668361
45 changed files with 244 additions and 281 deletions

View File

@@ -1,18 +1,14 @@
package com.aiosman.riderpro
import android.Manifest
import android.app.AlertDialog
import android.app.DownloadManager
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.os.Environment
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
@@ -22,20 +18,12 @@ import androidx.annotation.RequiresApi
import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.ExperimentalSharedTransitionApi
import androidx.compose.animation.SharedTransitionScope
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.BasicAlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.core.view.WindowCompat
import androidx.lifecycle.ProcessLifecycleOwner
import androidx.lifecycle.lifecycleScope
import androidx.navigation.NavHostController
import cn.jiguang.api.utils.JCollectionAuth
import cn.jpush.android.api.JPushInterface
@@ -44,21 +32,15 @@ import com.aiosman.riderpro.data.AccountServiceImpl
import com.aiosman.riderpro.data.UserService
import com.aiosman.riderpro.data.UserServiceImpl
import com.aiosman.riderpro.model.ApkInstallReceiver
import com.aiosman.riderpro.model.UpdateInfo
import com.aiosman.riderpro.ui.Navigation
import com.aiosman.riderpro.ui.NavigationRoute
import com.aiosman.riderpro.ui.dialogs.CheckUpdateDialog
import com.aiosman.riderpro.ui.navigateToPost
import com.aiosman.riderpro.ui.post.NewPostViewModel
import com.google.accompanist.systemuicontroller.rememberSystemUiController
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.gson.Gson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import okhttp3.OkHttpClient
import okhttp3.Request
class MainActivity : ComponentActivity() {
@@ -79,77 +61,6 @@ class MainActivity : ComponentActivity() {
}
}
@RequiresApi(Build.VERSION_CODES.P)
fun getVersionCode(context: Context): Int {
val packageManager = context.packageManager
val packageInfo = packageManager.getPackageInfo(context.packageName,0)
return packageInfo.longVersionCode.toInt()
}
@RequiresApi(Build.VERSION_CODES.P)
private fun checkUpdate() {
lifecycleScope.launch(Dispatchers.IO) {
try {
val client = OkHttpClient()
val request = Request.Builder()
.url("${ConstVars.BASE_SERVER}/static/update/beta/version.json")
.build()
val response = client.newCall(request).execute()
if (response.isSuccessful) {
val responseBody = response.body?.string()
val updateInfo = Gson().fromJson(responseBody, UpdateInfo::class.java)
val versionCode = getVersionCode(context)
// 检查是否有新版本
Log.d("MainActivity", "Current version code: $versionCode")
Log.d("MainActivity", "Server version code: ${updateInfo.versionCode}")
if (updateInfo.versionCode > versionCode || true) {
withContext(Dispatchers.Main) {
showUpdateDialog(updateInfo)
}
}
}
} catch (e: Exception) {
// 处理网络请求失败
e.printStackTrace()
}
}
}
private fun showUpdateDialog(updateInfo: UpdateInfo) {
val builder = AlertDialog.Builder(this)
builder.setTitle("🔴 ${getString(R.string.update_find_new_version)} v${updateInfo.versionName}")
builder.setMessage(updateInfo.updateContent)
builder.setPositiveButton(R.string.update_update_now) { dialog, _ ->
downloadApk(updateInfo.downloadUrl,updateInfo.versionName)
dialog.dismiss()
}
if (!updateInfo.forceUpdate) {
builder.setNegativeButton(R.string.update_later) { dialog, _ ->
dialog.dismiss()
}
} else {
builder.setCancelable(false)
}
builder.show()
}
private fun downloadApk(downloadUrl: String,versionName: String) {
// 使用 DownloadManager 下载 APK 文件
val downloadManager = getSystemService(Context.DOWNLOAD_SERVICE) as DownloadManager
val request = DownloadManager.Request(Uri.parse(downloadUrl))
val apkFileName = "RiderPro_v${versionName.replace(".","_")}.apk"
request.setMimeType("application/vnd.android.package-archive") // 添加这行代码
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, apkFileName)
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED)
val downloadId = downloadManager.enqueue(request)
Log.d("MainActivity", "Download enqueued with ID: $downloadId")
}
/**
* 获取账号信息
*/
@@ -194,15 +105,6 @@ class MainActivity : ComponentActivity() {
}
enableEdgeToEdge()
// // 注册广播接收器
// val intentFilter = IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)
// registerReceiver(apkInstallReceiver, intentFilter, RECEIVER_NOT_EXPORTED)
//
// checkUpdate()
// 初始化腾讯云通信 SDK
scope.launch {
// 检查是否有登录态
val isAccountValidate = getAccount()
@@ -214,64 +116,69 @@ class MainActivity : ComponentActivity() {
}
setContent {
CheckUpdateDialog()
Navigation(startDestination) { navController ->
// 处理带有 postId 的通知点击
val postId = intent.getStringExtra("POST_ID")
var commentId = intent.getStringExtra("COMMENT_ID")
val action = intent.getStringExtra("ACTION")
if (action == "newFollow") {
navController.navigate(NavigationRoute.Followers.route)
return@Navigation
}
if (action == "followCount") {
navController.navigate(NavigationRoute.Followers.route)
return@Navigation
}
if (action == "TRTC_NEW_MESSAGE") {
val userService:UserService = UserServiceImpl()
val sender = intent.getStringExtra("SENDER")
sender?.let {
scope.launch {
try {
val profile = userService.getUserProfileByTrtcUserId(it)
navController.navigate(NavigationRoute.Chat.route.replace(
"{id}",
profile.id.toString()
))
}catch (e:Exception){
e.printStackTrace()
CompositionLocalProvider(
LocalAppTheme provides AppState.appTheme
) {
CheckUpdateDialog()
Navigation(startDestination) { navController ->
// 处理带有 postId 的通知点击
val postId = intent.getStringExtra("POST_ID")
var commentId = intent.getStringExtra("COMMENT_ID")
val action = intent.getStringExtra("ACTION")
if (action == "newFollow") {
navController.navigate(NavigationRoute.Followers.route)
return@Navigation
}
if (action == "followCount") {
navController.navigate(NavigationRoute.Followers.route)
return@Navigation
}
if (action == "TRTC_NEW_MESSAGE") {
val userService:UserService = UserServiceImpl()
val sender = intent.getStringExtra("SENDER")
sender?.let {
scope.launch {
try {
val profile = userService.getUserProfileByTrtcUserId(it)
navController.navigate(NavigationRoute.Chat.route.replace(
"{id}",
profile.id.toString()
))
}catch (e:Exception){
e.printStackTrace()
}
}
}
return@Navigation
}
return@Navigation
}
if (commentId == null) {
commentId = "0"
}
if (postId != null) {
Log.d("MainActivity", "Navigation to Post$postId")
navController.navigateToPost(
id = postId.toInt(),
highlightCommentId = commentId.toInt(),
initImagePagerIndex = 0
)
}
// 处理分享过来的图片
if (intent?.action == Intent.ACTION_SEND || intent?.action == Intent.ACTION_SEND_MULTIPLE) {
val imageUris: List<Uri>? = if (intent.action == Intent.ACTION_SEND) {
listOf(intent.getParcelableExtra(Intent.EXTRA_STREAM)!!)
} else {
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM)
if (commentId == null) {
commentId = "0"
}
NewPostViewModel.asNewPostWithImageUris(imageUris!!.map { it.toString() })
navController.navigate(NavigationRoute.NewPost.route)
if (postId != null) {
Log.d("MainActivity", "Navigation to Post$postId")
navController.navigateToPost(
id = postId.toInt(),
highlightCommentId = commentId.toInt(),
initImagePagerIndex = 0
)
}
// 处理分享过来的图片
if (intent?.action == Intent.ACTION_SEND || intent?.action == Intent.ACTION_SEND_MULTIPLE) {
val imageUris: List<Uri>? = if (intent.action == Intent.ACTION_SEND) {
listOf(intent.getParcelableExtra(Intent.EXTRA_STREAM)!!)
} else {
intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM)
}
NewPostViewModel.asNewPostWithImageUris(imageUris!!.map { it.toString() })
navController.navigate(NavigationRoute.NewPost.route)
}
}
}
}
}
@@ -332,4 +239,6 @@ val LocalAnimatedContentScope = compositionLocalOf<AnimatedContentScope> {
}
val LocalAppTheme = compositionLocalOf<AppThemeData> {
error("AppThemeData not provided")
}