-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
26f4641
commit 2d566be
Showing
4 changed files
with
68 additions
and
26 deletions.
There are no files selected for viewing
16 changes: 16 additions & 0 deletions
16
sources/Features/Sources/Features/Setup/Extensions/String+Extensions.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// | ||
// File.swift | ||
// Features | ||
// | ||
// Created by Igor Kulman on 09.11.2024. | ||
// | ||
|
||
import Foundation | ||
|
||
extension String { | ||
var isValidURL: Bool { | ||
let regEx = "((?:http|https)://)?(?:www\\.)?[\\w\\d\\-_]+\\.\\w{2,3}(\\.\\w{2})?(/(?<=/)(?:[\\w\\d\\-./_]+)?)?" | ||
let predicate = NSPredicate(format: "SELF MATCHES %@", argumentArray: [regEx]) | ||
return predicate.evaluate(with: self) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
sources/Features/Sources/Features/Setup/Views/FormField.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// | ||
// File.swift | ||
// Features | ||
// | ||
// Created by Igor Kulman on 09.11.2024. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
struct FormField: View { | ||
enum ValidatedField { | ||
case title | ||
case url | ||
case rssUrl | ||
case imageUrl | ||
|
||
func validate(text: String) -> Bool { | ||
switch self { | ||
case .title: | ||
return !text.isEmpty | ||
case .rssUrl, .url: | ||
return text.isValidURL | ||
case .imageUrl: | ||
return text.isEmpty || text.isValidURL | ||
} | ||
} | ||
} | ||
|
||
let type: ValidatedField | ||
@Binding var text: String | ||
@State var isValid = true | ||
|
||
var body: some View { | ||
TextField("", text: $text, onEditingChanged: { isEditing in | ||
if isEditing { | ||
isValid = true | ||
} else { | ||
isValid = type.validate(text: text) | ||
} | ||
}) | ||
.keyboardType(.URL) | ||
.disableAutocorrection(true) | ||
.autocapitalization(.none) | ||
.foregroundColor(isValid ? .primary : .red) | ||
} | ||
} |