Compare commits
4 commits
master
...
azea/Class
Author | SHA1 | Date | |
---|---|---|---|
391b58565e | |||
17fe8e16b2 | |||
234bfc1e54 | |||
59b03aa5a6 |
1 changed files with 113 additions and 10 deletions
|
@ -24,26 +24,121 @@ import androidx.compose.ui.unit.dp
|
|||
import androidx.compose.ui.unit.sp
|
||||
import com.menagerie.ophelia.ui.theme.OpheliaTheme
|
||||
|
||||
data class Question<T>(
|
||||
val questionText: String,
|
||||
val answer: T,
|
||||
val difficulty: Difficulty
|
||||
)
|
||||
|
||||
enum class Difficulty {
|
||||
EASY, MEDIUM, HARD
|
||||
}
|
||||
|
||||
|
||||
interface ProgressPrintable {
|
||||
val progressText: String
|
||||
fun printProgressBar(): String
|
||||
}
|
||||
|
||||
class Quiz : ProgressPrintable {
|
||||
|
||||
override val progressText: String
|
||||
get() = "$answered of $total answered."
|
||||
|
||||
override fun printProgressBar(): String {
|
||||
var progress = ""
|
||||
repeat(Quiz.answered) { progress += "▓" }
|
||||
repeat(Quiz.total - Quiz.answered) {progress += "▒"}
|
||||
return progress
|
||||
}
|
||||
|
||||
fun printQuiz(): String {
|
||||
var quizText: String = ""
|
||||
question1.let {
|
||||
quizText += it.questionText + "\n"
|
||||
quizText += it.answer +"\n"
|
||||
quizText += it.difficulty.toString() + "\n"
|
||||
}
|
||||
quizText += "\n"
|
||||
question2.let {
|
||||
quizText += it.questionText + "\n"
|
||||
quizText += it.answer.toString() + "\n"
|
||||
quizText += it.difficulty.toString() + "\n"
|
||||
}
|
||||
quizText += "\n"
|
||||
question3.let {
|
||||
quizText += it.questionText + "\n"
|
||||
quizText += it.answer.toString() +"\n"
|
||||
quizText += it.difficulty.toString() + "\n"
|
||||
}
|
||||
return quizText
|
||||
}
|
||||
|
||||
private val question1 = Question<String> (
|
||||
"Quoth the raven ___",
|
||||
"nevermore",
|
||||
Difficulty.MEDIUM
|
||||
)
|
||||
private val question2 = Question<Boolean> (
|
||||
"The sky is green. True or false",
|
||||
false,
|
||||
Difficulty.EASY
|
||||
)
|
||||
private val question3 = Question<Int> (
|
||||
"How many days are there between full moons?",
|
||||
28,
|
||||
Difficulty.HARD
|
||||
)
|
||||
|
||||
//Declaring Progress a "companion" is what lets us directly access total and
|
||||
//answered from Quiz
|
||||
companion object Progress {
|
||||
var total: Int = 10
|
||||
var answered: Int = 5
|
||||
}
|
||||
}
|
||||
|
||||
class MainActivity : ComponentActivity() {
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContent {
|
||||
OpheliaTheme {
|
||||
// A surface container using the 'background' color from the theme
|
||||
Surface(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
color = Color.White
|
||||
) {
|
||||
GreetingImage(
|
||||
message = getString(R.string.main_welcome_message),
|
||||
from = getString(R.string.tagline)
|
||||
)
|
||||
}
|
||||
QuestionGame()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun QuestionGame(modifier: Modifier = Modifier) {
|
||||
|
||||
//just start doing stuff right away with .apply{}
|
||||
val quiz = Quiz().apply {
|
||||
Quiz.total = 15
|
||||
}
|
||||
|
||||
//You can even apply it to a class directly
|
||||
//and since Progress is an "object" or singleton,
|
||||
//it still updates what we display in the preview!
|
||||
Quiz().apply {
|
||||
Quiz.answered = 7
|
||||
}
|
||||
|
||||
Column(
|
||||
modifier = Modifier.fillMaxSize(),
|
||||
) {
|
||||
var text = quiz.printProgressBar()
|
||||
Text(text = text)
|
||||
Text(
|
||||
text = quiz.progressText //Because Progress is a companion, this is accessible, too!
|
||||
)
|
||||
Text(
|
||||
text = quiz.printQuiz()
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
fun GreetingImage(message: String, from: String, modifier: Modifier = Modifier) {
|
||||
val image = painterResource(R.drawable.androidparty)
|
||||
|
@ -89,7 +184,7 @@ fun GreetingText(message: String, from: String, modifier: Modifier = Modifier) {
|
|||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
//@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun BirthdayCardPreview() {
|
||||
OpheliaTheme {
|
||||
|
@ -99,3 +194,11 @@ fun BirthdayCardPreview() {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Preview(showBackground = true)
|
||||
@Composable
|
||||
fun QuestionGamePreview() {
|
||||
OpheliaTheme {
|
||||
QuestionGame()
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue