|
1 | 1 | package com.example.studysmart
|
2 | 2 |
|
| 3 | +import android.content.ComponentName |
| 4 | +import android.content.Context |
| 5 | +import android.content.Intent |
| 6 | +import android.content.ServiceConnection |
| 7 | +import android.os.Build |
3 | 8 | import android.os.Bundle
|
| 9 | +import android.os.IBinder |
4 | 10 | import androidx.activity.ComponentActivity
|
5 | 11 | import androidx.activity.compose.setContent
|
| 12 | +import androidx.compose.runtime.getValue |
| 13 | +import androidx.compose.runtime.mutableStateOf |
| 14 | +import androidx.compose.runtime.setValue |
6 | 15 | import androidx.compose.ui.graphics.toArgb
|
| 16 | +import androidx.core.app.ActivityCompat |
7 | 17 | import com.example.studysmart.domain.model.Session
|
8 | 18 | import com.example.studysmart.domain.model.Subject
|
9 | 19 | import com.example.studysmart.domain.model.Task
|
10 | 20 | import com.example.studysmart.presentation.NavGraphs
|
| 21 | +import com.example.studysmart.presentation.destinations.SessionScreenRouteDestination |
| 22 | +import com.example.studysmart.presentation.session.StudySessionTimerService |
11 | 23 | import com.example.studysmart.presentation.theme.StudySmartTheme
|
12 | 24 | import com.ramcosta.composedestinations.DestinationsNavHost
|
| 25 | +import com.ramcosta.composedestinations.navigation.dependency |
13 | 26 | import dagger.hilt.android.AndroidEntryPoint
|
14 | 27 |
|
15 | 28 | @AndroidEntryPoint
|
16 | 29 | class MainActivity : ComponentActivity() {
|
| 30 | + |
| 31 | + private var isBound by mutableStateOf(false) |
| 32 | + private lateinit var timerService: StudySessionTimerService |
| 33 | + private val connection = object : ServiceConnection { |
| 34 | + override fun onServiceConnected(p0: ComponentName?, service: IBinder?) { |
| 35 | + val binder = service as StudySessionTimerService.StudySessionTimerBinder |
| 36 | + timerService = binder.getService() |
| 37 | + isBound = true |
| 38 | + } |
| 39 | + |
| 40 | + override fun onServiceDisconnected(p0: ComponentName?) { |
| 41 | + isBound = false |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + override fun onStart() { |
| 46 | + super.onStart() |
| 47 | + Intent(this, StudySessionTimerService::class.java).also { intent -> |
| 48 | + bindService(intent, connection, Context.BIND_AUTO_CREATE) |
| 49 | + } |
| 50 | + } |
| 51 | + |
17 | 52 | override fun onCreate(savedInstanceState: Bundle?) {
|
18 | 53 | super.onCreate(savedInstanceState)
|
19 | 54 | setContent {
|
20 |
| - StudySmartTheme { |
21 |
| - DestinationsNavHost(navGraph = NavGraphs.root) |
| 55 | + if (isBound) { |
| 56 | + StudySmartTheme { |
| 57 | + DestinationsNavHost( |
| 58 | + navGraph = NavGraphs.root, |
| 59 | + dependenciesContainerBuilder = { |
| 60 | + dependency(SessionScreenRouteDestination) { timerService } |
| 61 | + } |
| 62 | + ) |
| 63 | + } |
22 | 64 | }
|
23 | 65 | }
|
| 66 | + requestPermission() |
| 67 | + } |
| 68 | + |
| 69 | + private fun requestPermission() { |
| 70 | + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { |
| 71 | + ActivityCompat.requestPermissions( |
| 72 | + this, |
| 73 | + arrayOf(android.Manifest.permission.POST_NOTIFICATIONS), |
| 74 | + 0 |
| 75 | + ) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + override fun onStop() { |
| 80 | + super.onStop() |
| 81 | + unbindService(connection) |
| 82 | + isBound = false |
24 | 83 | }
|
25 | 84 | }
|
26 | 85 |
|
|
0 commit comments