Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/166 home create schedule #182

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Projects/Domain/Source/Entity/Place.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import Foundation

public enum Place: String, Codable, CaseIterable, Hashable {

case programming1 = "프로그래밍1실"
case programming2 = "프로그래밍2실"
case programming3 = "프로그래밍3실"
Expand Down
22 changes: 22 additions & 0 deletions Projects/Domain/Source/Entity/SchedulePlace.swift
Original file line number Diff line number Diff line change
@@ -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"
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
102 changes: 58 additions & 44 deletions Projects/Feature/Source/Home/Component/ScheduleContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,60 +13,74 @@ 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]?
) {
self.scheduleData = scheduleData
}

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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
3 changes: 3 additions & 0 deletions Projects/Feature/Source/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ struct HomeView: View {
}
}
}
DodamContainer.default(title: "가까운 일정", icon: Dodam.icon(.calendar)) {
ScheduleContainer(data: viewModel.scheduleData)
}
}
.padding(.horizontal, 16)
}
Expand Down
22 changes: 20 additions & 2 deletions Projects/Feature/Source/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@ 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
func fetchAllData() async {
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]
}
}

Expand All @@ -65,6 +67,7 @@ class HomeViewModel: ObservableObject, OnAppearProtocol {
outGoingData = nil
outSleepingData = nil
nightStudyData = nil
scheduleData = nil
}

@MainActor
Expand Down Expand Up @@ -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)
}
}
}
7 changes: 7 additions & 0 deletions Projects/Shared/Source/Foundation/DateExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down