Skip to content

Commit

Permalink
Merge pull request #35 from Swift-Coding-Club/feat-33-coredata-stats
Browse files Browse the repository at this point in the history
통계뷰에 CoreData 붙이기(진행중)
  • Loading branch information
Jinsujin authored Dec 14, 2022
2 parents d1a4ebb + 4783dad commit a90f484
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 73 deletions.
4 changes: 2 additions & 2 deletions ThrowAway/ThrowAway.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<group>";
Expand All @@ -166,7 +167,6 @@
3A34128129164FE900BDFB34 /* CalendarView.swift */,
3A34128329164FE900BDFB34 /* DateScrollView.swift */,
3A34128229164FE900BDFB34 /* DayOfWeekView.swift */,
3A34128429164FE900BDFB34 /* StatisticsView.swift */,
);
path = View;
sourceTree = "<group>";
Expand Down
3 changes: 2 additions & 1 deletion ThrowAway/ThrowAway/Product+CoreDataProperties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ extension Product {
@nonobjc public class func fetchRequest() -> NSFetchRequest<Product> {
return NSFetchRequest<Product>(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 {
Expand Down
90 changes: 90 additions & 0 deletions ThrowAway/ThrowAway/SwiftUI/StaticsView/StatisticsView.swift
Original file line number Diff line number Diff line change
@@ -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<Product>

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)
}
}
66 changes: 0 additions & 66 deletions ThrowAway/ThrowAway/SwiftUI/StaticsView/View/StatisticsView.swift

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="20086" systemVersion="21C52" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier="">
<entity name="Product" representedClassName="Product" syncable="YES">
<attribute name="cleanedUpDay" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="cleaningDay" optional="YES" attributeType="Date" usesScalarValueType="NO"/>
<attribute name="isCleanedUp" optional="YES" attributeType="Boolean" defaultValueString="NO" usesScalarValueType="YES"/>
<attribute name="memo" optional="YES" attributeType="String"/>
<attribute name="photo" optional="YES" attributeType="Binary"/>
<attribute name="title" optional="YES" attributeType="String"/>
</entity>
<elements>
<element name="Product" positionX="-63" positionY="-18" width="128" height="104"/>
<element name="Product" positionX="-63" positionY="-18" width="128" height="119"/>
</elements>
</model>
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ class ProductListViewController: UIViewController {
}
let updatedItem = productList[index]
updatedItem.isCleanedUp = true
updatedItem.cleanedUpDay = Date()
completion()
do {
try viewContext.save()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())

Expand Down

0 comments on commit a90f484

Please sign in to comment.