|
| 1 | +/* |
| 2 | + * |
| 3 | + * * Copyright (c) BlazeCode / Ralf Lehmann, 2022. |
| 4 | + * |
| 5 | + */ |
| 6 | + |
| 7 | +package com.blazecode.eventtool |
| 8 | + |
| 9 | +import android.os.Bundle |
| 10 | +import android.widget.Toast |
| 11 | +import androidx.activity.ComponentActivity |
| 12 | +import androidx.activity.compose.setContent |
| 13 | +import androidx.compose.animation.ExperimentalAnimationApi |
| 14 | +import androidx.compose.material3.* |
| 15 | +import androidx.compose.runtime.mutableStateOf |
| 16 | +import androidx.compose.ui.res.stringResource |
| 17 | +import androidx.lifecycle.lifecycleScope |
| 18 | +import com.blazecode.eventtool.data.Event |
| 19 | +import com.blazecode.eventtool.database.DataBaseExporter |
| 20 | +import com.blazecode.eventtool.database.DataBaseImporter |
| 21 | +import com.blazecode.eventtool.navigation.NavRoutes |
| 22 | +import com.blazecode.eventtool.screens.* |
| 23 | +import com.blazecode.eventtool.ui.theme.EventToolTheme |
| 24 | +import com.blazecode.eventtool.util.PermissionManager |
| 25 | +import com.blazecode.eventtool.util.pdf.PdfPrinter |
| 26 | +import com.blazecode.eventtool.viewmodels.HomeViewModel |
| 27 | +import com.blazecode.eventtool.viewmodels.NewEventViewModel |
| 28 | +import com.blazecode.eventtool.viewmodels.SettingsViewModel |
| 29 | +import com.google.accompanist.navigation.animation.AnimatedNavHost |
| 30 | +import com.google.accompanist.navigation.animation.composable |
| 31 | +import com.google.accompanist.navigation.animation.rememberAnimatedNavController |
| 32 | +import kotlinx.coroutines.launch |
| 33 | + |
| 34 | +var errorDialogMessage = mutableStateOf("") |
| 35 | +var notificationTapEvent = mutableStateOf<Event?>(null) |
| 36 | + |
| 37 | +class MainActivity : ComponentActivity() { |
| 38 | + @OptIn(ExperimentalAnimationApi::class) |
| 39 | + override fun onCreate(savedInstanceState: Bundle?) { |
| 40 | + super.onCreate(savedInstanceState) |
| 41 | + |
| 42 | + // HACKY WAY TO PREVENT WHITE SCREEN BETWEEN TRANSITIONS |
| 43 | + lifecycleScope.launch { |
| 44 | + window.setBackgroundDrawableResource(android.R.color.transparent) |
| 45 | + } |
| 46 | + |
| 47 | + // GET EVENT IF TAPPED NOTIFICATION |
| 48 | + notificationTapEvent.value = intent.extras?.getParcelable("event", Event::class.java) |
| 49 | + |
| 50 | + val permissionManager = PermissionManager(this) |
| 51 | + |
| 52 | + val exporter = DataBaseExporter(this) |
| 53 | + val importer = DataBaseImporter(this) |
| 54 | + val printer = PdfPrinter(this) |
| 55 | + // CONTENT |
| 56 | + setContent { |
| 57 | + val navController = rememberAnimatedNavController() |
| 58 | + AnimatedNavHost(navController = navController, startDestination = NavRoutes.Home.route) { |
| 59 | + // HOME |
| 60 | + composable(route= NavRoutes.Home.route,) { Home(HomeViewModel(application), navController, printer) } |
| 61 | + // NEW EVENT |
| 62 | + composable(NavRoutes.NewEvent.route) { |
| 63 | + val event = navController.previousBackStackEntry?.savedStateHandle?.get<Event>("event") |
| 64 | + event?.let { |
| 65 | + NewEvent(NewEventViewModel(application, event), navController) } |
| 66 | + } |
| 67 | + // SETTINGS |
| 68 | + composable(NavRoutes.Settings.route){ Settings(SettingsViewModel(application), navController, permissionManager, exporter, importer) } |
| 69 | + // OPEN SOURCE LICENSES |
| 70 | + composable(NavRoutes.OpenSourceLicenses.route){ OpenSourceLicenses(navController) } |
| 71 | + } |
| 72 | + |
| 73 | + // ERROR DIALOG |
| 74 | + EventToolTheme { |
| 75 | + if(errorDialogMessage.value != ""){ |
| 76 | + AlertDialog( |
| 77 | + onDismissRequest = { errorDialogMessage.value = "" }, |
| 78 | + title = { Text(stringResource(R.string.error)) }, |
| 79 | + text = { Text(errorDialogMessage.value) }, |
| 80 | + confirmButton = { OutlinedButton(onClick = { errorDialogMessage.value = "" }) |
| 81 | + { Text(stringResource(R.string.close)) } } |
| 82 | + ) |
| 83 | + } |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + fun sendToast(text:String){ |
| 89 | + Toast.makeText(this, text, Toast.LENGTH_LONG).show() |
| 90 | + println(text) |
| 91 | + } |
| 92 | + |
| 93 | + fun sendErrorDialog(text: String){ |
| 94 | + errorDialogMessage.value = text |
| 95 | + } |
| 96 | +} |
| 97 | + |
0 commit comments