2024-06-22 04:25:20 +08:00
|
|
|
|
package com.aiosman.riderpro
|
|
|
|
|
|
|
2024-09-06 20:08:51 +08:00
|
|
|
|
import android.Manifest
|
|
|
|
|
|
import android.app.NotificationChannel
|
|
|
|
|
|
import android.app.NotificationManager
|
|
|
|
|
|
import android.content.Context
|
|
|
|
|
|
import android.content.pm.PackageManager
|
|
|
|
|
|
import android.os.Build
|
2024-06-22 04:25:20 +08:00
|
|
|
|
import android.os.Bundle
|
2024-09-06 20:08:51 +08:00
|
|
|
|
import android.util.Log
|
2024-06-22 04:25:20 +08:00
|
|
|
|
import androidx.activity.ComponentActivity
|
|
|
|
|
|
import androidx.activity.compose.setContent
|
|
|
|
|
|
import androidx.activity.enableEdgeToEdge
|
2024-09-05 22:04:41 +08:00
|
|
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
2024-07-20 16:06:37 +08:00
|
|
|
|
import androidx.compose.animation.AnimatedContentScope
|
|
|
|
|
|
import androidx.compose.animation.ExperimentalSharedTransitionApi
|
|
|
|
|
|
import androidx.compose.animation.SharedTransitionScope
|
2024-07-12 23:37:21 +08:00
|
|
|
|
import androidx.compose.runtime.compositionLocalOf
|
2024-09-05 22:04:41 +08:00
|
|
|
|
import androidx.core.content.ContextCompat
|
2024-07-13 17:41:51 +08:00
|
|
|
|
import androidx.core.view.WindowCompat
|
2024-09-06 20:08:51 +08:00
|
|
|
|
import androidx.lifecycle.ProcessLifecycleOwner
|
|
|
|
|
|
import androidx.lifecycle.viewModelScope
|
2024-06-22 04:25:20 +08:00
|
|
|
|
import androidx.navigation.NavHostController
|
2024-07-31 14:50:55 +08:00
|
|
|
|
import com.aiosman.riderpro.data.AccountService
|
2024-08-23 18:31:14 +08:00
|
|
|
|
import com.aiosman.riderpro.data.AccountServiceImpl
|
2024-07-23 15:25:00 +08:00
|
|
|
|
import com.aiosman.riderpro.ui.Navigation
|
2024-07-30 16:57:25 +08:00
|
|
|
|
import com.aiosman.riderpro.ui.NavigationRoute
|
2024-09-06 20:08:51 +08:00
|
|
|
|
import com.aiosman.riderpro.ui.post.PostViewModel
|
2024-07-15 19:02:42 +08:00
|
|
|
|
import com.google.android.libraries.places.api.Places
|
2024-09-01 16:59:11 +08:00
|
|
|
|
import com.google.firebase.analytics.FirebaseAnalytics
|
|
|
|
|
|
import com.google.firebase.analytics.ktx.analytics
|
|
|
|
|
|
import com.google.firebase.ktx.Firebase
|
2024-07-30 16:57:25 +08:00
|
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
|
|
import kotlinx.coroutines.launch
|
2024-06-22 04:25:20 +08:00
|
|
|
|
|
|
|
|
|
|
class MainActivity : ComponentActivity() {
|
2024-09-05 22:04:41 +08:00
|
|
|
|
// Firebase Analytics
|
2024-09-01 16:59:11 +08:00
|
|
|
|
private lateinit var analytics: FirebaseAnalytics
|
2024-07-30 16:57:25 +08:00
|
|
|
|
private val scope = CoroutineScope(Dispatchers.Main)
|
2024-09-05 22:04:41 +08:00
|
|
|
|
// 请求通知权限
|
|
|
|
|
|
private val requestPermissionLauncher = registerForActivityResult(
|
|
|
|
|
|
ActivityResultContracts.RequestPermission(),
|
|
|
|
|
|
) { isGranted: Boolean ->
|
|
|
|
|
|
if (isGranted) {
|
|
|
|
|
|
// FCM SDK (and your app) can post notifications.
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// TODO: Inform user that that your app will not show notifications.
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 获取账号信息
|
|
|
|
|
|
*/
|
2024-09-06 17:43:43 +08:00
|
|
|
|
private suspend fun getAccount(): Boolean {
|
2024-08-23 18:31:14 +08:00
|
|
|
|
val accountService: AccountService = AccountServiceImpl()
|
2024-08-11 17:15:17 +08:00
|
|
|
|
try {
|
|
|
|
|
|
val resp = accountService.getMyAccount()
|
2024-09-06 17:43:43 +08:00
|
|
|
|
// 设置当前登录用户 ID
|
|
|
|
|
|
AppState.UserId = resp.id
|
2024-08-11 17:15:17 +08:00
|
|
|
|
return true
|
2024-08-13 22:32:27 +08:00
|
|
|
|
} catch (e: Exception) {
|
2024-08-11 17:15:17 +08:00
|
|
|
|
return false
|
|
|
|
|
|
}
|
2024-07-30 16:57:25 +08:00
|
|
|
|
}
|
2024-08-11 17:15:17 +08:00
|
|
|
|
|
2024-06-22 04:25:20 +08:00
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
|
|
super.onCreate(savedInstanceState)
|
2024-09-05 22:04:41 +08:00
|
|
|
|
// 监听应用生命周期
|
|
|
|
|
|
ProcessLifecycleOwner.get().lifecycle.addObserver(MainActivityLifecycleObserver())
|
|
|
|
|
|
// 创建通知渠道
|
|
|
|
|
|
createNotificationChannel()
|
|
|
|
|
|
// 沉浸式状态栏
|
2024-07-13 17:41:51 +08:00
|
|
|
|
WindowCompat.setDecorFitsSystemWindows(window, false)
|
2024-09-05 22:04:41 +08:00
|
|
|
|
// 初始化 Places SDK
|
2024-07-15 19:02:42 +08:00
|
|
|
|
if (!Places.isInitialized()) {
|
2024-07-15 20:21:03 +08:00
|
|
|
|
Places.initialize(applicationContext, "AIzaSyDpgLDH1-SECw_pdjJq_msynq1XrxwgKVI")
|
2024-07-15 19:02:42 +08:00
|
|
|
|
}
|
2024-09-05 22:04:41 +08:00
|
|
|
|
// 初始化 Firebase Analytics
|
2024-09-01 16:59:11 +08:00
|
|
|
|
analytics = Firebase.analytics
|
2024-09-05 22:04:41 +08:00
|
|
|
|
// 请求通知权限
|
|
|
|
|
|
askNotificationPermission()
|
|
|
|
|
|
// 加载一些本地化的配置
|
2024-07-30 16:57:25 +08:00
|
|
|
|
AppStore.init(this)
|
2024-09-01 16:59:11 +08:00
|
|
|
|
|
2024-06-22 04:25:20 +08:00
|
|
|
|
enableEdgeToEdge()
|
2024-07-30 16:57:25 +08:00
|
|
|
|
|
|
|
|
|
|
scope.launch {
|
2024-09-05 22:04:41 +08:00
|
|
|
|
// 检查是否有登录态
|
2024-08-11 17:15:17 +08:00
|
|
|
|
val isAccountValidate = getAccount()
|
2024-07-30 16:57:25 +08:00
|
|
|
|
var startDestination = NavigationRoute.Login.route
|
2024-09-05 22:04:41 +08:00
|
|
|
|
// 如果有登录态,且记住登录状态,且账号有效,则初始化 FCM,下一步进入首页
|
2024-08-11 17:15:17 +08:00
|
|
|
|
if (AppStore.token != null && AppStore.rememberMe && isAccountValidate) {
|
2024-09-05 22:04:41 +08:00
|
|
|
|
Messaging.InitFCM(scope)
|
2024-07-30 16:57:25 +08:00
|
|
|
|
startDestination = NavigationRoute.Index.route
|
|
|
|
|
|
}
|
|
|
|
|
|
setContent {
|
2024-09-05 22:04:41 +08:00
|
|
|
|
Navigation(startDestination) { navController ->
|
|
|
|
|
|
// 处理带有 postId 的通知点击
|
|
|
|
|
|
val postId = intent.getStringExtra("POST_ID")
|
|
|
|
|
|
if (postId != null) {
|
|
|
|
|
|
Log.d("MainActivity", "Navigation to Post$postId")
|
|
|
|
|
|
PostViewModel.postId = postId
|
|
|
|
|
|
PostViewModel.viewModelScope.launch {
|
|
|
|
|
|
PostViewModel.initData()
|
|
|
|
|
|
navController.navigate(NavigationRoute.Post.route.replace("{id}", postId))
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-07-30 16:57:25 +08:00
|
|
|
|
}
|
2024-09-05 22:04:41 +08:00
|
|
|
|
|
2024-06-22 04:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-09-05 22:04:41 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 请求通知权限
|
|
|
|
|
|
*/
|
|
|
|
|
|
private fun askNotificationPermission() {
|
|
|
|
|
|
// This is only necessary for API level >= 33 (TIRAMISU)
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
|
|
|
|
|
if (ContextCompat.checkSelfPermission(this, Manifest.permission.POST_NOTIFICATIONS) ==
|
|
|
|
|
|
PackageManager.PERMISSION_GRANTED
|
|
|
|
|
|
) {
|
|
|
|
|
|
// FCM SDK (and your app) can post notifications.
|
|
|
|
|
|
} else if (shouldShowRequestPermissionRationale(android.Manifest.permission.POST_NOTIFICATIONS)) {
|
|
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
// Directly ask for the permission
|
|
|
|
|
|
requestPermissionLauncher.launch(Manifest.permission.POST_NOTIFICATIONS)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 创建通知渠道
|
|
|
|
|
|
*/
|
|
|
|
|
|
private fun createNotificationChannel() {
|
|
|
|
|
|
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
|
|
|
|
|
|
val channelId = ConstVars.MOMENT_LIKE_CHANNEL_ID
|
|
|
|
|
|
val channelName = ConstVars.MOMENT_LIKE_CHANNEL_NAME
|
|
|
|
|
|
val channel =
|
|
|
|
|
|
NotificationChannel(channelId, channelName, NotificationManager.IMPORTANCE_DEFAULT)
|
|
|
|
|
|
val notificationManager =
|
|
|
|
|
|
getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
|
|
|
|
|
|
notificationManager.createNotificationChannel(channel)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2024-06-22 04:25:20 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-12 23:37:21 +08:00
|
|
|
|
val LocalNavController = compositionLocalOf<NavHostController> {
|
|
|
|
|
|
error("NavController not provided")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-20 16:06:37 +08:00
|
|
|
|
@OptIn(ExperimentalSharedTransitionApi::class)
|
|
|
|
|
|
val LocalSharedTransitionScope = compositionLocalOf<SharedTransitionScope> {
|
|
|
|
|
|
error("SharedTransitionScope not provided")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
val LocalAnimatedContentScope = compositionLocalOf<AnimatedContentScope> {
|
|
|
|
|
|
error("AnimatedContentScope not provided")
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-07-14 17:14:05 +08:00
|
|
|
|
|