Good afternoon.
Guys, tell me how it is possible to check for switching focus by blocks?
I wanted to make an enumeration array but alas 🤔.
I picked something too much...
I used the work of @ygeras in the code.
struct TestModelField {
var first: String
var second: String
}
class CustomTextFieldViewModel: ObservableObject {
@Published var customFields: [TestModelField] = []
@Published var allSubmitPressed: [[Bool]] = []
@Published var allSatisfy: [Bool] = []
init() {
self.addField()
}
private func addField() {
for _ in 0...2 {
self.customFields.append(TestModelField(first: "", second: ""))
self.allSubmitPressed.append([false, false])
self.allSatisfy.append(false)
}
}
}
enum NameFieldsTest: String, CaseIterable {
case firstField = "First field"
case secondField = "Second field"
}
struct TestTextFields: View {
@ObservedObject var vm: CustomTextFieldViewModel = CustomTextFieldViewModel()
@FocusState private var nameFields: NameFieldsTest?
@State private var submitPressed = false
var body: some View {
VStack(spacing: 10) {
VStack(spacing: 10) {
// MARK: For visual information
HStack(spacing: 10) {
ForEach(vm.allSatisfy.indices, id: \.self) { index in
RoundedRectangle(cornerRadius: 7)
.fill(vm.allSatisfy[index] ? Color.green : Color.red)
.frame(width: 10, height: 10)
}
}
ForEach(vm.customFields.indices, id: \.self) { index in
VStack(spacing: 10) {
CustomTextField(text: $vm.customFields[index].first,
focus: $nameFields,
nameFieldFirst: .firstField,
submitPressed: vm.allSubmitPressed[index][0])
CustomTextField(text: $vm.customFields[index].second,
focus: $nameFields,
nameFieldFirst: .secondField,
submitPressed: vm.allSubmitPressed[index][1])
}
.padding(.vertical, 16)
.border(Color.black)
}
}
.padding(.vertical, 10)
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.gray).opacity(0.2)
)
Button(action: {
Task.init {
await self.showResult()
}
}) {
Text("Answer")
.font(.system(size: 16))
.foregroundColor(.black)
}
.padding(.horizontal, 16)
.padding(.vertical, 7)
.background(.gray)
.cornerRadius(12)
// MARK: For visual information
ForEach(vm.allSubmitPressed.indices, id: \.self) { vert in
HStack() {
ForEach(vm.allSubmitPressed[vert].indices, id: \.self) { horiz in
CheckCircle(isCheck: vm.allSubmitPressed[vert][horiz])
}
}
}
}
}
private func isCheck(_ index: Int) {
let mirrorStruct = Mirror(reflecting: vm.customFields[index])
var count = 0
for field in mirrorStruct.children {
guard let currentField = field.value as? String else {
return
}
if !currentField.isEmpty {
vm.allSubmitPressed[index][count] = true
} else {
vm.allSubmitPressed[index][count] = false
}
count += 1
}
count = 0
}
private func allSatisfy() {
for (index, _) in vm.allSubmitPressed.enumerated() {
if vm.allSubmitPressed[index].allSatisfy({
if $0 {
vm.allSatisfy[index] = true
} else {
vm.allSatisfy[index] = false
}
return $0
}) {}
}
print(vm.allSatisfy)
}
private func showResult() async {
for (index, _) in vm.customFields.enumerated() {
let mirrorStruct = Mirror(reflecting: vm.customFields[index])
var count = 0
for field in mirrorStruct.children {
guard let currentField = field.label else {
return
}
if currentField == "first" {
nameFields = .firstField
} else if currentField == "second" {
nameFields = .secondField
} else {
nameFields = nil
}
count += 1
}
count = 0
self.isCheck(index)
}
self.allSatisfy()
}
}
struct CustomTextField: View {
@Binding var text: String
@FocusState.Binding var focus: NameFieldsTest?
var nameFieldFirst: NameFieldsTest
var submitPressed: Bool
var body: some View {
HStack(spacing: 5) {
TextField("", text: $text)
.foregroundColor(.black)
.tint(.black)
.focused($focus, equals: nameFieldFirst)
}
.padding(.horizontal, 16)
.padding(.vertical, 16)
.background(
RoundedRectangle(cornerRadius: 7)
.fill(text.isEmpty && submitPressed
? Color.red.opacity(0.4)
: Color.gray.opacity(0.4))
)
.cornerRadius(12)
}
}
struct CheckCircle: View {
var isCheck: Bool
var body: some View {
Circle()
.fill(isCheck ? Color.green : Color.red)
.frame(width: 6, height: 6)
}
}
#if DEBUG
struct TestTextFields_Previews: PreviewProvider {
static var previews: some View {
TestTextFields()
}
}
#endif