diff --git a/ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj b/ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj index 042087b5..8db282fc 100644 --- a/ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj +++ b/ACHNBrowserUI/ACHNBrowserUI.xcodeproj/project.pbxproj @@ -18,6 +18,7 @@ 3DFC04E424D6DAA3002A6CED /* DreamCodeListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DFC04E324D6DAA3002A6CED /* DreamCodeListView.swift */; }; 3DFC04E624D6DAAE002A6CED /* DreamCodeRow.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DFC04E524D6DAAE002A6CED /* DreamCodeRow.swift */; }; 3DFC04E824D6DABC002A6CED /* DreamCodeDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3DFC04E724D6DABC002A6CED /* DreamCodeDetailView.swift */; }; + 4C062B12252BB5BE004DCFA3 /* CurrentDateEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C062B11252BB5BE004DCFA3 /* CurrentDateEnvironment.swift */; }; 4C30BFC3249627FD003E96A2 /* Sequence+Unique.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C30BFC2249627FC003E96A2 /* Sequence+Unique.swift */; }; 4C6E95FD24842F690074433B /* Collection+SafeDirectAccess.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C6E95FC24842F690074433B /* Collection+SafeDirectAccess.swift */; }; 4C7F555D248B91C80089F26C /* TodayVillagerVisitsSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4C7F555C248B91C80089F26C /* TodayVillagerVisitsSection.swift */; }; @@ -237,6 +238,7 @@ 3DFC04E324D6DAA3002A6CED /* DreamCodeListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DreamCodeListView.swift; sourceTree = ""; }; 3DFC04E524D6DAAE002A6CED /* DreamCodeRow.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DreamCodeRow.swift; sourceTree = ""; }; 3DFC04E724D6DABC002A6CED /* DreamCodeDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DreamCodeDetailView.swift; sourceTree = ""; }; + 4C062B11252BB5BE004DCFA3 /* CurrentDateEnvironment.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CurrentDateEnvironment.swift; sourceTree = ""; }; 4C2D676A245DA41E005831C4 /* TurnipsChartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TurnipsChartView.swift; sourceTree = ""; }; 4C2D676D245F0666005831C4 /* TurnipsChartBottomLegendView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TurnipsChartBottomLegendView.swift; sourceTree = ""; }; 4C2D676F245F0698005831C4 /* TurnipsChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TurnipsChart.swift; sourceTree = ""; }; @@ -564,6 +566,7 @@ children = ( 696B454B247A948A00E16252 /* MusicPlayerManager.swift */, 693E728D245D71DD00CD26F4 /* UIState.swift */, + 4C062B11252BB5BE004DCFA3 /* CurrentDateEnvironment.swift */, ); path = env; sourceTree = ""; @@ -1253,6 +1256,7 @@ 4C6E95FD24842F690074433B /* Collection+SafeDirectAccess.swift in Sources */, AE2D7EFC247E9726006C1F1A /* DesignListView.swift in Sources */, 69157BC12471A5A1005B9002 /* ItemsViewModel.swift in Sources */, + 4C062B12252BB5BE004DCFA3 /* CurrentDateEnvironment.swift in Sources */, 69157BC22471A5A1005B9002 /* TodayEventsSection.swift in Sources */, 69157BC32471A5A1005B9002 /* FeedbackGenerator.swift in Sources */, 3DFC04E824D6DABC002A6CED /* DreamCodeDetailView.swift in Sources */, diff --git a/ACHNBrowserUI/ACHNBrowserUI/env/CurrentDateEnvironment.swift b/ACHNBrowserUI/ACHNBrowserUI/env/CurrentDateEnvironment.swift new file mode 100644 index 00000000..f3ebcb9a --- /dev/null +++ b/ACHNBrowserUI/ACHNBrowserUI/env/CurrentDateEnvironment.swift @@ -0,0 +1,20 @@ +// +// CurrentDateEnvironment.swift +// ACHNBrowserUI +// +// Created by Renaud JENNY on 05/10/2020. +// Copyright © 2020 Thomas Ricouard. All rights reserved. +// + +import SwiftUI + +struct CurrentDateKey: EnvironmentKey { + static let defaultValue = Date() +} + +extension EnvironmentValues { + var currentDate: Date { + get { self[CurrentDateKey.self] } + set { self[CurrentDateKey.self] = newValue } + } +} diff --git a/ACHNBrowserUI/ACHNBrowserUI/utils/EventsDescription.swift b/ACHNBrowserUI/ACHNBrowserUI/utils/EventsDescription.swift index c86f6dae..7cbd1403 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/utils/EventsDescription.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/utils/EventsDescription.swift @@ -38,9 +38,8 @@ extension Event { } } - static func nextEvent() -> (Date, Event?) { + static func nextEvent(today: Date) -> (Date, Event?) { let aDay: TimeInterval = 24*60*60 - let today = Date() let endDate = today.addingTimeInterval(aDay * 365) var nextDate = today.addingTimeInterval(aDay) var event: Event? = nil diff --git a/ACHNBrowserUI/ACHNBrowserUI/viewModels/ActiveCrittersViewModel.swift b/ACHNBrowserUI/ACHNBrowserUI/viewModels/ActiveCrittersViewModel.swift index 70d4c11b..a2f79e08 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/viewModels/ActiveCrittersViewModel.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/viewModels/ActiveCrittersViewModel.swift @@ -49,6 +49,7 @@ class ActiveCrittersViewModel: ObservableObject { private let items: Items private let collection: UserCollection + private let filterOutInCollection: Bool private var cancellable: AnyCancellable? @@ -56,33 +57,36 @@ class ActiveCrittersViewModel: ObservableObject { self.items = items self.collection = collection self.isLoading = true - + self.filterOutInCollection = filterOutInCollection + } + + func updateCritters(for currentDate: Date) { cancellable = Publishers.CombineLatest(items.$categories, collection.$critters) .receive(on: DispatchQueue.main) .sink { [weak self] (items, critters) in guard let self = self else { return } for type in CritterType.allCases { - var active = items[type.category()]?.filterActiveThisMonth() ?? [] - var new = active.filter{ $0.isNewThisMonth() } - var leaving = active.filter{ $0.leavingThisMonth() } + var active = items[type.category()]?.filterActiveThisMonth(currentDate: currentDate) ?? [] + var new = active.filter{ $0.isNewThisMonth(currentDate: currentDate) } + var leaving = active.filter{ $0.leavingThisMonth(currentDate: currentDate) } let caught = active.filter{ critters.contains($0) } - let toCatchNow = active.filter{ !caught.contains($0) && $0.isActiveAtThisHour() } + let toCatchNow = active.filter{ !caught.contains($0) && $0.isActiveAtThisHour(currentDate: currentDate) } let toCatchLater = active.filter{ !caught.contains($0) && !toCatchNow.contains($0) } - - if filterOutInCollection { + + if self.filterOutInCollection { new = new.filter{ !critters.contains($0) } leaving = leaving.filter{ !critters.contains($0) } active = active.filter{ !critters.contains($0) } } - + self.crittersInfo[type] = CritterInfo(active: active, - new: new, - leaving: leaving, - caught: caught, - toCatchNow: toCatchNow, - toCatchLater: toCatchLater) + new: new, + leaving: leaving, + caught: caught, + toCatchNow: toCatchNow, + toCatchLater: toCatchLater) self.isLoading = false } - } + } } } diff --git a/ACHNBrowserUI/ACHNBrowserUI/viewModels/TabbarViewModel.swift b/ACHNBrowserUI/ACHNBrowserUI/viewModels/TabbarViewModel.swift index 5bc045a5..8fb0d127 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/viewModels/TabbarViewModel.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/viewModels/TabbarViewModel.swift @@ -16,6 +16,8 @@ class TabbarViewModel: ObservableObject { private var musicPlayerPlayingCancellable: AnyCancellable? @Published var showPlayerView = false + @Published private(set) var currentDate = Date() + @Published private(set) var cancellables: Set = [] init(musicPlayerManager: MusicPlayerManager) { self.musicPlayerManager = musicPlayerManager @@ -27,4 +29,11 @@ class TabbarViewModel: ObservableObject { } } } + + func updateCurrentDateOnTabChange(selectedTabPublisher: Published.Publisher) { + selectedTabPublisher + .removeDuplicates() + .sink { [weak self] _ in self?.currentDate = Date() } + .store(in: &cancellables) + } } diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/critters/ActiveCrittersView.swift b/ACHNBrowserUI/ACHNBrowserUI/views/critters/ActiveCrittersView.swift index de87d8ac..1b86a30c 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/critters/ActiveCrittersView.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/critters/ActiveCrittersView.swift @@ -10,6 +10,7 @@ import SwiftUI import Backend struct ActiveCritterSections: View { + @Environment(\.currentDate) private var currentDate @ObservedObject var viewModel: ActiveCrittersViewModel @Binding var selectedTab: ActiveCrittersViewModel.CritterType @@ -53,6 +54,9 @@ struct ActiveCritterSections: View { content: sectionContent) } } + .onAppear { + viewModel.updateCritters(for: currentDate) + } } } diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/home/TabbarView.swift b/ACHNBrowserUI/ACHNBrowserUI/views/home/TabbarView.swift index 475d3270..85b0220e 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/home/TabbarView.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/home/TabbarView.swift @@ -8,6 +8,7 @@ import SwiftUI import Backend +import Combine struct TabbarView: View { @EnvironmentObject private var uiState: UIState @@ -51,7 +52,10 @@ struct TabbarView: View { Text("Collection") } .tag(UIState.Tab.collection) - + } + .environment(\.currentDate, viewModel.currentDate) + .onAppear { + viewModel.updateCurrentDateOnTabChange(selectedTabPublisher: uiState.$selectedTab) } if viewModel.showPlayerView { diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/shared/CalendarView.swift b/ACHNBrowserUI/ACHNBrowserUI/views/shared/CalendarView.swift index 692bada1..450594fe 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/shared/CalendarView.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/shared/CalendarView.swift @@ -10,9 +10,9 @@ import SwiftUI import Backend struct CalendarView: View { + @Environment(\.currentDate) private var currentDate let activeMonths: [Int] - - private let currentMonth = Int(Item.monthFormatter.string(from: Date()))! + private let months = [[Calendar.current.shortMonthSymbols[0], Calendar.current.shortMonthSymbols[1], Calendar.current.shortMonthSymbols[2], @@ -32,6 +32,7 @@ struct CalendarView: View { private func makeMonthPill(month: String, selected: Bool) -> some View { let monthsArray = months.flatMap({ $0 }) + let currentMonth = Int(Item.monthFormatter.string(from: currentDate))! let isCurrentMonth = monthsArray.firstIndex(of: month) == currentMonth - 1 let border = RoundedRectangle(cornerRadius: 10) .stroke(lineWidth: isCurrentMonth ? 5 : 0) diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayCurrentlyAvailableSection.swift b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayCurrentlyAvailableSection.swift index c2599a65..780a9708 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayCurrentlyAvailableSection.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayCurrentlyAvailableSection.swift @@ -10,6 +10,7 @@ import SwiftUI import Backend struct TodayCurrentlyAvailableSection: View { + @Environment(\.currentDate) private var currentDate @StateObject private var viewModel = ActiveCrittersViewModel() @State private var isNavigationLinkActive = false @State private var openingTab: ActiveCrittersViewModel.CritterType = .fish @@ -102,7 +103,7 @@ struct TodayCurrentlyAvailableSection: View { } private func dayNumber() -> Int { - return Calendar.current.dateComponents([.day], from: Date()).day ?? 0 + return Calendar.current.dateComponents([.day], from: currentDate).day ?? 0 } } diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayEventsSection.swift b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayEventsSection.swift index b6c62491..8d1aa789 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayEventsSection.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayEventsSection.swift @@ -11,10 +11,7 @@ import ACNHEvents import Backend struct TodayEventsSection: View { - - let todaysEvents = Date().events(for: AppUserDefaults.shared.hemisphere == .north ? .north : .south) - let nextEvent = Event.nextEvent() - + @Environment(\.currentDate) private var currentDate var body: some View { VStack(alignment: .leading) { @@ -40,6 +37,12 @@ struct TodayEventsSection: View { } } + private var todaysEvents: [Event] { + currentDate.events(for: AppUserDefaults.shared.hemisphere == .north ? .north : .south) + } + + private var nextEvent: (Date, Event?) { Event.nextEvent(today: currentDate) } + private func subsectionHeader(_ text: String) -> some View { Text(LocalizedStringKey(text)) .font(.system(.caption, design: .rounded)) diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodaySpecialCharactersSection.swift b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodaySpecialCharactersSection.swift index 9a8c0792..eb2853af 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodaySpecialCharactersSection.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodaySpecialCharactersSection.swift @@ -11,11 +11,12 @@ import SwiftUIKit import Backend struct TodaySpecialCharactersSection: View { + @Environment(\.currentDate) private var currentDate @State private var selectedCharacter: SpecialCharacters? @Namespace private var namespace private var currentIcon: String { - let hour = Calendar.current.component(.hour, from: Date()) + let hour = Calendar.current.component(.hour, from: currentDate) if hour < 9 { return "sunrise.fill" } else if hour < 17 { @@ -54,7 +55,7 @@ struct TodaySpecialCharactersSection: View { .aspectRatio(contentMode: .fit) .frame(width: 25) .foregroundColor(.acHeaderBackground) - Text(Date(), style: .time) + Text(currentDate, style: .time) .style(appStyle: .rowDetail) } .padding(16) @@ -96,7 +97,7 @@ struct TodaySpecialCharactersSection: View { } else { ScrollView(.horizontal, showsIndicators: false) { HStack(spacing: 16) { - ForEach(SpecialCharacters.now(), id: \.self) { character in + ForEach(SpecialCharacters.forDate(currentDate), id: \.self) { character in Image(character.rawValue) .resizable() .aspectRatio(contentMode: .fit) diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayTurnipSection.swift b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayTurnipSection.swift index d761c2db..1744ebe0 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayTurnipSection.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayTurnipSection.swift @@ -10,10 +10,11 @@ import SwiftUI import Backend struct TodayTurnipSection: View { + @Environment(\.currentDate) private var currentDate @EnvironmentObject private var turnipService: TurnipPredictionsService var isSunday: Bool { - Calendar.current.component(.weekday, from: Date()) == 1 + Calendar.current.component(.weekday, from: currentDate) == 1 } var body: some View { diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayView.swift b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayView.swift index aa441d7f..9b2e6847 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayView.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/todayDashboard/TodayView.swift @@ -15,6 +15,7 @@ import UI struct TodayView: View { // MARK: - Vars + @Environment(\.currentDate) private var currentDate @StateObject private var viewModel = DashboardViewModel() @State private var selectedSheet: Sheet.SheetType? @@ -88,7 +89,7 @@ struct TodayView: View { private var dateString: String { let f = DateFormatter() f.setLocalizedDateFormatFromTemplate("EEEE, MMM d") - return f.string(from: Date()) + return f.string(from: currentDate) } } diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/turnips/TurnipsFormView.swift b/ACHNBrowserUI/ACHNBrowserUI/views/turnips/TurnipsFormView.swift index eb65eec9..ffd19631 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/turnips/TurnipsFormView.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/turnips/TurnipsFormView.swift @@ -13,6 +13,7 @@ struct TurnipsFormView: View { // MARK: - Properties @EnvironmentObject private var subscriptionManager: SubscriptionManager @Environment(\.presentationMode) private var presentationMode + @Environment(\.currentDate) private var currentDate @State private var fields = TurnipFields.decode() @State private var enableNotifications = SubscriptionManager.shared.subscriptionStatus == .subscribed diff --git a/ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartView.swift b/ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartView.swift index 977ead1d..2e62d31a 100644 --- a/ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartView.swift +++ b/ACHNBrowserUI/ACHNBrowserUI/views/turnips/charts/TurnipsChartView.swift @@ -119,7 +119,8 @@ struct TurnipsChartView_Previews: PreviewProvider { minBuyPrice: 83, averagePrices: averagePrices, minMax: minMax, - averageProfits: averageProfits + averageProfits: averageProfits, + currentDate: Date() ) static let averagePrices = [89, 85, 88, 104, 110, 111, 111, 111, 106, 98, 82, 77] diff --git a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Extensions/Collection.swift b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Extensions/Collection.swift index 52d51d8c..12474f41 100644 --- a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Extensions/Collection.swift +++ b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Extensions/Collection.swift @@ -10,8 +10,8 @@ import Foundation public extension BidirectionalCollection where Element == Item { - func filterActiveThisMonth() -> [Item] { - self.filter({ $0.isActiveThisMonth() }) + func filterActiveThisMonth(currentDate: Date) -> [Item] { + self.filter({ $0.isActiveThisMonth(currentDate: currentDate) }) } } diff --git a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/Item.swift b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/Item.swift index 67c18e41..46b91a9e 100644 --- a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/Item.swift +++ b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/Item.swift @@ -163,17 +163,17 @@ public extension Item { return nil } - func isActiveThisMonth() -> Bool { - let currentMonth = Int(Item.monthFormatter.string(from: Date()))! + func isActiveThisMonth(currentDate: Date) -> Bool { + let currentMonth = Int(Item.monthFormatter.string(from: currentDate))! return activeMonthsCalendar?.contains(currentMonth - 1) == true } - func isActiveAtThisHour() -> Bool { + func isActiveAtThisHour(currentDate: Date) -> Bool { guard let hours = activeHours() else { return false } if hours.start == 0 && hours.end == 0 { return true } // All day - let thisHour = Calendar.current.dateComponents([.hour], from: Date()).hour ?? 0 + let thisHour = Calendar.current.dateComponents([.hour], from: currentDate).hour ?? 0 if hours.start < hours.end { // Same day return thisHour >= hours.start && thisHour < hours.end @@ -182,13 +182,13 @@ public extension Item { } } - func isNewThisMonth() -> Bool { - let currentMonth = Int(Item.monthFormatter.string(from: Date()))! + func isNewThisMonth(currentDate: Date) -> Bool { + let currentMonth = Int(Item.monthFormatter.string(from: currentDate))! return activeMonthsCalendar?.contains(currentMonth - 2) == false } - func leavingThisMonth() -> Bool { - let currentMonth = Int(Item.monthFormatter.string(from: Date()))! + func leavingThisMonth(currentDate: Date) -> Bool { + let currentMonth = Int(Item.monthFormatter.string(from: currentDate))! return activeMonthsCalendar?.contains(currentMonth) == false } diff --git a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/SpecialCharacters.swift b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/SpecialCharacters.swift index 5cd723d5..33002817 100644 --- a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/SpecialCharacters.swift +++ b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/SpecialCharacters.swift @@ -11,9 +11,9 @@ import SwiftUI public enum SpecialCharacters: String, CaseIterable { case kk, daisy, cj, flick, kicks, saharah, gulliver, label, leif, redd, wisp, celeste - public static func now() -> [SpecialCharacters] { - let day = Calendar.current.component(.weekday, from: Date()) - let hour = Calendar.current.component(.hour, from: Date()) + public static func forDate(_ currentDate: Date) -> [SpecialCharacters] { + let day = Calendar.current.component(.weekday, from: currentDate) + let hour = Calendar.current.component(.hour, from: currentDate) if day == 1 { return [.daisy] } else if day == 7 { diff --git a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/Turnips.swift b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/Turnips.swift index 9f474d94..bc0fcbfa 100644 --- a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/Turnips.swift +++ b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Models/Turnips.swift @@ -18,6 +18,7 @@ public struct TurnipPredictions { public let averagePrices: [Int]? public let minMax: [[Int]]? public let averageProfits: [Int]? + public var currentDate: Date public var todayAverages: [Int]? { guard let averagePrices = averagePrices else { @@ -35,12 +36,14 @@ public struct TurnipPredictions { minBuyPrice: Int?, averagePrices: [Int]?, minMax: [[Int]]?, - averageProfits: [Int]? + averageProfits: [Int]?, + currentDate: Date ) { self.minBuyPrice = minBuyPrice self.averagePrices = averagePrices self.minMax = minMax self.averageProfits = averageProfits + self.currentDate = currentDate } } diff --git a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Services/TurnipPredictionsService.swift b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Services/TurnipPredictionsService.swift index 36d6bd86..90933c7c 100644 --- a/ACHNBrowserUI/Packages/Backend/Sources/Backend/Services/TurnipPredictionsService.swift +++ b/ACHNBrowserUI/Packages/Backend/Sources/Backend/Services/TurnipPredictionsService.swift @@ -30,6 +30,7 @@ public class TurnipPredictionsService: ObservableObject { base += "&first=false&pattern=-1&achelper" return URL(string: base) } + public var currentDate = Date() private var turnipsCancellable: AnyCancellable? private lazy var calculatorContext: JSContext? = { @@ -102,6 +103,7 @@ public class TurnipPredictionsService: ObservableObject { return TurnipPredictions(minBuyPrice: results?.toDictionary()["minWeekValue"] as? Int, averagePrices: averagePrices, minMax: minMax, - averageProfits: averageProfits) + averageProfits: averageProfits, + currentDate: currentDate) } } diff --git a/ACHNBrowserUI/Packages/Backend/Tests/BackendTests/CrittersTests.swift b/ACHNBrowserUI/Packages/Backend/Tests/BackendTests/CrittersTests.swift index 42fc0c5a..066193fa 100644 --- a/ACHNBrowserUI/Packages/Backend/Tests/BackendTests/CrittersTests.swift +++ b/ACHNBrowserUI/Packages/Backend/Tests/BackendTests/CrittersTests.swift @@ -127,7 +127,7 @@ final class CrittersTests: XCTestCase { let fish = critters.results.first!.content XCTAssertTrue(fish.isCritter, "This item should be a critter") - XCTAssertTrue(fish.isActiveThisMonth(), "This fish should be active all year") + XCTAssertTrue(fish.isActiveThisMonth(currentDate: Date()), "This fish should be active all year") XCTAssertTrue(fish.activeMonthsCalendar?.count == 12, "This fish should be active all year") XCTAssertTrue(fish.appCategory == .fish, "This item should be a fish") XCTAssertTrue(fish.formattedTimes() == "4AM - 9PM", "This fish should display all day")