Skip to content

Commit

Permalink
feat: 음식 리스트 선택, 선택 해제 로직 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
ericKwon95 committed Aug 20, 2024
1 parent bb29a27 commit df73c19
Show file tree
Hide file tree
Showing 10 changed files with 46 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
60C610E02C4D3C4700C94465 /* CustomTabBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60C610DF2C4D3C4700C94465 /* CustomTabBar.swift */; };
60DF1A4B2C4235E80090057B /* ComponentBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60DF1A4A2C4235E80090057B /* ComponentBackground.swift */; };
60DF1A4F2C4238060090057B /* MyDiets.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60DF1A4E2C4238060090057B /* MyDiets.swift */; };
60DF1A542C423CD40090057B /* ButtonEnabledStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60DF1A532C423CD40090057B /* ButtonEnabledStyle.swift */; };
60DF1A542C423CD40090057B /* MyButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60DF1A532C423CD40090057B /* MyButtonStyle.swift */; };
60DF1A572C4260960090057B /* Diet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60DF1A562C4260960090057B /* Diet.swift */; };
60DF1A592C429DFE0090057B /* TransparentBlurView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60DF1A582C429DFE0090057B /* TransparentBlurView.swift */; };
60DF1A5C2C42C0D50090057B /* AddDiet.swift in Sources */ = {isa = PBXBuildFile; fileRef = 60DF1A5B2C42C0D50090057B /* AddDiet.swift */; };
Expand Down Expand Up @@ -97,7 +97,7 @@
60C610DF2C4D3C4700C94465 /* CustomTabBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTabBar.swift; sourceTree = "<group>"; };
60DF1A4A2C4235E80090057B /* ComponentBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ComponentBackground.swift; sourceTree = "<group>"; };
60DF1A4E2C4238060090057B /* MyDiets.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyDiets.swift; sourceTree = "<group>"; };
60DF1A532C423CD40090057B /* ButtonEnabledStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ButtonEnabledStyle.swift; sourceTree = "<group>"; };
60DF1A532C423CD40090057B /* MyButtonStyle.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyButtonStyle.swift; sourceTree = "<group>"; };
60DF1A562C4260960090057B /* Diet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Diet.swift; sourceTree = "<group>"; };
60DF1A582C429DFE0090057B /* TransparentBlurView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TransparentBlurView.swift; sourceTree = "<group>"; };
60DF1A5B2C42C0D50090057B /* AddDiet.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddDiet.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -345,7 +345,7 @@
isa = PBXGroup;
children = (
60DF1A4A2C4235E80090057B /* ComponentBackground.swift */,
60DF1A532C423CD40090057B /* ButtonEnabledStyle.swift */,
60DF1A532C423CD40090057B /* MyButtonStyle.swift */,
);
path = Modifiers;
sourceTree = "<group>";
Expand Down Expand Up @@ -548,7 +548,7 @@
60C610E02C4D3C4700C94465 /* CustomTabBar.swift in Sources */,
6082A0EB2C410E8D00C72C97 /* PillyzeApp.swift in Sources */,
6082A11F2C41299900C72C97 /* TabBarButton.swift in Sources */,
60DF1A542C423CD40090057B /* ButtonEnabledStyle.swift in Sources */,
60DF1A542C423CD40090057B /* MyButtonStyle.swift in Sources */,
6076A3E42C45073100F95DC9 /* BottomPicker.swift in Sources */,
6082A11D2C41288300C72C97 /* TabBar.swift in Sources */,
60DF1A592C429DFE0090057B /* TransparentBlurView.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import Foundation

struct Food: Identifiable, Codable {
struct Food: Hashable, Identifiable, Codable {
let id = UUID().uuidString
let name: String
let unit: String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import Foundation

@Observable
class ModelData {
var foods: [Food] = JSONLoader.load("food_list_with_rank.json")
var foods: [Food] = JSONLoader.load("food_list_with_rank.json") {
didSet {
foodsWithRice = foods.filter { $0.name.contains("") }
}
}
var foodsWithRice: [Food] = []
var isAdded: Bool = false
var selectedFoods: [Food: Int] = [:]
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"components" : {
"alpha" : "1.000",
"blue" : "0xFF",
"green" : "0xAA",
"red" : "0xBC"
"green" : "0xE8",
"red" : "0xEC"
}
},
"idiom" : "universal"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import SwiftUI

struct BottomPicker: View {
@Binding var isAddingList: Bool
@Environment(ModelData.self) private var modelData

var body: some View {
VStack(spacing: 0) {
Expand All @@ -29,12 +30,13 @@ struct BottomPicker: View {
.font(.system(size: 18))
.fontWeight(.heavy)
.foregroundStyle(.textNormal)
.padding(.trailing, 29)

Button {

} label: {
Text("기록하기")
.buttonEnabledStyle()
.myButtonStyle(isEnabled: !modelData.selectedFoods.isEmpty)
}
.buttonStyle(PlainButtonStyle())
}
Expand All @@ -45,7 +47,8 @@ struct BottomPicker: View {
}

#Preview {
BottomPicker(isAddingList: .constant(true))
BottomPicker(isAddingList: .constant(false))
.environment(ModelData())
}

struct MealPicker: View {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct FoodInfo: View {
.font(.system(size: 16))
.fontWeight(.medium)
.foregroundStyle(.textSecondary)
AddButton()
AddButton(food: food)
}
}
.padding(.vertical, 16)
Expand Down Expand Up @@ -74,6 +74,7 @@ struct FoodInfo: View {
struct AddButton: View {
@Environment(ModelData.self) private var modelData
@State private var isAdded: Bool = false
let food: Food

var body: some View {
Image(isAdded ? .dietSelected : .dietUnselected)
Expand All @@ -84,10 +85,25 @@ struct AddButton: View {
isAdded.toggle()
if isAdded {
modelData.isAdded = true
modelData.selectedFoods[food] = food.hashValue
} else {
modelData.selectedFoods[food] = nil
}
}
.animation(.easeInOut, value: isAdded)
.disabled(modelData.isAdded)
.onAppear {
if modelData.selectedFoods[food] != nil {
isAdded = true
}
}
.onChange(of: modelData.selectedFoods) { oldValue, newValue in
if newValue[food] != nil {
isAdded = true
} else {
isAdded = false
}
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ struct CustomTabBar: View {
VStack(spacing: 0) {
TabBarHeader(selection: $selection, indicatorOffset: $indicatorOffset, proxy: proxy)
.onChange(of: selection) { oldValue, newValue in
modelData.selectedFoods = [:]
switch newValue {
case .frequentlyEaten:
indicatorOffset = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct FrequentlyEaten: View {
case .all:
FoodList(foods: modelData.foods, isRankedList: false)
case .food:
FoodList(foods: modelData.foods.filter { $0.name.contains("")}, isRankedList: false)
FoodList(foods: modelData.foodsWithRice, isRankedList: false)
case .set:
Image(.favoritesPlaceholder)
.resizable()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
//
// ButtonBackground.swift
// MyButtonStyle.swift
// Pillyze
//
// Created by 권승용 on 7/13/24.
//

import SwiftUI

struct ButtonEnabledStyle: ViewModifier {
struct MyButtonStyle: ViewModifier {
var isEnabled: Bool

func body(content: Content) -> some View {
content
.font(.system(size: 18))
Expand All @@ -17,14 +19,14 @@ struct ButtonEnabledStyle: ViewModifier {
.padding(.vertical, 16)
.background {
RoundedRectangle(cornerRadius: 8)
.foregroundStyle(.appPrimary)
.foregroundStyle(isEnabled ? .appPrimary : .appPrimaryDisabled)
}
}
}

extension View {
func buttonEnabledStyle() -> some View {
modifier(ButtonEnabledStyle())
func myButtonStyle(isEnabled: Bool = true) -> some View {
modifier(MyButtonStyle(isEnabled: isEnabled))
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ struct MyDiets: View {
isPresented = true
} label: {
Text("내 식단 점수 확인하기")
.buttonEnabledStyle()
.myButtonStyle()
}
.fullScreenCover(isPresented: $isPresented) {
AddDiet()
Expand Down

0 comments on commit df73c19

Please sign in to comment.