-
-
Save indexer/ae67e67eccd82d5ecaa05e9f64187884 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Define the reducer function to update the state of the application based on user intents | |
fun reduce(state: AppState, intent: UserIntent): AppState { | |
return when (intent) { | |
is UserIntent.AddStudent -> addStudent(state, intent.student) | |
is UserIntent.EditStudent -> editStudent(state, intent.student) | |
is UserIntent.DeleteStudent -> deleteStudent(state, intent.student) | |
} | |
} | |
// Define the data model for a student | |
data class Student(val name: String, val age: Int, val grade: String) | |
// Define the state of the application | |
data class AppState(val students: List<Student>) | |
// Define the actions that the user can perform | |
sealed class UserIntent { | |
data class AddStudent(val student: Student) : UserIntent() | |
data class EditStudent(val student: Student) : UserIntent() | |
data class DeleteStudent(val student: Student) : UserIntent() | |
} | |
// Define the UI component to display the list of | |
// students and handle user inputs | |
class StudentListView { | |
lateinit var presenter: StudentListPresenter | |
fun render(state: AppState) { | |
// Render the list of students on the screen | |
} | |
fun handleUserInput(intent: UserIntent) { | |
// Pass the user intent to the presenter to update | |
// the state of the application | |
presenter.handleUserIntent(intent) | |
} | |
} | |
// Define the presenter to handle user inputs and | |
// update the state of the application | |
class StudentListPresenter(private val reducer: (AppState, UserIntent) | |
-> AppState) { | |
lateinit var view: StudentListView | |
var state = AppState(emptyList()) | |
fun handleUserIntent(intent: UserIntent) { | |
state = reducer(state, intent) | |
view.render(state) | |
} | |
} | |
// Usage example | |
fun main() { | |
val presenter = StudentListPresenter(::reduce) | |
val view = StudentListView() | |
presenter.view = view | |
view.presenter = presenter | |
val newStudent = Student("Ye Mon", 18, "F-") | |
view.handleUserInput(UserIntent.AddStudent(newStudent)) | |
val updatedStudent = Student("Nyan", 19, "A+") | |
view.handleUserInput(UserIntent.EditStudent(updatedStudent)) | |
val deletedStudent = Student("Thet", 20, "A") | |
view.handleUserInput(UserIntent.DeleteStudent(deletedStudent)) | |
} | |
// Define separate functions to handle each type of user intent | |
fun addStudent(state: AppState, student: Student): AppState { | |
return state.copy(students = state.students + student) | |
} | |
fun editStudent(state: AppState, student: Student): AppState { | |
val updatedStudents = state.students.map { if (it.name == student.name) student else it } | |
return state.copy(students = updatedStudents) | |
} | |
fun deleteStudent(state: AppState, student: Student): AppState { | |
val filteredStudents = state.students.filterNot { it.name == student.name } | |
return state.copy(students = filteredStudents) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment