Skip to content

Commit

Permalink
Merge pull request #34 from verygoodsecurity/vitaliiobertynskyi/uploa…
Browse files Browse the repository at this point in the history
…d_photo

Upload photo
  • Loading branch information
miraving committed Oct 8, 2019
2 parents 4d78a45 + 732c7fb commit 4eed325
Show file tree
Hide file tree
Showing 17 changed files with 293 additions and 38 deletions.
59 changes: 55 additions & 4 deletions demoapp/demoapp/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14868" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="14868" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="afe-xS-bJP">
<device id="retina5_5" orientation="portrait" appearance="light"/>
<dependencies>
<deployment identifier="iOS"/>
Expand All @@ -8,7 +8,7 @@
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
<scenes>
<!--1-->
<!--Card Data-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="demoapp" customModuleProvider="target" sceneMemberID="viewController">
Expand All @@ -18,11 +18,62 @@
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<tabBarItem key="tabBarItem" title="1" id="GcF-ad-6mE"/>
<tabBarItem key="tabBarItem" title="Card Data" id="GcF-ad-6mE"/>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1136" y="-318"/>
<point key="canvasLocation" x="2042.0289855072465" y="-318.75"/>
</scene>
<!--Photo-->
<scene sceneID="tSJ-sT-vuR">
<objects>
<viewController id="3ul-g4-aET" customClass="ViewController2" customModule="demoapp" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="nI6-UX-yus">
<rect key="frame" x="0.0" y="0.0" width="414" height="736"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<view contentMode="scaleToFill" translatesAutoresizingMaskIntoConstraints="NO" id="xZ7-05-u0C" customClass="VGSButton" customModule="VGSFramework">
<rect key="frame" x="65" y="88" width="284" height="55"/>
<constraints>
<constraint firstAttribute="height" constant="55" id="rqx-ZU-evD"/>
</constraints>
</view>
</subviews>
<color key="backgroundColor" systemColor="systemBackgroundColor" cocoaTouchSystemColor="whiteColor"/>
<constraints>
<constraint firstItem="xZ7-05-u0C" firstAttribute="top" secondItem="y6D-N5-RTR" secondAttribute="top" constant="88" id="9iq-Wk-TiI"/>
<constraint firstItem="xZ7-05-u0C" firstAttribute="leading" secondItem="y6D-N5-RTR" secondAttribute="leading" constant="65" id="QS1-PN-Jbc"/>
<constraint firstItem="xZ7-05-u0C" firstAttribute="centerX" secondItem="nI6-UX-yus" secondAttribute="centerX" id="dbL-lF-R6l"/>
</constraints>
<viewLayoutGuide key="safeArea" id="y6D-N5-RTR"/>
</view>
<tabBarItem key="tabBarItem" title="Photo" id="YTB-AU-3I9"/>
<connections>
<outlet property="button" destination="xZ7-05-u0C" id="bfb-0n-eNB"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="AJq-bH-kNo" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="2040.5797101449277" y="345.6521739130435"/>
</scene>
<!--Tab Bar Controller-->
<scene sceneID="BEG-Sr-Dkt">
<objects>
<tabBarController automaticallyAdjustsScrollViewInsets="NO" id="afe-xS-bJP" sceneMemberID="viewController">
<toolbarItems/>
<tabBar key="tabBar" contentMode="scaleToFill" insetsLayoutMarginsFromSafeArea="NO" id="Qtf-el-iBF">
<rect key="frame" x="0.0" y="0.0" width="1000" height="1000"/>
<autoresizingMask key="autoresizingMask"/>
<color key="backgroundColor" white="0.0" alpha="0.0" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
</tabBar>
<connections>
<segue destination="BYZ-38-t0r" kind="relationship" relationship="viewControllers" id="N9H-N5-RQR"/>
<segue destination="3ul-g4-aET" kind="relationship" relationship="viewControllers" id="dNG-P4-6J2"/>
</connections>
</tabBarController>
<placeholder placeholderIdentifier="IBFirstResponder" id="bhk-cb-oLC" userLabel="First Responder" customClass="UIResponder" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="1009" y="-33"/>
</scene>
</scenes>
</document>
32 changes: 5 additions & 27 deletions demoapp/demoapp/ViewController2.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,10 @@ import VGSFramework

class ViewController2: UIViewController {

@IBOutlet weak var textField: VGSTextField!

var vgsCollector = VGSCollect(id: "test")

override func viewDidLoad() {
super.viewDidLoad()

let config = VGSConfiguration(collector: vgsCollector, fieldName: "test")
config.isRequired = false
config.placeholder = "name pppp"
config.type = .cardNumber

textField.configuration = config

textField.padding = UIEdgeInsets(top: 0, left: 10, bottom: 0, right: 10)
@IBOutlet weak var button: VGSButton! {
didSet {
button.presentViewController = self
button.type = .library
}
}


/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}
7 changes: 7 additions & 0 deletions framework/Sources/VGSFramework/Core/Enums.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ public enum FieldType: Int, CaseIterable {
}
}
}

public enum ButtonType: Int, CaseIterable {
case none
case library
case camera
case file
}
131 changes: 131 additions & 0 deletions framework/Sources/VGSFramework/UIElements/Button/VGSButton.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//
// VGSButton.swift
// VGSFramework
//
// Created by Vitalii Obertynskyi on 10/6/19.
// Copyright © 2019 Vitalii Obertynskyi. All rights reserved.
//

import UIKit

public class VGSButton: UIView {
private(set) weak var vgsCollector: VGSCollect?
internal var button = UIButton(type: .custom)
internal var fieldName: String!
internal var file: Data?
internal var image: UIImage?
internal var token: String?

public var type: ButtonType = .none
public var presentViewController: UIViewController?

public override init(frame: CGRect) {
super.init(frame: frame)
mainInitialization()
}

required init?(coder: NSCoder) {
super.init(coder: coder)
mainInitialization()
}

// deinit {
//
// }

// MARK: - private API
private func mainInitialization() {

button.setTitle("Upload", for: .normal)
button.setTitleColor(.black, for: .normal)

button.translatesAutoresizingMaskIntoConstraints = false
addSubview(button)

let views = ["view": self, "button": button]

let horizontalConstraints = NSLayoutConstraint.constraints(withVisualFormat: "H:|-0-[button]-0-|",
options: .alignAllCenterY,
metrics: nil,
views: views)
NSLayoutConstraint.activate(horizontalConstraints)

let verticalConstraint = NSLayoutConstraint.constraints(withVisualFormat: "V:|-0-[button]-0-|",
options: .alignAllCenterX,
metrics: nil,
views: views)
NSLayoutConstraint.activate(verticalConstraint)

button.addTarget(self, action: #selector(buttonAction), for: .touchUpInside)
}

@objc
internal func buttonAction(_ sender: UIButton) {
switch type {
case .camera:
getImageFromCamera()
case .library:
getImageFromLibrary()
case .file:
#warning("Need to investigate work with icloud file")
break
default:
showAlert(message: "Need to set correct type for VGSButton")
}
}
}

extension VGSButton: UIImagePickerControllerDelegate, UINavigationControllerDelegate {
internal func getImageFromLibrary() {
guard let presenter = presentViewController else {
fatalError("Need to set presentViewController for VGSButton")
}

let picker = UIImagePickerController()
picker.sourceType = .savedPhotosAlbum
picker.delegate = self
presenter.present(picker, animated: true, completion: nil)
}

internal func getImageFromCamera() {
guard let presenter = presentViewController else {
fatalError("Need to set presentViewController for VGSButton")
}

if UIImagePickerController.isSourceTypeAvailable(.camera) == false {
showAlert(message: "No camera - no photo")

} else {
let picker = UIImagePickerController()
picker.sourceType = .camera
presenter.present(picker, animated: true, completion: nil)
}
}

// MARK: - UIImage picker delegate
public func imagePickerControllerDidCancel(_ picker: UIImagePickerController) {
picker.dismiss(animated: true, completion: nil)
}

public func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) {

let originalImage = info[.originalImage] as! UIImage
image = originalImage

picker.dismiss(animated: true, completion: nil)
}
}

extension VGSButton {
internal func showAlert(message string: String) {
guard let presenter = presentViewController else {
fatalError("Need to set presentViewController for VGSButton")
}

let alert = UIAlertController(title: nil,
message: string,
preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .cancel, handler: nil))
presenter.present(alert, animated: true, completion: nil)
}
}
48 changes: 48 additions & 0 deletions framework/Tests/FrameworkTests/Button/ButtonTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
//
// ButtonTests.swift
// FrameworkTests
//
// Created by Vitalii Obertynskyi on 10/6/19.
// Copyright © 2019 Vitalii Obertynskyi. All rights reserved.
//

import XCTest
@testable import VGSFramework

class ButtonTests: XCTestCase {

var button: VGSButton!

override func setUp() {
button = VGSButton(frame: .zero)
button.type = .library
button.presentViewController = UIViewController()
}

override func tearDown() {
button = nil
}

func testInitialization() {
XCTAssertNotNil(button.button)
XCTAssert(button.type != .none)
}

func testAlert() {
button.showAlert(message: "test")
}

func testGetImage() {
button.getImageFromLibrary()
}

func testGetCamera() {
button.getImageFromCamera()
}

func testTarget() {
let tmp = button.button.actions(forTarget: button, forControlEvent: .touchUpInside)
XCTAssertNotNil(tmp)
XCTAssert(tmp?.first == "buttonAction:")
}
}
Loading

0 comments on commit 4eed325

Please sign in to comment.