Skip to content

Commit

Permalink
fix: MealContainer
Browse files Browse the repository at this point in the history
  • Loading branch information
hhhello0507 committed Aug 12, 2024
1 parent efa4d1e commit ac06419
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 46 deletions.
4 changes: 4 additions & 0 deletions Projects/Domain/Source/Response/Meal/MealResponse.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ public struct MealResponse: ResponseProtocol {
public let breakfast: Meal?
public let lunch: Meal?
public let dinner: Meal?

public var meals: [Meal] {
[self.breakfast, self.lunch, self.dinner].compactMap { $0 }
}
}
63 changes: 19 additions & 44 deletions Projects/Feature/Source/Home/Component/MealContainer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,72 +8,47 @@
import SwiftUI
import DDS
import Domain
import Shared

struct MealContainer: View {

@State private var pageSize: CGSize?
private let mealData: MealResponse?
@Binding var mealIdx: Int

public init(
data mealData: MealResponse?,
mealIdx: Binding<Int>
) {
self.mealData = mealData
self._mealIdx = mealIdx
self.animatedIdx = mealIdx.wrappedValue
}

@State private var animatedIdx: Int
@State private var heights: [Int: CGFloat] = [:]


var body: some View {
if let data = mealData {
if data.exists {
let meals = [
data.breakfast,
data.lunch,
data.dinner
]
DodamPageView(selection: $mealIdx) {
ForEach(meals.indices, id: \.self) { idx in
GeometryReader { geometryProxy in
let text = meals[idx]?.details.map {
$0.name
}.joined(separator: ", ") ?? "급식이 없어요."
Text(text)
.body1(.medium)
.foreground(DodamColor.Label.normal)
.multilineTextAlignment(.leading)
.fixedSize(horizontal: false, vertical: true)
.onAppear {
withAnimation(.spring) {
heights[idx] = text.boundingRect(
with: CGSize(
width: geometryProxy.size.width,
height: .greatestFiniteMagnitude
),
options: .usesLineFragmentOrigin,
attributes: [
.font: UIFont(
name: Pretendard.Weight.semibold.rawValue,
size: 18
)!
],
context: nil
).height
ForEach(data.meals, id: \.self) { meal in
let splitedArray = splitArray(array: meal.details)
HStack(alignment: .top) {
ForEach(splitedArray, id: \.self) { meals in
VStack(alignment: .leading, spacing: 0) {
ForEach(meals, id: \.self) { meal in
Text(meal.name)
.body1(.medium)
.foreground(DodamColor.Label.normal)
}
}
.frame(maxWidth: .infinity, alignment: .leading)
}
}
.onReadSize { size in
self.pageSize = size
}
.padding(.horizontal, 6)
.page()
}
}
.frame(height: heights[animatedIdx] ?? 44.928)
.onChange(of: mealIdx) { newValue in
withAnimation(.spring(duration: 0.2)) {
animatedIdx = newValue
}
}
.frame(height: pageSize?.height ?? 999)
.onAppear {
let currentTime = Date()
let calendar = Calendar.current
Expand Down
3 changes: 1 addition & 2 deletions Projects/Feature/Source/Meal/MealView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,8 @@ struct MealView: View {
.padding(.top, 12)
DodamDivider()
if let meals = viewModel.selectedMeal {
let meals = Array([meals.breakfast, meals.lunch, meals.dinner].compactMap { $0 }.enumerated())
VStack(spacing: 12) {
ForEach(meals, id: \.offset) { idx, meal in
ForEach(Array(meals.meals.enumerated()), id: \.offset) { idx, meal in
if let mealType = MealType(rawValue: idx) {
MealCell(type: mealType, meal: meal)
}
Expand Down
15 changes: 15 additions & 0 deletions Projects/Shared/Source/Extension/Foundation/ArrayUtil.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// ArrayExt.swift
// Shared
//
// Created by hhhello0507 on 8/12/24.
//

import Foundation

public func splitArray<T>(array: [T]) -> [[T]] {
let middleIndex: Int = Int(ceil(Double(array.count) / 2.0))
let firstHalf = Array(array[0..<middleIndex])
let secondHalf = Array(array[middleIndex..<array.count])
return [firstHalf, secondHalf]
}
21 changes: 21 additions & 0 deletions Projects/Shared/Source/Extension/SwiftUI/ViewExt.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,25 @@ public extension View {
self
}
}

@ViewBuilder
func onReadSize(_ perform: @escaping (CGSize) -> Void) -> some View {
self.customBackground {
GeometryReader { geometryProxy in
Color.clear
.preference(key: SizePreferenceKey.self, value: geometryProxy.size)
}
}
.onPreferenceChange(SizePreferenceKey.self, perform: perform)
}

@ViewBuilder
func customBackground<V: View>(alignment: Alignment = .center, @ViewBuilder content: () -> V) -> some View {
self.background(alignment: alignment, content: content)
}
}

struct SizePreferenceKey: PreferenceKey {
static var defaultValue: CGSize = .zero
static func reduce(value: inout CGSize, nextValue: () -> CGSize) { }
}

0 comments on commit ac06419

Please sign in to comment.