|
| 1 | +package com.byteutility.dev.leetcode.plus.ui.targetset |
| 2 | + |
| 3 | +import androidx.compose.foundation.background |
| 4 | +import androidx.compose.foundation.clickable |
| 5 | +import androidx.compose.foundation.layout.Arrangement |
| 6 | +import androidx.compose.foundation.layout.Column |
| 7 | +import androidx.compose.foundation.layout.Row |
| 8 | +import androidx.compose.foundation.layout.Spacer |
| 9 | +import androidx.compose.foundation.layout.fillMaxWidth |
| 10 | +import androidx.compose.foundation.layout.height |
| 11 | +import androidx.compose.foundation.layout.padding |
| 12 | +import androidx.compose.foundation.lazy.LazyColumn |
| 13 | +import androidx.compose.foundation.shape.RoundedCornerShape |
| 14 | +import androidx.compose.material3.Button |
| 15 | +import androidx.compose.material3.Card |
| 16 | +import androidx.compose.material3.CardDefaults |
| 17 | +import androidx.compose.material3.Checkbox |
| 18 | +import androidx.compose.material3.ExperimentalMaterial3Api |
| 19 | +import androidx.compose.material3.MaterialTheme |
| 20 | +import androidx.compose.material3.Scaffold |
| 21 | +import androidx.compose.material3.Text |
| 22 | +import androidx.compose.material3.TopAppBar |
| 23 | +import androidx.compose.runtime.Composable |
| 24 | +import androidx.compose.runtime.getValue |
| 25 | +import androidx.compose.runtime.mutableStateOf |
| 26 | +import androidx.compose.runtime.remember |
| 27 | +import androidx.compose.runtime.setValue |
| 28 | +import androidx.compose.ui.Modifier |
| 29 | +import androidx.compose.ui.graphics.Color |
| 30 | +import androidx.compose.ui.tooling.preview.Preview |
| 31 | +import androidx.compose.ui.unit.dp |
| 32 | +import androidx.hilt.navigation.compose.hiltViewModel |
| 33 | +import com.byteutility.dev.leetcode.plus.data.model.LeetCodeProblem |
| 34 | + |
| 35 | +@OptIn(ExperimentalMaterial3Api::class) |
| 36 | +@Composable |
| 37 | +fun SetWeeklyTargetScreen() { |
| 38 | + val viewModel: SetWeeklyTargetViewModel = hiltViewModel() |
| 39 | + viewModel.getProblemsByLimit(5L) |
| 40 | + val problems = remember { getProblemsFromApi() } |
| 41 | + Scaffold( |
| 42 | + topBar = { |
| 43 | + TopAppBar(title = { Text(text = "Set Weekly Goals") }) |
| 44 | + }, |
| 45 | + ) { innerPadding -> |
| 46 | + ProblemSelection(Modifier.padding(innerPadding), problems = problems) { selectedProblems -> |
| 47 | + println("Confirmed Problems: $selectedProblems") |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +@Composable |
| 53 | +fun ProblemSelection(modifier: Modifier = Modifier, problems: List<LeetCodeProblem>, onConfirm: (List<LeetCodeProblem>) -> Unit) { |
| 54 | + var selectedProblems by remember { mutableStateOf<List<LeetCodeProblem>>(emptyList()) } |
| 55 | + |
| 56 | + Column(modifier = Modifier.background(Color.White).padding(16.dp)) { |
| 57 | + LazyColumn( |
| 58 | + modifier = Modifier |
| 59 | + .weight(1f) |
| 60 | + .fillMaxWidth() |
| 61 | + .then(modifier), |
| 62 | + verticalArrangement = Arrangement.spacedBy(8.dp) |
| 63 | + ) { |
| 64 | + items(problems.size) { index -> |
| 65 | + val problem = problems[index] |
| 66 | + ProblemItem( |
| 67 | + problem = problem, |
| 68 | + isSelected = selectedProblems.contains(problem), |
| 69 | + onProblemSelected = { selected -> |
| 70 | + if (selectedProblems.size < 7 || selectedProblems.contains(problem)) { |
| 71 | + selectedProblems = if (selected) { |
| 72 | + selectedProblems + problem |
| 73 | + } else { |
| 74 | + selectedProblems - problem |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + ) |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + Spacer(modifier = Modifier.height(16.dp)) |
| 83 | + |
| 84 | + Button( |
| 85 | + onClick = { onConfirm(selectedProblems) }, |
| 86 | + enabled = selectedProblems.size == 7, |
| 87 | + modifier = Modifier.fillMaxWidth() |
| 88 | + ) { |
| 89 | + Text("Confirm") |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +@Composable |
| 95 | +fun ProblemItem( |
| 96 | + problem: LeetCodeProblem, |
| 97 | + isSelected: Boolean, |
| 98 | + onProblemSelected: (Boolean) -> Unit |
| 99 | +) { |
| 100 | + val backgroundColor: Color = when(problem.difficulty) { |
| 101 | + "Easy" -> Color(0xFFE0F7FA) |
| 102 | + "Medium" -> Color(0xFFFFF9C4) |
| 103 | + "Hard" -> Color(0xFFFFCDD2) |
| 104 | + else -> Color(0xFFE0F7FA) |
| 105 | + } |
| 106 | + Card( |
| 107 | + shape = RoundedCornerShape(16.dp), // Circular border |
| 108 | + elevation = CardDefaults.cardElevation(4.dp) |
| 109 | + ) { |
| 110 | + Row( |
| 111 | + modifier = Modifier |
| 112 | + .fillMaxWidth() |
| 113 | + .background(backgroundColor) |
| 114 | + .clickable { onProblemSelected(!isSelected) } |
| 115 | + .padding(16.dp), |
| 116 | + horizontalArrangement = Arrangement.SpaceBetween |
| 117 | + ) { |
| 118 | + Column(modifier = Modifier.fillMaxWidth(0.85f)) { |
| 119 | + Text(text = problem.title, style = MaterialTheme.typography.bodyLarge) |
| 120 | + Text(text = "Tag: ${problem.tag}", style = MaterialTheme.typography.bodySmall) |
| 121 | + Text(text = "Difficulty: ${problem.difficulty}", style = MaterialTheme.typography.bodySmall) |
| 122 | + } |
| 123 | + Checkbox( |
| 124 | + checked = isSelected, |
| 125 | + onCheckedChange = { onProblemSelected(it) } |
| 126 | + ) |
| 127 | + } |
| 128 | + } |
| 129 | +} |
| 130 | + |
| 131 | +// Dummy function to simulate API call |
| 132 | +fun getProblemsFromApi(): List<LeetCodeProblem> { |
| 133 | + return listOf( |
| 134 | + LeetCodeProblem("Two Sum", "Easy", "Array"), |
| 135 | + LeetCodeProblem("Binary Tree Level Order Traversal", "Medium", "Tree"), |
| 136 | + LeetCodeProblem("Longest Substring Without Repeating Characters", "Medium", "String"), |
| 137 | + LeetCodeProblem("Median of Two Sorted Arrays", "Hard", "Array"), |
| 138 | + LeetCodeProblem("Search in Rotated Sorted Array", "Medium", "Binary Search"), |
| 139 | + LeetCodeProblem("Longest Palindromic Substring", "Medium", "String"), |
| 140 | + LeetCodeProblem("Valid Parentheses", "Easy", "Stack"), |
| 141 | + LeetCodeProblem("Merge Intervals", "Medium", "Sorting"), |
| 142 | + LeetCodeProblem("Word Ladder", "Hard", "Graph") |
| 143 | + ) |
| 144 | +} |
| 145 | + |
| 146 | +@OptIn(ExperimentalMaterial3Api::class) |
| 147 | +@Composable |
| 148 | +@Preview |
| 149 | +fun ProblemSelectionApp() { |
| 150 | + val problems = remember { getProblemsFromApi() } |
| 151 | + Scaffold( |
| 152 | + topBar = { |
| 153 | + TopAppBar(title = { Text(text = "Set Weekly Goals") }) |
| 154 | + }, |
| 155 | + ) { innerPadding -> |
| 156 | + ProblemSelection(Modifier.padding(innerPadding), problems = problems) { selectedProblems -> |
| 157 | + println("Confirmed Problems: $selectedProblems") |
| 158 | + } |
| 159 | + } |
| 160 | +} |
0 commit comments