-
Notifications
You must be signed in to change notification settings - Fork 3
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 12dc5c9
Showing
6 changed files
with
99 additions
and
26 deletions.
There are no files selected for viewing
File renamed without changes.
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,20 @@ | ||
// | ||
// File.swift | ||
// Core | ||
// | ||
// Created by Igor Kulman on 09.11.2024. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
public extension View { | ||
@ViewBuilder | ||
func `if`<Content: View>(_ conditional: Bool, content: (Self) -> Content) -> some View { | ||
if conditional { | ||
content(self) | ||
} else { | ||
self | ||
} | ||
} | ||
} |
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
58 changes: 58 additions & 0 deletions
58
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,58 @@ | ||
// | ||
// 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 | ||
} | ||
} | ||
|
||
var isURL: Bool { | ||
switch self { | ||
case .rssUrl, .url, .imageUrl: | ||
return true | ||
case .title: | ||
return false | ||
} | ||
} | ||
} | ||
|
||
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) | ||
} | ||
}) | ||
.foregroundColor(isValid ? .primary : .red) | ||
.if(type.isURL) { | ||
$0.keyboardType(.URL) | ||
.disableAutocorrection(true) | ||
.autocapitalization(.none) | ||
} | ||
} | ||
} |