Skip to content

Commit

Permalink
CartView
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexKolch committed Oct 17, 2023
1 parent b5d59bc commit 621812b
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 8 deletions.
4 changes: 4 additions & 0 deletions PizzaShop.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
FC406EA32ADBDAD2007EF4B0 /* ProductDetailViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC406EA22ADBDAD2007EF4B0 /* ProductDetailViewModel.swift */; };
FC406EA52ADD5BEA007EF4B0 /* CartViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC406EA42ADD5BEA007EF4B0 /* CartViewModel.swift */; };
FC406EA72ADD5C41007EF4B0 /* Position.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC406EA62ADD5C41007EF4B0 /* Position.swift */; };
FC406EA92ADD743A007EF4B0 /* PositionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC406EA82ADD743A007EF4B0 /* PositionCell.swift */; };
FC4AE51A2ADA693E00CAF2EF /* MainTabBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC4AE5192ADA693E00CAF2EF /* MainTabBar.swift */; };
FC4AE51C2ADA6C9900CAF2EF /* CatalogView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC4AE51B2ADA6C9900CAF2EF /* CatalogView.swift */; };
FC4AE51F2ADA6D0A00CAF2EF /* CartView.swift in Sources */ = {isa = PBXBuildFile; fileRef = FC4AE51E2ADA6D0A00CAF2EF /* CartView.swift */; };
Expand All @@ -30,6 +31,7 @@
FC406EA22ADBDAD2007EF4B0 /* ProductDetailViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProductDetailViewModel.swift; sourceTree = "<group>"; };
FC406EA42ADD5BEA007EF4B0 /* CartViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CartViewModel.swift; sourceTree = "<group>"; };
FC406EA62ADD5C41007EF4B0 /* Position.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Position.swift; sourceTree = "<group>"; };
FC406EA82ADD743A007EF4B0 /* PositionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PositionCell.swift; sourceTree = "<group>"; };
FC4AE5192ADA693E00CAF2EF /* MainTabBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainTabBar.swift; sourceTree = "<group>"; };
FC4AE51B2ADA6C9900CAF2EF /* CatalogView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CatalogView.swift; sourceTree = "<group>"; };
FC4AE51E2ADA6D0A00CAF2EF /* CartView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CartView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -77,6 +79,7 @@
isa = PBXGroup;
children = (
FC4AE5252ADA8D1900CAF2EF /* ProductCell.swift */,
FC406EA82ADD743A007EF4B0 /* PositionCell.swift */,
);
path = Components;
sourceTree = "<group>";
Expand Down Expand Up @@ -208,6 +211,7 @@
FC4AE51A2ADA693E00CAF2EF /* MainTabBar.swift in Sources */,
FC728EC82AD98B460004DBE3 /* AuthView.swift in Sources */,
FC4AE51C2ADA6C9900CAF2EF /* CatalogView.swift in Sources */,
FC406EA92ADD743A007EF4B0 /* PositionCell.swift in Sources */,
FC406EA32ADBDAD2007EF4B0 /* ProductDetailViewModel.swift in Sources */,
FC406EA72ADD5C41007EF4B0 /* Position.swift in Sources */,
FC4AE5212ADA6D2C00CAF2EF /* ProfileView.swift in Sources */,
Expand Down
3 changes: 2 additions & 1 deletion PizzaShop/Model/Position.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

import Foundation

struct Position {
struct Position: Identifiable {

var product: Product
var id: String
var count: Int
Expand Down
55 changes: 52 additions & 3 deletions PizzaShop/View/CartView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,64 @@
import SwiftUI

struct CartView: View {
var viewModel: CartViewModel

@StateObject var viewModel: CartViewModel

var body: some View {
Text("Корзина!")
VStack {
List(viewModel.positions) { position in
PositionCell(position: position)
.swipeActions {
//УДАЛЯЕМ ЯЧЕЙКУ ПОЗИЦИИ ИЗ viewModel
Button {
viewModel.positions.removeAll { pos in
pos.id == position.id
}
} label: {
Text("Удалить")
}
}
}
.listStyle(.plain)
.navigationTitle("Корзина")

HStack {
Text("Итого:").fontWeight(.bold)
Spacer()
Text("\(viewModel.cost)").fontWeight(.bold)
}.padding()

HStack {
Button {
print("Отменить")
} label: {
Text("Отменить")
.fontWeight(.bold)
.padding()
.foregroundColor(.white)
.background(Color.red)
.cornerRadius(24)
}

Button {
print("Заказать")
} label: {
Text("Заказать")
.fontWeight(.bold)
.padding()
.foregroundColor(.white)
.frame(maxWidth: .infinity)
.background(Color.green)
.cornerRadius(24)
}

}.padding()
}
}
}

struct CartView_Previews: PreviewProvider {
static var previews: some View {
CartView(viewModel: CartViewModel())
CartView(viewModel: CartViewModel.shared)
}
}
35 changes: 35 additions & 0 deletions PizzaShop/View/Components/PositionCell.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
//
// PositionCell.swift
// PizzaShop
//
// Created by Алексей Колыченков on 16.10.2023.
//

import SwiftUI

struct PositionCell: View {

let position: Position

var body: some View {

HStack(spacing: 26.0) {
Text(position.product.title)
.fontWeight(.bold)

Spacer()
Text("\(position.count) шт.")
Text("\(position.cost)").frame(width: 75, alignment: .trailing)
}.padding(.horizontal, 8)
}
}

struct PositionCell_Previews: PreviewProvider {
static var previews: some View {
PositionCell(
position: Position(product: Product(id: UUID().uuidString, title: "Пицца Маргарита", imageUrl: "vegetable", price: 350, descript: "_"),
id: UUID().uuidString,
count: 2)
)
}
}
2 changes: 1 addition & 1 deletion PizzaShop/View/MainTabBar.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct MainTabBar: View {
Text("Каталог")
}

CartView(viewModel: CartViewModel())
CartView(viewModel: CartViewModel.shared)
.tabItem {
Image(systemName: "cart")
Text("Корзина")
Expand Down
23 changes: 20 additions & 3 deletions PizzaShop/View/ProductDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,19 @@ import SwiftUI
struct ProductDetailView: View {

var viewModel: ProductDetailViewModel

@State var size: Sizes
@State var count = 1

@Environment(\.dismiss) var dismiss
@Environment(\.presentationMode) var presentationMode

var body: some View {
VStack {
VStack(alignment: .leading) {

Image(viewModel.product.imageUrl).resizable()
.frame(maxWidth: .infinity, maxHeight: 260)
.frame(maxWidth: .infinity, maxHeight: 300)

HStack {
Text("\(viewModel.product.title)").font(.title2.bold())
Expand All @@ -44,20 +49,32 @@ struct ProductDetailView: View {

Button {
print("Добавить в корзину")

var position = Position(product: viewModel.product, id: UUID().uuidString, count: self.count)
position.product.price = viewModel.getPrice(size: self.size)

CartViewModel.shared.addPosition(position)

if #available(iOS 15, *) {
self.dismiss()
} else {
self.presentationMode.wrappedValue.dismiss()
}

} label: {
Text("Добавить в корзину")
}.foregroundColor(Color("darkOrange")).font(.title3).bold()
.frame(width: 250, height: 50)
.background(LinearGradient(colors: [Color("yellow"), Color("orange")], startPoint: .leading, endPoint: .trailing))
.cornerRadius(30)

Spacer(minLength: 300)
Spacer()
}
}
}

struct ProductDetailView_Previews: PreviewProvider {
static var previews: some View {
ProductDetailView(viewModel: ProductDetailViewModel(product: Product(id: "1", title: "Пицца", imageUrl: "_", price: 370, descript: "вкусная пицца")), size: .small)
ProductDetailView(viewModel: ProductDetailViewModel(product: Product(id: "1", title: "Пицца", imageUrl: "pizzaPlaceholder", price: 370, descript: "вкусная пицца")), size: .small)
}
}
15 changes: 15 additions & 0 deletions PizzaShop/ViewModel/CartViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,23 @@
import Foundation

class CartViewModel: ObservableObject {

static let shared = CartViewModel()

private init() {}

@Published var positions = [Position]()

var cost: Int {
var summ = 0

for pos in positions {
summ += pos.cost
}

return summ
}

func addPosition(_ position: Position) {
self.positions.append(position)
}
Expand Down

0 comments on commit 621812b

Please sign in to comment.