diff --git a/Projects/Domain/Source/Entity/Place.swift b/Projects/Domain/Source/Entity/Place.swift index f7448f8..d841770 100644 --- a/Projects/Domain/Source/Entity/Place.swift +++ b/Projects/Domain/Source/Entity/Place.swift @@ -8,7 +8,7 @@ import Foundation public enum Place: String, Codable, CaseIterable, Hashable { - + case programming1 = "프로그래밍1실" case programming2 = "프로그래밍2실" case programming3 = "프로그래밍3실" diff --git a/Projects/Domain/Source/Entity/SchedulePlace.swift b/Projects/Domain/Source/Entity/SchedulePlace.swift new file mode 100644 index 0000000..d270938 --- /dev/null +++ b/Projects/Domain/Source/Entity/SchedulePlace.swift @@ -0,0 +1,22 @@ +// +// SchedulePlace.swift +// Domain +// +// Created by dgsw8th36 on 11/18/24. +// + +import Foundation + +public enum SchedulePlace: String, Codable, CaseIterable, Hashable { + + case programming1 = "PROGRAMMING_1" + case programming2 = "PROGRAMMING_2" + case programming3 = "PROGRAMMING_3" + case korean = "KOREAN" + case math = "MATH" + case society = "SOCIETY" + case hall = "HALL" + case audiovisual_room = "AUDIOVISUAL_ROOM" + case none = "NONE" + case etc = "ETC" +} diff --git a/Projects/Domain/Source/Request/Schedule/FetchScheduleBetweenRequest.swift b/Projects/Domain/Source/Request/Schedule/FetchScheduleBetweenRequest.swift index ccb3909..ed3314f 100644 --- a/Projects/Domain/Source/Request/Schedule/FetchScheduleBetweenRequest.swift +++ b/Projects/Domain/Source/Request/Schedule/FetchScheduleBetweenRequest.swift @@ -7,11 +7,11 @@ public struct FetchScheduleBetweenRequest: RequestProtocol { - public let startDate: String - public let endDate: String + public let startAt: String + public let endAt: String - public init(startDate: String, endDate: String) { - self.startDate = startDate - self.endDate = endDate + public init(startAt: String, endAt: String) { + self.startAt = startAt + self.endAt = endAt } } diff --git a/Projects/Domain/Source/Response/Schedule/ScheduleResponse.swift b/Projects/Domain/Source/Response/Schedule/ScheduleResponse.swift index d9f1b39..a7754ef 100644 --- a/Projects/Domain/Source/Response/Schedule/ScheduleResponse.swift +++ b/Projects/Domain/Source/Response/Schedule/ScheduleResponse.swift @@ -9,12 +9,12 @@ public struct ScheduleResponse: ResponseProtocol { public let id: Int public let name: String - public let place: Place + public let place: SchedulePlace public let type: ScheduleType public let date: [String] public let targetGrades: [Grade] - public init(id: Int, name: String, place: Place, type: ScheduleType, date: [String], targetGrades: [Grade]) { + public init(id: Int, name: String, place: SchedulePlace, type: ScheduleType, date: [String], targetGrades: [Grade]) { self.id = id self.name = name self.place = place diff --git a/Projects/Feature/Source/Home/Component/ScheduleContainer.swift b/Projects/Feature/Source/Home/Component/ScheduleContainer.swift index f700163..ee504a1 100644 --- a/Projects/Feature/Source/Home/Component/ScheduleContainer.swift +++ b/Projects/Feature/Source/Home/Component/ScheduleContainer.swift @@ -13,6 +13,26 @@ struct ScheduleContainer: View { private let scheduleData: [ScheduleResponse]? + func getSchedule(scheduleResponse: [ScheduleResponse]) -> [ScheduleResponse] { + var schedules: [ScheduleResponse] = [] + var cnt: Int = 0 + + for data in scheduleResponse { + if schedules.count == 0 { + schedules.append(scheduleResponse[0]) + } else if schedules[0].date == data.date || cnt <= 1 { + schedules.append(data) + if schedules[0].date != data.date { + cnt += 1 + } + } + } + + + print("스케줄 \(schedules)") + return schedules + } + init( data scheduleData: [ScheduleResponse]? ) { @@ -20,53 +40,47 @@ struct ScheduleContainer: View { } var body: some View { - if let data = scheduleData { - HStack(alignment: .top, spacing: 12) { - ForEach(data, id: \.self) { data in - VStack(alignment: .leading, spacing: 0) { - Text("\(data.name)") -// Text("D - " + { () -> String in -// let dateFormatter = DateFormatter() -// dateFormatter.dateFormat = "yyyy-MM-dd" -// let currentDate = Date() -// guard let targetDate = dateFormatter.date(from: data.) else { -// return "오류" -// } -// let calendar = Calendar.current -// let dateComponents = calendar.dateComponents([.day], from: currentDate, to: targetDate) -// -// guard let daysDifference = dateComponents.day else { -// return "오류" -// } -// if daysDifference == 0 { -// return "day" -// } else { -// return "\(abs(daysDifference))" -// } -// }()) -// .font(.title(.small)) -// .dodamColor(.onSurfaceVariant) -// -// Text({ () -> String in -// let dateFormatter = DateFormatter() -// dateFormatter.dateFormat = "yyyy-MM-dd" -// dateFormatter.locale = Locale(identifier: "ko_KR") -// if let date = dateFormatter.date(from: data.startDate) { -// dateFormatter.dateFormat = "M월 d일 (E)" -// return dateFormatter.string(from: date) -// } -// return "오류" -// }()) -// .font(.label(.medium)) -// .dodamColor(.tertiary) + VStack(spacing: 10) { + if let data = scheduleData { + ForEach(0 ..< min(data.count, 2), id: \.self) { idx in + VStack(alignment: .leading, spacing: 8) { + HStack(alignment: .bottom, spacing: 4) { + Text("\(data[idx].date[0].suffix(2))일") + .heading2(.bold) + .foreground(DodamColor.Label.normal) + + let weekdayName = { (dateString: String) -> String in + let dateHelper = Date() + guard let date = dateHelper.from(string: dateString, format: "yyyy-MM-dd") else { + return "알 수 없음" + } + return date.parseString(format: "EEEE") + } + + Text("\(weekdayName(data[idx].date[0]))") + .label(.medium) + .foreground(DodamColor.Label.alternative) + + } + + HStack { + Circle() + .frame(width: 6, height: 6) + .foregroundStyle(Color(0xffFFB5B5)) + Text("\(data[idx].name)") + Spacer() + } } + .padding(.bottom, 10) + .padding(.horizontal, 10) } + } else { + SupportingContainer( + subTitle: "한달 간 일정이 없어요", + title: "다음 달 일정을 기다려주세요" + ) } - } else { - SupportingContainer( - subTitle: "한달 간 일정이 없어요", - title: "전체 일정 확인하기" - ) } + .padding(.top, 6) } } diff --git a/Projects/Feature/Source/Home/Component/SupportingContainer.swift b/Projects/Feature/Source/Home/Component/SupportingContainer.swift index 18435a4..2b7e470 100644 --- a/Projects/Feature/Source/Home/Component/SupportingContainer.swift +++ b/Projects/Feature/Source/Home/Component/SupportingContainer.swift @@ -34,6 +34,8 @@ struct SupportingContainer: View { .minimumScaleFactor(0.7) .foreground(DodamColor.Primary.normal) } + .padding(.top, 10) + .padding(.horizontal, 10) .frame(maxWidth: .infinity, alignment: .leading) } } diff --git a/Projects/Feature/Source/Home/HomeView.swift b/Projects/Feature/Source/Home/HomeView.swift index 286366f..cbe5fd5 100644 --- a/Projects/Feature/Source/Home/HomeView.swift +++ b/Projects/Feature/Source/Home/HomeView.swift @@ -124,6 +124,9 @@ struct HomeView: View { } } } + DodamContainer.default(title: "가까운 일정", icon: Dodam.icon(.calendar)) { + ScheduleContainer(data: viewModel.scheduleData) + } } .padding(.horizontal, 16) } diff --git a/Projects/Feature/Source/Home/HomeViewModel.swift b/Projects/Feature/Source/Home/HomeViewModel.swift index f5b67f2..6a529bb 100644 --- a/Projects/Feature/Source/Home/HomeViewModel.swift +++ b/Projects/Feature/Source/Home/HomeViewModel.swift @@ -35,6 +35,7 @@ class HomeViewModel: ObservableObject, OnAppearProtocol { @Inject var outGoingRepository: any OutGoingRepository @Inject var outSleepingRepository: any OutSleepingRepository @Inject var nightStudyRepository: any NightStudyRepository + @Inject var scheduleRepository: any ScheduleRepository // MARK: - Method @MainActor @@ -42,13 +43,14 @@ class HomeViewModel: ObservableObject, OnAppearProtocol { async let fetchBannerData: () = await fetchBannerData() async let fetchMealData: () = await fetchMealData() async let fetchWakeupSongData: () = await fetchWakeupSongData() + async let fetchScheduleData: () = await fetchSchedule() if Sign.isLoggedIn { async let fetchOutData: () = await fetchOutData() async let fetchNightStudy: () = await fetchNightStudy() - _ = await [fetchBannerData, fetchMealData, fetchWakeupSongData, fetchOutData, fetchNightStudy] + _ = await [fetchBannerData, fetchMealData, fetchWakeupSongData, fetchOutData, fetchNightStudy, fetchScheduleData] } else { - _ = await [fetchBannerData, fetchMealData, fetchWakeupSongData] + _ = await [fetchBannerData, fetchMealData, fetchWakeupSongData, fetchScheduleData] } } @@ -65,6 +67,7 @@ class HomeViewModel: ObservableObject, OnAppearProtocol { outGoingData = nil outSleepingData = nil nightStudyData = nil + scheduleData = nil } @MainActor @@ -162,4 +165,19 @@ class HomeViewModel: ObservableObject, OnAppearProtocol { print(error) } } + + @MainActor + func fetchSchedule() async { + let currentTime = Date.now + do { + scheduleData = try await scheduleRepository.fetchScheduleBetween( + .init( + startAt: currentTime.parseString(format: "yyyy-MM-dd"), + endAt: Calendar.current.date(byAdding: .month, value: 1, to: currentTime)?.parseString(format: "yyyy-MM-dd") ?? "" + ) + ) + } catch let error { + print(error) + } + } } diff --git a/Projects/Shared/Source/Foundation/DateExt.swift b/Projects/Shared/Source/Foundation/DateExt.swift index 97044ed..1a7f8ef 100644 --- a/Projects/Shared/Source/Foundation/DateExt.swift +++ b/Projects/Shared/Source/Foundation/DateExt.swift @@ -8,6 +8,13 @@ import Foundation public extension Date { + func from(string: String, format: String) -> Date? { + let dateFormatter = DateFormatter() + dateFormatter.dateFormat = format + dateFormatter.locale = Locale(identifier: "ko_KR") + return dateFormatter.date(from: string) + } + func parseString(format: String) -> String { let dateFormatter = DateFormatter() dateFormatter.dateFormat = format