Skip to content

Commit

Permalink
rename/move: HomeView 파일 내부 컴포넌트 파일 분리및 홈 뷰 폴더 구조 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
hyeonghwan committed Nov 8, 2023
1 parent f8b4585 commit 9d064ca
Show file tree
Hide file tree
Showing 11 changed files with 284 additions and 146 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// ClearBackground.swift
// Pickle
//
// Created by 박형환 on 11/6/23.
//

import Foundation
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,40 @@ import SwiftUI
//
// TODO: Image Cache 현재 PizzaSeleted의 이미지 메모리량을 많이 잡아먹는 상태

struct PizzaPuchasePresentKey: PreferenceKey {
static var defaultValue: Bool = false
static func reduce(value: inout Bool, nextValue: () -> Bool) {
value = nextValue()
}
}

struct CurrentPizzaKey: PreferenceKey {
static var defaultValue: Pizza = .defaultPizza
static func reduce(value: inout Pizza, nextValue: () -> Pizza) {
value = nextValue()
}
}

struct SeletedPizzaKey: PreferenceKey {
static var defaultValue: Pizza = .defaultPizza
static func reduce(value: inout Pizza, nextValue: () -> Pizza) {
value = nextValue()
}
}

struct PizzaSelectedView: View {

var columns: [GridItem] = Array(repeating: .init(.flexible()), count: 3)

@Binding var pizzas: [Pizza]
@Binding var seletedPizza: Pizza
@Binding var currentPizza: Pizza
@Binding var isPizzaPuchasePresented: Bool
@State private var isPizzaPuchasePresent: Bool = false
@Binding var selection: Selection

struct Selection {
var pizzas: [Pizza] = []
var seletedPizza: Pizza = .defaultPizza
var currentPizza: Pizza = .defaultPizza
var isPizzaSelected: Bool = false
}

var body: some View {
VStack { // ScrollView
Expand All @@ -37,25 +63,31 @@ struct PizzaSelectedView: View {
.padding(.top, 10)

LazyVGrid(columns: columns) {
ForEach(pizzas.indices, id: \.self) { index in
PizzaItemView(pizza: $pizzas[safe: index] ?? .constant(.potato),
currentPizza: $currentPizza)
.frame(width: CGFloat.screenWidth / 3 - 40)
.padding(.horizontal, 10)
.onTapGesture {
let seleted = seletedPizza
seletedPizza = pizzas[safe: index] ?? .defaultPizza
if seletedPizza == seleted,
seletedPizza.lock {
isPizzaPuchasePresented.toggle()
}
ForEach(selection.pizzas.indices, id: \.self) { index in
PizzaItemView(pizza: $selection.pizzas[safe: index] ?? .constant(.potato),
currentPizza: $selection.currentPizza)
.frame(width: CGFloat.screenWidth / 3 - 40)
.padding(.horizontal, 10)
.onTapGesture {
selectionLogic(index: index)
}
.preference(key: PizzaPuchasePresentKey.self,
value: isPizzaPuchasePresent)
}
}
Spacer()
}
.modeBackground() // MARK: safe Area 까지 확장되는 이슈 [] 해겨
}

private func selectionLogic(index: Int) {

selection.seletedPizza = selection.pizzas[safe: index] ?? .defaultPizza

if selection.seletedPizza.lock {
isPizzaPuchasePresent.toggle()
}
}
}

struct PizzaItemView: View {
Expand Down Expand Up @@ -112,15 +144,16 @@ struct PizzaItemView: View {
}

#Preview {
PizzaSelectedView(columns: Array(repeating: .init(.flexible()), count: 3),
pizzas: .constant( [Pizza(name: "고구마",
image: "baconPotato",
lock: false,
createdAt: Date())]),
seletedPizza: .constant(Pizza(name: "고구마",
image: "baconPotato",
lock: false,
createdAt: Date())),
currentPizza: .constant(.baconPotato),
isPizzaPuchasePresented: .constant(false))
Text("daf")
// PizzaSelectedView(columns: Array(repeating: .init(.flexible()), count: 3),
// pizzas: .constant( [Pizza(name: "고구마",
// image: "baconPotato",
// lock: false,
// createdAt: Date())]),
// seletedPizza: .constant(Pizza(name: "고구마",
// image: "baconPotato",
// lock: false,
// createdAt: Date())),
// currentPizza: .constant(.baconPotato)
// )
}
54 changes: 54 additions & 0 deletions Pickle/Pickle/Screen/Home/HomeView/Util/HomeView-extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
//
// HomeView-extension.swift
// Pickle
//
// Created by 박형환 on 11/7/23.
//

import SwiftUI


extension HomeView {

struct SheetModifier: ViewModifier {
@Binding var selection: PizzaSelectedView.Selection
/*@Binding var updateSignal: Bool*/ // TODO: 피자 업데이트 신호,,,추후 변경

@GestureState private var offset = CGSize.zero
@EnvironmentObject var pizzaStore: PizzaStore
@EnvironmentObject var userStore: UserStore

func body(content: Content) -> some View {

content
.overlay {
if selection.isPizzaSelected {
// For getting frame for image
GeometryReader { proxy in
let frame = proxy.frame(in: .global)
Color.black
.opacity(0.3)
.frame(width: frame.width, height: frame.height)
}
.ignoresSafeArea()

CustomSheetView(isPresented: $selection.isPizzaSelected) {
PizzaSelectedView(selection: $selection)
}.transition(.move(edge: .bottom).combined(with: .opacity))
}
}
.toolbar(selection.isPizzaSelected ? .hidden : .visible, for: .tabBar)
}
}

struct FullScreenCoverModifier: ViewModifier {
@Binding var isPresented: Bool
@Binding var seletedTodo: Todo
func body(content: Content) -> some View {
content.fullScreenCover(isPresented: $isPresented) {
UpdateTodoView(isShowingEditTodo: $isPresented,
todo: $seletedTodo)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// NavigationModifier.swift
// Pickle
//
// Created by 박형환 on 11/7/23.
//

import Foundation
8 changes: 8 additions & 0 deletions Pickle/Pickle/Screen/Home/HomeView/Util/PreferenceKey.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
//
// PreferenceKey.swift
// Pickle
//
// Created by 박형환 on 11/7/23.
//

import Foundation
57 changes: 57 additions & 0 deletions Pickle/Pickle/Screen/Home/HomeView/Util/View-extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// View.swift
// Pickle
//
// Created by 박형환 on 11/7/23.
//

import SwiftUI


// MARK: HomeView Modifier
extension View {

@ViewBuilder
func routing(routing: Binding<HomeView.Routing>) -> some View {

}

func navigationSetting(tabBarvisibility: Binding<Visibility>) -> some View {
modifier(NavigationModifier(tabBarvisibility: tabBarvisibility))
}

// func mutilpleModifier(selection: Binding<PizzaSelectedView.Selection>,
// isPresented: Binding<Bool>,
// seletedTodo item: Binding<Todo>,
// value: PZAContent) {
// modifier(<#T##modifier: T##T#>)
// }

func sheetModifier(selection: Binding<PizzaSelectedView.Selection>) -> some View {

modifier(HomeView.SheetModifier(selection: selection))
}

func fullScreenCover(isPresented: Binding<Bool>,
seletedTodo item: Binding<Todo>) -> some View {

modifier(HomeView.FullScreenCoverModifier(isPresented: isPresented,
seletedTodo: item))
}

func showPizzaPurchaseAlert(_ pizza: Pizza,
_ isPizzaPuchasePresented: Binding<Bool>,
_ purchaseAction: @escaping () -> Void,
_ navAction: (() -> Void)? = nil) -> some View {
modifier(PizzaAlertModifier(isPresented: isPizzaPuchasePresented,
title: "\(pizza.name)",
price: "",
descripation: "피자 2판을 완성하면 얻을수 있어요",
image: "\(pizza.image)",
lock: pizza.lock,
puchaseButtonTitle: "피자 구매하기 (₩1,200)",
primaryButtonTitle: "피자 완성하러 가기",
primaryAction: purchaseAction,
pizzaMakeNavAction: navAction))
}
}
Loading

0 comments on commit 9d064ca

Please sign in to comment.