From 2cf60da4531a4ecd9c7224cf7585b9c5528a7278 Mon Sep 17 00:00:00 2001 From: XataBq Date: Mon, 2 Mar 2026 16:24:46 +0300 Subject: [PATCH] nav: created navGraph --- .../com/example/notifymanager/MainActivity.kt | 119 +----------------- .../ui/navigation/AppNavGraph.kt | 42 +++++++ .../notifymanager/ui/navigation/AppRoute.kt | 13 ++ .../ui/screens/debug/DebugScreen.kt | 90 +++++++++++++ .../ui/screens/debug/DebugViewModel.kt | 13 ++ 5 files changed, 160 insertions(+), 117 deletions(-) create mode 100644 app/src/main/java/com/example/notifymanager/ui/navigation/AppNavGraph.kt create mode 100644 app/src/main/java/com/example/notifymanager/ui/navigation/AppRoute.kt create mode 100644 app/src/main/java/com/example/notifymanager/ui/screens/debug/DebugScreen.kt create mode 100644 app/src/main/java/com/example/notifymanager/ui/screens/debug/DebugViewModel.kt diff --git a/app/src/main/java/com/example/notifymanager/MainActivity.kt b/app/src/main/java/com/example/notifymanager/MainActivity.kt index 22d15f0..52c65ff 100644 --- a/app/src/main/java/com/example/notifymanager/MainActivity.kt +++ b/app/src/main/java/com/example/notifymanager/MainActivity.kt @@ -1,137 +1,22 @@ package com.example.notifymanager -import android.Manifest -import android.app.NotificationChannel -import android.app.NotificationManager -import android.content.Context -import android.content.pm.PackageManager -import android.os.Build import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.activity.enableEdgeToEdge -import androidx.compose.foundation.layout.Arrangement -import androidx.compose.foundation.layout.Column -import androidx.compose.foundation.layout.fillMaxSize -import androidx.compose.foundation.layout.padding -import androidx.compose.foundation.shape.RoundedCornerShape -import androidx.compose.material3.Button -import androidx.compose.material3.Scaffold -import androidx.compose.material3.Text -import androidx.compose.runtime.Composable -import androidx.compose.ui.Alignment -import androidx.compose.ui.Modifier -import androidx.compose.ui.tooling.preview.Preview -import androidx.compose.ui.unit.dp -import androidx.core.app.NotificationCompat -import androidx.core.content.ContextCompat -import androidx.lifecycle.lifecycleScope -import com.example.notifymanager.domain.mode.Mode -import com.example.notifymanager.domain.mode.ModeRepository +import com.example.notifymanager.ui.navigation.AppNavGraph import com.example.notifymanager.ui.theme.NotifyManagerTheme import dagger.hilt.android.AndroidEntryPoint -import kotlinx.coroutines.flow.first -import kotlinx.coroutines.launch -import javax.inject.Inject @AndroidEntryPoint class MainActivity : ComponentActivity() { - @Inject - lateinit var modeRepository: ModeRepository - override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) enableEdgeToEdge() setContent { NotifyManagerTheme { - Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding -> - Greeting( - name = "Android", - modifier = Modifier.padding(innerPadding), - ) - Column( - modifier = - Modifier - .fillMaxSize(), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.Center, - ) { - Button( - onClick = { - lifecycleScope.launch { - val current = modeRepository.currentMode.first() - val next = if (current == Mode.HOME) Mode.REST else Mode.HOME - modeRepository.setMode(next) - } - postTestNotification(this@MainActivity) - }, -// modifier = Modifier -// .width(100.dp) -// .height(50.dp), - shape = RoundedCornerShape(13.dp), - ) { - Text( - text = "Click to send Notification", - ) - } - } - } + AppNavGraph() } } } } - -private const val TEST_CH_ID = "test_channel" - -fun postTestNotification(context: Context) { - // Android 13+ требует runtime permission - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { - val granted = - ContextCompat.checkSelfPermission( - context, - Manifest.permission.POST_NOTIFICATIONS, - ) == PackageManager.PERMISSION_GRANTED - - if (!granted) return - } - - val nm = context.getSystemService(NotificationManager::class.java) - - val ch = - NotificationChannel( - TEST_CH_ID, - "Test", - NotificationManager.IMPORTANCE_DEFAULT, - ) - nm.createNotificationChannel(ch) - - val n = - NotificationCompat - .Builder(context, TEST_CH_ID) - .setSmallIcon(android.R.drawable.ic_dialog_info) - .setContentTitle("NotifyManager test") - .setContentText("Hello from test notification") - .setAutoCancel(true) - .build() - - nm.notify(1001, n) -} - -@Composable -fun Greeting( - name: String, - modifier: Modifier = Modifier, -) { - Text( - text = "Hello $name!", - modifier = modifier, - ) -} - -@Preview(showBackground = true) -@Composable -fun GreetingPreview() { - NotifyManagerTheme { - Greeting("Android") - } -} diff --git a/app/src/main/java/com/example/notifymanager/ui/navigation/AppNavGraph.kt b/app/src/main/java/com/example/notifymanager/ui/navigation/AppNavGraph.kt new file mode 100644 index 0000000..87adeca --- /dev/null +++ b/app/src/main/java/com/example/notifymanager/ui/navigation/AppNavGraph.kt @@ -0,0 +1,42 @@ +package com.example.notifymanager.ui.navigation + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.remember +import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel +import androidx.navigation.NavController +import androidx.navigation.NavGraphBuilder +import androidx.navigation.compose.NavHost +import androidx.navigation.compose.composable +import androidx.navigation.compose.rememberNavController +import androidx.navigation.navigation +import com.example.notifymanager.ui.screens.debug.DebugScreen +import com.example.notifymanager.ui.screens.debug.DebugViewModel + +@Composable +fun AppNavGraph() { + val navController = rememberNavController() + + NavHost( + navController = navController, + startDestination = Graph.Debug.route, + ) { + debugGraph(navController = navController) + } +} + +fun NavGraphBuilder.debugGraph(navController: NavController) { + navigation( + route = Graph.Debug.route, + startDestination = Screen.Mode.route, + ) { + composable(Screen.Mode.route) { backStackEntry -> + val parentEntry = + remember(backStackEntry) { + navController.getBackStackEntry(Graph.Debug.route) + } + val debugViewModel: DebugViewModel = hiltViewModel(parentEntry) + + DebugScreen(viewModel = debugViewModel) + } + } +} diff --git a/app/src/main/java/com/example/notifymanager/ui/navigation/AppRoute.kt b/app/src/main/java/com/example/notifymanager/ui/navigation/AppRoute.kt new file mode 100644 index 0000000..fad64be --- /dev/null +++ b/app/src/main/java/com/example/notifymanager/ui/navigation/AppRoute.kt @@ -0,0 +1,13 @@ +package com.example.notifymanager.ui.navigation + +sealed class Graph( + val route: String, +) { + data object Debug : Graph(route = "debug_route") +} + +sealed class Screen( + val route: String, +) { + data object Mode : Screen("mode_screen") +} diff --git a/app/src/main/java/com/example/notifymanager/ui/screens/debug/DebugScreen.kt b/app/src/main/java/com/example/notifymanager/ui/screens/debug/DebugScreen.kt new file mode 100644 index 0000000..00ac4a8 --- /dev/null +++ b/app/src/main/java/com/example/notifymanager/ui/screens/debug/DebugScreen.kt @@ -0,0 +1,90 @@ +package com.example.notifymanager.ui.screens.debug + +import android.Manifest +import android.R +import android.app.NotificationChannel +import android.app.NotificationManager +import android.content.Context +import android.content.pm.PackageManager +import android.os.Build +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.shape.RoundedCornerShape +import androidx.compose.material3.Button +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import androidx.core.app.NotificationCompat +import androidx.core.content.ContextCompat +import androidx.hilt.lifecycle.viewmodel.compose.hiltViewModel + +@Composable +fun DebugScreen(viewModel: DebugViewModel = hiltViewModel()) { + Scaffold { innerPadding -> + Column( + modifier = + Modifier + .padding(innerPadding) + .fillMaxSize(), + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.Center, + ) { + Button( + onClick = { +// lifecycleScope.launch { +// val current = modeRepository.currentMode.first() +// val next = if (current == Mode.HOME) Mode.REST else Mode.HOME +// modeRepository.setMode(next) +// } +// postTestNotification(this@Column) + }, + shape = RoundedCornerShape(13.dp), + ) { + Text( + text = "Click to send Notification", + ) + } + } + } +} + +private const val TEST_CH_ID = "test_channel" + +fun postTestNotification(context: Context) { + // Android 13+ требует runtime permission + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { + val granted = + ContextCompat.checkSelfPermission( + context, + Manifest.permission.POST_NOTIFICATIONS, + ) == PackageManager.PERMISSION_GRANTED + + if (!granted) return + } + + val nm = context.getSystemService(NotificationManager::class.java) + + val ch = + NotificationChannel( + TEST_CH_ID, + "Test", + NotificationManager.IMPORTANCE_DEFAULT, + ) + nm.createNotificationChannel(ch) + + val n = + NotificationCompat + .Builder(context, TEST_CH_ID) + .setSmallIcon(R.drawable.ic_dialog_info) + .setContentTitle("NotifyManager test") + .setContentText("Hello from test notification") + .setAutoCancel(true) + .build() + + nm.notify(1001, n) +} diff --git a/app/src/main/java/com/example/notifymanager/ui/screens/debug/DebugViewModel.kt b/app/src/main/java/com/example/notifymanager/ui/screens/debug/DebugViewModel.kt new file mode 100644 index 0000000..2b8f990 --- /dev/null +++ b/app/src/main/java/com/example/notifymanager/ui/screens/debug/DebugViewModel.kt @@ -0,0 +1,13 @@ +package com.example.notifymanager.ui.screens.debug + +import androidx.lifecycle.ViewModel +import com.example.notifymanager.domain.mode.ModeRepository +import dagger.hilt.android.lifecycle.HiltViewModel +import javax.inject.Inject + +@HiltViewModel +class DebugViewModel + @Inject + constructor( + modeRepository: ModeRepository, + ) : ViewModel()