From 853548225c4e2541841e7602d7fff2ed122ab455 Mon Sep 17 00:00:00 2001 From: "Alberto Duarte (PWC)" Date: Mon, 9 Oct 2023 16:54:46 +0100 Subject: Initial commit --- .../presentation/activities/MainActivity.kt | 31 +++ .../presentation/navigation/AppScreens.kt | 7 + .../presentation/screens/BottomNavScreen.kt | 60 +++++ .../presentation/screens/CalculatorScreen.kt | 276 +++++++++++++++++++++ .../presentation/screens/FunctionListScreen.kt | 72 ++++++ .../presentation/screens/WipeScreen.kt | 31 +++ .../calculatorv2/presentation/ui/theme/Color.kt | 11 + .../calculatorv2/presentation/ui/theme/Theme.kt | 70 ++++++ .../pwc/calculatorv2/presentation/ui/theme/Type.kt | 34 +++ .../presentation/ui/widgets/BottomNav.kt | 32 +++ 10 files changed, 624 insertions(+) create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/activities/MainActivity.kt create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/navigation/AppScreens.kt create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/screens/BottomNavScreen.kt create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/screens/CalculatorScreen.kt create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/screens/FunctionListScreen.kt create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/screens/WipeScreen.kt create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Color.kt create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Theme.kt create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Type.kt create mode 100644 app/src/main/java/com/pwc/calculatorv2/presentation/ui/widgets/BottomNav.kt (limited to 'app/src/main/java/com/pwc/calculatorv2/presentation') diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/activities/MainActivity.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/activities/MainActivity.kt new file mode 100644 index 0000000..59a91f0 --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/activities/MainActivity.kt @@ -0,0 +1,31 @@ +package com.pwc.calculatorv2.presentation.activities + +import android.os.Bundle +import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Surface +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.ui.Modifier +import androidx.compose.ui.tooling.preview.Preview +import com.pwc.calculatorv2.presentation.screens.BottomNavScreen +import com.pwc.calculatorv2.presentation.ui.theme.CalculatorV2Theme + +class MainActivity : ComponentActivity() { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContent { + CalculatorV2Theme { + // A surface container using the 'background' color from the theme + Surface( + modifier = Modifier.fillMaxSize(), + color = MaterialTheme.colorScheme.background + ) { + BottomNavScreen() + } + } + } + } +} diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/navigation/AppScreens.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/navigation/AppScreens.kt new file mode 100644 index 0000000..0418d2b --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/navigation/AppScreens.kt @@ -0,0 +1,7 @@ +package com.pwc.calculatorv2.presentation.navigation + +sealed class AppScreens(val route: String) { + object CalculatorScreen: AppScreens("calculator_screen") + object FunctionListScreen: AppScreens("function_list_screen") + object WipeScreen: AppScreens("wipe_screen") +} diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/screens/BottomNavScreen.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/screens/BottomNavScreen.kt new file mode 100644 index 0000000..1f05318 --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/screens/BottomNavScreen.kt @@ -0,0 +1,60 @@ +package com.pwc.calculatorv2.presentation.screens + +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.padding +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.Scaffold +import androidx.compose.material3.Surface +import androidx.compose.runtime.Composable +import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Modifier +import androidx.navigation.compose.NavHost +import androidx.navigation.compose.composable +import androidx.navigation.compose.rememberNavController +import com.pwc.calculatorv2.data.viewmodels.CalculatorViewModel +import com.pwc.calculatorv2.presentation.navigation.AppScreens +import com.pwc.calculatorv2.presentation.ui.widgets.BottomNav +import androidx.compose.runtime.getValue +import androidx.compose.ui.platform.LocalContext + +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun BottomNavScreen() { + val context = LocalContext.current + val bottomNavController = rememberNavController() + val viewModel by remember { mutableStateOf(CalculatorViewModel(context)) } + LaunchedEffect(Unit) { + viewModel.getFunctionList() + } + + Scaffold( + bottomBar = { + BottomNav(bottomNavController) + }, + ) { + Surface( + modifier = Modifier + .fillMaxSize() + .padding(bottom = it.calculateBottomPadding()), + color = MaterialTheme.colorScheme.background + ) { + NavHost( + navController = bottomNavController, + startDestination = AppScreens.CalculatorScreen.route + ) { + composable(AppScreens.CalculatorScreen.route) { + CalculatorScreen(viewModel) + } + composable(AppScreens.FunctionListScreen.route) { + FunctionListScreen(viewModel) + } + composable(AppScreens.WipeScreen.route) { + WipeScreen(viewModel) + } + } + } + } +} diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/screens/CalculatorScreen.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/screens/CalculatorScreen.kt new file mode 100644 index 0000000..5cef814 --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/screens/CalculatorScreen.kt @@ -0,0 +1,276 @@ +package com.pwc.calculatorv2.presentation.screens + +import android.annotation.SuppressLint +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width +import androidx.compose.material3.Button +import androidx.compose.material3.ExperimentalMaterial3Api +import androidx.compose.material3.Text +import androidx.compose.material3.TextField +import androidx.compose.runtime.Composable +import androidx.compose.runtime.MutableState +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.graphics.Color +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.style.TextAlign +import androidx.compose.ui.unit.dp +import androidx.compose.ui.unit.sp +import com.pwc.calculatorv2.data.viewmodels.CalculatorViewModel +import com.pwc.calculatorv2.data.viewmodels.CalculatorViewState + +@SuppressLint("StateFlowValueCalledInComposition") +@OptIn(ExperimentalMaterial3Api::class) +@Composable +fun CalculatorScreen( + viewModel: CalculatorViewModel, +) { + + val expression = remember { mutableStateOf("") } + + Box( + modifier = Modifier + .fillMaxSize(), + contentAlignment = Alignment.Center) + { + Column( + modifier = Modifier.width(width = 300.dp), + verticalArrangement = Arrangement.Center, + horizontalAlignment = Alignment.CenterHorizontally + ) { + TextField( + value = expression.value, + onValueChange = { expression.value = it }, + modifier = Modifier.width(300.dp), + textStyle = TextStyle(fontSize = 24.sp, textAlign = TextAlign.End) + ) + + Column( + modifier = Modifier + .padding(16.dp) + .width(300.dp) + ) { + Row( + modifier = Modifier + .fillMaxWidth() + .padding(horizontal = 0.dp), + horizontalArrangement = Arrangement.SpaceEvenly + ) { + Button( + onClick = { viewModel.calculatorViewState.value?.let { expression.value = viewModel.showLast() } }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "^", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, " ( ") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "(", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, " ) ") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = ")", color = Color.Black) + } + Button( + onClick = { erase(expression) }, + modifier = Modifier.width(57.dp) + ) { + Text(text = "<", color = Color.Black) + } + } + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceEvenly + ) { + Button( + onClick = { appendToExpression(expression, "7") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "7", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, "8") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "8", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, "9") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "9", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, " + ") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "+", color = Color.Black) + } + } + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceEvenly + ) { + Button( + onClick = { appendToExpression(expression, "4") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "4", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, "5") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "5", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, "6") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "6", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, " - ") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "-", color = Color.Black) + } + } + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceEvenly + ) { + Button( + onClick = { appendToExpression(expression, "1") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "1", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, "2") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "2", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, "3") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "3", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, " * ") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "*", color = Color.Black) + } + } + + Row( + modifier = Modifier.fillMaxWidth(), + horizontalArrangement = Arrangement.SpaceEvenly + ) { + Button( + onClick = { appendDotToExpression(expression) }, + modifier = Modifier.width(70.dp) + ) { + Text(text = ".", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, "0") }, + modifier = Modifier.width(75.dp) + ) { + Text(text = "0", color = Color.Black) + } + Button( + onClick = { viewModel.calculateResult(expression) }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "=", color = Color.Black) + } + Button( + onClick = { appendToExpression(expression, " / ") }, + modifier = Modifier.width(70.dp) + ) { + Text(text = "/", color = Color.Black) + } + } + // Add more rows of buttons for other numbers and operators + + Row(modifier = Modifier.fillMaxWidth(), horizontalArrangement = Arrangement.Center) { + Button( + onClick = { clearExpression(expression) }, + modifier = Modifier.width(145.dp) + ) { + Text(text = "Clear", color = Color.Black) + } + } + } + } +} +} + +fun erase(expression: MutableState) { + // erase last char from given expression + if (expression.value.isNotEmpty()){ + if(expression.value.last() == " ".last()){ + expression.value = expression.value.dropLast(3) + } else { + expression.value = expression.value.dropLast(1) + } + } +} + +fun appendToExpression(expression: MutableState, text: String) { + // Append the given text to the expression + if (text == " ) " || text == " ( ") { + if(expression.value.isNotEmpty()){ + if (expression.value.last() == ' ') { + expression.value = expression.value.substring(0, expression.value.length - 1) + text + } else { + expression.value += text + } + }else{ + expression.value += text + } + } else if (expression.value.isNotEmpty() && expression.value.last() == ' ' && text.toDoubleOrNull() == null) { + if (expression.value.substring(expression.value.length - 2) == ") " || expression.value.substring(expression.value.length - 2) == "( ") { + expression.value = expression.value.substring(0, expression.value.length - 1) + text + } else { + expression.value = expression.value.substring(0, expression.value.length - 3) + text + } + } else { + expression.value += text + if (expression.value.isNotEmpty() && expression.value.first() == ' ') { + expression.value = "" + } + } +} + + +fun appendDotToExpression(expression: MutableState) { + // Append the given text to the expression + val lastWord = expression.value.trim().split(" ").last() + if(lastWord.toDoubleOrNull() != null || lastWord.isEmpty()){ + if(!lastWord.contains(".")){ + expression.value += "." + } + } +} + + +fun clearExpression(expression: MutableState) { + // Clear the expression + expression.value = "" +} diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/screens/FunctionListScreen.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/screens/FunctionListScreen.kt new file mode 100644 index 0000000..01f9ea8 --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/screens/FunctionListScreen.kt @@ -0,0 +1,72 @@ +package com.pwc.calculatorv2.presentation.screens + +import android.content.Context +import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.foundation.layout.fillMaxWidth +import androidx.compose.foundation.lazy.LazyColumn +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.ui.Alignment +import androidx.compose.ui.Modifier +import androidx.compose.ui.unit.dp +import com.pwc.calculatorv2.data.viewmodels.CalculatorViewModel +import java.text.SimpleDateFormat + +@Composable +fun FunctionListScreen( + viewModel: CalculatorViewModel, +) { + + val calculatorViewState = viewModel.calculatorViewState.collectAsState() + val dateFormat = SimpleDateFormat("dd-MM-yyyy HH:mm") + + + Column() { + Row() { + Button(onClick = { viewModel.getFunctionList() }) { + Text(text = "ALL") + } + Button(onClick = { viewModel.getPlusFunctionList() }) { + Text(text = "+") + } + Button(onClick = { viewModel.getMinusFunctionList() }) { + Text(text = "-") + } + Button(onClick = { viewModel.getPerFunctionList() }) { + Text(text = "*") + } + Button(onClick = { viewModel.getDivideFunctionList() }) { + Text(text = "/") + } + } + LazyColumn( + modifier = Modifier + .fillMaxSize(), + verticalArrangement = Arrangement.spacedBy(8.dp) + ) { + calculatorViewState.value.functionList?.forEach() { response -> + item { + Row( + horizontalArrangement = Arrangement.SpaceBetween, + verticalAlignment = Alignment.CenterVertically, + modifier = Modifier + .fillMaxWidth() + ) { + Text(text = response.function) + Text(text = dateFormat.format(response.date)) + Button(onClick = {viewModel.removeFunction(response) }) { + Text(text = "X") + } + } + } + } + } + + } + +} \ No newline at end of file diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/screens/WipeScreen.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/screens/WipeScreen.kt new file mode 100644 index 0000000..45810ad --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/screens/WipeScreen.kt @@ -0,0 +1,31 @@ +package com.pwc.calculatorv2.presentation.screens + +import androidx.compose.foundation.layout.Column +import androidx.compose.material3.Button +import androidx.compose.material3.Text +import androidx.compose.runtime.Composable +import com.pwc.calculatorv2.data.viewmodels.CalculatorViewModel + +@Composable +fun WipeScreen(viewModel: CalculatorViewModel) { + + Column() { + Button(onClick = { viewModel.wipeFunctionList()}) { + Text(text = "Wipe") + } + Button(onClick = { viewModel.wipePlusFunctionList()}) { + Text(text = "Wipe +") + } + Button(onClick = { viewModel.wipeMinusFunctionList()}) { + Text(text = "Wipe -") + } + Button(onClick = { viewModel.wipePerFunctionList()}) { + Text(text = "Wipe *") + } + Button(onClick = { viewModel.wipeDividedFunctionList()}) { + Text(text = "Wipe /") + } + } + + +} diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Color.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Color.kt new file mode 100644 index 0000000..47ec008 --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Color.kt @@ -0,0 +1,11 @@ +package com.pwc.calculatorv2.presentation.ui.theme + +import androidx.compose.ui.graphics.Color + +val Purple80 = Color(0xFFD0BCFF) +val PurpleGrey80 = Color(0xFFCCC2DC) +val Pink80 = Color(0xFFEFB8C8) + +val Purple40 = Color(0xFF6650a4) +val PurpleGrey40 = Color(0xFF625b71) +val Pink40 = Color(0xFF7D5260) \ No newline at end of file diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Theme.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Theme.kt new file mode 100644 index 0000000..5568c46 --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Theme.kt @@ -0,0 +1,70 @@ +package com.pwc.calculatorv2.presentation.ui.theme + +import android.app.Activity +import android.os.Build +import androidx.compose.foundation.isSystemInDarkTheme +import androidx.compose.material3.MaterialTheme +import androidx.compose.material3.darkColorScheme +import androidx.compose.material3.dynamicDarkColorScheme +import androidx.compose.material3.dynamicLightColorScheme +import androidx.compose.material3.lightColorScheme +import androidx.compose.runtime.Composable +import androidx.compose.runtime.SideEffect +import androidx.compose.ui.graphics.toArgb +import androidx.compose.ui.platform.LocalContext +import androidx.compose.ui.platform.LocalView +import androidx.core.view.WindowCompat + +private val DarkColorScheme = darkColorScheme( + primary = Purple80, + secondary = PurpleGrey80, + tertiary = Pink80 +) + +private val LightColorScheme = lightColorScheme( + primary = Purple40, + secondary = PurpleGrey40, + tertiary = Pink40 + + /* Other default colors to override + background = Color(0xFFFFFBFE), + surface = Color(0xFFFFFBFE), + onPrimary = Color.White, + onSecondary = Color.White, + onTertiary = Color.White, + onBackground = Color(0xFF1C1B1F), + onSurface = Color(0xFF1C1B1F), + */ +) + +@Composable +fun CalculatorV2Theme( + darkTheme: Boolean = isSystemInDarkTheme(), + // Dynamic color is available on Android 12+ + dynamicColor: Boolean = true, + content: @Composable () -> Unit +) { + val colorScheme = when { + dynamicColor && Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> { + val context = LocalContext.current + if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context) + } + + darkTheme -> DarkColorScheme + else -> LightColorScheme + } + val view = LocalView.current + if (!view.isInEditMode) { + SideEffect { + val window = (view.context as Activity).window + window.statusBarColor = colorScheme.primary.toArgb() + WindowCompat.getInsetsController(window, view).isAppearanceLightStatusBars = darkTheme + } + } + + MaterialTheme( + colorScheme = colorScheme, + typography = Typography, + content = content + ) +} \ No newline at end of file diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Type.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Type.kt new file mode 100644 index 0000000..424eda5 --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/ui/theme/Type.kt @@ -0,0 +1,34 @@ +package com.pwc.calculatorv2.presentation.ui.theme + +import androidx.compose.material3.Typography +import androidx.compose.ui.text.TextStyle +import androidx.compose.ui.text.font.FontFamily +import androidx.compose.ui.text.font.FontWeight +import androidx.compose.ui.unit.sp + +// Set of Material typography styles to start with +val Typography = Typography( + bodyLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 16.sp, + lineHeight = 24.sp, + letterSpacing = 0.5.sp + ) + /* Other default text styles to override + titleLarge = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Normal, + fontSize = 22.sp, + lineHeight = 28.sp, + letterSpacing = 0.sp + ), + labelSmall = TextStyle( + fontFamily = FontFamily.Default, + fontWeight = FontWeight.Medium, + fontSize = 11.sp, + lineHeight = 16.sp, + letterSpacing = 0.5.sp + ) + */ +) \ No newline at end of file diff --git a/app/src/main/java/com/pwc/calculatorv2/presentation/ui/widgets/BottomNav.kt b/app/src/main/java/com/pwc/calculatorv2/presentation/ui/widgets/BottomNav.kt new file mode 100644 index 0000000..1ba2d4c --- /dev/null +++ b/app/src/main/java/com/pwc/calculatorv2/presentation/ui/widgets/BottomNav.kt @@ -0,0 +1,32 @@ +package com.pwc.calculatorv2.presentation.ui.widgets + +import androidx.compose.runtime.Composable +import androidx.compose.material.BottomNavigation +import androidx.compose.material.Button +import androidx.compose.material.Text +import androidx.compose.material.TopAppBar +import androidx.navigation.NavController +import com.pwc.calculatorv2.presentation.navigation.AppScreens + +@Composable +fun BottomNav(navController: NavController) { + BottomNavigation( + + ) { + Button(onClick = { + navController.navigate(AppScreens.CalculatorScreen.route) + }) { + Text(text = "Calculator") + } + Button(onClick = { + navController.navigate(AppScreens.FunctionListScreen.route) + }) { + Text(text = "History") + } + Button(onClick = { + navController.navigate(AppScreens.WipeScreen.route) + }) { + Text(text = "Wipe") + } + } +} -- cgit v1.2.3-54-g00ecf