Advertisement
Samuel_Berkat_Hulu

AffirmationMainActifity

Jun 25th, 2024
510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.11 KB | None | 0 0
  1. package com.example.affirmations
  2.  
  3. import android.os.Bundle
  4. import androidx.activity.ComponentActivity
  5. import androidx.activity.compose.setContent
  6. import androidx.compose.foundation.Image
  7. import androidx.compose.foundation.layout.Column
  8. import androidx.compose.foundation.layout.fillMaxSize
  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.lazy.items
  14. import androidx.compose.material3.Card
  15. import androidx.compose.material3.MaterialTheme
  16. import androidx.compose.material3.Surface
  17. import androidx.compose.material3.Text
  18. import androidx.compose.runtime.Composable
  19. import androidx.compose.ui.Modifier
  20. import androidx.compose.ui.layout.ContentScale
  21. import androidx.compose.ui.platform.LocalContext
  22. import androidx.compose.ui.res.painterResource
  23. import androidx.compose.ui.res.stringResource
  24. import androidx.compose.ui.tooling.preview.Preview
  25. import androidx.compose.ui.unit.dp
  26. import com.example.affirmations.data.Datasource
  27. import com.example.affirmations.model.Affirmation
  28. import com.example.affirmations.ui.theme.AffirmationsTheme
  29.  
  30. class MainActivity : ComponentActivity() {
  31.  
  32.     override fun onCreate(savedInstanceState: Bundle?) {
  33.         super.onCreate(savedInstanceState)
  34.         setContent {
  35.             AffirmationsTheme {
  36.                 // A surface container using the 'background' color from the theme
  37.                 Surface(
  38.                     modifier = Modifier.fillMaxSize(),
  39.                     color = MaterialTheme.colorScheme.background
  40.                 ) {
  41.                     AffirmationsApp()
  42.                 }
  43.             }
  44.         }
  45.     }
  46. }
  47.  
  48. @Composable
  49. fun AffirmationsApp() {
  50.     AffirmationList(
  51.         affirmationList = Datasource().loadAffirmations(),
  52.     )
  53. }
  54.  
  55. @Composable
  56. fun AffirmationList(affirmationList: List<Affirmation>, modifier: Modifier = Modifier) {
  57.     LazyColumn(modifier = modifier) {
  58.         items(affirmationList) { affirmation ->
  59.             AffirmationCard(
  60.                 affirmation = affirmation,
  61.                 modifier = Modifier.padding(8.dp)
  62.             )
  63.         }
  64.     }
  65. }
  66.  
  67. @Composable
  68. fun AffirmationCard(affirmation: Affirmation, modifier: Modifier = Modifier) {
  69.     Card(modifier = modifier) {
  70.         Column {
  71.             Image(
  72.                 painter = painterResource(affirmation.imageResourceId),
  73.                 contentDescription = stringResource(affirmation.stringResourceId),
  74.                 modifier = Modifier
  75.                     .fillMaxWidth()
  76.                     .height(194.dp),
  77.                 contentScale = ContentScale.Crop
  78.             )
  79.             Text(
  80.                 text = LocalContext.current.getString(affirmation.stringResourceId),
  81.                 modifier = Modifier.padding(16.dp),
  82.                 style = MaterialTheme.typography.headlineSmall
  83.             )
  84.         }
  85.     }
  86. }
  87.  
  88. @Preview
  89. @Composable
  90. private fun AffirmationCardPreview() {
  91.     AffirmationCard(Affirmation(R.string.affirmation1, R.drawable.image1))
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement