Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ios_zip_validation] Validate zip #56

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/java/ch/dreipol/rezhycle/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ class MainActivity : ReduxSampleActivity(), Navigator<AppState> {
}

override fun onBackPressed() {
if (!store.state.settingsViewState.zipSettingsViewState.enterZipViewState.canGoBack) {
return
}
super.onBackPressed()
rootDispatch(NavigationAction.BACK)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.dreipol.rezhycle.fragments.settings

import android.graphics.PorterDuff
import android.os.Bundle
import android.view.View
import ch.dreipol.dreimultiplatform.reduxkotlin.PresenterLifecycleObserver
Expand Down Expand Up @@ -30,6 +31,15 @@ class ZipSettingsFragment : BaseFragment<FragmentZipSettingsBinding, ZipSettings

override fun render(zipSettingsViewState: ZipSettingsViewState) {
bindHeader(zipSettingsViewState.headerViewState, viewBinding.header)
if (zipSettingsViewState.enterZipViewState.canGoBack) {
viewBinding.header.iconLeft.setColorFilter(resources.getColor(R.color.accent, null), PorterDuff.Mode.SRC_IN)
viewBinding.header.title.setTextColor(resources.getColor(R.color.primary_dark, null))
} else {
val disabledColor = resources.getColor(R.color.monochromes_grey_light, null)
viewBinding.header.iconLeft.setColorFilter(disabledColor, PorterDuff.Mode.SRC_IN)
viewBinding.header.title.setTextColor(disabledColor)
}

viewBinding.enterZipView.update(zipSettingsViewState.enterZipViewState)
}
}
1 change: 1 addition & 0 deletions app/src/main/res/layout/view_enter_zip.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
android:imeOptions="actionDone"
android:inputType="number"
android:padding="11dp"
android:maxLength="4"
android:textAlignment="center"
android:textAppearance="@style/EditTextStyle"
app:layout_constraintEnd_toEndOf="parent"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class ZipSettingsViewController: StackPresenterViewController<ZipSettingsView>,
func render(zipSettingsViewState: ZipSettingsViewState) {
zipViewState = zipSettingsViewState
headerView.titleLabel.text = zipSettingsViewState.headerViewState.title.localized
headerView.canGoBack = zipSettingsViewState.enterZipViewState.canGoBack
zipEnterControl.updateControl(
title: zipSettingsViewState.enterZipViewState.enterZipLabel.localized,
enterText: zipSettingsViewState.enterZipViewState.selectedZip?.stringValue,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ import ReduxSampleShared

class HeaderView: HighlightableControl {
let titleLabel = UILabel.h3()
var canGoBack: Bool = true {
didSet {
isHighlighted = !canGoBack
isEnabled = canGoBack
}
}
private let image = UIImageView.autoLayout()

init() {
Expand Down Expand Up @@ -41,6 +47,8 @@ class HeaderView: HighlightableControl {
}

@objc func didTabBack() {
_ = dispatch(NavigationAction.back)
if canGoBack {
_ = dispatch(NavigationAction.back)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,24 @@

import UIKit
import ReduxSampleShared
import dreiKit

let kZipCellIdentifier = "possibleZipCell"

class ZipValidator: DefaultBehaviorTextFieldDelegate {
override func isValid(text: String, in textField: UITextField) -> Bool {
text.count <= 4 && text.allSatisfy(\.isNumber)
}
}

class ZipEnterControl: UIView {
private let zipLabel = UILabel.h4()
private let enterView = UITextField.autoLayout()
private let zipCollectionView = UICollectionView(frame: .zero, collectionViewLayout: UICollectionViewFlowLayout())
private var possibleZips: [KotlinInt] = []

private let entryValidator = ZipValidator()

init(isLightTheme: Bool = false) {
super.init(frame: .zero)
translatesAutoresizingMaskIntoConstraints = false
Expand Down Expand Up @@ -65,6 +74,7 @@ class ZipEnterControl: UIView {
enterView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
enterView.topAnchor.constraint(equalTo: zipLabel.bottomAnchor, constant: kUnit3).isActive = true
enterView.keyboardType = .numberPad
enterView.delegate = entryValidator
enterView.addTarget(self, action: #selector(zipValueChanged), for: .editingChanged)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ val enterZipViewReducer: Reducer<EnterZipViewState> = { state, action ->

private fun copyAndValidate(enterZipState: EnterZipViewState, selectedZip: Int?, possibleZips: List<Int>): EnterZipViewState {
val invalidZip = selectedZip != null && possibleZips.contains(selectedZip).not()
return enterZipState.copy(possibleZips = possibleZips, selectedZip = selectedZip, invalidZip = invalidZip)
val canGoBack = selectedZip != null && !invalidZip
return enterZipState.copy(possibleZips = possibleZips, selectedZip = selectedZip, invalidZip = invalidZip, canGoBack = canGoBack)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package ch.dreipol.multiplatform.reduxsample.shared.ui
data class EnterZipViewState(
val possibleZips: List<Int> = emptyList(),
val selectedZip: Int? = null,
val invalidZip: Boolean = false
val invalidZip: Boolean = false,
val canGoBack: Boolean = true,
) {
val enterZipLabel = "onboarding_enter_zip_label"
val filterEmptyText = "zip_invalid"
Expand Down