Skip to content

Commit

Permalink
ANDROAPP-5584-Implement-InputFileResource-adaptations (#142)
Browse files Browse the repository at this point in the history
* [ANDROAPP-5584] make upload state stateless, add focus requester

* [ANDROAPP-5584] fix tests

* reduce padding
  • Loading branch information
xavimolloy authored Nov 8, 2023
1 parent 865d953 commit 25cbe14
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,11 @@ import androidx.compose.material.icons.outlined.FileUpload
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.platform.testTag
import org.hisp.dhis.mobile.ui.designsystem.component.UploadFileState.ADD
import org.hisp.dhis.mobile.ui.designsystem.component.UploadFileState.LOADED
Expand Down Expand Up @@ -51,11 +50,9 @@ fun InputFileResource(
isRequired: Boolean = false,
modifier: Modifier = Modifier,
) {
var currentState by remember(uploadFileState) {
mutableStateOf(uploadFileState)
}
val focusRequester = remember { FocusRequester() }

val primaryButton: @Composable (() -> Unit)? = if (currentState == LOADED && inputShellState != InputShellState.DISABLED) {
val primaryButton: @Composable (() -> Unit)? = if (uploadFileState == LOADED && inputShellState != InputShellState.DISABLED) {
{
IconButton(
modifier = Modifier.testTag(INPUT_FILE_TEST_TAG + CLEAR_BUTTON_TEST_TAG),
Expand All @@ -66,7 +63,6 @@ fun InputFileResource(
)
},
onClick = {
currentState = ADD
onClear.invoke()
},
)
Expand All @@ -76,7 +72,7 @@ fun InputFileResource(
}

val secondaryButton: @Composable (() -> Unit)? =
if (currentState == LOADED) {
if (uploadFileState == LOADED) {
{
SquareIconButton(
modifier = Modifier.testTag(INPUT_FILE_TEST_TAG + UPLOAD_BUTTON_TEST_TAG),
Expand All @@ -87,7 +83,7 @@ fun InputFileResource(
)
},
) {
currentState = UPLOADING
focusRequester.requestFocus()
onUploadFile.invoke()
}
}
Expand All @@ -112,7 +108,7 @@ fun InputFileResource(
}
},
inputField = {
when (currentState) {
when (uploadFileState) {
ADD -> {
ButtonBlock(
modifier = Modifier.padding(top = Spacing.Spacing8, bottom = Spacing.Spacing8),
Expand All @@ -132,7 +128,7 @@ fun InputFileResource(
)
},
) {
currentState = LOADED
focusRequester.requestFocus()
onSelectFile.invoke()
}
},
Expand Down Expand Up @@ -164,7 +160,8 @@ fun InputFileResource(
text = it,
color = if (inputShellState != InputShellState.DISABLED) TextColor.OnSurface else TextColor.OnDisabledSurface,
maxLines = 1,
modifier = Modifier.testTag(INPUT_FILE_TEST_TAG + UPLOAD_TEXT_FILE_NAME_TEST_TAG),
modifier = Modifier.testTag(INPUT_FILE_TEST_TAG + UPLOAD_TEXT_FILE_NAME_TEST_TAG)
.padding(end = Spacing.Spacing2),
)
}
fileWeight?.let {
Expand All @@ -182,7 +179,7 @@ fun InputFileResource(
primaryButton = primaryButton,
secondaryButton = secondaryButton,
isRequiredField = isRequired,
modifier = modifier,
modifier = modifier.focusRequester(focusRequester),
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package org.hisp.dhis.mobile.ui.designsystem.component

import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.test.assert
Expand All @@ -21,14 +22,16 @@ class InputFileResourceTest {
@Test
fun shouldShowLoaderAfterUploadFile() {
rule.setContent {
var state by remember { mutableStateOf(UploadFileState.LOADED) }

InputFileResource(
title = "Label",
buttonText = provideStringResource("add_file"),
fileName = "filename.extension",
fileWeight = "524kb",
uploadFileState = UploadFileState.LOADED,
uploadFileState = state,
onSelectFile = {},
onUploadFile = {},
onUploadFile = { state = UploadFileState.UPLOADING },
)
}

Expand All @@ -47,13 +50,12 @@ class InputFileResourceTest {
buttonText = provideStringResource("add_file"),
fileName = testFileName,
fileWeight = testFileWeight,
uploadFileState = UploadFileState.LOADED,
onSelectFile = {},
onUploadFile = {},
)
}

rule.onNodeWithTag(INPUT_FILE_TEST_TAG + ADD_BUTTON_TEST_TAG).performClick()
rule.onNodeWithTag(INPUT_FILE_TEST_TAG + UPLOAD_BUTTON_TEST_TAG).assertExists()
rule.onNodeWithTag(INPUT_FILE_TEST_TAG + UPLOAD_TEXT_FILE_NAME_TEST_TAG).assertExists()
rule.onNodeWithTag(INPUT_FILE_TEST_TAG + UPLOAD_TEXT_FILE_WEIGHT_TEST_TAG).assertExists()
rule.onNodeWithTag(INPUT_FILE_TEST_TAG + UPLOAD_TEXT_FILE_NAME_TEST_TAG).assert(hasText(testFileName))
Expand Down Expand Up @@ -82,6 +84,8 @@ class InputFileResourceTest {
val newFileWeight = "512gb"

rule.setContent {
var state by remember { mutableStateOf(UploadFileState.LOADED) }

var testFileName by rememberSaveable { mutableStateOf("filename.extension") }
var testFileWeight by rememberSaveable { mutableStateOf("524kb") }

Expand All @@ -90,13 +94,17 @@ class InputFileResourceTest {
buttonText = provideStringResource("add_file"),
fileName = testFileName,
fileWeight = testFileWeight,
uploadFileState = UploadFileState.LOADED,
uploadFileState = state,
onSelectFile = {
testFileName = newFileName
testFileWeight = newFileWeight
state = UploadFileState.LOADED
},
onUploadFile = {
},
onClear = {
state = UploadFileState.ADD
},
)
}
rule.onNodeWithTag(INPUT_FILE_TEST_TAG + UPLOAD_TEXT_FILE_NAME_TEST_TAG).assert(hasText("filename.extension"))
Expand All @@ -110,16 +118,22 @@ class InputFileResourceTest {
@Test
fun shouldAppearIconTextButtonWhenUploadIsCancelled() {
rule.setContent {
var state by remember { mutableStateOf(UploadFileState.LOADED) }

InputFileResource(
title = "Label",
buttonText = "add file",
fileName = "filename.extension",
fileWeight = "524kb",
uploadFileState = UploadFileState.LOADED,
uploadFileState = state,
onSelectFile = {},
onUploadFile = {},
onClear = {
state = UploadFileState.ADD
},
)
}
rule.onNodeWithTag(INPUT_FILE_TEST_TAG + CLEAR_BUTTON_TEST_TAG).assertExists()

rule.onNodeWithTag(INPUT_FILE_TEST_TAG + CLEAR_BUTTON_TEST_TAG).performClick()
rule.onNodeWithTag(INPUT_FILE_TEST_TAG + ADD_BUTTON_TEST_TAG).assertExists()
Expand Down

0 comments on commit 25cbe14

Please sign in to comment.