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

Checkbox for user age instead of a birth date #247

Merged
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 Style/Sources/Style/Assets.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ public enum Asset {
public static let linkedinIcon = ImageAsset(name: "linkedin_icon")
public static let websiteIcon = ImageAsset(name: "website_icon")
public static let onboardingFeed = ImageAsset(name: "onboarding_feed")
public static let blueCheckedIcon = ImageAsset(name: "blue_checked_icon")
public static let blueUncheckedIcon = ImageAsset(name: "blue_unchecked_icon")
public static let calendar = ImageAsset(name: "calendar")
public static let redUncheckedIcon = ImageAsset(name: "red_unchecked_icon")
public static let signInApple = ImageAsset(name: "sign_in_apple")
public static let signInFacebook = ImageAsset(name: "sign_in_facebook")
public static let signInMobile = ImageAsset(name: "sign_in_mobile")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "blue_checked_icon.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "blue_unchecked_icon.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"images" : [
{
"filename" : "red_unchecked_icon.pdf",
"idiom" : "universal"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
//
// AgeConsentView.swift
//
//
// Created by Shashank Kaushik on 26/05/24.
//

import Combine
import Style
import UIKit

public final class AgeConsentView: UIStackView {
private let checkboxImageView = UIImageView()
private let label = UILabel()
private var viewModel: AgeConsentViewModel? {
didSet {
updateUI(viewModel?.state ?? .unchecked)
}
}

public var onTap: ((Bool) -> (Void))?

// MARK: - Initialization
override init(frame: CGRect) {
super.init(frame: frame)
setup()
}

Check warning on line 28 in UIComponents/Sources/UIComponents/Components/Views/AgeConsentView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
required init(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}

public func configure(_ viewModel: AgeConsentViewModel) {
self.viewModel = viewModel
}
}

private extension AgeConsentView {
// MARK: - Setup
func setup() {
axis = .horizontal
spacing = 8.0
distribution = .fill
alignment = .leading
translatesAutoresizingMaskIntoConstraints = false

label.numberOfLines = 0
label.lineBreakMode = .byWordWrapping
label.font = designEngine.fonts.primary.semibold(14.0)
checkboxImageView.contentMode = .scaleAspectFit

addArrangedSubview(checkboxImageView)
addArrangedSubview(label)

isUserInteractionEnabled = true
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(handleTapGesture))
addGestureRecognizer(tapGesture)
}

@objc func handleTapGesture() {
viewModel?.toggleState()
onTap?(viewModel?.state == .checked)
}

private func updateUI(_ newState: CheckBoxState) {
label.text = viewModel?.title
switch newState {
case .checked:
checkboxImageView.image = Asset.Images.blueCheckedIcon.image
label.textColor = designEngine.colors.textPrimary
case .unchecked:
checkboxImageView.image = Asset.Images.blueUncheckedIcon.image
label.textColor = designEngine.colors.textPrimary
}
}
}

public extension AgeConsentView {

class AgeConsentViewModel: ObservableObject, AgeConsentViewModelProtocol {
public var title: String
@Published public var state: CheckBoxState

var cancellables = Set<AnyCancellable>()

public init(state: CheckBoxState, title: String) {
self.state = state
self.title = title
}

Check warning on line 90 in UIComponents/Sources/UIComponents/Components/Views/AgeConsentView.swift

View workflow job for this annotation

GitHub Actions / SwiftLint

Trailing Whitespace Violation: Lines should not have trailing whitespace (trailing_whitespace)
func toggleState() {
switch state {
case .checked:
state = .unchecked
case .unchecked:
state = .checked
}
}
}
}

public enum CheckBoxState {
case checked
case unchecked
}

public protocol AgeConsentViewModelProtocol {
var title: String { get }
var state: CheckBoxState { get }
}
1 change: 1 addition & 0 deletions animeal/res/en.lproj/Localizable.strings
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
"profile.errors.incorrectFormat" = "Format is incorrect";
"profile.errors.incorrectCharactersLength" = "Length must be between 2 and 35";
"profile.errors.incorrectCharacters" = "Must contain only letters";
"profile.consent" = "You acknowledge that you are atleast 15 years old";
"phone.title" = "Please, enter your phone";
"phone.fields.phoneTitlle" = "Phone Number";
"phone.fields.passwordTitlle" = "Password";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class UserValidationModel: UserProfileValidationModel {
emailVerified = attributes[.emailVerified]
.map { Bool($0) ?? false } ?? false

areAllNecessaryFieldsFilled = [UserProfileAttributeKey.name, .familyName, .email, .birthDate, .phoneNumber]
areAllNecessaryFieldsFilled = [UserProfileAttributeKey.name, .familyName, .email, .phoneNumber]
.allSatisfy { attributes[$0]?.isEmpty == false }
}
}
Expand Down
2 changes: 2 additions & 0 deletions animeal/src/Common/Strings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,8 @@ internal enum L10n {
internal static let birthDate = L10n.tr("Localizable", "profile.birthDate", fallback: "Birthdate")
/// Cancel
internal static let cancel = L10n.tr("Localizable", "profile.cancel", fallback: "Cancel")
/// You acknowledge that you are atleast 15 years old
internal static let consent = L10n.tr("Localizable", "profile.consent", fallback: "You acknowledge that you are atleast 15 years old")
/// Done
internal static let done = L10n.tr("Localizable", "profile.done", fallback: "Done")
/// Edit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,9 +165,9 @@ final class ProfileModelEditAction: ProfileModelAction {
let items = await state.items
let editableItems = items.toEditable { item in
switch item.type {
case .name, .surname, .email, .birthday:
case .name, .surname, .email:
return false
case .phone:
case .phone, .birthday:
return true
}
}
Expand Down Expand Up @@ -218,9 +218,9 @@ final class ProfileModelSaveAction: ProfileModelAction {
let items = await state.items
let editableItems = items.toEditable { item in
switch item.type {
case .name, .surname, .email, .birthday:
case .name, .surname, .email:
return false
case .phone:
case .phone, .birthday:
return true
}
}
Expand Down
18 changes: 17 additions & 1 deletion animeal/src/Flows/Profile/Model/Items/ProfileModelItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,17 @@ struct ProfileModelItem: Hashable, ProfileModelValidatable {
_value = transform(newValue)
}
}
var selected: Bool {
get {
style == .readonly ? true : _selected ?? false
}
set {
_selected = style == .readonly ? true : newValue
}
}
var date: Date? { transformDate(text) }
private var _value: String?
private var _selected: Bool?

init(
identifier: String,
Expand Down Expand Up @@ -80,7 +89,7 @@ struct ProfileModelItem: Hashable, ProfileModelValidatable {
case .phone(let region):
return try validatePhone(region)
case .birthday:
return try validateDate()
return try validateCheckBox()
}
}

Expand Down Expand Up @@ -167,6 +176,13 @@ extension ProfileModelItem {
return region.phoneNumberCode + text
}

func validateCheckBox() throws -> String {
guard selected == true else {
throw ProfileModelItemAgeError(selected: false)
}
return ""
}

func validateDate() throws -> String {
let text = try validateForEmptiness()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@ struct ProfileModelItemError: LocalizedError {
let itemIdentifier: String
var errorDescription: String?
}

struct ProfileModelItemAgeError: LocalizedError {
let selected: Bool
}
4 changes: 2 additions & 2 deletions animeal/src/Flows/Profile/Model/ProfileModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ final class ProfileModel: ProfileModelProtocol {
await fetchItems(identifier)
}

func updateItem(_ text: String?, forIdentifier identifier: String) async {
await updateItems(text, forIdentifier: identifier)
func updateItem(_ text: String?, _ selected: Bool, forIdentifier identifier: String) async {
await updateItems(text, selected, forIdentifier: identifier)
}

func validateItems() async -> Bool {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protocol FetchProfileItemRequiredActionUseCase {
}

protocol UpdateProfileItemsUseCaseLogic {
func callAsFunction(_ text: String?, forIdentifier identifier: String) async
func callAsFunction(_ text: String?, _ selected: Bool, forIdentifier identifier: String) async
}

protocol ValidateProfileItemsUseCaseLogic {
Expand Down Expand Up @@ -113,13 +113,6 @@ extension FetchProfileItemsUseCase: FetchProfileItemsUseCaseLogic {
} else {
item.text = nil
}
case .birthday where isFacebook:
dateFormatter.dateFormat = "MM/dd/yyyy"
if let birthDate = knownAttributes[key]?.value,
let date = dateFormatter.date(from: birthDate) {
dateFormatter.dateFormat = "dd/MM/yyyy"
item.text = dateFormatter.string(from: date)
}
default:
item.text = knownAttributes[key]?.value
}
Expand Down Expand Up @@ -165,14 +158,15 @@ extension FetchProfileItemsUseCase: UpdateProfileItemsUseCaseLogic {
/// - Parameters:
/// - text: the value of the profile item. for name is the updated name, for email it's the updated email.
/// - identifier: Identifer of the particular profile item which was updated.
func callAsFunction(_ text: String?, forIdentifier identifier: String) async {
func callAsFunction(_ text: String?, _ selected: Bool, forIdentifier identifier: String) async {
var items = await state.items
guard
let itemIndex = items.firstIndex(where: { $0.identifier == identifier })
else { return }

var value = items[itemIndex]
value.text = text
value.selected = selected
items[itemIndex] = value

var changedItemsTypes = await state.changedItemsTypes
Expand Down
2 changes: 1 addition & 1 deletion animeal/src/Flows/Profile/ProfileContract.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ protocol ProfileModelProtocol {
func fetchCachedItems() async throws -> [ProfileModelItem]
func fetchItems() async throws -> [ProfileModelItem]
func fetchItem(_ identifier: String) async -> ProfileModelItem?
func updateItem(_ text: String?, forIdentifier identifier: String) async
func updateItem(_ text: String?, _ selected: Bool, forIdentifier identifier: String) async
func validateItems() async -> Bool

func fetchRequiredAction(forIdentifier identifier: String) async -> PhoneModelRequiredAction?
Expand Down
Loading
Loading