From 53611518c429ada860395a8c269c435850badee9 Mon Sep 17 00:00:00 2001 From: Jinsujin Date: Sat, 10 Dec 2022 23:13:19 +0900 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20#33=20Add=20cleanedUpDay=20Attr?= =?UTF-8?q?ibute=20in=20CoreData?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 통계에서 필요한 날짜인 실제로 청소가 된 날짜 추가 - 리스트 화면에서 물건을 비웠을때 오늘 날짜로 업데이트 --- ThrowAway/ThrowAway/Product+CoreDataProperties.swift | 3 ++- .../ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift | 1 - .../ThrowAway.xcdatamodeld/ThrowAway.xcdatamodel/contents | 3 ++- .../ThrowAway/View Controllers/ProductListViewController.swift | 1 + 4 files changed, 5 insertions(+), 3 deletions(-) diff --git a/ThrowAway/ThrowAway/Product+CoreDataProperties.swift b/ThrowAway/ThrowAway/Product+CoreDataProperties.swift index 68fa23a..7c933ec 100644 --- a/ThrowAway/ThrowAway/Product+CoreDataProperties.swift +++ b/ThrowAway/ThrowAway/Product+CoreDataProperties.swift @@ -14,12 +14,13 @@ extension Product { @nonobjc public class func fetchRequest() -> NSFetchRequest { return NSFetchRequest(entityName: "Product") } - + @NSManaged public var cleaningDay: Date? @NSManaged public var title: String? @NSManaged public var photo: Data? @NSManaged public var memo: String? @NSManaged public var isCleanedUp: Bool + @NSManaged public var cleanedUpDay: Date? } extension Product: Identifiable { diff --git a/ThrowAway/ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift b/ThrowAway/ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift index ed39244..03b0546 100644 --- a/ThrowAway/ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift +++ b/ThrowAway/ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift @@ -16,7 +16,6 @@ struct StatisticsView: View { @EnvironmentObject var dateHolder: DateHolder var body: some View { - VStack { VStack { DateScrollView() diff --git a/ThrowAway/ThrowAway/ThrowAway.xcdatamodeld/ThrowAway.xcdatamodel/contents b/ThrowAway/ThrowAway/ThrowAway.xcdatamodeld/ThrowAway.xcdatamodel/contents index 8539352..45969fc 100644 --- a/ThrowAway/ThrowAway/ThrowAway.xcdatamodeld/ThrowAway.xcdatamodel/contents +++ b/ThrowAway/ThrowAway/ThrowAway.xcdatamodeld/ThrowAway.xcdatamodel/contents @@ -1,6 +1,7 @@ + @@ -8,6 +9,6 @@ - + \ No newline at end of file diff --git a/ThrowAway/ThrowAway/View Controllers/ProductListViewController.swift b/ThrowAway/ThrowAway/View Controllers/ProductListViewController.swift index 8253fac..9a8cf9b 100644 --- a/ThrowAway/ThrowAway/View Controllers/ProductListViewController.swift +++ b/ThrowAway/ThrowAway/View Controllers/ProductListViewController.swift @@ -97,6 +97,7 @@ class ProductListViewController: UIViewController { } let updatedItem = productList[index] updatedItem.isCleanedUp = true + updatedItem.cleanedUpDay = Date() completion() do { try viewContext.save() From 4783dad587fd707121c32ad31f5e30acbf90e1c8 Mon Sep 17 00:00:00 2001 From: Jinsujin Date: Sun, 11 Dec 2022 01:37:05 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8=20#33=20Fetch=20Products=20in=20S?= =?UTF-8?q?tatsView?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 함수명을 소문자로 변경 - 이미지이름으로 사용하고 있던 데이터 타입을 [String] 을 [Product] 로 변경 --- ThrowAway/ThrowAway.xcodeproj/project.pbxproj | 4 +- .../SwiftUI/StaticsView/StatisticsView.swift | 90 +++++++++++++++++++ .../StaticsView/View/StatisticsView.swift | 65 -------------- .../TabBarViewController.swift | 8 +- 4 files changed, 97 insertions(+), 70 deletions(-) create mode 100644 ThrowAway/ThrowAway/SwiftUI/StaticsView/StatisticsView.swift delete mode 100644 ThrowAway/ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift diff --git a/ThrowAway/ThrowAway.xcodeproj/project.pbxproj b/ThrowAway/ThrowAway.xcodeproj/project.pbxproj index 90dbe47..47dbb81 100644 --- a/ThrowAway/ThrowAway.xcodeproj/project.pbxproj +++ b/ThrowAway/ThrowAway.xcodeproj/project.pbxproj @@ -144,9 +144,10 @@ 3A34127829164FB000BDFB34 /* StaticsView */ = { isa = PBXGroup; children = ( + 3A34128429164FE900BDFB34 /* StatisticsView.swift */, + 3A34127929164FC100BDFB34 /* CalendarManager.swift */, 3A34127B29164FC600BDFB34 /* Model */, 3A34127C29164FCB00BDFB34 /* View */, - 3A34127929164FC100BDFB34 /* CalendarManager.swift */, ); path = StaticsView; sourceTree = ""; @@ -166,7 +167,6 @@ 3A34128129164FE900BDFB34 /* CalendarView.swift */, 3A34128329164FE900BDFB34 /* DateScrollView.swift */, 3A34128229164FE900BDFB34 /* DayOfWeekView.swift */, - 3A34128429164FE900BDFB34 /* StatisticsView.swift */, ); path = View; sourceTree = ""; diff --git a/ThrowAway/ThrowAway/SwiftUI/StaticsView/StatisticsView.swift b/ThrowAway/ThrowAway/SwiftUI/StaticsView/StatisticsView.swift new file mode 100644 index 0000000..3cab1a0 --- /dev/null +++ b/ThrowAway/ThrowAway/SwiftUI/StaticsView/StatisticsView.swift @@ -0,0 +1,90 @@ +// +// StatisticsView.swift +// ThrowAway +// +// Created by 지준용 on 2022/10/30. +// + +import SwiftUI + +struct StatisticsView: View { + + @EnvironmentObject var dateHolder: DateHolder + @Environment(\.managedObjectContext) private var viewContext + + @FetchRequest( + sortDescriptors: [NSSortDescriptor(keyPath: \Product.cleaningDay, ascending: true)], + animation: .default + ) + private var products: FetchedResults + + private let setWidth = UIScreen.main.bounds.width + private let screenWidth = UIScreen.main.bounds.width + + var body: some View { + VStack { + VStack { + DateScrollView() + .environmentObject(dateHolder) + + Text("이번 달의 달성도는 81%입니다.") + .padding(.vertical, 20) + .font(.system(size: 20)) + + DayOfWeekView() + CalendarView() + } + .padding(.horizontal, 16) + + // TODO: 조건에 맞는 물건을 coredata에서 불러오기 + let notYetCleaningProducts = products.filter{ $0.isCleanedUp == false } + let lateCleanedUpProducts = products.filter{ $0.isCleanedUp == false } + + makeSectionView(label: "버리지 못한 물건", products: notYetCleaningProducts) + makeSectionView(label: "늦게 버린 물건", products: lateCleanedUpProducts) + } + } + + private func makeSectionView(label: String, products: [Product]) -> some View { + VStack(alignment: .leading) { + HStack { + Text("\(label)(\(products.count))") + .font(.system(size: 18)) + .fontWeight(.semibold) + .padding(.top, 10) + Spacer() + } + makeHorizontalScrollView(from: products) + } + .padding(.leading, 16) + } + + private func makeHorizontalScrollView(from products: [Product]) -> some View { + let items: [(id: ObjectIdentifier, image: Image)] = products.map { product in + var image = Image(systemName: "photo") + if (product.photo != nil), let photo = Image(data: product.photo!) { + image = photo + } + return (product.id, image: image) + } + + return ScrollView(.horizontal) { + HStack { + ForEach(items, id: \.self.id) { item in + item.image + .resizable() + .frame(width: screenWidth / 6, height: screenWidth / 6, alignment: .center) + .aspectRatio(contentMode: .fit) + .clipShape(Circle()) + } + } + } + } +} + +private extension Image { + init?(data: Data) { + guard let image = UIImage(data: data) else { return nil } + self = .init(uiImage: image) + } +} diff --git a/ThrowAway/ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift b/ThrowAway/ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift deleted file mode 100644 index 03b0546..0000000 --- a/ThrowAway/ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift +++ /dev/null @@ -1,65 +0,0 @@ -// -// StatisticsView.swift -// ThrowAway -// -// Created by 지준용 on 2022/10/30. -// - -import SwiftUI - -struct StatisticsView: View { - var notThrowArray = ["Test01", "Test02", "Test03", "Test04"] - var lateThrowArray = ["Test01", "Test02", "Test03"] - let setWidth = UIScreen.main.bounds.width - let screenWidth = UIScreen.main.bounds.width - - @EnvironmentObject var dateHolder: DateHolder - - var body: some View { - VStack { - VStack { - DateScrollView() - .environmentObject(dateHolder) - - Text("이번 달의 달성도는 81%입니다.") - .padding(.vertical, 20) - .font(.system(size: 20)) - - DayOfWeekView() - CalendarView() - } - .padding(.horizontal, 16) - - ProductArrayView(text: "버리지 못한 물건", productArray: notThrowArray) - ProductArrayView(text: "늦게 버린 물건", productArray: lateThrowArray) - } - } - - func ProductArrayView(text: String, productArray: [String]) -> some View { - VStack(alignment: .leading) { - HStack { - Text("\(text)(\(productArray.count))") - .font(.system(size: 18)) - .fontWeight(.semibold) - .padding(.top, 10) - Spacer() - } - productScroll(productArray: productArray) - } - .padding(.leading, 16) - } - - func productScroll(productArray: [String]) -> some View { - ScrollView(.horizontal) { - HStack { - ForEach(productArray, id: \.self) {product in - Image(product) - .resizable() - .frame(width: screenWidth / 6, height: screenWidth / 6, alignment: .center) - .aspectRatio(contentMode: .fit) - .clipShape(Circle()) - } - } - } - } -} diff --git a/ThrowAway/ThrowAway/View Controllers/TabBarViewController.swift b/ThrowAway/ThrowAway/View Controllers/TabBarViewController.swift index 3713955..6c5f94d 100644 --- a/ThrowAway/ThrowAway/View Controllers/TabBarViewController.swift +++ b/ThrowAway/ThrowAway/View Controllers/TabBarViewController.swift @@ -21,13 +21,15 @@ class TabBarViewController: UITabBarController { } func setupVCs() { + let viewContext = persistence.container.viewContext let storyboard = UIStoryboard(name: "ProductList", bundle: nil) let productListVC = storyboard.instantiateViewController(withIdentifier: "ProductListViewController") as! ProductListViewController - productListVC.viewContext = persistence.container.viewContext + productListVC.viewContext = viewContext - // date holder - let staticsView = StatisticsView().environmentObject(DateHolder()) + let staticsView = StatisticsView() + .environment(\.managedObjectContext, viewContext) + .environmentObject(DateHolder()) let staticsVC = UIHostingController(rootView: staticsView) let settingVC = UIHostingController(rootView: SettingView())