Skip to content

Commit

Permalink
날짜 투두 및 메모 여부 표시 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ghkdemrdus committed Nov 24, 2024
1 parent cfb3205 commit 4e2022d
Show file tree
Hide file tree
Showing 12 changed files with 132 additions and 58 deletions.
1 change: 1 addition & 0 deletions App/Moda/Sources/Feature/Bookmark/BookmarkView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ struct BookmarkView: View {
} else if !bookmarks.isEmpty {
modelContext.insert(YearlyBookmarksEntity(id: id, bookmarks: bookmarks.toEntity))
}
try? modelContext.save()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct HomeDailyMemoCore: Reducer {
case delegate(Delegate)
enum Delegate {
case showMemoOption(DailyMemo)
case memoUpdated([DailyMemo])
}

case passThrough(PassThrough)
Expand Down Expand Up @@ -85,7 +86,7 @@ struct HomeDailyMemoCore: Reducer {

case let .addMemo(memo):
state.memos.append(memo)
return .none
return .send(.delegate(.memoUpdated(state.memos)))

case let .updateMemo(memo):
for idx in state.memos.indices {
Expand All @@ -98,9 +99,14 @@ struct HomeDailyMemoCore: Reducer {

case let .deleteMemo(memo):
state.memos = state.memos.filter { $0.id != memo.id }
return .run { _ in
await toast.show(.init(icon: .icDelete, message: "삭제 완료!"))
}
return .merge(
.run { [memos = state.memos] send in
await send(.delegate(.memoUpdated(memos)))
},
.run { _ in
await toast.show(.init(icon: .icDelete, message: "삭제 완료!"))
}
)
}

default:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,14 +254,13 @@ private extension HomeRoutineView {
func updateRoutineRecords(records: [RoutineRecord]) {
let record = records.first(where: { $0.date == store.currentDate })!
let storedIdx = routineRecordEntitys.firstIndex(where: { $0.id == record.id })
guard let storedIdx else {
if !record.routineIds.isEmpty {
modelContext.insert(record.toEntity)
}
return
}
if routineRecordEntitys[storedIdx].routineIds != record.routineIds {

if let storedIdx, routineRecordEntitys[storedIdx].routineIds != record.routineIds {
routineRecordEntitys[storedIdx].routineIds = record.routineIds
} else if !record.routineIds.isEmpty {
modelContext.insert(record.toEntity)
}

try? modelContext.save()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,34 @@ private extension HomeDateItemView {
Text("\(date.date.day)")
.font(.spoqaHans(size: 13, weight: .bold))
.foregroundStyle(dayColor)
.frame(width: 32, height: 32)
.background(alignment: .bottom) {
HStack(spacing: 2) {
if date.hasTodo {
Circle()
.fill(date.timeline == .current ? Color.backgroundPrimary : Color.brandStrong)
.frame(size: 4)
}

if date.hasMemo {
Circle()
.fill(Color.textInactive)
.frame(size: 4)
}
}
.padding(.bottom, 4)
}
.background(if: date.timeline == .current) {
RoundedRectangle(cornerRadius: 8)
.fill(dayBgColor)
.frame(width: 32, height: 32)
}
.background(if: date.timeline == .previous && currentDate == date) {
Circle()
.stroke(dayBgColor, lineWidth: 1)
.frame(width: 32, height: 32)
}
.background(if: date.timeline == .following && currentDate == date) {
Circle()
.fill(dayBgColor)
.frame(width: 32, height: 32)
}
}
.frame(height: 64)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
// Copyright © 2024 Moda. All rights reserved.
//

import SwiftData
import SwiftUI

struct HomeDateSelectorView: View {

static let height: CGFloat = 64

@Query var dailyTodosEntities: [DailyTodos]
@Query var memoEntities: [DailyMemosEntity]

@Binding var currentDate: HomeDate
@Binding var weekDates: [[HomeDate]]

Expand Down Expand Up @@ -47,8 +51,14 @@ struct HomeDateSelectorView: View {
.onFirstAppear {
initialize()
}
.onChange(of: currentDate) {
initialize()
.onChange(of: currentDate) { old, new in
if old.date != new.date {
initialize()
} else if weekDates.count == 3 {
if let idx = weekDates[1].firstIndex(where: { $0.date == new.date }) {
weekDates[1][idx] = new
}
}
}
}

Expand All @@ -58,26 +68,20 @@ struct HomeDateSelectorView: View {
let next = currentDate.date.addDays(7)

weekDates = [
DateManager.shared.weekDates(for: prev),
DateManager.shared.weekDates(for: current),
DateManager.shared.weekDates(for: next)
DateManager.shared.weekDates(for: prev, dailyTodosEntities: dailyTodosEntities, memosEntities: memoEntities),
DateManager.shared.weekDates(for: current, dailyTodosEntities: dailyTodosEntities, memosEntities: memoEntities),
DateManager.shared.weekDates(for: next, dailyTodosEntities: dailyTodosEntities, memosEntities: memoEntities)
]
}

private func paginate() {
if selection == 0 {
let prev = DateManager.shared.weekDates(for: weekDates[0].first!.date.addDays(-7))
currentDate = weekDates[0].first!
weekDates.insert(prev, at: 0)
weekDates.removeLast()
selection = 1
}

if selection == 2 {
let next = DateManager.shared.weekDates(for: weekDates[2].first!.date.addDays(7))
currentDate = weekDates[2].first!
weekDates.append(next)
weekDates.removeFirst()
selection = 1
}
}
Expand Down
29 changes: 8 additions & 21 deletions App/Moda/Sources/Feature/Home/HomeCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ struct HomeCore: Reducer {
case monthChanged(Int)
case monthTapped

case todoAdded(Todo.Category, String)
case monthlyAdded(Todo)
case monthlyEditTapped
case monthlyTodoDoneTapped(Todo)
Expand Down Expand Up @@ -168,27 +167,8 @@ struct HomeCore: Reducer {
return .send(.view(.binding(.set(\.currentDate, dates.first!))))

case .monthTapped:
let dates = DateManager.shared.homeDatas(from: .today)
return .send(.view(.binding(.set(\.currentDate, HomeDate.today))))

case let .todoAdded(category, content):
let content = content.trimmed
if content.isEmpty { return .none }

switch category {
case .monthly:
let todo = Todo(content: content, category: .monthly)
let updatedTodos = state.monthlyTodos.updating(todo: todo)
state.monthlyTodos = updatedTodos
return .none

case .daily:
let todo = Todo(content: content, category: .daily)
let updatedTodos = state.dailyTodos.updating(todo: todo)
state.dailyTodos = updatedTodos
return .none
}

case let .monthlyAdded(todo):
var todo = todo
todo.content = todo.content.trimmed
Expand Down Expand Up @@ -247,7 +227,7 @@ struct HomeCore: Reducer {
var todo = todo
todo.content = todo.content.trimmed
if todo.content.isEmpty { return .none }

state.currentDate.hasTodo = true
state.dailyTodos = state.dailyTodos.updating(todo: todo)
return .none

Expand Down Expand Up @@ -302,6 +282,9 @@ struct HomeCore: Reducer {
case let .view(.doneTapped(todos)):
state.isDailyEditing = false
state.dailyTodos = todos.reordering()
if todos.isEmpty {
state.currentDate.hasTodo = false
}
return .none

case let .view(.delayTapped(todo)):
Expand All @@ -320,6 +303,10 @@ struct HomeCore: Reducer {

// MARK: Child - Routine

case let .memo(.delegate(.memoUpdated(memos))):
state.currentDate.hasMemo = !memos.isEmpty
return .none

default:
return .none
}
Expand Down
11 changes: 9 additions & 2 deletions App/Moda/Sources/Feature/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@ struct HomeView: View {
send(.onTask)
}
.onFirstAppear {
changeTodos(oldDate: store.currentDate, newDate: store.currentDate)
send(.onFirstAppear)
}
.onForeground {
if store.currentDate.date == Date.today { return }
send(.binding(.set(\.currentDate, HomeDate.today)))
}
.onDisappear {
Expand All @@ -44,8 +46,10 @@ struct HomeView: View {
.onChange(of: store.dailyTodos) {
updateLocalData(dailyTodos: $1)
}
.onChange(of: store.currentDate, initial: true) {
changeTodos(oldDate: $0, newDate: $1)
.onChange(of: store.currentDate) {
if $0.date != $1.date {
changeTodos(oldDate: $0, newDate: $1)
}
}
.onChange(of: store.delayTodo) {
guard let todo = $1, let date = store.delayDate else { return }
Expand Down Expand Up @@ -174,6 +178,7 @@ private extension HomeView {
} else if !monthlyTodos.isEmpty {
modelContext.insert(MonthlyTodos(id: id, todos: monthlyTodos.toEntity))
}
try? modelContext.save()
}

func updateLocalData(dailyTodos: [Todo]) {
Expand All @@ -188,6 +193,7 @@ private extension HomeView {
} else if !dailyTodos.isEmpty {
modelContext.insert(DailyTodos(id: id, todos: dailyTodos.toEntity))
}
try? modelContext.save()
}

func delayTodo(todo: Todo, date: Date) {
Expand All @@ -213,6 +219,7 @@ private extension HomeView {
let updatedTodo = Todo(content: todo.content, isDone: todo.isDone, category: todo.category)
modelContext.insert(DailyTodos(id: id, todos: [updatedTodo].toEntity))
}
try? modelContext.save()
}
}

Expand Down
3 changes: 3 additions & 0 deletions App/Moda/Sources/Feature/Splash/SplashView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,21 @@ struct SplashView: View {
newValue.forEach {
modelContext.insert($0.toEntity)
}
try? modelContext.save()
}
.onChange(of: store.routineRecords) { oldValue, newValue in
guard routineRecords.count == 0 else { return }
newValue.forEach {
modelContext.insert($0.toEntity)
}
try? modelContext.save()
}
.onChange(of: store.memoCategories) {
guard memoCategories.count == 0 else { return }
$1.forEach {
modelContext.insert($0.toEntity)
}
try? modelContext.save()
}
}
}
Expand Down
24 changes: 17 additions & 7 deletions App/Moda/Sources/Util/DateManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ struct DateManager {

while date < endDate {
if date == today {
dates.append(HomeDate(date: date, timeline: .current, hasTodo: false))
dates.append(HomeDate(date: date, timeline: .current))
} else if date < today {
dates.append(HomeDate(date: date, timeline: .previous, hasTodo: false))
dates.append(HomeDate(date: date, timeline: .previous))
} else {
dates.append(HomeDate(date: date, timeline: .following, hasTodo: false))
dates.append(HomeDate(date: date, timeline: .following))
}
date = date.addDays(1)
}
return dates
}

func weekDates(for date: Date) -> [HomeDate] {
func weekDates(for date: Date, dailyTodosEntities: [DailyTodos] = [], memosEntities: [DailyMemosEntity] = []) -> [HomeDate] {
var calendar = Calendar.current
calendar.firstWeekday = 1 // 일요일을 주의 시작으로 설정 (1: 일요일, 2: 월요일, ...)
let today = Date.today
Expand All @@ -45,12 +45,22 @@ struct DateManager {
var dates: [HomeDate] = []
for i in 0..<7 {
if let weekDate = calendar.date(byAdding: .day, value: i, to: startOfWeek) {
var hasTodo = false
if let idx = dailyTodosEntities.firstIndex(where: { $0.id == weekDate.format(.yyMMdd) }) {
hasTodo = dailyTodosEntities[idx].todos.count > 0
}

var hasMemo = false
if let idx = memosEntities.firstIndex(where: { $0.id == weekDate.format(.yyMMdd) }) {
hasMemo = memosEntities[idx].memos.count > 0
}

if weekDate == today {
dates.append(HomeDate(date: weekDate, timeline: .current, hasTodo: false))
dates.append(HomeDate(date: weekDate, timeline: .current, hasTodo: hasTodo, hasMemo: hasMemo))
} else if weekDate < today {
dates.append(HomeDate(date: weekDate, timeline: .previous, hasTodo: false))
dates.append(HomeDate(date: weekDate, timeline: .previous, hasTodo: hasTodo, hasMemo: hasMemo))
} else {
dates.append(HomeDate(date: weekDate, timeline: .following, hasTodo: false))
dates.append(HomeDate(date: weekDate, timeline: .following, hasTodo: hasTodo, hasMemo: hasMemo))
}
}
}
Expand Down
10 changes: 7 additions & 3 deletions Core/ModaData/Sources/Model/HomeDate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@ public struct HomeDate: Hashable {

public var date: Date
public let timeline: Timeline
public let hasTodo: Bool
public var hasTodo: Bool
public var hasMemo: Bool

public init(date: Date, timeline: Timeline, hasTodo: Bool) {
public init(date: Date, timeline: Timeline, hasTodo: Bool = false, hasMemo: Bool = false) {
self.date = date
self.timeline = timeline
self.hasTodo = hasTodo
self.hasMemo = hasMemo
}

public static var today: HomeDate { .init(date: .today, timeline: .current, hasTodo: false) }
public static var today: HomeDate {
.init(date: .today, timeline: .current, hasTodo: false, hasMemo: false)
}
}
Loading

0 comments on commit 4e2022d

Please sign in to comment.