How to make a Calendar List in swiftUI? - swift

Hello guys I'm trying to make a Calendar List in swiftUI :
I want to App get the current date info and show it in index 0 of allDays list and based on the subscription , App Generate an N number of Day object after current date automatically. ie : 365 for 1 year subscription.
The final result is look like this right now it's a hard code.
Here is my code DataModel :
import SwiftUI
import Foundation
import Combine
struct Day : Identifiable {
var id = UUID()
var name : String
var date : String
var title : String
var color : Color
var month : String
var List : [Name?]
}
struct Name : Identifiable {
var id = UUID()
var name: String
var color: Color
}
class AppState : ObservableObject {
#Published var allDays : [Day] = [
Day(name: "Sunday", date: "20", title: "", color: .pink, month: "Jun", List: [
Name(id: UUID(), name: "John", color: .pink),
Name(id: UUID(), name: "Rose", color: .pink),
Name(id: UUID(), name: "Mike", color: .pink),
]),
Day(name: "Monday", date: "21", title: "", color: .yellow, month: "Jun", List: [
Name(id: UUID(), name: "Sara", color: .yellow),
Name(id: UUID(), name: "Jack",color: .yellow),
]),
Day(name: "Tuesday", date: "22", title: "", color: .blue, month: "Jun", List: [
Name(id: UUID(), name: "Rachel",color: .blue),
]),
Day(name: "Wednesday", date: "23", title: "", color: .green, month: "Jun", List: []),
Day(name: "Thursday", date: "24", title: "", color: .orange, month: "Jun", List: []),
Day(name: "Friday", date: "25", title: "", color: .purple, month: "Jun", List: []),
Day(name: "Saturday", date: "26", title: "", color: .red, month: "Jun", List: []),
]
}
and View :
import SwiftUI
struct CalendarList: View {
#EnvironmentObject var appState : AppState
var body: some View {
NavigationView {
List {
ForEach(appState.allDays.indices, id:\.self) { index in
NavigationLink(destination: Text(appState.allDays[index].name) ) {
HStack(alignment: .top) {
RoundedRectangle(cornerRadius: 23)
.frame(width: 74, height: 74)
.foregroundColor(Color.blue)
.overlay(
VStack {
Text(appState.allDays[index].date)
.font(.system(size: 35, weight: .regular))
.foregroundColor(.white)
Text(appState.allDays[index].month)
.foregroundColor(.white)
}
)
.padding(.trailing ,4)
VStack(alignment: .leading, spacing: 5) {
Text(appState.allDays[index].name)
.font(.system(size: 20, weight: .semibold))
Text(appState.allDays[index].title)
.font(.subheadline)
.foregroundColor(Color(#colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)))
}
}
.padding(.vertical ,6)
}
}
}
.navigationTitle("Calendar")
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
struct CalendarList_Previews: PreviewProvider {
static var previews: some View {
CalendarList()
}
}

You can generate the Day models with a couple of DateFormatter and Calendar methods:
extension Date {
func dayOfWeek(withFormatter dateFormatter: DateFormatter) -> String? {
dateFormatter.dateFormat = "EEEE"
return dateFormatter.string(from: self).capitalized
}
func nameOfMonth(withFormatter dateFormatter: DateFormatter) -> String? {
dateFormatter.dateFormat = "LLLL"
return dateFormatter.string(from: self).capitalized
}
}
func getDays(number: Int) -> [Day] {
let today = Date()
let formatter = DateFormatter()
return (0..<number).map { index -> Day in
let date = Calendar.current.date(byAdding: .day, value: index, to: today) ?? Date()
return Day(name: date.dayOfWeek(withFormatter: formatter) ?? "", date: "\(Calendar.current.component(.day, from: date))", month: date.nameOfMonth(withFormatter: formatter) ?? "")
}
}
Update, due to comments. Full example:
struct Day : Identifiable {
var id = UUID()
var name : String
var date : String
var title : String
var color : Color
var month : String
var List : [Name?]
}
struct Name : Identifiable {
var id = UUID()
var name: String
var color: Color
}
class AppState : ObservableObject {
#Published var allDays : [Day] = []
func generateDays(number: Int){
let today = Date()
let formatter = DateFormatter()
self.allDays = (0..<number).map { index -> Day in
let date = Calendar.current.date(byAdding: .day, value: index, to: today) ?? Date()
return Day(name: date.dayOfWeek(withFormatter: formatter) ?? "", date: "\(Calendar.current.component(.day, from: date))", title: "", color: .red, month: date.nameOfMonth(withFormatter: formatter) ?? "", List: [])
}
}
}
extension Date {
func dayOfWeek(withFormatter dateFormatter: DateFormatter) -> String? {
dateFormatter.dateFormat = "EEEE"
return dateFormatter.string(from: self).capitalized
}
func nameOfMonth(withFormatter dateFormatter: DateFormatter) -> String? {
dateFormatter.dateFormat = "LLLL"
return dateFormatter.string(from: self).capitalized
}
}
struct CalendarList: View {
#EnvironmentObject var appState : AppState
var body: some View {
NavigationView {
List {
ForEach(appState.allDays.indices, id:\.self) { index in
NavigationLink(destination: Text(appState.allDays[index].name) ) {
HStack(alignment: .top) {
RoundedRectangle(cornerRadius: 23)
.frame(width: 74, height: 74)
.foregroundColor(Color.blue)
.overlay(
VStack {
Text(appState.allDays[index].date)
.font(.system(size: 35, weight: .regular))
.foregroundColor(.white)
Text(appState.allDays[index].month)
.foregroundColor(.white)
}
)
.padding(.trailing ,4)
VStack(alignment: .leading, spacing: 5) {
Text(appState.allDays[index].name)
.font(.system(size: 20, weight: .semibold))
Text(appState.allDays[index].title)
.font(.subheadline)
.foregroundColor(Color(#colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)))
}
}
.padding(.vertical ,6)
}
}
}
.navigationTitle("Calendar")
.onAppear {
appState.generateDays(number: 365)
}
}
.navigationViewStyle(StackNavigationViewStyle())
}
}
This is unrelated to your issue, but I'd also personally choose to use ForEach with direct access to the Day without going through the indices:
ForEach(appState.allDays, id:\.id) { day in
NavigationLink(destination: Text(day.name) ) {
HStack(alignment: .top) {
RoundedRectangle(cornerRadius: 23)
.frame(width: 74, height: 74)
.foregroundColor(Color.blue)
.overlay(
VStack {
Text(day.date)
.font(.system(size: 35, weight: .regular))
.foregroundColor(.white)
Text(day.month)
.foregroundColor(.white)
}
)
.padding(.trailing ,4)
VStack(alignment: .leading, spacing: 5) {
Text(day.name)
.font(.system(size: 20, weight: .semibold))
Text(day.title)
.font(.subheadline)
.foregroundColor(Color(#colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)))
}
}
.padding(.vertical ,6)
}
}

Related

Filter function inside For Each SwiftUI

I am trying to filter through data that I have retrieved from an API based on a model of car, I have a #State that holds an empty string initially and will update based on what the user has typed. I can't figure out how to filter the data based on the input inside of my existing ForEach loop, any help would be appreciated
import SwiftUI
struct HomeView: View {
#EnvironmentObject var users: Network
let columns = [
GridItem(.adaptive(minimum: 150))
]
#State var isActive: Bool = false
#State private var shouldAnimate = false
#State var show = false
#State private var searchText = ""
#Environment(\.colorScheme) var colorScheme
var searchResults: [Car] {
let searchModel = String(searchText)
return searchModel == nil ? users.users : users.users.filter{$0.model == searchModel}
}
var body: some View {
ScrollView {
if self.users.users.count > 0 {
Text("All \(self.users.users[1].make) vehicles")
.bold()
.textCase(.uppercase)
.font(.headline)
} else {
Spacer()
Spacer()
HStack(alignment: VerticalAlignment.center) {
Circle()
.fill(shouldAnimate ? Color.orange : Color.white)
.frame(width: 20, height: 20)
.scaleEffect(shouldAnimate ? 1.0 : 0.5)
.animation(
Animation.easeInOut(duration: 0.5).repeatForever(autoreverses: true), value: show)
Circle()
.fill(shouldAnimate ? Color.orange : Color.white)
.frame(width: 20, height: 20)
.scaleEffect(shouldAnimate ? 1.0 : 0.5)
.animation(
Animation.easeInOut(duration: 0.5).repeatForever(autoreverses: true).delay(0.3),
value: show)
Circle()
.fill(shouldAnimate ? Color.orange : Color.white)
.frame(width: 20, height: 20)
.scaleEffect(shouldAnimate ? 1.0 : 0.5)
.animation(
Animation.easeInOut(duration: 0.5).repeatForever(autoreverses: true).delay(0.6),
value: show)
}
Spacer()
VStack {
Text("Retrieving the Data...")
.foregroundColor(Color.white)
.textCase(.uppercase)
}
}
SearchBar(text: $searchText)
.padding(.top, -30)
LazyVGrid(columns: columns, spacing: 10) {
ForEach(searchResults) { car in
if car.year >= 2017 {
HStack(alignment: .top) {
VStack {
Image(systemName: "car.fill")
.resizable()
.scaledToFit()
.frame(width: 140.0, height: 70)
.foregroundColor(.white)
Text(car.model ?? "lol")
.font(.system(size: 15, weight: .bold, design: .default))
.foregroundColor(colorScheme == .dark ? Color.white : Color.black)
.frame(maxWidth: .infinity, alignment: .center)
Text(String(car.year))
.font(.system(size: 16, weight: .regular, design: .default))
.foregroundColor(.orange)
.frame(maxWidth: .infinity, alignment: .center)
Text(car.make)
.font(.system(size: 16, weight: .bold, design: .default))
.foregroundColor(colorScheme == .dark ? Color.white : Color.gray)
.frame(maxWidth: .infinity, alignment: .center)
}
.frame(height: 250)
.background(colorScheme == .dark ? Color.black.opacity(0.2) : Color.white)
.cornerRadius(20)
.padding()
.clipped()
.shadow(color: Color.black.opacity(0.24), radius: 10, x: 0, y: 0)
}
}
}
}
}
.background(colorScheme == .dark ? Color.black.opacity(0.1) : Color.white)
.onAppear {
users.getCars()
self.shouldAnimate = true
self.show = true
}
}
}
struct HomeView_Previews: PreviewProvider {
static var previews: some View {
HomeView()
.environmentObject(Network())
}
}
Data:
[
{
"id": 1119,
"make": "Audi",
"model": "100",
"generation": "100 Avant (4A,C4)",
"engine_modification": "2.8 V6 E (174 Hp) quattro Automatic",
"year": 1991,
"powertrain_architecture": "Internal Combustion engine",
"body_type": "Station wagon (estate)",
"number_of_seats": 5,
"number_of_doors": 5,
"urban_fuel_consumption": 13.5,
"extra_urban_fuel_consumption": 8,
"combined_fuel_consumption": 10,
"fuel_type": "Petrol (Gasoline)",
"acceleration": 9.5,
"top_speed": 207,
"power": 174,
"torque": 250,
"engine_location": "Front, Longitudinal",
"engine_displacement": 2771,
"number_of_cylinders": 6,
"position_of_cylinders": "V-engine",
"number_of_valves_per_cylinder": 2,
"fuel_system": "Multi-point indirect injection",
"engine_aspiration": "Naturally aspirated engine",
"kerb_weight": 1550,
"fuel_tank_capacity": 80,
"drive_wheel": "All wheel drive (4x4)",
"number_of_gears": 4,
"front_brakes": "Ventilated discs",
"rear_brakes": "Disc"
},
{
"id": 1120,
"make": "Audi",
"model": "100",
"generation": "100 Avant (4A,C4)",
"engine_modification": "2.8 V6 E (174 Hp) quattro",
"year": 1991,
"powertrain_architecture": "Internal Combustion engine",
"body_type": "Station wagon (estate)",
"number_of_seats": 5,
"number_of_doors": 5,
"urban_fuel_consumption": 13.5,
"extra_urban_fuel_consumption": 8,
"combined_fuel_consumption": 10,
"fuel_type": "Petrol (Gasoline)",
"acceleration": null,
"top_speed": null,
"power": 174,
"torque": 250,
"engine_location": "Front, Longitudinal",
"engine_displacement": 2771,
"number_of_cylinders": 6,
"position_of_cylinders": "V-engine",
"number_of_valves_per_cylinder": 2,
"fuel_system": "Multi-point indirect injection",
"engine_aspiration": "Naturally aspirated engine",
"kerb_weight": 1550,
"fuel_tank_capacity": 80,
"drive_wheel": "All wheel drive (4x4)",
"number_of_gears": 5,
"front_brakes": "Ventilated discs",
"rear_brakes": "Disc"
},
users.users
import Foundation
struct Car: Hashable,Codable, Identifiable {
let id : Int?
var make: String
var model: String?
var generation: String?
var engine_modification: String?
var year: Int
var powertrain_architecture: String?
var body_type: String?
var number_of_seats: Int?
var number_of_doors: Int?
var urban_fuel_consumption: Double?
var extra_urban_fuel_consumption: Double?
var combined_fuel_consumption: Double?
var fuel_type: String?
var acceleration: Double? // was Int
var top_speed: Int? // was Int
var power: Int?
var torque: Int?
var engine_location: String?
var engine_displacement: Int?
var number_of_cylinders: Int?
var position_of_cylinders: String?
var number_of_valves_per_cylinder: Int?
var fuel_system: String?
var engine_aspiration: String?
var kerb_weight: Int?
var fuel_tank_capacity: Double?
var drive_wheel: String?
var number_of_gears: Int?
var front_brakes: String?
var rear_brakes: String?
}
try something like this example code, assuming you want to search by year:
#State var searchText = ""
var searchResults: [Car] {
let searchYear = Int(searchText)
searchYear == nil ? users.users : users.users.filter{$0.year == searchYear!}
}
var body: some View {
// ...
SearchBar(text: $searchText)
// ...
LazyVGrid(columns: columns, spacing: 10) {
ForEach(searchResults) { car in
HStack(alignment: .top) {
VStack {
// ...
}
}
}
}
}
EDIT-1: to search for car model, try this:
var searchResults: [Car] {
searchText.isEmpty ? users.users : users.users.filter{
$0.model == nil ? false : $0.model!.contains(searchText)
}
}
EDIT-2: here is the code I used in my tests, to show that the approach works:
struct Car: Identifiable {
let id = UUID()
var year: Int
var make: String
var model: String?
}
struct ContentView: View {
#State var cars = [Car(year: 123, make: "aaa", model: "bbb"),
Car(year: 456, make: "cc", model: "dd"),
Car(year: 789, make: "eee", model: "dd")]
#State private var searchText = ""
var searchResults: [Car] {
searchText.isEmpty ? cars : cars.filter{
$0.model == nil ? false : $0.model!.contains(searchText)
}
}
var body: some View {
NavigationView {
List (searchResults) { car in
Text(car.model ?? "no data")
}
}
.searchable(text: $searchText)
}
}

How to save a value correctly to userdefaults?

I have an issue and I hope that I will find the answer here to my questions. I made a Circular Timer, but everytime I'm closing the view , is going back from the start.
I tried to save the value where the time is calculated "onTick", is working for 2-3 seconds, and after again is going back from the start and is countdown from there.
I'll put a picture/video bellow with the behaviour, maybe somone can help me fix it .
Thanks !
This is WaitingOrderView.
struct WaitingOrderView: View {
#State private var timer: AnyCancellable?
#EnvironmentObject var syncViewModel : SyncViewModel
var body: some View {
ZStack {
if syncViewModel._order.id == 0 && syncViewModel._order.status == 0 {
CartView()
}
else if syncViewModel._order.status == syncViewModel.statusList.first(where: { status in
status.key == StatusKey.accepted.rawValue
})?.id
{
OrderConfirmedView()
}
}
.onAppear() {
startFetchStatus()
}
}
func startFetchStatus() {
timer = Timer.publish(every: 20, on: .main, in: .common)
.autoconnect()
.sink { _ in
syncViewModel.fetchOrder(id: syncViewModel._order.id)
}
}
}
The function startFetchStatus() gets the data from the backend every 20 seconds, and it looks like this, for example: Fetch response
This is the CircularTimer View :
let timer = Timer
.publish(every: 5, on: .main, in: .common)
.autoconnect()
#available(iOS 15, *)
struct CircularTimer: View {
#EnvironmentObject var syncViewModel : SyncViewModel
#State var orderDate : Date
#State var orderDeliveryDate : Date
#State var onTick: Double = 1
#State var savedOnTick : Double = 1
let date = Date()
var body: some View {
VStack(spacing : 0){
Image("clock_button")
ZStack{
Circle()
.fill(Color.clear)
.frame(width: 250, height: 250)
.overlay(
Circle().stroke(Color.gray.opacity(22/100), lineWidth: 5)
)
Circle()
.fill(Color.clear)
.frame(width: 250, height: 250)
.overlay(
Circle().trim(from:0, to: onTick)
.stroke(
style: StrokeStyle(
lineWidth: 5,
lineCap: .round ,
lineJoin:.round
)
)
.foregroundColor(
( completed() ? Color.orange : Color.orange)
).animation(
.easeInOut(duration: 0.2)
)
)
.rotationEffect(Angle(degrees: 270.0))
Image("indicator_ellipse")
.resizable()
.frame(width: 230, height: 230)
}
.onAppear {
getData()
print(savedOnTick)
}
.onDisappear {
saveData()
}
}
.onReceive(timer) { time in
progress(time: Int(time.timeIntervalSince1970))
}
}
func completed() -> Bool {
return onTick == 1
}
func progress(time: Int) {
let minutesOrderDeliveryDate = Int(orderDeliveryDate.timeIntervalSince1970)
let minutesOrderDate = Int(orderDate.timeIntervalSince1970)
let minutesCurrentDate = time
let totalMinutes = minutesOrderDeliveryDate - minutesOrderDate
let remainingMinutes = minutesOrderDeliveryDate - minutesCurrentDate
onTick = CGFloat(remainingMinutes) / CGFloat(totalMinutes)
}
func dateFormatTime(date : String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.calendar = Calendar(identifier: .gregorian)
dateFormatter.locale = Locale(identifier: "en_US_POSIX")
return dateFormatter.date(from: date) ?? Date()
}
func saveData() {
UserDefaults.standard.set(onTick, forKey: "onTick")
}
func getData() {
onTick = UserDefaults.standard.double(forKey: "onTick")
}
}
This is OrderConfirmedView :
struct OrderConfirmedVieww: View {
#EnvironmentObject var syncViewModel : SyncViewModel
#State var nowDate: Date = Date()
var body: some View {
VStack {
Spacer()
Text(Texts.orderConfirmedText1)
.font(.title)
.fontWeight(.semibold)
.foregroundColor(.colorGrayDark)
.multilineTextAlignment(.center)
.lineLimit(3)
.padding(40)
CircularTimer(orderDate: dateFormatTime(date: syncViewModel._order.date ?? ""), orderDeliveryDate: dateFormatTime(date: syncViewModel._order.deliveryDate ?? ""))
.padding()
// Spacer()
Text(Texts.orderOraLivrareText)
.font(.headline)
.fontWeight(.thin)
.padding(.bottom)
Text(dateFormatTime(date: syncViewModel._order.deliveryDate ?? ""), style: .time)
.font(.title2)
.fontWeight(.bold)
Spacer()
Spacer()
Button {
} label: {
Text(Texts.orderButtonText)
.font(.headline)
.foregroundColor(.white)
.frame(height: 55)
.frame(maxWidth: .infinity)
.background(Color.onboardingColor)
.cornerRadius(20)
}
.padding()
}
}
func dateFormatTime(date : String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = .current
return dateFormatter.date(from: date) ?? Date.now
}
}

How to save a Timer value when the view is closed?

I've made a similiar question yesterday, but I have fixed the problem. Now I have another issue and I made a new question to ask.
I've made a Circular Timer which countdown : CircularTimer
But if I'm closing the view, and open it again, it starts again from scratch. How can I save the progress of the timer or something like that, so if I'm closing the view, when I'm coming back to be where it was last time ?
Thanks !
This is WaitingOrderView.
struct WaitingOrderView: View {
#State private var timer: AnyCancellable?
#EnvironmentObject var syncViewModel : SyncViewModel
var body: some View {
ZStack {
if syncViewModel._order.id == 0 && syncViewModel._order.status == 0 {
CartView()
}
else if syncViewModel._order.status == syncViewModel.statusList.first(where: { status in
status.key == StatusKey.accepted.rawValue
})?.id
{
OrderConfirmedView()
}
}
.onAppear() {
startFetchStatus()
}
}
func startFetchStatus() {
timer = Timer.publish(every: 20, on: .main, in: .common)
.autoconnect()
.sink { _ in
syncViewModel.fetchOrder(id: syncViewModel._order.id)
}
}
}
The function startFetchStatus() gets the data from the backend every 20 seconds, and it looks like this, for example: Fetch response
This is the CircularTimer View :
let timer = Timer
.publish(every: 1, on: .main, in: .common)
.autoconnect()
#available(iOS 15, *)
struct CircularTimer: View {
var orderDate : Date
var orderDeliveryDate : Date
#State var onTick: CGFloat = 1
let date = Date()
var body: some View {
VStack(spacing : 0){
Image("clock_button")
ZStack{
Circle()
.fill(Color.clear)
.frame(width: 250, height: 250)
.overlay(
Circle().stroke(Color.gray.opacity(22/100), lineWidth: 5)
)
Circle()
.fill(Color.clear)
.frame(width: 250, height: 250)
.overlay(
Circle().trim(from:0, to: onTick)
.stroke(
style: StrokeStyle(
lineWidth: 5,
lineCap: .round ,
lineJoin:.round
)
)
.foregroundColor(
( completed() ? Color.orange: Color.orange)
).animation(
.easeInOut(duration: 0.2)
)
)
.rotationEffect(Angle(degrees: 270.0))
Image("indicator_ellipse")
.resizable()
.frame(width: 230, height: 230)
}
}.onReceive(timer) { time in
progress(time: Int(time.timeIntervalSince1970))
}
}
func completed() -> Bool {
return onTick == 1
}
func progress(time: Int) {
let minutesOrderDeliveryDate = Int(orderDeliveryDate.timeIntervalSince1970)
let minutesOrderDate = Int(orderDate.timeIntervalSince1970)
let minutesCurrentDate = time
let totalMinutes = minutesOrderDeliveryDate - minutesOrderDate
let remainingMinutes = minutesOrderDeliveryDate - minutesCurrentDate
onTick = CGFloat(remainingMinutes) / CGFloat(totalMinutes)
print(onTick)
}
func dateFormatTime(date : String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = .current
return dateFormatter.date(from: date) ?? Date.now
}
}
This is OrderConfirmedView :
struct OrderConfirmedVieww: View {
#EnvironmentObject var syncViewModel : SyncViewModel
#State var nowDate: Date = Date()
var body: some View {
VStack {
Spacer()
Text(Texts.orderConfirmedText1)
.font(.title)
.fontWeight(.semibold)
.foregroundColor(.colorGrayDark)
.multilineTextAlignment(.center)
.lineLimit(3)
.padding(40)
CircularTimer(orderDate: dateFormatTime(date: syncViewModel._order.date ?? ""), orderDeliveryDate: dateFormatTime(date: syncViewModel._order.deliveryDate ?? ""))
.padding()
// Spacer()
Text(Texts.orderOraLivrareText)
.font(.headline)
.fontWeight(.thin)
.padding(.bottom)
Text(dateFormatTime(date: syncViewModel._order.deliveryDate ?? ""), style: .time)
.font(.title2)
.fontWeight(.bold)
Spacer()
Spacer()
Button {
} label: {
Text(Texts.orderButtonText)
.font(.headline)
.foregroundColor(.white)
.frame(height: 55)
.frame(maxWidth: .infinity)
.background(Color.onboardingColor)
.cornerRadius(20)
}
.padding()
}
}
func dateFormatTime(date : String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss"
dateFormatter.timeZone = .current
return dateFormatter.date(from: date) ?? Date.now
}
}

Change selected cell Baground Color in SwiftUI

I write the following code and brought this kind of output.
By using the .onTapGesture method I find the selected day from HStack
But I want to change the background color based on cell selection in Hstack.
Anyone give some idea or suggestions for changing the selected cell Background color on HStack.
Or share any references with me.
struct CalendarDay: Identifiable {
let id = UUID()
var number: String
var weekday: String
var isToday: Bool
}
struct ContentView: View {
#State var days = [CalendarDay]()
var body: some View {
ZStack{
VStack {
//MARK: CALENDAR
ScrollView(.horizontal, showsIndicators: false){
HStack(spacing: 20) {
ForEach(days) { day in
CalendarView(
number: day.number,
days: day.weekday,
color: day.isToday ? #colorLiteral(red: 0.9060331583, green: 0.2547450066, blue: 0.3359550834, alpha: 1) : #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1),
textcolor: day.isToday ? #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) : #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
)
.onTapGesture{
print(day)
}
}
}
.padding(.leading,10)
.padding(.bottom, 10)
.shadow(radius: 3, x: 3, y: 3)
}
}
}
.onAppear {
getCurrentWeekdays()
}
}
func getCurrentWeekdays() {
/// from https://stackoverflow.com/a/62355272/14351818
let dateComponents = Calendar(identifier: .gregorian).dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date())
let startOfWeek = Calendar(identifier: .gregorian).date(from: dateComponents)!
let startOfWeekNoon = Calendar(identifier: .gregorian).date(bySettingHour: 12, minute: 0, second: 0, of: startOfWeek)!
days = (0...6).map {
let calendar = Calendar(identifier: .gregorian)
let date = calendar.date(byAdding: .day, value: $0, to: startOfWeekNoon)!
let numberDateFormatter = DateFormatter()
numberDateFormatter.dateFormat = "d"
let number = numberDateFormatter.string(from: date)
let weekdayDateFormatter = DateFormatter()
weekdayDateFormatter.dateFormat = "E"
let weekday = weekdayDateFormatter.string(from: date)
let calendarDay = CalendarDay(
number: number,
weekday: weekday,
isToday: calendar.component(.day, from: Date()) == calendar.component(.day, from: date)
)
return calendarDay
}
}
}
struct CalendarView: View {
var number : String
var days : String
var color : UIColor
var textcolor : UIColor
var body: some View {
VStack{
Text(self.number)
.font(.system(size: 20, weight: .bold, design: .rounded))
.foregroundColor(Color(self.textcolor))
Text(self.days)
.font(.headline)
.foregroundColor(Color(self.textcolor))
}.padding([.top,.bottom], 10)
.padding([.leading,.trailing],10)
.background(Color(self.color))
.cornerRadius(30)
}
}
try something like this:
import SwiftUI
struct CalendarDay: Identifiable {
let id = UUID()
var number: String
var weekday: String
var isToday: Bool
}
struct ContentView: View {
#State var days = [CalendarDay]()
var body: some View {
ZStack{
VStack {
ScrollView(.horizontal, showsIndicators: false){
HStack(spacing: 20) {
// <---
ForEach(days.indices, id: \.self) { i in
CalendarView(
number: days[i].number,
days: days[i].weekday,
color: days[i].isToday ? #colorLiteral(red: 0.9060331583, green: 0.2547450066, blue: 0.3359550834, alpha: 1) : #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1),
textcolor: days[i].isToday ? #colorLiteral(red: 1, green: 1, blue: 1, alpha: 1) : #colorLiteral(red: 0, green: 0, blue: 0, alpha: 1)
)
.onTapGesture{
print(days[i])
// this is just for replacing the current selection
for j in days.indices { days[j].isToday = false }
days[i].isToday = true
}
}
// <---
}
.padding(.leading,10)
.padding(.bottom, 10)
.shadow(radius: 3, x: 3, y: 3)
}
}
}
.onAppear {
getCurrentWeekdays()
}
}
func getCurrentWeekdays() {
/// from https://stackoverflow.com/a/62355272/14351818
let dateComponents = Calendar(identifier: .gregorian).dateComponents([.yearForWeekOfYear, .weekOfYear], from: Date())
let startOfWeek = Calendar(identifier: .gregorian).date(from: dateComponents)!
let startOfWeekNoon = Calendar(identifier: .gregorian).date(bySettingHour: 12, minute: 0, second: 0, of: startOfWeek)!
days = (0...6).map {
let calendar = Calendar(identifier: .gregorian)
let date = calendar.date(byAdding: .day, value: $0, to: startOfWeekNoon)!
let numberDateFormatter = DateFormatter()
numberDateFormatter.dateFormat = "d"
let number = numberDateFormatter.string(from: date)
let weekdayDateFormatter = DateFormatter()
weekdayDateFormatter.dateFormat = "E"
let weekday = weekdayDateFormatter.string(from: date)
let calendarDay = CalendarDay(
number: number,
weekday: weekday,
isToday: calendar.component(.day, from: Date()) == calendar.component(.day, from: date)
)
return calendarDay
}
}
}
struct CalendarView: View {
var number : String
var days : String
var color : UIColor
var textcolor : UIColor
var body: some View {
VStack{
Text(self.number)
.font(.system(size: 20, weight: .bold, design: .rounded))
.foregroundColor(Color(self.textcolor))
Text(self.days)
.font(.headline)
.foregroundColor(Color(self.textcolor))
}.padding([.top,.bottom], 10)
.padding([.leading,.trailing],10)
.background(Color(self.color))
.cornerRadius(30)
}
}

SwiftUI Problem GeometryReader with Zstack

I'm trying to create my own PhoneTextField in and I have found a tutorial in here it's working fine if don't call it inside ZStack let's see the code example as below
countryCode View
struct CountryCodes: View {
#Binding var countryCode : String
#Binding var countryFlag : String
#Binding var y : CGFloat
var body: some View {
GeometryReader { geo in
List(self.countryDictionary.sorted(by: <), id: \.key) { key , value in
HStack {
Text("\(self.flag(country: key))")
Text("\(self.countryName(countryCode: key) ?? key)")
Spacer()
Text("+\(value)").foregroundColor(.secondary)
}.background(Color.white)
.font(.system(size: 20))
.onTapGesture {
self.countryCode = value
self.countryFlag = self.flag(country: key)
withAnimation(.spring()) {
self.y = 350
}
}
}
.padding(.bottom)
.frame(width: geo.size.width, height: 500)
.position(x: geo.frame(in: .global).midX, y: geo.frame(in: .global).maxY - -200)
}
}
let countryDictionary = ["AF":"93","AL":"355","DZ":"213","US":"1",
"AD":"376","AO":"244","AI":"1","AG":"1","AR":"54",
"AM":"374","AW":"297","AU":"61","AT":"43","AZ":"994",
"BS":"1","BH":"973","BD":"880","BB":"1","BY":"375",
"BE":"32","BZ":"501","BJ":"229","BM":"1","BT":"975",
"BA":"387","BW":"267","BR":"55","IO":"246","BG":"359",
"BF":"226","BI":"257","KH":"855","CM":"237","CA":"1",
"CV":"238","KY":"345","CF":"236","TD":"235","CL":"56","CN":"86",
"CX":"61","CO":"57","KM":"269","CG":"242","CK":"682","CR":"506",
"HR":"385","CU":"53","CY":"537","CZ":"420","DK":"45","DJ":"253",
"DM":"1","DO":"1","EC":"593","EG":"20","SV":"503","GQ":"240",
"ER":"291","EE":"372","ET":"251","FO":"298","FJ":"679","FI":"358",
"FR":"33","GF":"594","PF":"689","GA":"241","GM":"220","GE":"995",
"DE":"49","GH":"233","GI":"350","GR":"30","GL":"299","GD":"1",
"GP":"590","GU":"1","GT":"502","GN":"224","GW":"245","GY":"595","HT":"509",
"HN":"504","HU":"36","IS":"354","IN":"91","ID":"62","IQ":"964",
"IE":"353","IL":"972","IT":"39","JM":"1","JP":"81","JO":"962",
"KZ":"77","KE":"254","KI":"686","KW":"965","KG":"996","LV":"371",
"LB":"961","LS":"266","LR":"231","LI":"423","LT":"370","LU":"352",
"MG":"261","MW":"265","MY":"60","MV":"960","ML":"223",
"MT":"356","MH":"692","MQ":"596","MR":"222","MU":"230","YT":"262",
"MX":"52","MC":"377","MN":"976", "ME":"382","MS":"1","MA":"212",
"MM":"95","NA":"264","NR":"674","NP":"977","NL":"31","NC":"687",
"NZ":"64","NI":"505","NE":"227","NG":"234","NU":"683",
"NF":"672","MP":"1","NO":"47","OM":"968","PK":"92","PW":"680",
"PA":"507","PG":"675","PY":"595","PE":"51","PH":"63","PL":"48",
"PT":"351","PR":"1","QA":"974","RO":"40","RW":"250","WS":"685",
"SM":"378","SA":"966","SN":"221","RS":"381","SC":"248",
"SL":"232","SG":"65","SK":"421","SI":"386","SB":"677",
"ZA":"27","GS":"500","ES":"34","LK":"94","SD":"249","SR":"597",
"SZ":"268","SE":"46","CH":"41","TJ":"992","TH":"66","TG":"228",
"TK":"690","TO":"676","TT":"1","TN":"216","TR":"90",
"TM":"993","TC":"1","TV":"688","UG":"256","UA":"380",
"AE":"971","GB":"44","AS":"1","UY":"598","UZ":"998",
"VU":"678","WF":"681","YE":"967","ZM":"260",
"ZW":"263","BO":"591","BN":"673","CC":"61",
"CD":"243","CI":"225","FK":"500","GG":"44",
"VA":"379","HK":"852","IR":"98","IM":"44",
"JE":"44","KP":"850","KR":"82","LA":"856",
"LY":"218","MO":"853","MK":"389","FM":"691",
"MD":"373","MZ":"258","PS":"970","PN":"872",
"RE":"262","RU":"7","BL":"590","SH":"290","KN":"1",
"LC":"1","MF":"590","PM":"508","VC":"1","ST":"239",
"SO":"252","SJ":"47","SY":"963","TW":"886","TZ":"255",
"TL":"670","VE":"58","VN":"84","VG":"284","VI":"340"]
func countryName(countryCode: String) -> String? {
let current = Locale(identifier: "en_US")
return current.localizedString(forRegionCode: countryCode)
}
func flag(country:String) -> String {
let base : UInt32 = 127397
var flag = ""
for v in country.unicodeScalars {
flag.unicodeScalars.append(UnicodeScalar(base + v.value)!)
}
return flag
}
}
RegistrationView
//
// RegisterView.swift
// Instagram
//
// Created by Admin on 4/4/21.
//
import SwiftUI
import Combine
struct RegistrationView: View {
#State var isEditing: Bool = false
// navigate to verification view
#State private var selection: String? = nil
#State private var email:String = ""
#State private var phone:String = ""
#State private var fullname = ""
#State private var username = ""
#State private var password = ""
#State private var selectedImage:UIImage?
#State private var image:Image?
#Environment(\.presentationMode) var mode
#State var imagePickerPresented = false
#EnvironmentObject var viewModel: AuthViewModel
#State var isVerification = false
#State var phoneNumber = ""
#State var y : CGFloat = 350
#State var countryCode = ""
#State var countryFlag = ""
#State var isInputPhoneNumber = true
#State var isInputEmail = false
#State var tempData = DataTempo()
#State var isShowSignInButton = true
// var successClosure: (Bool) -> Void {
//
// }
#EnvironmentObject var mobileVerificationViewModel: MobileVerificationNvm
var body: some View {
ZStack {
LinearGradient(gradient: Gradient(colors: [Color.purple, Color("PrimaryColor")]), startPoint: .top, endPoint: .bottom)
.ignoresSafeArea()
VStack{
// Spacer()
VStack(spacing:20){
VStack(alignment:.leading){
Text("Sign Up")
.font(.largeTitle)
.bold()
.foregroundColor(.white)
}.padding(.horizontal,32).frame(width: UIScreen.main.bounds.width, alignment: .leading)
Toggle( isInputEmail ? "Use Email" : "Use Phone Number", isOn: $isInputEmail)
.foregroundColor(.white)
.toggleStyle(SwitchToggleStyle(tint: Color(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1))))
.padding(.horizontal,32)
if isInputEmail == true {
CustomTextField(text: $email, placeholder: Text("Email"), imageName: "envelope")
.padding()
.background(Color(.init(white: 1, alpha: 0.15)))
.cornerRadius(10)
.foregroundColor(.white)
.padding(.horizontal,32)
.onChange(of: email) {
// self.autocomplete($0) // like this
if viewModel.isValidEmail(testStr: $0){
print("email")
//phone number
}
}
}
if isInputEmail == false{
ZStack(alignment:.center) {
HStack (spacing: 0) {
Text(countryCode.isEmpty ? "🇹🇭 +66" : "\(countryFlag) +\(countryCode)")
.frame(width: 100, height: 50)
// .background(Color.secondary.opacity(0.2))
.background(Color(.init(white: 1, alpha: 0.15)))
.cornerRadius(10)
// .foregroundColor(countryCode.isEmpty ? .secondary : .black)
.foregroundColor(.white)
.onTapGesture {
isShowSignInButton = false
withAnimation (.spring()) {
self.y = 0
}
}
TextField("Phone Number", text: $phone)
.onChange(of: phone) { newValue in
if !phone.isEmpty {
self.isShowSignInButton = true
if viewModel.validatePhoneNumber(value:newValue){
print("phone")
//phone number
self.isInputPhoneNumber = true
self.isInputEmail = false
}
}
else{
self.isShowSignInButton = false
}
}
.frame(width: 250, height: 50)
.keyboardType(.phonePad)
.background(Color(.init(white: 1, alpha: 0.15)))
.foregroundColor(.white)
}
CountryCodes(countryCode: $countryCode, countryFlag: $countryFlag, y: $y)
.offset(y: y)
RoundedRectangle(cornerRadius: 10).stroke()
.frame(width: 350, height: 50)
.foregroundColor(.white)
} .zIndex(1).frame( height: 50, alignment: .center)
}
CustomTextField(text: $username, placeholder: Text("Username"), imageName: "person")
.padding()
.background(Color(.init(white: 1, alpha: 0.15)))
.cornerRadius(10)
.foregroundColor(.white)
.padding(.horizontal,32)
CustomTextField(text: $fullname, placeholder: Text("Full Name"), imageName: "person")
.padding()
.background(Color(.init(white: 1, alpha: 0.15)))
.cornerRadius(10)
.foregroundColor(.white)
.padding(.horizontal,32)
CustomSecureField(text: $password, placeholder: Text("Password"))
.padding()
.background(Color(.init(white: 1, alpha: 0.15)))
.cornerRadius(10)
.foregroundColor(.white)
.padding(.horizontal,32)
}
.alert(isPresented: $isInputPhoneNumber, content: {
Alert(title: Text("Message"), message: Text("you're using phone number to register"), dismissButton: .default(Text("Bye alert!"), action: {
isInputPhoneNumber = true
}))
})
.alert(isPresented: mobileVerificationViewModel.alertBinding) {
Alert(title: Text(mobileVerificationViewModel.errorMessage ?? "(unknown)"), message: nil, dismissButton: .cancel())
}
HStack{
Spacer()
Button(action: /*#START_MENU_TOKEN#*/{}/*#END_MENU_TOKEN#*/, label: {
Text("Forgot Password")
})
.font(.system(size: 13, weight: .semibold))
.foregroundColor(.white)
.padding(.top)
.padding(.trailing,28)
}
ZStack{
NavigationLink(destination:VerificationView { (pin, successClosure) in
print(pin)
mobileVerificationViewModel.codeVerification(phoneNumber: isInputEmail ? tempData.email : tempData.phone, pinCode: pin, email: email, password: password, fullname: fullname, username: username)
//navigation back
self.mode.wrappedValue.dismiss()
// print(successClosure)
}, tag: "verification", selection: $selection) { EmptyView() }
Button(action: {
self.selection = "verification"
var phoneWithCountryCode = ""
// if countryCode == "" {
// phoneWithCountryCode = "+66\(phone)"
// }
// if countryCode != ""{
// phoneWithCountryCode = "+\(countryCode)\(phone)"
// }
//
// // "+\(countryCode == "" ? "66" : countryCode + phone)"
// if (!email.isEmpty || !phone.isEmpty) && !password.isEmpty{
//
// if isInputEmail {
// MobileVerificationNvm.shared.sendVerification(phoneNumber:email)
// isVerification = true
// tempData.email = email
// }
//
// else{
// DispatchQueue.main.async {
// // print("phone:\(phoneWithCountryCode)")
// MobileVerificationNvm.shared.sendVerification(phoneNumber: phoneWithCountryCode)
// isVerification = true
// tempData.phone = phone
// }
//
// }
//
// }
// viewModel.register(withEmail: email, password: password,image: selectedImage,fullname: fullname,username: username)
}, label: {
Text("Sign Up")
}).zIndex(0)
// Spacer()
}
.font(.headline)
.frame(width: 360, height: 50)
.foregroundColor(.white)
.background(Color(#colorLiteral(red: 0.5568627715, green: 0.3529411852, blue: 0.9686274529, alpha: 1)))
// .clipShape(Capsule())
.clipShape(RoundedRectangle(cornerRadius: 16, style: .continuous))
.padding()
.alert(isPresented: $isInputEmail, content: {
Alert(title: Text("Message"), message: Text("Now! you're using Email to register but you also register with email just swtich on togle button"), dismissButton: .default(Text("Bye alert!"), action: {
isInputEmail = true
}))
})
Button(action: {
mode.wrappedValue.dismiss()
// isVerification = true
}, label: {
Text("Already have an account? ")
.font(.system(size: 14))
+ Text("Sign In")
.font(.system(size: 14, weight: .bold))
})
.foregroundColor(.white)
.padding(.bottom,32)
}
}
.ignoresSafeArea()
}
}
extension RegistrationView{
func loadImage() {
guard let selectedImage = selectedImage else {
return
}
image = Image(uiImage: selectedImage)
}
}
//
struct RegistrationView_Previews: PreviewProvider {
static var previews: some View {
RegistrationView()
}
}
this is my issue when I tap on country code to choose the country code you will see the button sign in over countries code list
you also can check from [here]:github.com/PhanithNoch/SwiftUIPhoneTextField this is the repo that I tried to separate code from my real project as an example
I stuck with this a few days ago hope anyone can help, please.