Skip to content

Commit

Permalink
Fixes part of #281: Updated StateRetriever to create VoiceoverMappings (
Browse files Browse the repository at this point in the history
#290)

* Fix broken AudioFragmentTest build for Espresso runs.

* Fixed StateRetriever to create VoiceoverMappings
  • Loading branch information
jamesxu0 authored Nov 4, 2019
1 parent dd5bbab commit b1f8e8f
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions domain/src/main/java/org/oppia/domain/util/StateRetriever.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,31 @@ import org.oppia.app.model.RuleSpec
import org.oppia.app.model.State
import org.oppia.app.model.StringList
import org.oppia.app.model.SubtitledHtml
import org.oppia.app.model.Voiceover
import org.oppia.app.model.VoiceoverMapping
import javax.inject.Inject

/** Utility that helps create a [State] object given its JSON representation. */
class StateRetriever @Inject constructor() {

/** Creates a single state object from JSON */
fun createStateFromJson(stateName: String, stateJson: JSONObject?): State {
return State.newBuilder()
val state = State.newBuilder()
.setName(stateName)
.setContent(
SubtitledHtml.newBuilder().setHtml(
stateJson?.getJSONObject("content")?.getString("html")
).setContentId(
stateJson?.getJSONObject("content")?.optString("content_id")
)
)
.setInteraction(createInteractionFromJson(stateJson?.getJSONObject("interaction")))
.build()

if (stateJson != null && stateJson.has("recorded_voiceovers")) {
createVoiceOverMappingsFromJson(stateJson.getJSONObject("recorded_voiceovers"), state)
}

return state.build()
}

// Creates an interaction from JSON
Expand Down Expand Up @@ -114,6 +123,29 @@ class StateRetriever @Inject constructor() {
.build()
}

// Creates VoiceoverMappings from JSON and adds onto State
private fun createVoiceOverMappingsFromJson(recordedVoiceovers: JSONObject, stateBuilder: State.Builder) {
val voiceoverMappingJson = recordedVoiceovers.getJSONObject("voiceovers_mapping")
voiceoverMappingJson?.let {
for (key in it.keys()) {
val voiceoverMapping = VoiceoverMapping.newBuilder()
val voiceoverJson = it.getJSONObject(key)
for (lang in voiceoverJson.keys()) {
voiceoverMapping.putVoiceoverMapping(lang, createVoiceOverFromJson(voiceoverJson.getJSONObject(lang)))
}
stateBuilder.putRecordedVoiceovers(key, voiceoverMapping.build())
}
}
}

// Creates a Voiceover from Json
private fun createVoiceOverFromJson(voiceoverJson: JSONObject): Voiceover {
return Voiceover.newBuilder()
.setNeedsUpdate(voiceoverJson.getBoolean("needs_update"))
.setFileName(voiceoverJson.getString("filename"))
.build()
}

// Creates the list of rule spec objects from JSON
private fun createRuleSpecsFromJson(
ruleSpecJson: JSONArray?, interactionId: String
Expand Down

0 comments on commit b1f8e8f

Please sign in to comment.