Skip to content

Commit

Permalink
Merge pull request #65 from boostcamp3-iOS/feature/scrapView_59
Browse files Browse the repository at this point in the history
Feature/scrap view 59
  • Loading branch information
kimhyeri authored Feb 9, 2019
2 parents bf29041 + 838a6ea commit 80560b4
Show file tree
Hide file tree
Showing 13 changed files with 498 additions and 50 deletions.
28 changes: 24 additions & 4 deletions tree/tree.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,12 @@
D9D145CB220B0B8400CC58DA /* ScrapManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9D145C6220B0B8400CC58DA /* ScrapManager.swift */; };
D9D145CC220B0B8400CC58DA /* ScrappedArticle+CoreDataClass.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9D145C8220B0B8400CC58DA /* ScrappedArticle+CoreDataClass.swift */; };
D9D145CD220B0B8400CC58DA /* ScrappedArticle+CoreDataProperties.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9D145C9220B0B8400CC58DA /* ScrappedArticle+CoreDataProperties.swift */; };
D9D145D0220B0B9300CC58DA /* Scrap.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D9D145CF220B0B9300CC58DA /* Scrap.storyboard */; };
D9DFFB41220EEA3E0047E326 /* ScrapFilterViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9DFFB40220EEA3E0047E326 /* ScrapFilterViewController.swift */; };
D9DFFB45220EEA7F0047E326 /* ScrapFilterTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9DFFB42220EEA7E0047E326 /* ScrapFilterTableViewCell.swift */; };
D9DFFB46220EEA7F0047E326 /* ScrapFilterTableViewCell.xib in Resources */ = {isa = PBXBuildFile; fileRef = D9DFFB43220EEA7E0047E326 /* ScrapFilterTableViewCell.xib */; };
D9DFFB47220EEA7F0047E326 /* ScrapFilter.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D9DFFB44220EEA7F0047E326 /* ScrapFilter.storyboard */; };
D9DFFB49220EEA990047E326 /* Scrap.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = D9DFFB48220EEA990047E326 /* Scrap.storyboard */; };
D9DFFB4B220EEADC0047E326 /* ScrapFilterDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = D9DFFB4A220EEADC0047E326 /* ScrapFilterDelegate.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -157,7 +162,12 @@
D9D145C6220B0B8400CC58DA /* ScrapManager.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScrapManager.swift; sourceTree = "<group>"; };
D9D145C8220B0B8400CC58DA /* ScrappedArticle+CoreDataClass.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ScrappedArticle+CoreDataClass.swift"; sourceTree = "<group>"; };
D9D145C9220B0B8400CC58DA /* ScrappedArticle+CoreDataProperties.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "ScrappedArticle+CoreDataProperties.swift"; sourceTree = "<group>"; };
D9D145CF220B0B9300CC58DA /* Scrap.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Scrap.storyboard; sourceTree = "<group>"; };
D9DFFB40220EEA3E0047E326 /* ScrapFilterViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScrapFilterViewController.swift; sourceTree = "<group>"; };
D9DFFB42220EEA7E0047E326 /* ScrapFilterTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ScrapFilterTableViewCell.swift; sourceTree = "<group>"; };
D9DFFB43220EEA7E0047E326 /* ScrapFilterTableViewCell.xib */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.xib; path = ScrapFilterTableViewCell.xib; sourceTree = "<group>"; };
D9DFFB44220EEA7F0047E326 /* ScrapFilter.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = ScrapFilter.storyboard; sourceTree = "<group>"; };
D9DFFB48220EEA990047E326 /* Scrap.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = Scrap.storyboard; sourceTree = "<group>"; };
D9DFFB4A220EEADC0047E326 /* ScrapFilterDelegate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = ScrapFilterDelegate.swift; path = tree/Helper/ScrapFilterDelegate.swift; sourceTree = SOURCE_ROOT; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -445,6 +455,7 @@
D9C6AB7E21FED67A001C0AB8 /* PresentationManager */ = {
isa = PBXGroup;
children = (
D9DFFB4A220EEADC0047E326 /* ScrapFilterDelegate.swift */,
D9C6AB7F21FED9F7001C0AB8 /* PresentationController.swift */,
D9C6AB8121FEDB08001C0AB8 /* PresentationManager.swift */,
);
Expand All @@ -454,6 +465,7 @@
D9D145BC220B0B6800CC58DA /* Scrap */ = {
isa = PBXGroup;
children = (
D9DFFB40220EEA3E0047E326 /* ScrapFilterViewController.swift */,
D9D145BD220B0B6800CC58DA /* ScrapViewController.swift */,
D9D145BE220B0B6800CC58DA /* ScrapTabBarController.swift */,
);
Expand Down Expand Up @@ -481,7 +493,10 @@
D9D145CE220B0B9300CC58DA /* Scrap */ = {
isa = PBXGroup;
children = (
D9D145CF220B0B9300CC58DA /* Scrap.storyboard */,
D9DFFB44220EEA7F0047E326 /* ScrapFilter.storyboard */,
D9DFFB42220EEA7E0047E326 /* ScrapFilterTableViewCell.swift */,
D9DFFB43220EEA7E0047E326 /* ScrapFilterTableViewCell.xib */,
D9DFFB48220EEA990047E326 /* Scrap.storyboard */,
);
path = Scrap;
sourceTree = "<group>";
Expand Down Expand Up @@ -546,8 +561,8 @@
isa = PBXResourcesBuildPhase;
buildActionMask = 2147483647;
files = (
D9DFFB47220EEA7F0047E326 /* ScrapFilter.storyboard in Resources */,
BC5D4E42220B1D17000465B0 /* KeywordDetailGraphCell.xib in Resources */,
D9D145D0220B0B9300CC58DA /* Scrap.storyboard in Resources */,
35DC0DFF21F6FB3200F30416 /* .swiftlint.yml in Resources */,
6A65DA432203D22B005EB2BC /* LoadingView.xib in Resources */,
BCDB9E282205A1040095E4E4 /* TrendHeaderCell.xib in Resources */,
Expand All @@ -558,9 +573,11 @@
6A198CCA220CAA9A002421AC /* DefaultLabelView.xib in Resources */,
BC7969C72200AD190003F520 /* TrendPageView.xib in Resources */,
35DC0DD621F6F3CB00F30416 /* Assets.xcassets in Resources */,
D9DFFB46220EEA7F0047E326 /* ScrapFilterTableViewCell.xib in Resources */,
35DC0DD421F6F3C700F30416 /* Main.storyboard in Resources */,
BC7969BC22002EB20003F520 /* Live.storyboard in Resources */,
D9C6AB4421FA9CF8001C0AB8 /* Search.storyboard in Resources */,
D9DFFB49220EEA990047E326 /* Scrap.storyboard in Resources */,
BC7969D4220174B40003F520 /* TrendListCell.xib in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down Expand Up @@ -642,10 +659,13 @@
BC2656BD21FD94EE00003413 /* EventRegistryAPI.swift in Sources */,
BC7969C52200AD0E0003F520 /* TrendPageView.swift in Sources */,
D9C6AB8221FEDB08001C0AB8 /* PresentationManager.swift in Sources */,
D9DFFB4B220EEADC0047E326 /* ScrapFilterDelegate.swift in Sources */,
BC2656C321FD94EE00003413 /* NetworkResult.swift in Sources */,
BC2656C021FD94EE00003413 /* APICenter.swift in Sources */,
BC63852E2207442B00074CF3 /* Extension+Notification.swift in Sources */,
D9DFFB45220EEA7F0047E326 /* ScrapFilterTableViewCell.swift in Sources */,
BC2656C221FD94EE00003413 /* ParametersEncoder.swift in Sources */,
D9DFFB41220EEA3E0047E326 /* ScrapFilterViewController.swift in Sources */,
BC2656B021FD94E000003413 /* Extension+URL.swift in Sources */,
BC2656C121FD94EE00003413 /* NetworkResponse.swift in Sources */,
BC5D4E4E220B730F000465B0 /* GraphView.swift in Sources */,
Expand Down
64 changes: 64 additions & 0 deletions tree/tree/Controller/Scrap/ScrapFilterViewController.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// ScrapFilterViewController.swift
// tree
//
// Created by Hyeontae on 05/02/2019.
// Copyright © 2019 gardener. All rights reserved.
//

import UIKit

class ScrapFilterViewController: UIViewController {

@IBOutlet weak var headerView: UIView!
@IBOutlet weak var headerTitleLabel: UILabel!
@IBOutlet weak var tableView: UITableView!

private let cellIdentifier = "ScrapFilterTableViewCell"
private lazy var categories: [ArticleCategory] = {
ArticleCategory.allCases.filter({
if $0 == .all { return true }
return ScrapManager.countArticle(category: $0, nil) != 0
})
}()
weak var filterDelegate: ScrapFilterDelegate?

override func viewDidLoad() {
super.viewDidLoad()
tableViewSetup()
}

private func tableViewSetup() {
tableView.delegate = self
tableView.dataSource = self
tableView.register(
UINib(nibName: cellIdentifier, bundle: nil),
forCellReuseIdentifier: cellIdentifier)
tableView.separatorStyle = .none
}
}

extension ScrapFilterViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return categories.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell =
tableView.dequeueReusableCell(withIdentifier: cellIdentifier, for: indexPath)
as? ScrapFilterTableViewCell else {
return UITableViewCell()
}
if indexPath.row == 0 {
cell.setAllCategory(width: view.bounds.width - 32)
} else {
cell.configure(categories[indexPath.row], width: view.bounds.width - 32)
}
return cell
}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
filterDelegate?.filterArticles(categories[indexPath.row])
dismiss(animated: true)
}
}
61 changes: 59 additions & 2 deletions tree/tree/Controller/Scrap/ScrapViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,66 @@ import CoreData

class ScrapViewController: UIViewController {

@IBOutlet weak var tableView: UITableView!
@IBOutlet weak var filterButton: UIButton!

private lazy var scrappedArticles: [ScrappedArticle] = {
ScrapManager.fetchArticles()
}()

override func viewDidLoad() {
super.viewDidLoad()
// ScrapManager.fetchArticles()

tableViewSetup()
filterButtonSetup()
}

private func tableViewSetup() {
tableView.delegate = self
tableView.dataSource = self
}

private func filterButtonSetup() {
filterButton.addTarget(
self,
action: #selector(filterButtonDidTap),
for: .touchUpInside)
}

@objc func filterButtonDidTap(_ sender: UIButton) {
guard let tempUIViewController =
UIStoryboard.init(name: "ScrapFilter", bundle: nil)
.instantiateViewController(withIdentifier: "ScrapFilterViewController")
as? ScrapFilterViewController else {
return
}
tempUIViewController.filterDelegate = self
present(tempUIViewController, animated: true)
}

private func fetchAndReload(selectedCategory category: ArticleCategory) {
if category == .all {
scrappedArticles = ScrapManager.fetchArticles()
} else {
scrappedArticles = ScrapManager.fetchArticles(category)
}
tableView.reloadData()
}
}

extension ScrapViewController: UITableViewDataSource, UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return scrappedArticles.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath)
cell.textLabel?.text = scrappedArticles[indexPath.row].articleTitle
return cell
}
}

extension ScrapViewController: ScrapFilterDelegate {
func filterArticles(_ article: ArticleCategory) {
fetchAndReload(selectedCategory: article)
}
}
20 changes: 20 additions & 0 deletions tree/tree/Extension/Extension+UIColor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,26 @@
import UIKit

extension UIColor {
public convenience init(hexString: String) {
let red, green, blue: CGFloat
if hexString.hasPrefix("#") {
let start = hexString.index(hexString.startIndex, offsetBy: 1)
let hexColor = String(hexString[start...])
if hexColor.count == 6 {
let scanner = Scanner(string: hexColor)
var hexNumber: UInt64 = 0
if scanner.scanHexInt64(&hexNumber) {
red = CGFloat((hexNumber & 0xff0000) >> 16) / 255
green = CGFloat((hexNumber & 0x00ff00) >> 8) / 255
blue = CGFloat((hexNumber & 0x0000ff) >> 0) / 255
self.init(red: red, green: green, blue: blue, alpha: 1.0)
return
}
}
}
self.init(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
}

class var lightGray: UIColor {
return UIColor(red: 142/255.0, green: 142/255.0, blue: 147/255.0, alpha: 0.12)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ class PresentationController: UIPresentationController {
}

override func size(forChildContentContainer container: UIContentContainer,
withParentContainerSize parentSize: CGSize) -> CGSize {
withParentContainerSize parentSize: CGSize
) -> CGSize {
return CGSize(width: parentSize.width, height: 375)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class PresentationManager: NSObject {
}

extension PresentationManager: UIViewControllerTransitioningDelegate {
func presentationController(forPresented presented: UIViewController,
presenting: UIViewController?,
source: UIViewController) -> UIPresentationController? {
func presentationController(
forPresented presented: UIViewController,
presenting: UIViewController?,
source: UIViewController
) -> UIPresentationController? {
let presentationController = PresentationController(presentedViewController: presented,
presenting: presenting)
return presentationController
Expand Down
11 changes: 11 additions & 0 deletions tree/tree/Helper/ScrapFilterDelegate.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//
// ScrapFilterDelegate.swift
// tree
//
// Created by Hyeontae on 09/02/2019.
// Copyright © 2019 gardener. All rights reserved.
//

protocol ScrapFilterDelegate: class {
func filterArticles(_ article: ArticleCategory)
}
59 changes: 31 additions & 28 deletions tree/tree/Model/DataManager/ScrapManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -71,45 +71,48 @@ final class ScrapManager {
return result
}

static func unreadArticlesCount() -> Int {
var result = 0
let request: NSFetchRequest = NSFetchRequest<NSNumber>(entityName: "ScrappedArticle")
let predicate = NSPredicate(format: "isRead == %@", NSNumber(value: false))
request.predicate = predicate
request.resultType = .countResultType
do {
let countResult = try managedContext.fetch(request)
guard let resultCount = countResult.first?.intValue else {
return 0
}
result = resultCount
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
static func countArticle(_ isRead: Bool?) -> Int {
if let isRead = isRead {
return countArticleFetch(NSPredicate(format: "isRead == %@", NSNumber(value: isRead)))
}
return result
return countArticleFetch(nil)
}

static func unreadArticlesCount(category: ArticleCategory) -> Int {
var result = 0
static func countArticle(category: ArticleCategory, _ isRead: Bool?) -> Int {
var predicate: NSPredicate
if let isRead = isRead {
predicate = NSPredicate(
format: "isRead == %@ AND %K == %@",
NSNumber(value: isRead),
#keyPath(ScrappedArticle.category),
category.toString()
)
} else {
predicate = NSPredicate(
format: "%K == %@",
#keyPath(ScrappedArticle.category),
category.toString()
)
}
return countArticleFetch(predicate)
}

static func countArticleFetch(_ predicate: NSPredicate?) -> Int{
let request: NSFetchRequest = NSFetchRequest<NSNumber>(entityName: "ScrappedArticle")
let predicate = NSPredicate(
format: "isRead == %@ AND %K == %@",
NSNumber(value: false),
#keyPath(ScrappedArticle.category),
category.toString()
)
request.predicate = predicate
request.resultType = .countResultType
if let predicate = predicate {
request.predicate = predicate
}
do {
let countResult = try managedContext.fetch(request)
guard let resultCount = countResult.first?.intValue else {
let result = try managedContext.fetch(request)
guard let resultCount = result.first?.intValue else {
return 0
}
result = resultCount
return resultCount
} catch let error as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
return result
return 0
}

static func removeAllScrappedArticle() {
Expand Down
Loading

0 comments on commit 80560b4

Please sign in to comment.