Skip to content

Commit

Permalink
Merge pull request #213 from myofficework000/poll_ui_demo
Browse files Browse the repository at this point in the history
added poll ui demo
  • Loading branch information
myofficework000 authored Dec 1, 2023
2 parents 75650b3 + 5d8d51a commit 6bcb102
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.example.jetpack_compose_all_in_one.demos.polls

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.Button
import androidx.compose.material3.RadioButton
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.constraintlayout.compose.ConstraintLayout

@Composable
fun PollScreen(viewModel: QuizViewModel) {
val radioOptions = listOf("Mango","Banana","Apple","Peach")
var selectedOption by remember { mutableStateOf(radioOptions[0]) }
val attemptedTime = viewModel.attemptedTime.observeAsState()

ConstraintLayout(modifier = Modifier.fillMaxSize()) {
val (questionStatement, attemptTime, radioGroup, submitButton) = createRefs()

Text(text = "Question: Question statement here",
modifier = Modifier
.padding(8.dp)
.constrainAs(questionStatement) {
top.linkTo(parent.top)
start.linkTo(parent.start)
},
style = TextStyle(fontWeight = FontWeight.Bold, fontSize = 20.sp)
)

Text(text = "attempted ${attemptedTime.value} times",
modifier = Modifier
.padding(5.dp)
.constrainAs(attemptTime) {
top.linkTo(questionStatement.bottom)
end.linkTo(parent.end)
})

Column(modifier = Modifier.constrainAs(radioGroup){
top.linkTo(attemptTime.bottom, 20.dp)
start.linkTo(parent.start,20.dp)
}) {
radioOptions.forEach { fruitName ->
Row(verticalAlignment = Alignment.CenterVertically) {
RadioButton(
selected = (fruitName == selectedOption),
onClick = { selectedOption = fruitName }
)
Text(
text = fruitName,
modifier = Modifier.padding(start = 8.dp)
)
}
}
}
Button(onClick = { viewModel.submitPoll() }, modifier = Modifier.constrainAs(submitButton){
top.linkTo(radioGroup.bottom,50.dp)
start.linkTo(parent.start)
end.linkTo(parent.end)
}) {
Text("Submit")
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.jetpack_compose_all_in_one.demos.polls

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class QuizViewModel @Inject constructor():ViewModel() {

private var _attemptedTime = MutableLiveData(0)
var attemptedTime:LiveData<Int> = _attemptedTime

fun submitPoll() = _attemptedTime.postValue(_attemptedTime.value!!+1)
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import com.example.jetpack_compose_all_in_one.application_components.content_pro
import com.example.jetpack_compose_all_in_one.demos.currency_converter.presentation.view.CurrencyFromToScreen
import com.example.jetpack_compose_all_in_one.demos.history_of_day.HistoryOfTheDayUI
import com.example.jetpack_compose_all_in_one.demos.news_app.view.NewsScreen
import com.example.jetpack_compose_all_in_one.demos.polls.PollScreen
import com.example.jetpack_compose_all_in_one.features.alarm.AlarmMainUI
import com.example.jetpack_compose_all_in_one.features.chatmodule.ChatViewModel
import com.example.jetpack_compose_all_in_one.features.download_manager.Download
Expand Down Expand Up @@ -302,6 +303,10 @@ fun MainContainerOfApp(
NewsScreen(hiltViewModel())
}

composable(NavDes.polls.route()){
PollScreen(viewModel = hiltViewModel())
}

composable(NavDes.ChatDemoUI.route()) {
val vm = hiltViewModel<ChatViewModel>()
DemoFullChat2(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,9 @@ object NavConstants {
const val TiKTokUI = "TiKTokUI"
const val TiKTokUI_About = "TiKTok"

const val PollUI = "PollUI"
const val PollUI_About = "Poll"

const val CurrencyConverterUI = "CurrencyConverterUI"
const val CurrencyConverterUI_About = "CurrencyConverter"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.PASS
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.PASSWORD_VALIDATION_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.PROFILE_UPDATE
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.PROFILE_UPDATE_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.PollUI
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.PollUI_About
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.QRCODE_SCANNER
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.QRCODE_SCANNER_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.QUOTES
Expand Down Expand Up @@ -422,6 +424,8 @@ sealed class NavDes(val data: INavigationDrawerItem, val customAppBarStringId: I
object currencyConverter : NavDes(NavigationDrawerData(CurrencyConverterUI, CurrencyConverterUI_About))
object youTube : NavDes(NavigationDrawerData(YoutubeUI, YoutubeUI_ABOUT))
object news : NavDes(NavigationDrawerData(NewsUI, NewsUI_ABOUT))

object polls : NavDes(NavigationDrawerData(PollUI, PollUI_About))
object faceBook : NavDes(NavigationDrawerData(FacebookUI, FacebookUI_About))
object historyOfDay : NavDes(NavigationDrawerData(HistoryOfDayUI, HistoryOfDayUI_About))

Expand Down Expand Up @@ -531,7 +535,8 @@ sealed class NavDes(val data: INavigationDrawerItem, val customAppBarStringId: I
tikTok,
currencyConverter,
historyOfDay,
news
news,
polls
), DEMO_UI
)
)
Expand Down

0 comments on commit 6bcb102

Please sign in to comment.