-
Notifications
You must be signed in to change notification settings - Fork 0
/
ThirdTabView.swift
116 lines (104 loc) · 4.89 KB
/
ThirdTabView.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import SwiftUI
struct ThirdTabView: View {
@ObservedObject var sharedDataModel: SharedDataModel
@State private var searchText: String = ""
@State private var selectedName: String? = nil // Track only one selected name
@State private var enteredAmount: String = "" // Track entered amount
let circleSize: CGFloat = 90 // Circle size for buttons
var body: some View {
VStack {
// Search Bar
TextField("Search contacts", text: $searchText)
.padding()
.background(Color(.systemGray6))
.cornerRadius(10)
.padding(.horizontal)
VStack {
// List of Contacts with checkmarks
List(filteredContacts, id: \.1) { name, phoneNumber in
Button(action: {
// Select only one person
if selectedName == name {
selectedName = nil // Deselect if tapped again
enteredAmount = ""
} else {
selectedName = name // Select new person
enteredAmount = "" // Reset amount when switching to a new person
}
}) {
HStack {
// Checkmark circle
Image(systemName: selectedName == name ? "checkmark.circle.fill" : "circle")
.foregroundColor(selectedName == name ? .blue : .gray)
VStack(alignment: .leading, spacing: 5) {
Text(name)
.font(.headline)
.padding(.bottom, 2)
.foregroundColor(.primary)
Text(phoneNumber)
.font(.subheadline)
.foregroundColor(.gray)
}
}
.padding(.vertical, 1)
}
.padding(.vertical, 10) // Fixed vertical padding to ensure consistency
.frame(minHeight: 60) // Minimum height for each row to ensure consistency
}
.background(Color.white)
.scrollContentBackground(.hidden)
.cornerRadius(10)
.padding(.bottom, 10) // Reduce bottom padding
Spacer()
// Static bottom UI with amount input and request button
HStack {
// Input box for amount
TextField("Enter Amount", text: $enteredAmount)
.keyboardType(.decimalPad)
.padding()
.background(Color(.systemGray6))
.cornerRadius(10)
.frame(width: 200) // Adjust width for proper alignment
Spacer()
// Request button
Button(action: {
if let selectedName = selectedName, !enteredAmount.isEmpty {
if let amountAsNumber = Double(enteredAmount) {
if let index = sharedDataModel.peopleDebt.firstIndex(where: { $0.0 == selectedName }) {
sharedDataModel.peopleDebt[index].1 += amountAsNumber
print("New value:");
print(sharedDataModel.peopleDebt[index]);
} else {
print("\(selectedName) not found in the peopleDebt array")
}
} else {
print("Invalid amount entered")
}
}
}) {
Text("Request")
.foregroundColor(.white)
.padding()
.background(Color.purple)
.cornerRadius(10)
}
}
.padding(.horizontal)
.padding(.bottom, 20) // Bring the buttons closer to the bottom of the list
}
}
}
// Filter contacts based on search text
var filteredContacts: [(String, String)] {
if searchText.isEmpty {
return sharedDataModel.fetchPeopleContactInfo()
} else {
return sharedDataModel.fetchPeopleContactInfo().filter {
$0.0.lowercased().contains(searchText.lowercased()) || $0.1.contains(searchText)
}
}
}
}
#Preview {
ThirdTabView(sharedDataModel: SharedDataModel())
}