Skip to content

Commit

Permalink
refine extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
Nick Lee authored and Nick Lee committed Aug 4, 2017
1 parent 312dc6c commit 379236a
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 135 deletions.
4 changes: 4 additions & 0 deletions Journal.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
B80B61E81F343507004A0E1F /* JournalModel.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = B80B61E61F343507004A0E1F /* JournalModel.xcdatamodeld */; };
B80B61EA1F3436EF004A0E1F /* AppDelegateExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B80B61E91F3436EF004A0E1F /* AppDelegateExtensions.swift */; };
B80B61EC1F344ACA004A0E1F /* TitleTableViewCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = B80B61EB1F344ACA004A0E1F /* TitleTableViewCell.swift */; };
B80B61F51F3471C6004A0E1F /* Extensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = B80B61F41F3471C6004A0E1F /* Extensions.swift */; };
B8D9ECEB1F34006000B9FEED /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8D9ECEA1F34006000B9FEED /* AppDelegate.swift */; };
B8D9ECED1F34006000B9FEED /* MainViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = B8D9ECEC1F34006000B9FEED /* MainViewController.swift */; };
B8D9ECF01F34006000B9FEED /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B8D9ECEE1F34006000B9FEED /* Main.storyboard */; };
Expand All @@ -29,6 +30,7 @@
B80B61E71F343507004A0E1F /* JournalModel.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = JournalModel.xcdatamodel; sourceTree = "<group>"; };
B80B61E91F3436EF004A0E1F /* AppDelegateExtensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AppDelegateExtensions.swift; sourceTree = "<group>"; };
B80B61EB1F344ACA004A0E1F /* TitleTableViewCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TitleTableViewCell.swift; sourceTree = "<group>"; };
B80B61F41F3471C6004A0E1F /* Extensions.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Extensions.swift; sourceTree = "<group>"; };
B8D9ECE71F34005F00B9FEED /* Journal.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = Journal.app; sourceTree = BUILT_PRODUCTS_DIR; };
B8D9ECEA1F34006000B9FEED /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
B8D9ECEC1F34006000B9FEED /* MainViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MainViewController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -96,6 +98,7 @@
B80B61E31F3420F4004A0E1F /* CoreDataProvider.swift */,
B80B61E61F343507004A0E1F /* JournalModel.xcdatamodeld */,
B80B61EB1F344ACA004A0E1F /* TitleTableViewCell.swift */,
B80B61F41F3471C6004A0E1F /* Extensions.swift */,
);
path = Journal;
sourceTree = "<group>";
Expand Down Expand Up @@ -253,6 +256,7 @@
B80B61EA1F3436EF004A0E1F /* AppDelegateExtensions.swift in Sources */,
B8D9ECED1F34006000B9FEED /* MainViewController.swift in Sources */,
B8D9ECFD1F3405ED00B9FEED /* JournalTableViewCell.swift in Sources */,
B80B61F51F3471C6004A0E1F /* Extensions.swift in Sources */,
B8D9ECEB1F34006000B9FEED /* AppDelegate.swift in Sources */,
B8D9ECFF1F3407D300B9FEED /* Journal.swift in Sources */,
B80B61EC1F344ACA004A0E1F /* TitleTableViewCell.swift in Sources */,
Expand Down
130 changes: 130 additions & 0 deletions Journal/Extensions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
//
// Extensions.swift
// Journal
//
// Created by Nick Lee on 2017/8/4.
// Copyright © 2017年 nicklee. All rights reserved.
//

import UIKit

extension JournalViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

uploadImageView.image = pickedImage

imagePlaceholder.isHidden = true

remindLabel.isHidden = true

isImageLoaded = true

}

dismiss(animated: true, completion: nil)

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

dismiss(animated: true, completion: nil)

}

}

extension JournalViewController: UITextViewDelegate {

func textViewDidBeginEditing(_ textView: UITextView) {

if inputContent.text == "Content" {

inputContent.text = ""

}

}

func textViewDidEndEditing(_ textView: UITextView) {

if inputContent.text.isEmpty {

inputContent.text = "Content"

}
}

}

extension JournalViewController {

func addTapForImagePicker() {

view.bringSubview(toFront: imageTapView)

view.bringSubview(toFront: outletBtnCancel)

view.bringSubview(toFront: imagePlaceholder)

imageTapView.backgroundColor = UIColor.clear

let tap = UITapGestureRecognizer(target: self, action: #selector(JournalViewController.pushImagePicker))

imageTapView.addGestureRecognizer(tap)

}

func pushImagePicker() {

imagePicker.sourceType = .photoLibrary

present(imagePicker, animated: true, completion: nil)

}

func addImageSpotlight() {

outletBtnCancel.layer.cornerRadius = 22

imagePlaceholder.layer.shadowColor = UIColor.white.cgColor

imagePlaceholder.layer.shadowOpacity = 1

imagePlaceholder.layer.shadowOffset = CGSize(width: 0, height: 0)

imagePlaceholder.layer.shadowRadius = 50.0

imagePlaceholder.layer.cornerRadius = 40

imagePlaceholder.layer.masksToBounds = false

}

func saveToCoreData() {

if isImageLoaded == true,
inputTitle.text != "",
inputContent.text != "" {

guard
let imageData = UIImagePNGRepresentation(uploadImageView.image!) as? NSData,
let title = inputTitle.text,
let content = inputContent.text else { return }

let timeToInteger = Int(Date().timeIntervalSince1970 * 1000.0)
let timestamp = String(timeToInteger)

let journal = Journal.init(imageData: imageData, title: title, content: content, timeStamp: timestamp)

CoreDataProvider().saveJournal(withJournal: journal)

self.navigationController?.popViewController(animated: true)

}

}

}
16 changes: 0 additions & 16 deletions Journal/Journal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ struct Journal {

extension Journal {

// typealias JournalObject = [String: Any]

enum FetchJournalError: Error {

case invalidJournalObject, missingImageData, missingTitle, missingContent, missingTimeStamp
Expand Down Expand Up @@ -84,18 +82,4 @@ extension Journal {

}

func toDictionary() -> [String: Any] {

let journalDictionary: [String: Any] = [Schema.imageData: self.imageData,

Schema.title: self.title,

Schema.content: self.content,

Schema.timeStamp: self.timeStamp]

return journalDictionary

}

}
123 changes: 4 additions & 119 deletions Journal/JournalViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class JournalViewController: UIViewController {

} else {

// MARK: Update journal.

CoreDataProvider().deleteJournal(withJournal: receivedJournal!)

saveToCoreData()
Expand All @@ -54,46 +56,20 @@ class JournalViewController: UIViewController {

}

func saveToCoreData() {

if isImageLoaded == true,
inputTitle.text != "",
inputContent.text != "" {

guard
let imageData = UIImagePNGRepresentation(uploadImageView.image!) as? NSData,
let title = inputTitle.text,
let content = inputContent.text else { return }

let timeToInteger = Int(Date().timeIntervalSince1970 * 1000.0)
let timestamp = String(timeToInteger)

let journal = Journal.init(imageData: imageData, title: title, content: content, timeStamp: timestamp)

CoreDataProvider().saveJournal(withJournal: journal)

self.navigationController?.popViewController(animated: true)

}

}

override func viewDidLoad() {
super.viewDidLoad()

inputContent.delegate = self

imagePlaceholder.isEnabled = false

addImageSpotlight()
imagePicker.delegate = self

UIApplication.shared.statusBarStyle = .lightContent

outletBtnCancel.layer.cornerRadius = 22

addTapForImagePicker()

imagePicker.delegate = self
addImageSpotlight()

if self.receivedJournal != nil {

Expand Down Expand Up @@ -124,95 +100,4 @@ class JournalViewController: UIViewController {

}

func addTapForImagePicker() {

view.bringSubview(toFront: imageTapView)

view.bringSubview(toFront: outletBtnCancel)

view.bringSubview(toFront: imagePlaceholder)

imageTapView.backgroundColor = UIColor.clear

let tap = UITapGestureRecognizer(target: self, action: #selector(JournalViewController.pushImagePicker))

imageTapView.addGestureRecognizer(tap)

}

func pushImagePicker() {

imagePicker.sourceType = .photoLibrary

present(imagePicker, animated: true, completion: nil)

}

func addImageSpotlight() {

imagePlaceholder.layer.shadowColor = UIColor.white.cgColor

imagePlaceholder.layer.shadowOpacity = 1

imagePlaceholder.layer.shadowOffset = CGSize(width: 0, height: 0)

imagePlaceholder.layer.shadowRadius = 50.0

imagePlaceholder.layer.cornerRadius = 40

imagePlaceholder.layer.masksToBounds = false

}

}

extension JournalViewController: UIImagePickerControllerDelegate, UINavigationControllerDelegate {

func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [String : Any]) {

if let pickedImage = info[UIImagePickerControllerOriginalImage] as? UIImage {

uploadImageView.image = pickedImage

imagePlaceholder.isHidden = true

remindLabel.isHidden = true

isImageLoaded = true

}

dismiss(animated: true, completion: nil)

}

func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {

dismiss(animated: true, completion: nil)

}

}

extension JournalViewController: UITextViewDelegate {

func textViewDidBeginEditing(_ textView: UITextView) {

if inputContent.text == "Content" {

inputContent.text = ""

}

}

func textViewDidEndEditing(_ textView: UITextView) {

if inputContent.text.isEmpty {

inputContent.text = "Content"

}
}

}

0 comments on commit 379236a

Please sign in to comment.