Skip to content

Commit 9db8ee7

Browse files
committed
Added PromptsCollectionMetadata with defaultSection field.
1 parent 727c0a6 commit 9db8ee7

File tree

4 files changed

+72
-34
lines changed

4 files changed

+72
-34
lines changed

app/src/main/java/edu/gatech/ccg/recordthesehands/home/HomeScreenActivity.kt

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,6 @@ class HomeScreenActivity : ComponentActivity() {
129129
*/
130130
private var emailing: Boolean = true
131131

132-
/**
133-
* Whether or not we have already asked the user for permissions.
134-
*/
135-
private var permissionRequestedPreviously: Boolean = false
136-
137132
/**
138133
* The total number of recordings the user has done in the current session (i.e., since they
139134
* last cold-booted the app). After this value reaches [Constants.MAX_RECORDINGS_IN_SITTING],
@@ -665,7 +660,7 @@ fun HomeScreenContent(
665660
) {
666661
// Section Name Text
667662
Text(
668-
text = promptState?.currentSectionName ?: "",
663+
text = promptState?.currentSectionName ?: "<NO SECTION>",
669664
fontSize = 18.sp,
670665
)
671666

@@ -699,7 +694,7 @@ fun HomeScreenContent(
699694
// Total Progress Count Text
700695
Text(
701696
// TODO replace with stringResource.
702-
text = "${totalCompleted}/${totalPrompts}",
697+
text = "${totalCompleted} / ${totalPrompts}",
703698
fontSize = 18.sp,
704699
modifier = Modifier
705700
.constrainAs(totalProgressCountText) {
@@ -891,28 +886,32 @@ fun HomeScreenContent(
891886
val startButtonText: String
892887
val startRecordingShouldSwitchPrompts: Boolean
893888

894-
if (promptState != null && promptState!!.currentPrompts != null && promptState!!.username != null) {
895-
if ((promptState!!.currentPromptIndex ?: 0) < (promptState!!.totalPromptsInCurrentSection
896-
?: 0)
897-
) {
898-
startButtonEnabled = true
899-
startRecordingShouldSwitchPrompts = false
900-
startButtonText = stringResource(id = R.string.start_button)
901-
} else {
902-
if (totalCompleted >= totalPrompts) {
903-
startButtonEnabled = false
904-
startRecordingShouldSwitchPrompts = false
905-
startButtonText = stringResource(id = R.string.no_more_prompts)
906-
} else {
907-
startButtonEnabled = true
908-
startRecordingShouldSwitchPrompts = true
909-
startButtonText = stringResource(id = R.string.switch_prompts)
910-
}
911-
}
912-
} else {
889+
if (promptState == null || promptState!!.username == null) {
913890
startButtonEnabled = false
914891
startRecordingShouldSwitchPrompts = false
915892
startButtonText = stringResource(id = R.string.start_disabled)
893+
} else if (promptState!!.currentSectionName == null) {
894+
startButtonEnabled = true
895+
startRecordingShouldSwitchPrompts = true
896+
startButtonText = stringResource(id = R.string.switch_prompts)
897+
} else if (promptState!!.currentPrompts == null) {
898+
startButtonEnabled = false
899+
startRecordingShouldSwitchPrompts = false
900+
startButtonText = stringResource(id = R.string.start_disabled)
901+
} else if (
902+
(promptState!!.currentPromptIndex ?: 0) < (promptState!!.totalPromptsInCurrentSection ?: 0)
903+
) {
904+
startButtonEnabled = true
905+
startRecordingShouldSwitchPrompts = false
906+
startButtonText = stringResource(id = R.string.start_button)
907+
} else if (totalCompleted >= totalPrompts) {
908+
startButtonEnabled = false
909+
startRecordingShouldSwitchPrompts = false
910+
startButtonText = stringResource(id = R.string.no_more_prompts)
911+
} else {
912+
startButtonEnabled = true
913+
startRecordingShouldSwitchPrompts = true
914+
startButtonText = stringResource(id = R.string.switch_prompts)
916915
}
917916

918917
PrimaryButton(

app/src/main/java/edu/gatech/ccg/recordthesehands/upload/DataManager.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,22 @@ class DataManager private constructor(val context: Context) {
234234
}
235235

236236
if (currentSectionName == null && promptsCollection != null && promptsCollection.sections.isNotEmpty()) {
237-
currentSectionName = promptsCollection.sections.values.first().name
238-
setCurrentSectionPrefStoreUnderLock(currentSectionName)
237+
promptsCollection.collectionMetadata.defaultSection?.let {
238+
// if default is not null...
239+
Log.i(TAG, "defaultSection == ${it}")
240+
if (promptsCollection.sections.containsKey(it)) {
241+
// If it is valid, set it to that section.
242+
currentSectionName = it
243+
}
244+
// If it is not valid, leave it as null.
245+
} ?: run {
246+
// If default is null, set to the first section.
247+
currentSectionName = promptsCollection.sections.values.first().name
248+
}
249+
if (currentSectionName != null) {
250+
Log.i(TAG, "Setting current section from null to $currentSectionName")
251+
setCurrentSectionPrefStoreUnderLock(currentSectionName)
252+
}
239253
}
240254

241255
val initialState = PromptState(
@@ -803,7 +817,7 @@ class DataManager private constructor(val context: Context) {
803817
*
804818
* @param sectionName The name of the section to set as current.
805819
*/
806-
suspend fun setCurrentSection(sectionName: String) {
820+
suspend fun setCurrentSection(sectionName: String?) {
807821
dataManagerData.lock.withLock {
808822
setCurrentSectionUnderLock(sectionName)
809823
}
@@ -820,17 +834,21 @@ class DataManager private constructor(val context: Context) {
820834
* @param sectionName The name of the section to set as current.
821835
* @throws IllegalStateException if the prompt state has not been initialized.
822836
*/
823-
private suspend fun setCurrentSectionUnderLock(sectionName: String) {
837+
private suspend fun setCurrentSectionUnderLock(sectionName: String?) {
824838
val currentState = dataManagerData.promptStateContainer
825839
?: throw IllegalStateException("Attempted to set current section before prompt state was initialized.")
826840
updatePromptStateAndPost(currentState.copy(currentSectionName = sectionName))
827841
setCurrentSectionPrefStoreUnderLock(sectionName)
828842
}
829843

830-
private suspend fun setCurrentSectionPrefStoreUnderLock(sectionName: String) {
844+
private suspend fun setCurrentSectionPrefStoreUnderLock(sectionName: String?) {
831845
val keyObject = stringPreferencesKey("currentSectionName")
832846
context.prefStore.edit {
833-
it[keyObject] = sectionName
847+
if (sectionName != null) {
848+
it[keyObject] = sectionName
849+
} else {
850+
it.remove(keyObject)
851+
}
834852
}
835853
}
836854

app/src/main/java/edu/gatech/ccg/recordthesehands/upload/PromptsCollection.kt

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import java.io.File
99

1010
class PromptsCollection(val context: Context) {
1111
var sections = mutableMapOf<String, PromptsSection>()
12+
lateinit var collectionMetadata: PromptsCollectionMetadata
1213

1314
companion object {
1415
private val TAG = PromptsCollection::class.simpleName
@@ -24,7 +25,14 @@ class PromptsCollection(val context: Context) {
2425
Log.e(TAG, "Failed to parse prompts file: $e")
2526
return false
2627
}
27-
val data = json.getJSONObject("data")
28+
val collectionMetadataJson = json.optJSONObject("metadata") ?: JSONObject()
29+
collectionMetadata = PromptsCollectionMetadata(
30+
defaultSection = collectionMetadataJson.opt("defaultSection") as? String,
31+
)
32+
val data = json.optJSONObject("data") ?: run {
33+
Log.e(TAG, "Prompts file did not include \"data\" section")
34+
return false
35+
}
2836
data.keys().forEach { sectionName ->
2937
val sectionJson = data.getJSONObject(sectionName)
3038
val metadataJson = sectionJson.optJSONObject("metadata") ?: JSONObject()
@@ -69,6 +77,19 @@ class PromptsCollection(val context: Context) {
6977
sectionsJson.put(key, value.toJson())
7078
}
7179
json.put("sections", sectionsJson)
80+
json.put("metadata", collectionMetadata.toJson())
81+
return json
82+
}
83+
}
84+
85+
data class PromptsCollectionMetadata(
86+
val defaultSection: String?,
87+
) {
88+
fun toJson(): JSONObject {
89+
val json = JSONObject()
90+
if (defaultSection != null) {
91+
json.put("defaultSection", defaultSection)
92+
}
7293
return json
7394
}
7495
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,5 +115,5 @@
115115
<string name="perm_alert_message">In order to record your data, we will need access to the camera and write functionality.</string>
116116
<string name="ok">OK</string>
117117
<string name="words_signed">Words signed</string>
118-
<string name="no_more_prompts">Cannot start, prompts completed.</string>
118+
<string name="no_more_prompts">Tasks completed</string>
119119
</resources>

0 commit comments

Comments
 (0)