1
1
package com.bff.wespot
2
2
3
+ import android.Manifest
4
+ import android.content.pm.PackageManager
5
+ import android.os.Build
3
6
import android.os.Bundle
4
7
import androidx.activity.ComponentActivity
5
8
import androidx.activity.compose.setContent
@@ -26,20 +29,32 @@ import androidx.compose.runtime.State
26
29
import androidx.compose.runtime.getValue
27
30
import androidx.compose.runtime.mutableStateOf
28
31
import androidx.compose.runtime.remember
32
+ import androidx.compose.runtime.setValue
29
33
import androidx.compose.ui.Alignment
30
34
import androidx.compose.ui.Modifier
31
35
import androidx.compose.ui.graphics.painter.Painter
32
36
import androidx.compose.ui.res.painterResource
33
37
import androidx.compose.ui.res.stringResource
34
38
import androidx.compose.ui.unit.dp
39
+ import androidx.core.app.ActivityCompat
40
+ import androidx.core.content.ContextCompat
35
41
import androidx.navigation.NavController
36
42
import androidx.navigation.NavGraph.Companion.findStartDestination
37
43
import androidx.navigation.compose.rememberNavController
44
+ import com.ramcosta.composedestinations.dynamic.within
38
45
import com.bff.wespot.designsystem.R
39
46
import com.bff.wespot.designsystem.component.header.WSTopBar
47
+ import com.bff.wespot.designsystem.component.indicator.WSToast
40
48
import com.bff.wespot.designsystem.theme.StaticTypeScale
41
49
import com.bff.wespot.designsystem.theme.WeSpotTheme
42
50
import com.bff.wespot.designsystem.theme.WeSpotThemeManager
51
+ import com.bff.wespot.model.ToastState
52
+ import com.bff.wespot.message.screen.MessageScreenArgs
53
+ import com.bff.wespot.message.screen.destinations.MessageScreenDestination
54
+ import com.bff.wespot.message.screen.destinations.ReceiverSelectionScreenDestination
55
+ import com.bff.wespot.message.screen.send.ReceiverSelectionScreenArgs
56
+ import com.bff.wespot.model.notification.NotificationType
57
+ import com.bff.wespot.model.notification.convertNotificationType
43
58
import com.bff.wespot.navigation.Navigator
44
59
import com.ramcosta.composedestinations.navigation.navigate
45
60
import com.ramcosta.composedestinations.spec.NavGraphSpec
@@ -54,26 +69,67 @@ class MainActivity : ComponentActivity() {
54
69
55
70
override fun onCreate (savedInstanceState : Bundle ? ) {
56
71
super .onCreate(savedInstanceState)
72
+ requestNotificationPermission()
57
73
58
74
setContent {
59
75
WeSpotTheme {
60
- MainScreen (navigator)
76
+ MainScreen (
77
+ navigator = navigator,
78
+ navArgs = getMainScreenArgsFromIntent(),
79
+ )
80
+ }
81
+ }
82
+ }
83
+
84
+ private fun requestNotificationPermission () {
85
+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .TIRAMISU ) {
86
+ val hasPermission = ContextCompat .checkSelfPermission(
87
+ this ,
88
+ Manifest .permission.POST_NOTIFICATIONS
89
+ ) == PackageManager .PERMISSION_GRANTED
90
+
91
+ if (! hasPermission) {
92
+ ActivityCompat .requestPermissions(
93
+ this ,
94
+ arrayOf(Manifest .permission.POST_NOTIFICATIONS ),
95
+ 0
96
+ )
61
97
}
62
98
}
63
99
}
100
+
101
+ private fun getMainScreenArgsFromIntent (): MainScreenNavArgs {
102
+ val targetId = intent.getStringExtra(" targetId" )?.toInt() ? : - 1
103
+ val date = intent.getStringExtra(" date" ) ? : " "
104
+ val type = intent.getStringExtra(" type" ) ? : " "
105
+
106
+ return MainScreenNavArgs (
107
+ targetId = targetId,
108
+ date = date,
109
+ type = convertNotificationType(type),
110
+ )
111
+ }
64
112
}
65
113
114
+ data class MainScreenNavArgs (
115
+ val type : NotificationType ,
116
+ val date : String ,
117
+ val targetId : Int ,
118
+ )
119
+
66
120
@OptIn(ExperimentalMaterial3Api ::class )
67
121
@Composable
68
- private fun MainScreen (navigator : Navigator ) {
122
+ private fun MainScreen (navigator : Navigator , navArgs : MainScreenNavArgs ) {
69
123
val navController = rememberNavController()
124
+ var toast by remember { mutableStateOf(ToastState ()) }
70
125
71
- val checkScreen by navController.checkCurrentScreen()
126
+ val isTopNavigationScreen by navController.checkCurrentScreen(NavigationBarPosition .TOP )
127
+ val isBottomNavigationScreen by navController.checkCurrentScreen(NavigationBarPosition .BOTTOM )
72
128
73
129
Scaffold (
74
130
modifier = Modifier .fillMaxSize(),
75
131
topBar = {
76
- if (checkScreen ) {
132
+ if (isTopNavigationScreen ) {
77
133
WSTopBar (
78
134
title = " " ,
79
135
navigation = {
@@ -106,7 +162,7 @@ private fun MainScreen(navigator: Navigator) {
106
162
}
107
163
},
108
164
bottomBar = {
109
- if (checkScreen ) {
165
+ if (isBottomNavigationScreen ) {
110
166
val currentSelectedItem by navController.currentScreenAsState()
111
167
BottomNavigationTab (
112
168
selectedNavigation = currentSelectedItem,
@@ -121,8 +177,24 @@ private fun MainScreen(navigator: Navigator) {
121
177
AppNavigation (
122
178
navController = navController,
123
179
modifier = Modifier .padding(it),
124
- navigator = navigator
180
+ navigator = navigator,
181
+ showToast = { toastState -> toast = toastState }
125
182
)
183
+
184
+ navigateScreenFromNavArgs(navArgs, navController)
185
+ }
186
+
187
+ if (toast.show) {
188
+ Box (modifier = Modifier .fillMaxSize(), contentAlignment = Alignment .TopCenter ) {
189
+ WSToast (
190
+ text = stringResource(toast.message),
191
+ showToast = toast.show,
192
+ toastType = toast.type,
193
+ closeToast = {
194
+ toast = toast.copy(show = false )
195
+ },
196
+ )
197
+ }
126
198
}
127
199
}
128
200
@@ -177,12 +249,12 @@ private fun NavController.currentScreenAsState(): State<NavGraphSpec> {
177
249
178
250
@Stable
179
251
@Composable
180
- private fun NavController.checkCurrentScreen (): State <Boolean > {
252
+ private fun NavController.checkCurrentScreen (position : NavigationBarPosition ): State <Boolean > {
181
253
val showBar = remember { mutableStateOf(false ) }
182
254
183
255
DisposableEffect (this ) {
184
256
val listener = NavController .OnDestinationChangedListener { _, destination, _ ->
185
- showBar.value = destination.checkDestination()
257
+ showBar.value = destination.checkDestination(position )
186
258
}
187
259
188
260
addOnDestinationChangedListener(listener)
@@ -235,6 +307,38 @@ private fun TabItem(
235
307
}
236
308
}
237
309
310
+ private fun navigateScreenFromNavArgs (navArgs : MainScreenNavArgs , navController : NavController ) {
311
+ when (navArgs.type) {
312
+ NotificationType .MESSAGE -> {
313
+ navController.navigate(
314
+ ReceiverSelectionScreenDestination (
315
+ ReceiverSelectionScreenArgs (false ),
316
+ ) within AppNavGraphs .message
317
+ )
318
+ }
319
+
320
+ NotificationType .MESSAGE_SENT , NotificationType .MESSAGE_RECEIVED -> {
321
+ navController.navigate(
322
+ MessageScreenDestination (
323
+ MessageScreenArgs (
324
+ type = navArgs.type,
325
+ messageId = navArgs.targetId,
326
+ ),
327
+ ) within AppNavGraphs .message
328
+ )
329
+ }
330
+
331
+ NotificationType .VOTE -> {
332
+ }
333
+ NotificationType .VOTE_RESULT -> {
334
+ }
335
+ NotificationType .VOTE_RECEIVED -> {
336
+ }
337
+ NotificationType .IDLE -> {
338
+ }
339
+ }
340
+ }
341
+
238
342
private fun NavController.navigateToNavGraph (navGraph : NavGraphSpec ) {
239
343
this .navigate(navGraph) {
240
344
launchSingleTop = true
0 commit comments