Skip to content

Commit

Permalink
Home view cells
Browse files Browse the repository at this point in the history
  • Loading branch information
frankfka committed Jun 11, 2019
1 parent 06abb18 commit 62a363a
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 30 deletions.
2 changes: 2 additions & 0 deletions SimpleScanner/Constant/Color.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Color {
static let BodyBackground = UIColor(red: 0.93, green: 0.93, blue: 0.93, alpha: 1.0)
static let BodyBackgroundContrast = UIColor.white
static let Text = UIColor.black
static let LightText = UIColor.gray
static let ContrastText = UIColor.white
static let Border = UIColor.gray
static let Button = Primary
static let ButtonText = UIColor.white
Expand Down
8 changes: 8 additions & 0 deletions SimpleScanner/Constant/Text.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ class Text {
static let HomeViewTitle = "Documents"
static let NewScanButton = "New Scan"
static let PastScansHeader = "Past Scans"
static func NumPages(numPages: Int) -> String {
return "\(numPages) Pages"
}
static func DateString(for date: Date) -> String {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "MMM dd, YYYY"
return dateFormatter.string(from: date)
}

// New Scan Page
static let NewScanTitle = "New Scan"
Expand Down
14 changes: 9 additions & 5 deletions SimpleScanner/Constant/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class View {
static let ViewPadding: CGFloat = 16
static let SectionVerticalMargin: CGFloat = 24
static let SubSectionVerticalMargin: CGFloat = 8
static let TextLabelPadding: CGFloat = 8

// Divider
static let DividerHeight: CGFloat = 1
Expand All @@ -22,15 +23,19 @@ class View {
static let ButtonHeight: CGFloat = 50

// Fonts
static let SmallFont: UIFont = .systemFont(ofSize: 13)
static let NormalFont: UIFont = .systemFont(ofSize: 16)
static let ButtonFont: UIFont = .systemFont(ofSize: 20)
static let HeaderFont: UIFont = .systemFont(ofSize: 36)

// TODO: Combine fields
static let CollectionViewVerticalMargin: CGFloat = 48

static let CollectionViewVerticalMargin: CGFloat = 36
static let CollectionViewHorizontalMargin: CGFloat = 16
static let CollectionViewCellVerticalMargin: CGFloat = 36
static let CollectionViewSubviewPadding: CGFloat = 4
// Document Collection View
static let DocumentCollectionViewItemsPerRow: CGFloat = 2
static let DocumentCollectionViewSectionInsets = UIEdgeInsets(top: CollectionViewVerticalMargin, left: ViewPadding, bottom: CollectionViewVerticalMargin, right: ViewPadding)
static let DocumentCollectionViewSectionInsets = UIEdgeInsets(top: CollectionViewVerticalMargin, left: CollectionViewHorizontalMargin, bottom: CollectionViewVerticalMargin, right: CollectionViewHorizontalMargin)
static func DocumentCollectionViewSize(frameWidth: CGFloat) -> CGSize {
let widthPerItem = computeCollectionViewCellWidth(frameWidth: frameWidth, insets: DocumentCollectionViewSectionInsets, itemsPerRow: DocumentCollectionViewItemsPerRow)
return CGSize(width: widthPerItem, height: widthPerItem)
Expand All @@ -39,13 +44,12 @@ class View {

// Pages Collection View
static let PagesCollectionViewItemsPerRow: CGFloat = 2
static let PagesCollectionViewSectionInsets = UIEdgeInsets(top: CollectionViewVerticalMargin, left: ViewPadding, bottom: CollectionViewVerticalMargin, right: ViewPadding)
static let PagesCollectionViewSectionInsets = UIEdgeInsets(top: CollectionViewVerticalMargin, left: CollectionViewHorizontalMargin, bottom: CollectionViewVerticalMargin, right: CollectionViewHorizontalMargin)
static func PagesCollectionViewSize(frameWidth: CGFloat) -> CGSize {
let widthPerItem = computeCollectionViewCellWidth(frameWidth: frameWidth, insets: PagesCollectionViewSectionInsets, itemsPerRow: PagesCollectionViewItemsPerRow)
return CGSize(width: widthPerItem, height: widthPerItem)
}
static let PageCollectionCellReuseID: String = "PageCell"
static let AddPageCollectionCellReuseID: String = "AddPageCell" //TODO: Not being used

// Computes width of a collection view cell
private static func computeCollectionViewCellWidth(frameWidth: CGFloat, insets: UIEdgeInsets, itemsPerRow: CGFloat) -> CGFloat {
Expand Down
1 change: 1 addition & 0 deletions SimpleScanner/Screen/Home/HomeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ extension HomeView {
private func initDocumentCollectionView() {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.sectionInset = View.DocumentCollectionViewSectionInsets
flowLayout.minimumLineSpacing = View.CollectionViewCellVerticalMargin
documentCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
documentCollectionView.backgroundColor = Color.BodyBackground
documentCollectionView.delegate = self
Expand Down
3 changes: 2 additions & 1 deletion SimpleScanner/Screen/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class HomeViewModel {
let documents: Results<PDF>

init(state: HomeState, documents: Results<PDF>) {
self.documents = documents
// Reverse chronological order
self.documents = documents.sorted(byKeyPath: "dateCreated", ascending: false)
}

}
87 changes: 71 additions & 16 deletions SimpleScanner/Screen/Home/PDFCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import PDFKit

// Optional in case UIImage not loaded properly, in which case we display a blank
class PDFCollectionViewCellModel {
let thumbnail: UIImage?
let name: String
let thumbnail: UIImage? //TODO: Specific error icon, make non-optional
let fileName: String
let dateCreated: String

init(from pdf: PDF) {
Expand All @@ -21,28 +21,83 @@ class PDFCollectionViewCellModel {
print("PDF Document with filename \(pdf.fileName) not found.")
self.thumbnail = nil
}
name = pdf.fileName
dateCreated = "TODO"
fileName = pdf.fileName
dateCreated = Text.DateString(for: pdf.dateCreated)
}
}

class PDFCollectionViewCell: UICollectionViewCell {

private var imageView: UIImageView?
private var vm: PDFCollectionViewCellModel?
private var cellView: UIView?
private var thumbnail: UIImageView?
private var fileNameLabel: UILabel?
private var dateCreatedLabel: UILabel?

func loadCell(with model: PDFCollectionViewCellModel) {
if let imageView = imageView {
imageView.image = model.thumbnail // TODO: specific error placeholder
} else {
// Create new ImageView
imageView = UIImageView()
imageView!.image = model.thumbnail
imageView!.contentMode = .scaleAspectFit
contentView.addSubview(imageView!)
imageView!.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}
self.vm = model
if cellView == nil {
initSubviews()
}
loadSubviews()
}

private func loadSubviews() {
thumbnail!.image = vm!.thumbnail
fileNameLabel!.text = vm!.fileName
dateCreatedLabel!.text = vm!.dateCreated
}

private func initSubviews() {
let mainView = UIView()
let thumbnailView = UIImageView()
let nameLabel = UILabel()
let dateLabel = UILabel()

contentView.addSubview(mainView)
mainView.snp.makeConstraints { (make) in
make.edges.equalToSuperview()
}

thumbnailView.contentMode = .scaleAspectFit

nameLabel.font = View.NormalFont
nameLabel.textColor = Color.Text
nameLabel.lineBreakMode = .byTruncatingTail
nameLabel.numberOfLines = 1

dateLabel.font = View.SmallFont
dateLabel.textColor = Color.LightText
dateLabel.lineBreakMode = .byTruncatingTail
dateLabel.numberOfLines = 1

mainView.addSubview(thumbnailView)
mainView.addSubview(nameLabel)
mainView.addSubview(dateLabel)

thumbnailView.snp.makeConstraints { (make) in
make.top.equalToSuperview()
make.left.equalToSuperview()
make.right.equalToSuperview()
make.bottom.equalTo(nameLabel.snp.top).offset(-View.CollectionViewSubviewPadding)
}
nameLabel.snp.makeConstraints { (make) in
make.left.equalToSuperview()
make.right.equalToSuperview()
make.height.equalTo(nameLabel.font.pointSize + View.TextLabelPadding)
make.bottom.equalTo(dateLabel.snp.top)
}
dateLabel.snp.makeConstraints { (make) in
make.left.equalToSuperview()
make.right.equalToSuperview()
make.height.equalTo(dateLabel.font.pointSize + 8)
make.bottom.equalToSuperview()
}

self.cellView = mainView
self.thumbnail = thumbnailView
self.fileNameLabel = nameLabel
self.dateCreatedLabel = dateLabel
}

}
4 changes: 2 additions & 2 deletions SimpleScanner/Screen/NewScan/NewScanController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ extension NewScanController: ImageScannerControllerDelegate {

func imageScannerController(_ scanner: ImageScannerController, didFinishScanningWithResults results: ImageScannerResults) {
let newPage: UIImage
if let enhancedImage = results.enhancedImage {
if let enhancedImage = results.enhancedImage, results.doesUserPreferEnhancedImage {
newPage = enhancedImage
} else {
newPage = results.scannedImage
Expand Down Expand Up @@ -144,7 +144,7 @@ extension NewScanController: StoreSubscriber {
actionsController.addAction(UIAlertAction(title: Text.PageActionsView, style: .default) { _ in
self.store.dispatch(PresentPageAction(index: pageIndex))
})
actionsController.addAction(UIAlertAction(title: Text.PageActionsDelete, style: .destructive) { _ in
actionsController.addAction(UIAlertAction(title: Text.PageActionsDelete, style: .default) { _ in
self.store.dispatch(DeletePageAction(index: pageIndex))
})
actionsController.addAction(UIAlertAction(title: "Dismiss", style: .cancel))
Expand Down
1 change: 1 addition & 0 deletions SimpleScanner/Screen/NewScan/NewScanView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ extension NewScanView {

private func initPagesCollectionView() {
let flowLayout = UICollectionViewFlowLayout()
flowLayout.minimumLineSpacing = View.CollectionViewCellVerticalMargin
flowLayout.sectionInset = View.PagesCollectionViewSectionInsets
pagesCollectionView = UICollectionView(frame: .zero, collectionViewLayout: flowLayout)
pagesCollectionView.backgroundColor = Color.BodyBackground
Expand Down
10 changes: 4 additions & 6 deletions SimpleScanner/Screen/NewScan/PageCollectionViewCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,10 @@ import PDFKit
// Optional in case UIImage not loaded properly, in which case we display a blank
class PageCollectionViewCellModel {
let thumbnail: UIImage
let page: Int
let totalPages: Int
let pageLabel: String

init(page: PDFPage, pageNum: Int, totalPages: Int) {
self.page = pageNum
self.totalPages = totalPages
self.pageLabel = Text.PageCellNumberLabel(currentPage: pageNum, totalPages: totalPages)
self.thumbnail = ImageService.shared.getThumbnailForPage(page: page)
}
}
Expand Down Expand Up @@ -44,7 +42,7 @@ class PageCollectionViewCell: UICollectionViewCell {

private func loadSubviews() {
pageThumbnail!.image = vm!.thumbnail
pageLabel!.text = Text.PageCellNumberLabel(currentPage: vm!.page, totalPages: vm!.totalPages)
pageLabel!.text = vm!.pageLabel
}

private func initSubviews() {
Expand All @@ -67,7 +65,7 @@ class PageCollectionViewCell: UICollectionViewCell {
make.left.equalToSuperview()
make.right.equalToSuperview()
make.bottom.equalToSuperview()
make.height.equalTo(View.NormalFont.pointSize + 8)
make.height.equalTo(pageNumLabel.font.pointSize + View.TextLabelPadding)
}
thumbnailView.snp.makeConstraints { (make) in
make.left.equalToSuperview()
Expand Down

0 comments on commit 62a363a

Please sign in to comment.