diff --git a/app/src/main/java/com/menagerie/ophelia/MainActivity.kt b/app/src/main/java/com/menagerie/ophelia/MainActivity.kt index 6db5332..6e8eb90 100644 --- a/app/src/main/java/com/menagerie/ophelia/MainActivity.kt +++ b/app/src/main/java/com/menagerie/ophelia/MainActivity.kt @@ -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( + 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 ( + "Quoth the raven ___", + "nevermore", + Difficulty.MEDIUM + ) + private val question2 = Question ( + "The sky is green. True or false", + false, + Difficulty.EASY + ) + private val question3 = Question ( + "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() + } +} \ No newline at end of file