TiebaLite/app/src/main/java/com/huanchengfly/tieba/post/MainActivityV2.kt

162 lines
6.3 KiB
Kotlin
Raw Normal View History

2022-09-24 16:05:21 +08:00
package com.huanchengfly.tieba.post
2022-10-16 12:06:27 +08:00
import android.annotation.SuppressLint
import android.app.job.JobInfo
import android.app.job.JobScheduler
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
2022-09-24 16:05:21 +08:00
import android.os.Build
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import androidx.compose.animation.AnimatedContentScope
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.material.Surface
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
2023-01-01 21:26:46 +08:00
import androidx.core.content.ContextCompat
2022-09-24 16:05:21 +08:00
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.lifecycleScope
import com.google.accompanist.navigation.material.ExperimentalMaterialNavigationApi
import com.google.accompanist.systemuicontroller.SystemUiController
import com.huanchengfly.tieba.post.api.retrofit.exception.getErrorMessage
import com.huanchengfly.tieba.post.arch.BaseComposeActivity
2022-10-16 12:06:27 +08:00
import com.huanchengfly.tieba.post.services.NotifyJobService
2022-09-24 16:05:21 +08:00
import com.huanchengfly.tieba.post.ui.common.theme.compose.ExtendedTheme
import com.huanchengfly.tieba.post.ui.page.NavGraphs
import com.huanchengfly.tieba.post.ui.page.destinations.MainPageDestination
import com.huanchengfly.tieba.post.utils.AccountUtil
2022-10-16 12:06:27 +08:00
import com.huanchengfly.tieba.post.utils.JobServiceUtil
2022-09-24 16:05:21 +08:00
import com.huanchengfly.tieba.post.utils.PermissionUtils
2022-10-16 12:06:27 +08:00
import com.huanchengfly.tieba.post.utils.ReceiverUtil
2022-09-24 16:05:21 +08:00
import com.huanchengfly.tieba.post.utils.TiebaUtil
import com.huanchengfly.tieba.post.utils.requestPermission
import com.ramcosta.composedestinations.DestinationsNavHost
import com.ramcosta.composedestinations.animations.defaults.RootNavGraphDefaultAnimations
import com.ramcosta.composedestinations.animations.rememberAnimatedNavHostEngine
import com.ramcosta.composedestinations.navigation.dependency
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.launch
@AndroidEntryPoint
class MainActivityV2 : BaseComposeActivity() {
private val handler = Handler(Looper.getMainLooper())
2022-10-16 12:06:27 +08:00
private val newMessageReceiver: BroadcastReceiver = NewMessageReceiver()
2022-09-24 16:05:21 +08:00
private fun fetchAccount() {
lifecycleScope.launch(Dispatchers.Main) {
if (AccountUtil.isLoggedIn()) {
AccountUtil.fetchAccountFlow()
.flowOn(Dispatchers.IO)
.catch { e ->
toastShort(e.getErrorMessage())
e.printStackTrace()
}
.collect()
}
}
}
private fun requestNotificationPermission() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU && AccountUtil.isLoggedIn()) {
requestPermission {
permissions = listOf(PermissionUtils.POST_NOTIFICATIONS)
description = getString(R.string.desc_permission_post_notifications)
}
}
}
private fun initAutoSign() {
runCatching {
TiebaUtil.initAutoSign(this)
}
}
override fun onStart() {
super.onStart()
2022-10-16 12:06:27 +08:00
runCatching {
2023-01-01 21:26:46 +08:00
ContextCompat.registerReceiver(
this,
2022-10-16 12:06:27 +08:00
newMessageReceiver,
ReceiverUtil.createIntentFilter(NotifyJobService.ACTION_NEW_MESSAGE),
2023-01-01 21:26:46 +08:00
ContextCompat.RECEIVER_NOT_EXPORTED
2022-10-16 12:06:27 +08:00
)
startService(Intent(this, NotifyJobService::class.java))
val builder = JobInfo.Builder(
JobServiceUtil.getJobId(this),
ComponentName(this, NotifyJobService::class.java)
)
.setPersisted(true)
.setPeriodic(30 * 60 * 1000L)
.setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
val jobScheduler = getSystemService(Context.JOB_SCHEDULER_SERVICE) as JobScheduler
jobScheduler.schedule(builder.build())
}
2022-09-24 16:05:21 +08:00
handler.postDelayed({
requestNotificationPermission()
}, 100)
}
override fun onCreate(savedInstanceState: Bundle?) {
installSplashScreen()
super.onCreate(savedInstanceState)
}
override fun onCreateContent(systemUiController: SystemUiController) {
super.onCreateContent(systemUiController)
fetchAccount()
initAutoSign()
}
@OptIn(ExperimentalAnimationApi::class, ExperimentalMaterialNavigationApi::class)
@Composable
override fun createContent() {
Surface(
color = ExtendedTheme.colors.background
) {
val engine = rememberAnimatedNavHostEngine(
navHostContentAlignment = Alignment.TopStart,
rootDefaultAnimations = RootNavGraphDefaultAnimations(
enterTransition = {
slideIntoContainer(AnimatedContentScope.SlideDirection.Start, initialOffset = { it })
},
exitTransition = {
slideOutOfContainer(AnimatedContentScope.SlideDirection.End, targetOffset = { -it })
},
popEnterTransition = {
slideIntoContainer(AnimatedContentScope.SlideDirection.Start, initialOffset = { -it })
},
popExitTransition = {
slideOutOfContainer(AnimatedContentScope.SlideDirection.End, targetOffset = { it })
},
),
)
DestinationsNavHost(
navGraph = NavGraphs.root,
engine = engine,
dependenciesContainerBuilder = {
dependency(MainPageDestination) { this@MainActivityV2 }
}
)
}
}
2022-10-16 12:06:27 +08:00
private inner class NewMessageReceiver : BroadcastReceiver() {
@SuppressLint("RestrictedApi")
override fun onReceive(context: Context, intent: Intent) {
if (intent.action == NotifyJobService.ACTION_NEW_MESSAGE) {
val channel = intent.getStringExtra("channel")
val count = intent.getIntExtra("count", 0)
if (channel != null && channel == NotifyJobService.CHANNEL_TOTAL) {
}
}
}
}
2022-09-24 16:05:21 +08:00
}