You've already forked RekomenciMobile
Initial with template
This commit is contained in:
@@ -0,0 +1,133 @@
|
||||
package com.prodhack.moscow2025.presentation
|
||||
|
||||
import android.Manifest
|
||||
import android.content.pm.PackageManager
|
||||
import android.os.Build
|
||||
import android.os.Bundle
|
||||
import android.util.Log
|
||||
import androidx.activity.ComponentActivity
|
||||
import androidx.activity.compose.setContent
|
||||
import androidx.activity.enableEdgeToEdge
|
||||
import androidx.compose.foundation.layout.Box
|
||||
import androidx.compose.foundation.layout.fillMaxSize
|
||||
import androidx.compose.foundation.text.selection.SelectionContainer
|
||||
import androidx.compose.material3.Text
|
||||
import androidx.compose.runtime.LaunchedEffect
|
||||
import androidx.compose.runtime.collectAsState
|
||||
import androidx.compose.runtime.getValue
|
||||
import androidx.compose.runtime.mutableStateOf
|
||||
import androidx.compose.runtime.remember
|
||||
import androidx.compose.ui.Alignment
|
||||
import androidx.compose.ui.Modifier
|
||||
import androidx.core.content.ContextCompat
|
||||
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
|
||||
import androidx.core.view.WindowCompat
|
||||
import com.google.firebase.messaging.FirebaseMessaging
|
||||
import com.prodhack.moscow2025.domain.usecase.auth.CheckSessionUseCase
|
||||
import com.prodhack.moscow2025.presentation.navigation.AppDestination
|
||||
import com.prodhack.moscow2025.presentation.navigation.TTasksApp
|
||||
import kotlinx.coroutines.flow.MutableStateFlow
|
||||
import kotlinx.coroutines.runBlocking
|
||||
import org.koin.android.ext.android.inject
|
||||
import kotlin.getValue
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
|
||||
private val checkSessionUseCase: CheckSessionUseCase by inject()
|
||||
|
||||
private val sessionDestinationState = MutableStateFlow<AppDestination?>(null)
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
val splashScreen = installSplashScreen()
|
||||
var stateLoaded = false
|
||||
splashScreen.setKeepOnScreenCondition {
|
||||
stateLoaded.not()
|
||||
}
|
||||
super.onCreate(savedInstanceState)
|
||||
enableEdgeToEdge()
|
||||
WindowCompat.setDecorFitsSystemWindows(window, false)
|
||||
|
||||
runBlocking {
|
||||
val isAuthorized = try {
|
||||
checkSessionUseCase()
|
||||
} catch (e: Exception) {
|
||||
false
|
||||
}
|
||||
sessionDestinationState.value =
|
||||
if (isAuthorized) AppDestination.Main else AppDestination.Login
|
||||
|
||||
stateLoaded = true
|
||||
}
|
||||
|
||||
setContent {
|
||||
val sessionDestination by sessionDestinationState.collectAsState()
|
||||
TTasksApp(sessionDestination = sessionDestination, context = this)
|
||||
LaunchedEffect(Unit) {
|
||||
requestPermissions(
|
||||
arrayOf(Manifest.permission.ACCESS_NOTIFICATION_POLICY), 123
|
||||
)
|
||||
FirebaseMessaging.getInstance().token
|
||||
.addOnCompleteListener { task ->
|
||||
if (task.isSuccessful) {
|
||||
val token = task.result
|
||||
Log.d("TOKEN", token)
|
||||
}
|
||||
}
|
||||
|
||||
checkAndRequestNotificationPermission()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun checkAndRequestNotificationPermission() {
|
||||
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
|
||||
when {
|
||||
ContextCompat.checkSelfPermission(
|
||||
this,
|
||||
Manifest.permission.POST_NOTIFICATIONS
|
||||
) == PackageManager.PERMISSION_GRANTED -> {
|
||||
// Разрешение уже есть, получаем токен
|
||||
getFCMToken()
|
||||
}
|
||||
|
||||
else -> {
|
||||
// Запрашиваем разрешение
|
||||
requestPermissions(
|
||||
arrayOf(Manifest.permission.POST_NOTIFICATIONS),
|
||||
123
|
||||
)
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Для версий ниже Android 13 разрешение не требуется
|
||||
getFCMToken()
|
||||
}
|
||||
}
|
||||
|
||||
private fun getFCMToken() {
|
||||
FirebaseMessaging.getInstance().token
|
||||
.addOnCompleteListener { task ->
|
||||
if (task.isSuccessful) {
|
||||
val token = task.result
|
||||
Log.d("TOKEN", token)
|
||||
} else {
|
||||
Log.e("TOKEN", "Failed to get token", task.exception)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun onRequestPermissionsResult(
|
||||
requestCode: Int,
|
||||
permissions: Array<String>,
|
||||
grantResults: IntArray,
|
||||
deviceId: Int
|
||||
) {
|
||||
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
|
||||
if (requestCode == 123) {
|
||||
if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
|
||||
getFCMToken()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user