Created
July 8, 2021 08:30
-
-
Save AdrianoCelentano/897c9b55b3d585b2426728553aac2dd4 to your computer and use it in GitHub Desktop.
TODO
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.androidvsios | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.animation.AnimatedVisibility | |
import androidx.compose.animation.ExperimentalAnimationApi | |
import androidx.compose.foundation.Image | |
import androidx.compose.foundation.clickable | |
import androidx.compose.foundation.layout.* | |
import androidx.compose.foundation.lazy.LazyColumn | |
import androidx.compose.foundation.lazy.items | |
import androidx.compose.material.* | |
import androidx.compose.material.icons.Icons | |
import androidx.compose.material.icons.filled.Add | |
import androidx.compose.runtime.Composable | |
import androidx.compose.runtime.mutableStateListOf | |
import androidx.compose.runtime.remember | |
import androidx.compose.runtime.snapshots.SnapshotStateList | |
import androidx.compose.ui.Alignment | |
import androidx.compose.ui.Modifier | |
import androidx.compose.ui.graphics.Color | |
import androidx.compose.ui.graphics.ColorFilter | |
import androidx.compose.ui.unit.dp | |
import com.example.androidvsios.ui.theme.AndroidvsIOSTheme | |
@OptIn(ExperimentalAnimationApi::class) | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
window.statusBarColor = android.graphics.Color.parseColor("#FE9F71") | |
setContent { | |
val todosState = remember { initialTodos() } | |
AndroidvsIOSTheme { | |
Scaffold( | |
topBar = { | |
AddingTopAppBar( | |
onClick = { addItem(todosState) } | |
) | |
}, | |
content = { | |
LazyColumn { | |
items(todosState) { todo -> | |
ListItem( | |
todo = todo, | |
onClick = { expandItem(todo, todosState) } | |
) | |
} | |
} | |
} | |
) | |
} | |
} | |
} | |
@Composable | |
private fun AddingTopAppBar( | |
onClick: () -> Unit | |
) { | |
TopAppBar( | |
backgroundColor = Color(0xFFFE9F71) | |
) { | |
Spacer(Modifier.weight(1f)) | |
Text( | |
modifier = Modifier.align(Alignment.CenterVertically), | |
text = "TO DO", | |
color = Color.White, | |
style = MaterialTheme.typography.h4 | |
) | |
Spacer(Modifier.weight(1f)) | |
Image( | |
modifier = Modifier | |
.padding(end = 8.dp) | |
.align(Alignment.CenterVertically) | |
.clickable(onClick = onClick), | |
imageVector = Icons.Filled.Add, | |
contentDescription = "add", | |
colorFilter = ColorFilter.tint(Color.White) | |
) | |
} | |
} | |
@Composable | |
private fun ListItem( | |
todo: Todo, | |
onClick: () -> Unit | |
) { | |
Surface( | |
Modifier | |
.clickable(onClick = onClick) | |
.fillMaxWidth() | |
.padding(start = 16.dp, end = 16.dp, top = 16.dp), | |
color = todo.color, | |
elevation = 4.dp | |
) { | |
Column( | |
modifier = Modifier.padding(16.dp) | |
) { | |
Text( | |
text = todo.title, | |
color = Color.White, | |
style = MaterialTheme.typography.body1 | |
) | |
AnimatedVisibility(visible = todo.expanded) { | |
Text( | |
modifier = Modifier.padding(top = 8.dp), | |
text = todo.description, | |
color = Color.White, | |
style = MaterialTheme.typography.caption | |
) | |
} | |
} | |
} | |
} | |
private fun expandItem( | |
currentItem: Todo, | |
listItems: SnapshotStateList<Todo> | |
) { | |
val newList: List<Todo> = listItems.map { item -> | |
if (currentItem != item) item | |
else item.copy(expanded = !item.expanded) | |
} | |
listItems.clear() | |
listItems.addAll(newList) | |
} | |
private fun addItem(listItems: SnapshotStateList<Todo>) { | |
listItems.add( | |
Todo( | |
title = "NEUE AUFGABE", | |
description = "Es gibt viel zu tun", | |
expanded = false, | |
color = Color(0xFFbc8cbe) | |
) | |
) | |
} | |
} | |
fun initialTodos() = mutableStateListOf( | |
Todo( | |
title = "PUTZEN", | |
description = "Am besten mit dem Staubsauger", | |
expanded = false, | |
color = Color(0xFFF48786L) | |
), | |
Todo( | |
title = "EINKAUFEN", | |
description = "Essen und Trinken", | |
expanded = false, | |
color = Color(0xFFF39AC0) | |
) | |
) | |
data class Todo( | |
val title: String, | |
val description: String, | |
val expanded: Boolean, | |
val color: Color | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment