在 Swiftui 中按日期过滤列表

Filter List as per Date in Swiftui

正在显示来自 API 的列表。我想根据日期显示列表。我的视图中有三个按钮:前一天、过去三天和一个提供日历以选择日期的按钮。(还没有实现第三个按钮)。这是视图:

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)
             }
             }
         }
         }

这是视图模型:

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 是一个包含 URL 请求代码的结构。四个参数被传递到 POST URL 请求主体 - CenterId(为了测试我硬编码了它将从 userdefaults 返回的值),SearchText(如果这两个按钮将用于另一个模块),StartDate 和 EndDate。现在代码是 运行 列表已成功加载到以下 YdaySample 结构中。

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 是用于详细视图的结构,YdayRow 用于列表行。正在加载列表。

但是,整个列表正在完全加载,而不是日期条件。在调试中,开始和结束日期显示正确。 简单地说,当我单击前一天时,列表应该只显示昨天和当前时间之间的记录。 IMO,问题是通过单击按钮发送 searchToday 变量以查看模型。 我想不通。

需要添加传递视图数据:YdaySample(tracking: trackViewModel)