Skip to content

Commit

Permalink
Merge pull request #2 from marty-suzuki/refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
marty-suzuki authored Dec 10, 2018
2 parents dcf1b07 + 9496d93 commit a8a7c36
Show file tree
Hide file tree
Showing 76 changed files with 2,083 additions and 1,324 deletions.
130 changes: 101 additions & 29 deletions ArtShredder.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions ArtShredder/AR/ARViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ final class ARViewController: UIViewController {
}
@IBOutlet private(set) weak var selectImageButton: UIButton! {
didSet {
let title = NSLocalizedString("select_to_add_art_title", comment: "")
let title = LocalizedString.selectToAddArtTitle
selectImageButton.setTitle(title, for: .normal)
selectImageButton.layer.borderWidth = 2
selectImageButton.layer.borderColor = UIColor.white.cgColor
Expand All @@ -55,8 +55,7 @@ final class ARViewController: UIViewController {
}
@IBOutlet private(set) weak var noRecognitionLabel: UILabel! {
didSet {
let title = NSLocalizedString("ar_no_recognition_text", comment: "")
noRecognitionLabel.text = title
noRecognitionLabel.text = LocalizedString.arNoRecognitionText
}
}

Expand All @@ -70,8 +69,8 @@ final class ARViewController: UIViewController {
}()

private lazy var presenter = ARPresenter(view: self)
private lazy var delegateProxy = ARViewDelegateProxy(presenter: presenter,
pointOfView: { [weak self] in self?.sceneView.pointOfView })
private lazy var delegateHandler = ARViewDelegateHandler(presenter: presenter,
pointOfView: { [weak self] in self?.sceneView.pointOfView })

override var prefersStatusBarHidden: Bool {
return true
Expand All @@ -90,8 +89,8 @@ final class ARViewController: UIViewController {

presenter.reflect()

bannerView.delegate = delegateProxy
sceneView.delegate = delegateProxy
bannerView.delegate = delegateHandler
sceneView.delegate = delegateHandler
sceneView.scene = SCNScene()
sceneView.debugOptions = .showFeaturePoints

Expand Down Expand Up @@ -120,7 +119,7 @@ final class ARViewController: UIViewController {
if UIImagePickerController.isSourceTypeAvailable(.photoLibrary) {
let picker = UIImagePickerController()
picker.sourceType = .photoLibrary
picker.delegate = delegateProxy
picker.delegate = delegateHandler
picker.modalPresentationStyle = .overFullScreen
present(picker, animated: true, completion: nil)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// ARViewDelegateProxy.swift
// ARViewDelegateHandler.swift
// ArtShredder
//
// Created by marty-suzuki on 2018/10/19.
Expand All @@ -11,7 +11,7 @@ import GoogleMobileAds
import SceneKit
import UIKit

final class ARViewDelegateProxy: NSObject {
final class ARViewDelegateHandler: NSObject {
private let presenter: ARPresenter
private let pointOfView: () -> SCNNode?

Expand All @@ -21,7 +21,7 @@ final class ARViewDelegateProxy: NSObject {
}
}

extension ARViewDelegateProxy: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
extension ARViewDelegateHandler: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {
presenter.setImage(info[.originalImage] as? UIImage)

Expand All @@ -31,7 +31,7 @@ extension ARViewDelegateProxy: UIImagePickerControllerDelegate, UINavigationCont
}
}

extension ARViewDelegateProxy: ARSCNViewDelegate {
extension ARViewDelegateHandler: ARSCNViewDelegate {
func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
let semaphore = DispatchSemaphore(value: 1)
DispatchQueue.main.async {
Expand All @@ -49,7 +49,7 @@ extension ARViewDelegateProxy: ARSCNViewDelegate {
}
}

extension ARViewDelegateProxy: GADBannerViewDelegate {
extension ARViewDelegateHandler: GADBannerViewDelegate {
/// Tells the delegate an ad request loaded an ad.
func adViewDidReceiveAd(_ bannerView: GADBannerView) {
print("adViewDidReceiveAd")
Expand Down
7 changes: 7 additions & 0 deletions ArtShredder/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,13 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
// Override point for customization after application launch.
GADMobileAds.configure(withApplicationID: AdMobConfig.make().appID)

self.window = {
let window = UIWindow(frame: UIScreen.main.bounds)
window.rootViewController = ShredderViewController()
window.makeKeyAndVisible()
return window
}()
return true
}

Expand Down
748 changes: 0 additions & 748 deletions ArtShredder/Base.lproj/Main.storyboard

This file was deleted.

File renamed without changes.
File renamed without changes.
28 changes: 28 additions & 0 deletions ArtShredder/Common/Extension/DispatchQueue.extension.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//
// DispatchQueue.extension.swift
// ArtShredder
//
// Created by marty-suzuki on 2018/12/03.
// Copyright © 2018年 marty-suzuki. All rights reserved.
//

import Foundation

extension DispatchQueue: AdditionalCompatible {}

extension Additional where Base: DispatchQueue {
func throttle(delay: DispatchTimeInterval) -> (_ action: @escaping () -> ()) -> () {
var lastFireTime: DispatchTime = .now()

return { [weak base, delay] action in
let deadline: DispatchTime = .now() + delay
base?.asyncAfter(deadline: deadline) { [delay] in
let now: DispatchTime = .now()
let when: DispatchTime = lastFireTime + delay
if now < when { return }
lastFireTime = .now()
action()
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@

import UIKit



extension UIImage: AdditionalCompatible {}


extension Additional where Base: UIImage {
func fixedOrientation() -> UIImage {
let imageOrientation = base.imageOrientation
Expand Down
68 changes: 68 additions & 0 deletions ArtShredder/Common/GIFMaker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
//
// GIFMaker.swift
// ArtShredder
//
// Created by marty-suzuki on 2018/12/04.
// Copyright © 2018年 marty-suzuki. All rights reserved.
//

import CoreImage
import Foundation
import MobileCoreServices
import UIKit

protocol GIFMakerType: AnyObject {
var didCreateGIFWithURL: ((URL?) -> ())? { get set }
func create(with imageHolder: ImageHolder)
}

final class GIFMaker: GIFMakerType {

var didCreateGIFWithURL: ((URL?) -> ())?

private let queue: DispatchQueue

init(queue: DispatchQueue = .global()) {
self.queue = queue
}

func create(with imageHolder: ImageHolder) {
queue.async { [weak self] in
let images = imageHolder.images
let filename = imageHolder.filename

guard
let url = NSURL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(filename),
let destination = CGImageDestinationCreateWithURL(url as CFURL, kUTTypeGIF, images.count, nil)
else {
self?.didCreateGIFWithURL?(nil)
return
}

let delay = imageHolder.delay

let properties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFLoopCount as String: 0]]
CGImageDestinationSetProperties(destination, properties as CFDictionary)

let frameProperties = [kCGImagePropertyGIFDictionary as String: [kCGImagePropertyGIFDelayTime as String: delay]]
for image in images {
if let cgImage = image.cgImage {
CGImageDestinationAddImage(destination, cgImage, frameProperties as CFDictionary)
}
}

guard CGImageDestinationFinalize(destination) else {
self?.didCreateGIFWithURL?(nil)
return
}

self?.didCreateGIFWithURL?(url)
}
}
}

struct ImageHolder {
let images: [UIImage]
let delay: TimeInterval
let filename: String
}
29 changes: 29 additions & 0 deletions ArtShredder/Common/LocalizedString.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// LocalizedString.swift
// ArtShredder
//
// Created by marty-suzuki on 2018/10/27.
// Copyright © 2018年 marty-suzuki. All rights reserved.
//

import Foundation

enum LocalizedString {
static let frameDescription = NSLocalizedString("frame_description", comment: "")
static let selectImageName = NSLocalizedString("select_image_name", comment: "")
static let saveGIFButton = NSLocalizedString("save_gif_button", comment: "")
static let saveImageButton = NSLocalizedString("save_image_button", comment: "")
static let didSaveToCameraRollTitle = NSLocalizedString("did_save_to_camera_roll_title", comment: "")
static let didSaveToCameraRollMessage = NSLocalizedString("did_save_to_camera_roll_message", comment: "")
static let closeAction = NSLocalizedString("close_action", comment: "")
static let imageSourceSelectTitle = NSLocalizedString("image_source_select_title", comment: "")
static let imageSourceSelectMessage = NSLocalizedString("image_source_select_message", comment: "")
static let imageSourceSelectCamera = NSLocalizedString("image_source_select_camera", comment: "")
static let imageSourceSelectCameraRoll = NSLocalizedString("image_source_select_camera_roll", comment: "")
static let cancelAction = NSLocalizedString("cancel_action", comment: "")
static let supportURL = NSLocalizedString("support_url", comment: "")
static let supprotTitle = NSLocalizedString("supprot_title", comment: "")
static let selectToAddArtTitle = NSLocalizedString("select_to_add_art_title", comment: "")
static let arModeTitle = NSLocalizedString("ar_mode_title", comment: "")
static let arNoRecognitionText = NSLocalizedString("ar_no_recognition_text", comment: "")
}
Loading

0 comments on commit a8a7c36

Please sign in to comment.