Displaying a list from API. I want to show list based on date. There are three buttons in my view : Previous Day, Last Three Days, and a button providing calendar to chose dates.(Havent implemented the third button yet). Here is the view:
struct TrackSampleOptions: View {
@StateObject var trackViewModel = TrackViewModel()
var body: some View {
NavigationView{
VStack{
NavigationLink(destination: YdaySample(),isActive:
$trackViewModel.navigate,
label:{ Button(action: {
trackViewModel.searchToday = "One"
trackViewModel.getTrack()
},
label:{ Text("Previous Day")
}).buttonStyle(trackButton())
}) .navigationTitle("Track Options")
.navigationBarTitleDisplayMode(.inline)
NavigationLink(destination: YdaySample(),isActive:
$trackViewModel.navigate,
label:{Button(action: {
trackViewModel.searchToday = "Two"
trackViewModel.getTrack()
},
label:{
Text("Last Three Days")
}).buttonStyle(trackButton())
}) .navigationTitle("Track Options")
.navigationBarTitleDisplayMode(.inline)
}
}
}
}
Here is the viewModel:
class TrackViewModel : ObservableObject
{
@Published var trackReport = [TrackResponse]()
@Published var navigate:Bool = false
@Published var searchToday: String = ""
@Published var centerID: String = String()
@Published var errorMessage:String = String()
private let trackResource = TrackResource()
func getTrack()
{
var startDate:String = String()
var endDate:String = String()
var searchText:String = String()
if(searchToday == "One")
{
let today:Date = Date()
var dayafter:Date = Date()
dayafter = Calendar.current.date(byAdding: .day, value: -1,
to: Date())!
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let start = formatter.string(from: dayafter)
startDate = start + " 00:00:00"
let endFormatter = DateFormatter()
endFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
endDate = endFormatter.string(from: today)
searchText = ""
}
else if (searchToday == "Two")
{
let today:Date = Date()
var dayafter:Date = Date()
dayafter = Calendar.current.date(byAdding: .day, value: -3,
to: Date())!
let formatter = DateFormatter()
formatter.dateFormat = "yyyy-MM-dd"
let start = formatter.string(from: dayafter)
startDate = start + " 00:00:00"
let endFormatter = DateFormatter()
endFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
endDate = endFormatter.string(from: today)
searchText = ""
}
centerID = "668"
let trackRequest = TrackRequest(CenterId:centerID,
SearchText:searchText, StartDate:startDate, EndDate:endDate)
trackResource.track(trackRequest: trackRequest)
{
response in
if(response?.success==true)
{
DispatchQueue.main.async {
self.navigate = true
self.trackReport = response?.trackResponse ?? []
}
}
else
{
DispatchQueue.main.async {
self.errorMessage = response?.message ?? "No Data"
}
}
}
}
TrackResource is a struct which contains the URL Request code. Four params are passed into the POST URLRequest body - CenterId(for testing i hardcoded the value it will be returned from userdefaults), SearchText(which is empty in case of these two buttons will be used in another module), StartDate and EndDate. Now code is running list is loaded into the following YdaySample struct successfully.
import SwiftUI
struct YdaySample: View {
@StateObject var tracking = TrackViewModel()
var body: some View {
NavigationView{
List{
ForEach(tracking.trackReport)
{
truck in
NavigationLink(destination:
TrackDetail(track:truck))
{
YdayRow(truck: truck)
}
}
}.onAppear
{
tracking.getTrack()
}
}
}
}
TrackDetail is a struct for detail view and YdayRow is for row of list. List is loading.
However, entire list is loading completely instead of date criteria. In debug, start and end date are showing correctly. Simply, when i click previous Day, list should only show records between yesterday and current time. IMO, the issue is sending searchToday variable to view model by click of button. I am not able to figure this out.
need to add to pass view data: YdaySample(tracking: trackViewModel)