Skip to content

Commit

Permalink
Create replies using companion helper functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-magda committed Jul 31, 2024
1 parent f10eb8f commit 5c75858
Show file tree
Hide file tree
Showing 18 changed files with 46 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ChoiceStepQuizFormDelegate(
}

override fun createReply(): Reply =
Reply(choices = choicesAdapter.items.map { ChoiceAnswer.Choice(it.isSelected) })
Reply.choice(choices = choicesAdapter.items.map { ChoiceAnswer.Choice(it.isSelected) })

private fun handleSingleChoiceClick(choice: Choice) {
choicesAdapter.items = choicesAdapter.items.map {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,5 +67,5 @@ class SortingStepQuizFormDelegate(
}

override fun createReply(): Reply =
Reply(ordering = optionsAdapter.items.map(SortingOption::id))
Reply.sorting(ordering = optionsAdapter.items.map(SortingOption::id))
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TableStepQuizFormDelegate(

override fun createReply(): Reply =
Reply.table(
answers = tableAdapter.items.map { item ->
choices = tableAdapter.items.map { item ->
ChoiceAnswer.Table(
TableChoiceAnswer(
nameRow = item.titleText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object MathStepQuizConfig : TextStepQuizConfig {
get() = InputType.TYPE_CLASS_TEXT

override fun createReply(inputText: String, markedAsCorrect: Boolean): Reply =
Reply(formula = inputText)
Reply.math(formula = inputText)

override fun getText(reply: Reply): String? =
reply.formula
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ object NumberStepQuizConfig : TextStepQuizConfig {
InputType.TYPE_NUMBER_FLAG_SIGNED

override fun createReply(inputText: String, markedAsCorrect: Boolean): Reply =
Reply(number = inputText)
Reply.number(number = inputText)

override fun getText(reply: Reply): String? =
reply.number
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ object PlainTextStepQuizConfig : TextStepQuizConfig {
get() = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_FLAG_MULTI_LINE

override fun createReply(inputText: String, markedAsCorrect: Boolean): Reply =
Reply(text = inputText, files = emptyList())
Reply.string(text = inputText)

override fun getText(reply: Reply): String? =
reply.text
Expand Down
1 change: 1 addition & 0 deletions config/detekt/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@
<ID>TooManyFunctions:MainActivity.kt$MainActivity : AppCompatActivityAppNavigationContainerReduxView</ID>
<ID>TooManyFunctions:NotificationInteractor.kt$NotificationInteractor</ID>
<ID>TooManyFunctions:ProfileFragment.kt$ProfileFragment : FragmentReduxViewCallbackCallback</ID>
<ID>TooManyFunctions:Reply.kt$Reply$Companion</ID>
<ID>TooManyFunctions:SharedDateFormatter.kt$SharedDateFormatter</ID>
<ID>TooManyFunctions:StepActionDispatcher.kt$StepActionDispatcher : CoroutineActionDispatcher</ID>
<ID>TooManyFunctions:StudyPlanWidgetDelegate.kt$StudyPlanWidgetDelegate</ID>
Expand Down
4 changes: 0 additions & 4 deletions iosHyperskillApp/iosHyperskillApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
2C0146AA28FDF2350083DA9C /* StepQuizCodeFullScreenInputProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C0146A928FDF2350083DA9C /* StepQuizCodeFullScreenInputProtocol.swift */; };
2C023C86285D927A00D2D5A9 /* StepQuizTableAssembly.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C023C85285D927A00D2D5A9 /* StepQuizTableAssembly.swift */; };
2C023C88285D928100D2D5A9 /* StepQuizTableViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C023C87285D928100D2D5A9 /* StepQuizTableViewModel.swift */; };
2C023C8B285DCA2100D2D5A9 /* ReplyExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C023C8A285DCA2100D2D5A9 /* ReplyExtensions.swift */; };
2C023C8D285DCA4300D2D5A9 /* DatasetExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C023C8C285DCA4300D2D5A9 /* DatasetExtensions.swift */; };
2C0409842B863EA600E9CF41 /* Publishers+KeyboardIsVisible.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C0409832B863EA600E9CF41 /* Publishers+KeyboardIsVisible.swift */; };
2C05AC462A0E9EBC0039C7EF /* ProjectSelectionListFeatureViewStateKsExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2C05AC452A0E9EBC0039C7EF /* ProjectSelectionListFeatureViewStateKsExtensions.swift */; };
Expand Down Expand Up @@ -806,7 +805,6 @@
2C0146A928FDF2350083DA9C /* StepQuizCodeFullScreenInputProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StepQuizCodeFullScreenInputProtocol.swift; sourceTree = "<group>"; };
2C023C85285D927A00D2D5A9 /* StepQuizTableAssembly.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StepQuizTableAssembly.swift; sourceTree = "<group>"; };
2C023C87285D928100D2D5A9 /* StepQuizTableViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StepQuizTableViewModel.swift; sourceTree = "<group>"; };
2C023C8A285DCA2100D2D5A9 /* ReplyExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReplyExtensions.swift; sourceTree = "<group>"; };
2C023C8C285DCA4300D2D5A9 /* DatasetExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DatasetExtensions.swift; sourceTree = "<group>"; };
2C0409832B863EA600E9CF41 /* Publishers+KeyboardIsVisible.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Publishers+KeyboardIsVisible.swift"; sourceTree = "<group>"; };
2C05AC452A0E9EBC0039C7EF /* ProjectSelectionListFeatureViewStateKsExtensions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProjectSelectionListFeatureViewStateKsExtensions.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -3016,7 +3014,6 @@
2C96744328883E710091B6C9 /* BlockOptionsExtensions.swift */,
2C406C362A440E8200FA838E /* BuildVariant+Current.swift */,
2C023C8C285DCA4300D2D5A9 /* DatasetExtensions.swift */,
2C023C8A285DCA2100D2D5A9 /* ReplyExtensions.swift */,
2CEB50C5288A92820044F9AB /* StepExtensions.swift */,
2C5261A42993CBF100B4E8F6 /* TopicProgressExtensions.swift */,
);
Expand Down Expand Up @@ -5511,7 +5508,6 @@
E9F655CA2875914200291143 /* StreakCardView.swift in Sources */,
2C9ECBA3284736090015CFD2 /* StepViewDataMapper.swift in Sources */,
2CAE8CF2280525C900E6C83D /* StepView.swift in Sources */,
2C023C8B285DCA2100D2D5A9 /* ReplyExtensions.swift in Sources */,
2C7CB67B2ADFD9B4006F78DA /* FillBlanksTextCollectionViewCell.swift in Sources */,
2CB2BF272AE91F38000A144F /* FillBlanksModeWrapper.swift in Sources */,
E9FB89B02893EA900011EFFB /* UserNotificationsCenterDelegate.swift in Sources */,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ final class StepQuizChoiceViewModel: ObservableObject, StepQuizChildQuizInputPro
}

func createReply() -> Reply {
Reply(sortingChoices: viewData.choices.map(\.isSelected))
Reply.Companion.shared.choice(
choices: viewData.choices.map { ChoiceAnswerChoice(boolValue: $0.isSelected) }
)
}

func doChoiceSelection(at selectedIndex: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,31 +45,3 @@ final class StepQuizPyCharmAssembly: StepQuizChildQuizAssembly {
return StepQuizPyCharmView(viewModel: viewModel)
}
}

#if DEBUG
extension StepQuizPyCharmAssembly {
static func makePlaceholder() -> StepQuizPyCharmAssembly {
StepQuizPyCharmAssembly(
step: .init(
block: .init(
name: "pycharm",
text: "",
options: .init(language: "kotlin")
)
),
dataset: .init(),
reply: .init(
solution: [
.init(
name: "src/Zookeeper.kt",
isVisible: true,
text: "fun main() {\n // write your code here\n}"
)
]
),
provideModuleInputCallback: { _ in },
moduleOutput: nil
)
}
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,3 @@ struct StepQuizPyCharmView: View {
.onAppear(perform: viewModel.doProvideModuleInput)
}
}

#if DEBUG
#Preview {
StepQuizPyCharmAssembly
.makePlaceholder()
.makeModule()
.padding()
}

#Preview("Disabled") {
StepQuizPyCharmAssembly
.makePlaceholder()
.makeModule()
.padding()
.disabled(true)
}
#endif
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ extension StepQuizSQLAssembly {
StepQuizSQLAssembly(
step: .init(),
dataset: .init(),
reply: .init(solveSql: "DROP TABLE students;"),
reply: Reply.Companion.shared.sql(sqlCode: "DROP TABLE students;"),
provideModuleInputCallback: { _ in },
moduleOutput: nil
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import shared

final class StepQuizSQLViewModel: StepQuizCodeViewModel {
override func createReply() -> Reply {
Reply(solveSql: viewData.code)
Reply.Companion.shared.sql(sqlCode: viewData.code)
}

override func syncReply(code: String?) {
let reply = Reply(solveSql: code)
let reply = Reply.Companion.shared.sql(sqlCode: code)
moduleOutput?.handleChildQuizSync(reply: reply)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ final class StepQuizSortingViewModel: ObservableObject, StepQuizChildQuizInputPr
}

func createReply() -> Reply {
Reply(ordering: viewData.items.map(\.id))
Reply.Companion.shared.sorting(
ordering: viewData.items.map { KotlinInt(value: Int32($0.id)) }
)
}

private func outputCurrentReply() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ final class StepQuizStringViewModel: ObservableObject, StepQuizChildQuizInputPro
}

func createReply() -> Reply {
switch self.dataType {
switch dataType {
case .string:
Reply(text: viewData.text, files: [])
Reply.companion.string(text: viewData.text)
case .number:
Reply(number: viewData.text)
Reply.companion.number(number: viewData.text)
case .math:
Reply(formula: viewData.text)
Reply.companion.math(formula: viewData.text)
case .prompt:
Reply.companion.prompt(
prompt: viewData.text,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ final class StepQuizTableViewModel: ObservableObject, StepQuizChildQuizInputProt
}

func createReply() -> Reply {
Reply(
tableChoices: viewData.rows.map { row in
TableChoiceAnswer(
nameRow: row.text,
columns: viewData.columns.map { column in
.init(id: column.text, answer: row.answers.contains(where: { $0.id == column.id }))
}
Reply.companion.table(
choices: viewData.rows.map { row in
ChoiceAnswerTable(
tableChoice: TableChoiceAnswer(
nameRow: row.text,
columns: viewData.columns.map { column in
.init(id: column.text, answer: row.answers.contains(where: { $0.id == column.id }))
}
)
)
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,26 @@ data class Reply(
score = if (markedAsCorrect) ReplyScore.Float(PROMPT_MANUALLY_CONFIRMED_SCORE) else null
)

fun table(answers: List<ChoiceAnswer>): Reply =
Reply(choices = answers)
fun table(choices: List<ChoiceAnswer.Table>): Reply =
Reply(choices = choices)

fun choice(choices: List<ChoiceAnswer.Choice>): Reply =
Reply(choices = choices)

fun matching(ordering: List<Int?>): Reply =
Reply(ordering = ordering)

fun sorting(ordering: List<Int>): Reply =
Reply(ordering = ordering)

fun string(text: String): Reply =
Reply(text = text, files = emptyList())

fun number(number: String): Reply =
Reply(number = number)

fun math(formula: String): Reply =
Reply(formula = formula)
}
}

Expand Down

0 comments on commit 5c75858

Please sign in to comment.