Skip to content

Commit

Permalink
Merge pull request #212 from myofficework000/lesson_16_state_management
Browse files Browse the repository at this point in the history
lesson 16 state management
  • Loading branch information
myofficework000 authored Dec 1, 2023
2 parents 585abdc + 928c17b commit 75650b3
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.example.jetpack_compose_all_in_one.lessons.lesson_16

import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
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.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
@Preview(showBackground = true)
fun Lesson_16() {
var t1 by rememberSaveable { mutableStateOf("T1") }
var t2 by rememberSaveable { mutableStateOf("T2") }
var t3 by rememberSaveable { mutableStateOf("T3") }
var visible by rememberSaveable { mutableStateOf(true) }
Column(
Modifier
.fillMaxSize()
.height(300.dp)
) {
Column(
modifier = Modifier
.fillMaxWidth()
.background(Color.Cyan)
.padding(10.dp)
) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.height(80.dp)
) {
Box(Modifier.fillMaxSize()) {
if (visible) {
Text(
text = t1,
modifier = Modifier.align(Alignment.Center),
style = TextStyle(fontWeight = FontWeight.Bold, fontSize = 30.sp),
)
}
}
}
Card(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.height(80.dp)
) {
Box(Modifier.fillMaxSize()) {
if (visible) {
Text(
text = t2,
modifier = Modifier.align(Alignment.Center),
style = TextStyle(fontWeight = FontWeight.Bold, fontSize = 30.sp)
)
}
}
}
Card(
modifier = Modifier
.fillMaxWidth()
.padding(10.dp)
.height(80.dp)
) {
Box(Modifier.fillMaxSize()) {
if (visible) {
Text(
text = t3,
modifier = Modifier.align(Alignment.Center),
style = TextStyle(fontWeight = FontWeight.Bold, fontSize = 30.sp)
)
}
}
}
}

Spacer(modifier = Modifier.height(30.dp))

Box(
modifier = Modifier
.fillMaxWidth()
.padding(16.dp)
) {
Button(onClick = { t1 = "Text 1" }, Modifier.align(Alignment.TopStart)) {
Text(text = "button 1")
}
Button(onClick = { t2 = "Text 2" }, Modifier.align(Alignment.TopCenter)) {
Text(text = "button 2")
}
Button(onClick = { t3 = "Text 3" }, Modifier.align(Alignment.TopEnd)) {
Text(text = "button 3")
}
}

Spacer(modifier = Modifier.height(30.dp))

Box(
modifier = Modifier
.fillMaxWidth()
.padding(32.dp)
) {
Button(onClick = { visible = false }, Modifier.align(Alignment.TopStart)) {
Text(text = "Hide")
}
Button(onClick = { visible = true }, Modifier.align(Alignment.TopEnd)) {
Text(text = "Show")
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ import com.example.jetpack_compose_all_in_one.lessons.lesson_12.Lesson_12
import com.example.jetpack_compose_all_in_one.lessons.lesson_13.Lesson_13
import com.example.jetpack_compose_all_in_one.lessons.lesson_14.Lesson_14
import com.example.jetpack_compose_all_in_one.lessons.lesson_15.Lesson_15
import com.example.jetpack_compose_all_in_one.lessons.lesson_16.Lesson_16
import com.example.jetpack_compose_all_in_one.lessons.lesson_2.Lesson_2_Chapter_2_Screen
import com.example.jetpack_compose_all_in_one.lessons.lesson_2.Lesson_2_Chapter_4_Image
import com.example.jetpack_compose_all_in_one.lessons.lesson_2.Lesson_2_Chapter_5_Progressbar
Expand Down Expand Up @@ -396,6 +397,10 @@ fun MainContainerOfApp(
Lesson_15()
}

composable(NavDes.Lesson16.route()){
Lesson_16()
}

composable(NavDes.L6Chapter1.route()) {
Lesson_6_ThemeChange()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ object NavConstants {
const val LESSON_15 = "LESSON_15"
const val LESSON_15_ABOUT = "Lesson 15: Custom Radio Buttons"

const val LESSON_16 = "LESSON_16"
const val LESSON_16_ABOUT = "Lesson 16: State and Visibility"

const val QUOTE_2 = "quote2"
const val QUOTE_2_ABOUT = "Swipe Quotes"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESS
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_14_DROP_DOWN_MENU_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_15
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_15_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_16
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_16_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_1_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_2_CHAPTER_1
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.LESSON_2_CHAPTER_1_ABOUT
Expand Down Expand Up @@ -274,6 +276,9 @@ sealed class NavDes(val data: INavigationDrawerItem, val customAppBarStringId: I
object Lesson15 :
NavDes(NavigationDrawerData(LESSON_15, LESSON_15_ABOUT))

object Lesson16 :
NavDes(NavigationDrawerData(LESSON_16, LESSON_16_ABOUT))

object Lesson14DropDownMenu :
NavDes(NavigationDrawerData(LESSON_14_DROP_DOWN_MENU, LESSON_14_DROP_DOWN_MENU_ABOUT))

Expand Down Expand Up @@ -470,7 +475,8 @@ sealed class NavDes(val data: INavigationDrawerItem, val customAppBarStringId: I
Lesson12WebView,
Lesson13Localization,
Lesson14DropDownMenu,
Lesson15
Lesson15,
Lesson16
), LESSONS
)
)
Expand Down

0 comments on commit 75650b3

Please sign in to comment.