Skip to content

Commit

Permalink
feat #104: DailyMenu 분리
Browse files Browse the repository at this point in the history
  • Loading branch information
seungyooooong committed Jan 15, 2025
1 parent 01635ec commit 6598ce2
Showing 1 changed file with 89 additions and 71 deletions.
160 changes: 89 additions & 71 deletions Daily/Presentation/Record/DailyRecordList.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import SwiftData

// MARK: - DailyRecordList
struct DailyRecordList: View {
@Environment(\.modelContext) private var modelContext
@EnvironmentObject var navigationEnvironment: NavigationEnvironment
let date: Date
let records: [DailyRecordModel]

Expand Down Expand Up @@ -43,79 +41,98 @@ struct DailyRecordList: View {
if let goal = record.goal {
if processed.showTimeline { DailyTimeLine(setTime: goal.setTime) }
DailyRecord(record: record)
.contextMenu {
// MARK: ModifyGoal
if goal.cycleType == .date || goal.parentGoal != nil {
Button {
let data = ModifyDataModel(date: date, modifyRecord: record, modifyType: .record)
let navigationObject = NavigationObject(viewType: .modify, data: data)
navigationEnvironment.navigate(navigationObject)
} label: {
Label("목표 수정", systemImage: "pencil.line")
}
} else {
Menu {
Button {
let data = ModifyDataModel(date: date, modifyRecord: record, modifyType: .single)
let navigationObject = NavigationObject(viewType: .modify, data: data)
navigationEnvironment.navigate(navigationObject)
} label: {
Text("단일 수정")
}
Button {
let data = ModifyDataModel(date: date, modifyRecord: record, modifyType: .all)
let navigationObject = NavigationObject(viewType: .modify, data: data)
navigationEnvironment.navigate(navigationObject)
} label: {
Text("일괄 수정")
}
} label: {
Label("목표 수정", systemImage: "pencil.line")
.contextMenu { DailyMenu(record: record, date: date) }
}
}
}
}
}

// MARK: - DailyMenu
struct DailyMenu: View {
@Environment(\.modelContext) private var modelContext
@EnvironmentObject var navigationEnvironment: NavigationEnvironment
let record: DailyRecordModel
let date: Date

init(record: DailyRecordModel, date: Date) {
self.record = record
self.date = date
}

var body: some View {
if let goal = record.goal {
VStack {
// MARK: ModifyGoal
if goal.cycleType == .date || goal.parentGoal != nil {
Button {
let data = ModifyDataModel(date: date, modifyRecord: record, modifyType: .record)
let navigationObject = NavigationObject(viewType: .modify, data: data)
navigationEnvironment.navigate(navigationObject)
} label: {
Label("목표 수정", systemImage: "pencil.line")
}
} else {
Menu {
Button {
let data = ModifyDataModel(date: date, modifyRecord: record, modifyType: .single)
let navigationObject = NavigationObject(viewType: .modify, data: data)
navigationEnvironment.navigate(navigationObject)
} label: {
Text("단일 수정")
}
Button {
let data = ModifyDataModel(date: date, modifyRecord: record, modifyType: .all)
let navigationObject = NavigationObject(viewType: .modify, data: data)
navigationEnvironment.navigate(navigationObject)
} label: {
Text("일괄 수정")
}
} label: {
Label("목표 수정", systemImage: "pencil.line")
}
}
// MARK: DeleteGoal
if goal.cycleType == .date {
Button {
modelContext.delete(goal)
try? modelContext.save()
} label: {
Label("목표 삭제", systemImage: "trash")
}
} else {
Menu {
Button {
modelContext.delete(record)
try? modelContext.save()
} label: {
Text("단일 삭제")
}
Menu {
Button {
guard let totalRecords = try? modelContext.fetch(FetchDescriptor<DailyRecordModel>()) else { return }
let deleteRecords = totalRecords.filter { currentRecord in
guard let currentGoal = currentRecord.goal else { return false }
return currentGoal.parentGoal?.id ?? currentGoal.id == goal.id && currentRecord.date >= Calendar.current.startOfDay(for: Date())
}
deleteRecords.forEach { modelContext.delete($0) }
try? modelContext.save()
} label: {
Text("오늘 이후의 목표만 삭제")
}
// MARK: DeleteGoal
if goal.cycleType == .date {
Button {
modelContext.delete(goal)
try? modelContext.save()
} label: {
Label("목표 삭제", systemImage: "trash")
}
} else {
Menu {
Button {
modelContext.delete(record)
try? modelContext.save()
} label: {
Text("단일 삭제")
}
Menu {
Button {
guard let totalRecords = try? modelContext.fetch(FetchDescriptor<DailyRecordModel>()) else { return }
let deleteRecords = totalRecords.filter { currentRecord in
guard let currentGoal = currentRecord.goal else { return false }
return currentGoal.parentGoal?.id ?? currentGoal.id == goal.id && currentRecord.date >= Calendar.current.startOfDay(for: Date())
}
deleteRecords.forEach { modelContext.delete($0) }
try? modelContext.save()
} label: {
Text("오늘 이후의 목표만 삭제")
}
Button {
goal.childGoals.forEach { modelContext.delete($0) }
modelContext.delete(goal)
try? modelContext.save()
} label: {
Text("과거의 기록도 함께 삭제")
}
} label: {
Text("일괄 삭제")
}
} label: {
Label("목표 삭제", systemImage: "trash")
}
Button {
goal.childGoals.forEach { modelContext.delete($0) }
modelContext.delete(goal)
try? modelContext.save()
} label: {
Text("과거의 기록도 함께 삭제")
}
} label: {
Text("일괄 삭제")
}
} label: {
Label("목표 삭제", systemImage: "trash")
}
}
}
}
Expand Down Expand Up @@ -153,6 +170,7 @@ struct DailyRecord: View {
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topTrailing)
}
.padding(.horizontal, CGFloat.fontSize * 2)
.foregroundStyle(Colors.reverse)
.background {
RoundedRectangle(cornerRadius: 15).fill(Colors.background)
}
Expand Down

0 comments on commit 6598ce2

Please sign in to comment.