154 lines
4.2 KiB
Kotlin
154 lines
4.2 KiB
Kotlin
|
|
package xyz.pixelatedw.recipe
|
||
|
|
|
||
|
|
import android.app.Activity
|
||
|
|
import android.content.Intent
|
||
|
|
import android.content.res.Configuration
|
||
|
|
import android.net.Uri
|
||
|
|
import android.os.Bundle
|
||
|
|
import androidx.activity.ComponentActivity
|
||
|
|
import androidx.activity.compose.setContent
|
||
|
|
import androidx.activity.enableEdgeToEdge
|
||
|
|
import androidx.activity.result.contract.ActivityResultContracts
|
||
|
|
import androidx.compose.foundation.Image
|
||
|
|
import androidx.compose.foundation.layout.Arrangement
|
||
|
|
import androidx.compose.foundation.layout.Box
|
||
|
|
import androidx.compose.foundation.layout.Column
|
||
|
|
import androidx.compose.foundation.layout.PaddingValues
|
||
|
|
import androidx.compose.foundation.layout.Row
|
||
|
|
import androidx.compose.foundation.layout.Spacer
|
||
|
|
import androidx.compose.foundation.layout.fillMaxSize
|
||
|
|
import androidx.compose.foundation.layout.fillMaxWidth
|
||
|
|
import androidx.compose.foundation.layout.padding
|
||
|
|
import androidx.compose.foundation.layout.size
|
||
|
|
import androidx.compose.foundation.layout.width
|
||
|
|
import androidx.compose.foundation.lazy.LazyColumn
|
||
|
|
import androidx.compose.material3.MaterialTheme
|
||
|
|
import androidx.compose.material3.Scaffold
|
||
|
|
import androidx.compose.material3.Surface
|
||
|
|
import androidx.compose.material3.Text
|
||
|
|
import androidx.compose.runtime.Composable
|
||
|
|
import androidx.compose.ui.Modifier
|
||
|
|
import androidx.compose.ui.res.painterResource
|
||
|
|
import androidx.compose.ui.text.TextStyle
|
||
|
|
import androidx.compose.ui.text.style.TextAlign
|
||
|
|
import androidx.compose.ui.tooling.preview.Preview
|
||
|
|
import androidx.compose.ui.unit.Dp
|
||
|
|
import androidx.compose.ui.unit.TextUnit
|
||
|
|
import androidx.compose.ui.unit.TextUnitType
|
||
|
|
import androidx.compose.ui.unit.dp
|
||
|
|
import androidx.compose.ui.unit.em
|
||
|
|
import xyz.pixelatedw.recipe.ui.theme.RecipeTheme
|
||
|
|
import androidx.compose.foundation.lazy.items
|
||
|
|
import androidx.compose.ui.platform.LocalContext
|
||
|
|
import androidx.core.net.toUri
|
||
|
|
|
||
|
|
class MainActivity : ComponentActivity() {
|
||
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||
|
|
super.onCreate(savedInstanceState)
|
||
|
|
|
||
|
|
val recipes = listOf(
|
||
|
|
Recipe(title = "Test", tags = listOf("tag1", "tag2")),
|
||
|
|
Recipe(title = "Actual Recipe", tags = listOf("test"))
|
||
|
|
)
|
||
|
|
|
||
|
|
requestDirLauncher()
|
||
|
|
|
||
|
|
enableEdgeToEdge()
|
||
|
|
setContent {
|
||
|
|
RecipeTheme {
|
||
|
|
Surface {
|
||
|
|
Scaffold(modifier = Modifier.fillMaxSize()) { innerPadding ->
|
||
|
|
RecipeList(innerPadding, recipes)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
private val requestDirLauncher = {
|
||
|
|
val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
|
||
|
|
|
||
|
|
val getContent = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
|
||
|
|
if (result.resultCode == Activity.RESULT_OK) {
|
||
|
|
result.data?.data?.let { uri ->
|
||
|
|
println(uri)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
getContent.launch(intent)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
data class Recipe(val title: String, val tags: List<String>)
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun RecipeList(padding: PaddingValues, recipes: List<Recipe>) {
|
||
|
|
LazyColumn(modifier = Modifier.padding(padding)) {
|
||
|
|
items(recipes) { recipe ->
|
||
|
|
RecipePreview(recipe)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Preview
|
||
|
|
@Composable
|
||
|
|
fun RecipeListPreview() {
|
||
|
|
val recipes = listOf(
|
||
|
|
Recipe(title = "Test", tags = listOf("tag1", "tag2")),
|
||
|
|
Recipe(title = "Actual Recipe", tags = listOf("test"))
|
||
|
|
)
|
||
|
|
RecipeTheme {
|
||
|
|
RecipeList(PaddingValues(), recipes)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Composable
|
||
|
|
fun RecipePreview(recipe: Recipe) {
|
||
|
|
Column(modifier = Modifier.padding(8.dp)) {
|
||
|
|
Row(horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth()) {
|
||
|
|
Image(
|
||
|
|
painter = painterResource(R.drawable.ic_launcher_background),
|
||
|
|
contentDescription = "Recipe image",
|
||
|
|
modifier = Modifier.size(256.dp)
|
||
|
|
)
|
||
|
|
}
|
||
|
|
Row(horizontalArrangement = Arrangement.Center, modifier = Modifier.fillMaxWidth()) {
|
||
|
|
Text(
|
||
|
|
text = recipe.title,
|
||
|
|
modifier = Modifier.fillMaxWidth(),
|
||
|
|
style = TextStyle(
|
||
|
|
textAlign = TextAlign.Center,
|
||
|
|
fontSize = 7.em,
|
||
|
|
)
|
||
|
|
)
|
||
|
|
}
|
||
|
|
Row(horizontalArrangement = Arrangement.End, modifier = Modifier.fillMaxWidth()) {
|
||
|
|
for (tag in recipe.tags) {
|
||
|
|
Text(
|
||
|
|
text = tag,
|
||
|
|
modifier = Modifier.padding(start = 8.dp)
|
||
|
|
)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
@Preview(
|
||
|
|
showBackground = true,
|
||
|
|
name = "Light Mode"
|
||
|
|
)
|
||
|
|
@Preview(
|
||
|
|
uiMode = Configuration.UI_MODE_NIGHT_YES,
|
||
|
|
showBackground = true,
|
||
|
|
name = "Dark Mode"
|
||
|
|
)
|
||
|
|
@Composable
|
||
|
|
fun RecipePreviewPreview() {
|
||
|
|
RecipeTheme {
|
||
|
|
Surface {
|
||
|
|
RecipePreview(Recipe("Test", listOf("test", "test2", "test3")))
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|