Compare commits

...
Sign in to create a new pull request.

4 commits

Author SHA1 Message Date
391b58565e Scope 2023-10-08 23:12:14 -04:00
17fe8e16b2 Interfaces Demo 2023-10-08 23:01:19 -04:00
234bfc1e54 Singletons and Extensions Demo 2023-10-08 22:53:50 -04:00
59b03aa5a6 Classes Demo 2023-10-08 22:33:56 -04:00

View file

@ -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()
}
}