Skip to content

Commit

Permalink
fix di and auth password fields
Browse files Browse the repository at this point in the history
  • Loading branch information
stslex committed Aug 2, 2023
1 parent 8191b9b commit 4ca24dd
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.tooling.preview.Preview
import com.stslex.aproselection.core.ui.theme.AppTheme
import com.stslex.aproselection.feature.auth.ui.model.screen.text_field.PasswordTextFieldState
import com.stslex.aproselection.feature.auth.ui.model.screen.text_field.base.PasswordTextFieldState

@Composable
fun AuthPasswordTextField(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.stslex.aproselection.feature.auth.ui.model.screen.text_field

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.hapticfeedback.HapticFeedback
import com.stslex.aproselection.feature.auth.R
import com.stslex.aproselection.feature.auth.ui.model.mvi.ScreenAction.InputAction.PasswordInput
import com.stslex.aproselection.feature.auth.ui.model.screen.text_field.base.PasswordTextFieldState
import com.stslex.aproselection.feature.auth.ui.model.screen.text_field.hidden.HiddenState

@Stable
data class PasswordInputTextFieldState(
private val hapticFeedback: HapticFeedback,
private val processAction: (PasswordInput) -> Unit,
private val hiddenState: HiddenState,
override val text: String,
) : PasswordTextFieldState(hapticFeedback) {
) : PasswordTextFieldState(
hapticFeedback = hapticFeedback,
hiddenState = hiddenState
) {

override val hint: Int = R.string.auth_password_enter_hint_text

Expand All @@ -25,10 +33,14 @@ fun rememberPasswordInputTextFieldState(
hapticFeedback: HapticFeedback,
processAction: (PasswordInput) -> Unit,
text: String,
) = remember(text) {
PasswordInputTextFieldState(
hapticFeedback = hapticFeedback,
processAction = processAction,
text = text
)
): PasswordInputTextFieldState {
val hiddenState = remember { HiddenState() }
return remember(text) {
PasswordInputTextFieldState(
hapticFeedback = hapticFeedback,
processAction = processAction,
hiddenState = hiddenState,
text = text
)
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,24 @@
package com.stslex.aproselection.feature.auth.ui.model.screen.text_field

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import androidx.compose.ui.hapticfeedback.HapticFeedback
import com.stslex.aproselection.feature.auth.R
import com.stslex.aproselection.feature.auth.ui.model.mvi.ScreenAction.InputAction.PasswordSubmitInput
import com.stslex.aproselection.feature.auth.ui.model.screen.text_field.base.PasswordTextFieldState
import com.stslex.aproselection.feature.auth.ui.model.screen.text_field.hidden.HiddenState

@Stable
data class PasswordSubmitTextFieldState(
private val hapticFeedback: HapticFeedback,
private val processAction: (PasswordSubmitInput) -> Unit,
private val hiddenState: HiddenState,
override val text: String,
) : PasswordTextFieldState(hapticFeedback) {
) : PasswordTextFieldState(
hapticFeedback = hapticFeedback,
hiddenState = hiddenState
) {

override val hint: Int = R.string.auth_password_submit_hint_text

Expand All @@ -25,10 +33,16 @@ fun rememberPasswordSubmitTextFieldState(
hapticFeedback: HapticFeedback,
processAction: (PasswordSubmitInput) -> Unit,
text: String,
) = remember(text) {
PasswordSubmitTextFieldState(
hapticFeedback = hapticFeedback,
processAction = processAction,
text = text
)
): PasswordSubmitTextFieldState {
val hiddenState = remember {
HiddenState()
}
return remember(text) {
PasswordSubmitTextFieldState(
hapticFeedback = hapticFeedback,
processAction = processAction,
hiddenState = hiddenState,
text = text
)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.stslex.aproselection.feature.auth.ui.model.screen.text_field

import androidx.compose.runtime.Composable
import androidx.compose.runtime.Stable
import androidx.compose.runtime.remember
import com.stslex.aproselection.feature.auth.R
import com.stslex.aproselection.feature.auth.ui.model.mvi.ScreenAction.InputAction.UsernameInput
import com.stslex.aproselection.feature.auth.ui.model.screen.text_field.base.AuthTextField

@Stable
data class UsernameTextFieldState(
private val processAction: (UsernameInput) -> Unit,
override val text: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package com.stslex.aproselection.feature.auth.ui.model.screen.text_field
package com.stslex.aproselection.feature.auth.ui.model.screen.text_field.base

import androidx.compose.runtime.Stable

@Stable
abstract class AuthTextField {

abstract val text: String
Expand All @@ -10,8 +13,9 @@ abstract class AuthTextField {
get() = "${text.length}/$MAX_SYMBOL_COUNT"

fun onTextChange(value: String) {
if (text == value || value.length > MAX_SYMBOL_COUNT) return
sendAction(value)
val currentValue = value.trim()
if (text == currentValue || currentValue.length > MAX_SYMBOL_COUNT) return
sendAction(currentValue)
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
package com.stslex.aproselection.feature.auth.ui.model.screen.text_field
package com.stslex.aproselection.feature.auth.ui.model.screen.text_field.base

import androidx.compose.runtime.Stable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.ui.hapticfeedback.HapticFeedback
import androidx.compose.ui.hapticfeedback.HapticFeedbackType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.text.input.VisualTransformation
import com.stslex.aproselection.feature.auth.R
import com.stslex.aproselection.feature.auth.ui.model.screen.text_field.hidden.HiddenState

@Stable
abstract class PasswordTextFieldState(
private val hapticFeedback: HapticFeedback
private val hapticFeedback: HapticFeedback,
private val hiddenState: HiddenState
) : AuthTextField() {

private val isPasswordHidden = mutableStateOf(true)

override val label: Int = R.string.auth_password_text
abstract val hint: Int

val visualTransformation = derivedStateOf {
if (isPasswordHidden.value) {
if (hiddenState.isHidden) {
PasswordVisualTransformation()
} else {
VisualTransformation.None
}
}

val trailingIconRes = derivedStateOf {
if (isPasswordHidden.value) {
if (hiddenState.isHidden) {
R.drawable.baseline_visibility_off_24
} else {
R.drawable.baseline_visibility_24
Expand All @@ -35,7 +36,7 @@ abstract class PasswordTextFieldState(

fun onPasswordHideClicked() {
hapticFeedback.performHapticFeedback(HapticFeedbackType.TextHandleMove)
isPasswordHidden.value = isPasswordHidden.value.not()
hiddenState.isHidden = hiddenState.isHidden.not()
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.stslex.aproselection.feature.auth.ui.model.screen.text_field.hidden

import androidx.compose.runtime.Stable
import androidx.compose.runtime.mutableStateOf

@Stable
class HiddenState {

private val _isHidden = mutableStateOf(true)
var isHidden: Boolean
get() = _isHidden.value
set(value) {
_isHidden.value = value
}
}

0 comments on commit 4ca24dd

Please sign in to comment.