SwiftUI: как отображать новое представление каждый день в зависимости от даты, при этом предыдущие просмотры продолжались неделю - PullRequest
0 голосов
/ 11 июля 2020

Так что, честно говоря, я застрял. Я пытаюсь сделать что-то похожее на App Store, где есть представление «Сегодня», которое содержит вещи с текущей даты. Под этим я хочу показать последние 6 дней. Я понял, как сделать так, чтобы новое представление появлялось каждый день с прошлой неделей, если последняя неделя находится в том же месяце. Я не могу понять, как заставить его работать, когда наступает новый месяц, поскольку я делаю это, используя текущую дату, вычитая 6 дней, а затем получаю диапазон между этими двумя днями. Он прерывается с наступлением нового месяца, так как диапазон меняется с 12 ... 18 на 30 ... 5, что нарушает его! При этом все немного сложнее. Представление, которое я пытаюсь повторить, имеет 3 входа - год, месяц и день. День - это то, что я использую для итерации. Вот мой код:

// HomeView //

struct HomeView: View{
    
    let future = Calendar.current.date(byAdding: DateComponents(day: 25), to: Date())!
    
    let aWeekAgo = Calendar.current.date(byAdding: DateComponents(day: -6), to: Calendar.current.date(byAdding: DateComponents(day: 28), to: Date())!)!
    
    var body: some View {
        ScrollView {
            VStack(spacing: 40) {
                ForEach((dayRange).reversed(), id: \.self) { i in
                    CardView(day: yearData[0].months[0].dayInfo[i])
                }
            }.padding(.bottom, 40)
        }
    }
}

// Расширение HomeView //

extension HomeView {
    
    var dayRange: Range<Int> {
        let day = Calendar.current.dateComponents([.day, .month, .year], from: future)
        let dayAWeekago = Calendar.current.dateComponents([.day, .month, .year], from: aWeekAgo)
        
        return ((dayAWeekago.day! - 1)..<day.day!)
    }
    
    var monthRange: ClosedRange<Int> {
        let month = Calendar.current.dateComponents([.month], from: future)
        let monthAWeekAgo = Calendar.current.dateComponents([.month], from: aWeekAgo)
        
        return ((monthAWeekAgo.month! - 7)...(month.month! - 7))
    }
}

// CardView //

struct CardView: View {
    
    var day: YearModel.MonthModel.DayModel
    @Environment(\.colorScheme) var colorScheme
    
    var body: some View {
        Image(day.imageName)
            .resizable()
            .scaledToFill()
            .frame(width: UIScreen.main.bounds.width - 40, height: UIScreen.main.bounds.height * 0.6, alignment: .top)
            .border(Color(.sRGB, red: 150/255, green: 150/255, blue: 150/255, opacity: 0.1), width: 1)
            .overlay(
                VStack {
                Spacer()
                
                Rectangle()
                    .frame(minHeight: 50, maxHeight: 100)
                    .overlay(
                        HStack {
                            VStack(alignment: .leading) {
                                Text("\(day.date) - DAY \(day.id)")
                                    .font(.subheadline)
                                    .fontWeight(.bold)
                                    .foregroundColor(.secondary)
                                
                                Text(day.whatILove)
                                    .font(.title)
                                    .fontWeight(.bold)
                                    .foregroundColor(.primary)
                                    .fixedSize(horizontal: false, vertical: true)
                                    
                            }
                            .padding()
                            
                            Spacer()
                        }
                    )
            }
                .foregroundColor(.white)

            )
            .cornerRadius(15)
            .shadow(color: Color.black.opacity(colorScheme == .dark ? 0 : 0.3), radius: 15, y: 20)
    }
}

// YearModel.swift //

struct YearModel: Codable {
    let year: Int
    let months: [MonthModel]
    
    struct MonthModel: Codable {
        let monthName: String
        let dayInfo: [DayModel]
        
        struct DayModel: Codable {
            let whatILove: String
            let imageName: String
            let date: String
            let paragraph: String
            let id: Int
        }
    }
}

// JSON Файл декодирования //

let yearData: [YearModel] = load("Data.json")

func load<T: Decodable>(_ filename: String) -> T {
    let data: Data
    
    guard let file = Bundle.main.url(forResource: filename, withExtension: nil)
        else {
            fatalError("Couldn't find \(filename) in main bundle.")
    }
    
    do {
        data = try Data(contentsOf: file)
    } catch {
        fatalError("Couldn't load \(filename) from main bundle:\n\(error)")
    }
    
    do {
        let decoder = JSONDecoder()
        return try decoder.decode(T.self, from: data)
    } catch {
        fatalError("Couldn't parse \(filename) as \(T.self):\n\(error)")
    }
}

// и, наконец, JSON Файл I ' m декодирование информации //

[
    {
        "year": 2020,
        "months": [
            {
                "monthName": "July",
                "dayInfo": [
                    {
                        "whatILove": "I Love You",
                        "imageName": "image1",
                        "date": "07/22/2020",
                        "paragraph": "...",
                        "id": 1
                    },
                    {
                        "whatILove": " I Love Your Laugh",
                        "imageName": "image2",
                        "date": "07/23/2020",
                        "paragraph": "...",
                        "id": 2
                    },
                    {
                        "whatILove": "I Love Your Smile",
                        "imageName": "image3",
                        "date": "07/24/2020",
                        "paragraph": "...",
                        "id": 3
                    },
                    {
                        "whatILove": "I Love Your Kindness",
                        "imageName": "image4",
                        "date": "07/25/2020",
                        "paragraph": "...",
                        "id": 4
                    },
                    {
                        "whatILove": " I Love Your Nose",
                        "imageName": "image5",
                        "date": "07/26/2020",
                        "paragraph": "...",
                        "id": 5
                    },
                    {
                        "whatILove": "I Love Who You Are",
                        "imageName": "image6",
                        "date": "07/27/2020",
                        "paragraph": "...",
                        "id": 6
                    },
                    {
                        "whatILove": "I Love Your Freckles",
                        "imageName": "image7",
                        "date": "07/28/2020",
                        "paragraph": "...",
                        "id": 7
                    },
                    {
                        "whatILove": "I Love That You Are Trustworthy",
                        "imageName": "image8",
                        "date": "07/29/2020",
                        "paragraph": "...",
                        "id": 8
                    },
                    {
                        "whatILove": " I Love Your Ears",
                        "imageName": "image9",
                        "date": "07/30/2020",
                        "paragraph": "...",
                        "id": 9
                    },
                    {
                        "whatILove": "Happy 2 Years!",
                        "imageName": "image10",
                        "date": "07/31/2020",
                        "paragraph": "...",
                        "id": 10
                    }
                ]
            },
            {
                "monthName": "August",
                "dayInfo": [
                    {
                        "whatILove": "I Love Your Music",
                        "imageName": "image11",
                        "date": "08/01/2020",
                        "paragraph": "...",
                        "id": 11
                    },
                    {
                        "whatILove": "I Love That You Are Trustworthy",
                        "imageName": "image12",
                        "date": "08/02/2020",
                        "paragraph": "...",
                        "id": 12
                    },
                    {
                        "whatILove": "I Love Your Kisses",
                        "imageName": "image13",
                        "date": "08/03/2020",
                        "paragraph": "...",
                        "id": 13
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image14",
                        "date": "08/04/2020",
                        "paragraph": "...",
                        "id": 14
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image15",
                        "date": "08/05/2020",
                        "paragraph": "",
                        "id": 15
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image16",
                        "date": "08/06/2020",
                        "paragraph": "...",
                        "id": 16
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image17",
                        "date": "08/07/2020",
                        "paragraph": "...",
                        "id": 17
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image18",
                        "date": "08/08/2020",
                        "paragraph": "...",
                        "id": 18
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image19",
                        "date": "08/09/2020",
                        "paragraph": "...",
                        "id": 19
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image20",
                        "date": "08/10/2020",
                        "paragraph": "...",
                        "id": 20
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image6",
                        "date": "08/11/2020",
                        "paragraph": "...",
                        "id": 21
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image6",
                        "date": "08/12/2020",
                        "paragraph": "...",
                        "id": 22
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image6",
                        "date": "08/13/2020",
                        "paragraph": "...",
                        "id": 23
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image6",
                        "date": "08/14/2020",
                        "paragraph": "...",
                        "id": 24
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image6",
                        "date": "08/15/2020",
                        "paragraph": "...",
                        "id": 25
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image6",
                        "date": "08/16/2020",
                        "paragraph": "...",
                        "id": 26
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image6",
                        "date": "08/17/2020",
                        "paragraph": "...",
                        "id": 27
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image6",
                        "date": "08/18/2020",
                        "paragraph": "...",
                        "id": 28
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image4",
                        "date": "08/19/2020",
                        "paragraph": "...",
                        "id": 29
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image2",
                        "date": "08/20/2020",
                        "paragraph": "...",
                        "id": 30
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/21/2020",
                        "paragraph": "...",
                        "id": 31
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/22/2020",
                        "paragraph": "...",
                        "id": 32
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/23/2020",
                        "paragraph": "...",
                        "id": 33
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/24/2020",
                        "paragraph": "...",
                        "id": 34
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/25/2020",
                        "paragraph": "...",
                        "id": 35
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/26/2020",
                        "paragraph": "...",
                        "id": 36
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/27/2020",
                        "paragraph": "...",
                        "id": 37
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/28/2020",
                        "paragraph": "...",
                        "id": 38
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/29/2020",
                        "paragraph": "...",
                        "id": 39
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/31/2020",
                        "paragraph": "...",
                        "id": 41
                    }
                ]
                
            },
            {
                "monthName": "September",
                "dayInfo": [
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 42
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 40
                    },
                    {
                        "whatILove": "I Love Your...",
                        "imageName": "image3",
                        "date": "08/30/2020",
                        "paragraph": "...",
                        "id": 71
                    },
                ]
            }
        ]
    },
    {
        "year": 2021,
        "months": [
            {
                "monthName": "January",
                "dayInfo": [
                    {
                        "whatILove": "I Love Your Smile",
                        "imageName": "image1",
                        "date": "01/01/2021",
                        "paragraph": "...",
                        "id": 1
                    },
                    {
                        "whatILove": "I Love Your Laugh",
                        "imageName": "image2",
                        "date": "01/02/2021",
                        "paragraph": "...",
                        "id": 2
                    }
                ]
            },
            {
                "monthName": "February",
                "dayInfo": [
                    {
                        "whatILove": "I Love Your Music",
                        "imageName": "image4",
                        "date": "08/01/2020",
                        "paragraph": "...",
                        "id": 11
                    },
                    {
                        "whatILove": "I Love Your Kiss",
                        "imageName": "image6",
                        "date": "08/02/2020",
                        "paragraph": "...",
                        "id": 12
                    }
                ]
            }
        ]
    }
]

Я понимаю, что кода много, но надеюсь, что это не отпугнет людей! В любом случае, любая помощь приветствуется!

1 Ответ

2 голосов
/ 13 июля 2020

РЕДАКТИРОВАТЬ 2:

Используйте массив DateComponents для описания последних шести дней, включая их месяц и год. Таким образом, вы можете позволить DateComponents обрабатывать диапазон, который использует два месяца или даже два года.

/// Calculates Dates for the last n days, starting with the previous day.
func lastNDays(n: Int) -> [Date]? {
        guard n > 0 else { return nil }
        var result = [Date]()
        for i in 1...n {
            guard let ithPreviousDay = Calendar.current.date(byAdding: DateComponents(day: -i), to: Date()) else { 
                return nil 
            }
            result.append(ithPreviousDay)
        }
        return result
    }

var lastSixDaysAsDateComponents: [DateComponents] {
    guard let lastSixDays = lastNDays(n: 6) else { return [] }
    return lastSixDays.map{ day in 
        Calendar.current.dateComponents([.day, .month, .year], from: day) 
    }
}

Затем в теле вашего представления:

ForEach(self.lastSixDaysAsDateComponents, id: \.self) { dateComp in
    CardViewday: yearData[dateComp.year!].months[dateComp.month!].dayInfo[dateComp.day!])
}
...