Commit ea177b5b authored by Zandor Smith's avatar Zandor Smith 💻

Add main functionality.

parent f931c1fb
PODS:
- Connectivity (3.1.0)
- GeneralToolsFramework (1.3.1):
- GeneralToolsFramework (1.4.0):
- Connectivity (~> 3.0)
- PINCache
- PINCache (2.3)
......@@ -16,7 +16,7 @@ SPEC REPOS:
SPEC CHECKSUMS:
Connectivity: b900acf9c33029dd441ca0c1d5cf6cbdd7c9ee0b
GeneralToolsFramework: 86ac74f2016ac49560887b7072aecf1088fca730
GeneralToolsFramework: 0a83900f25438521612130680f0533fa4937df5a
PINCache: ce36ed282031b92fc7733ffe831f474ff80fddc2
PODFILE CHECKSUM: ff913a7083428b2ae8c026df0f355309ba034a5a
......
......@@ -16,10 +16,9 @@ open class Image {
private var downloading: Bool = false
private var getCallbacks: [(UIImage) -> Void] = []
private var image: UIImage?
public var downloaded: Bool {
return image != nil
return PINCache.shared().containsObject(forKey: self.url)
}
public init(url: String) {
......@@ -27,7 +26,14 @@ open class Image {
}
public func removeCachedImage() {
self.image = nil
PINCache.shared().removeObject(forKey: self.url)
}
private func getImageFromCache() -> UIImage? {
if let data = PINCache.shared().object(forKey: self.url) as? NSData {
return UIImage(data: data as Data)
}
return nil
}
open func getImage(available: @escaping (UIImage) -> Void, onError: @escaping (APICallError) -> Void) {
......@@ -35,18 +41,13 @@ open class Image {
self.getCallbacks.append(available)
return
}
if self.downloaded, let image = self.image {
DispatchQueue.main.async {
available(image)
}
} else if let image = PINCache.shared().object(forKey: self.url) as? UIImage {
if let image = self.getImageFromCache() {
DispatchQueue.main.async {
available(image)
}
} else {
self.downloadImage(onCompletion: {
if let image = self.image {
PINCache.shared().setObject(image, forKey: self.url)
if let image = self.getImageFromCache() {
DispatchQueue.main.async {
available(image)
for callback in self.getCallbacks {
......@@ -94,7 +95,7 @@ open class Image {
if let data = data {
NetworkActivityHandler.popNetworkActivity()
self.image = UIImage(data: data)
PINCache.shared().setObject(data as NSData, forKey: self.url)
self.downloading = false
onCompletion()
} else {
......
PODS:
- Connectivity (3.1.0)
- GeneralToolsFramework (1.3.1):
- GeneralToolsFramework (1.4.0):
- Connectivity (~> 3.0)
- PINCache
- PINCache (2.3)
......@@ -16,7 +16,7 @@ SPEC REPOS:
SPEC CHECKSUMS:
Connectivity: b900acf9c33029dd441ca0c1d5cf6cbdd7c9ee0b
GeneralToolsFramework: 86ac74f2016ac49560887b7072aecf1088fca730
GeneralToolsFramework: 0a83900f25438521612130680f0533fa4937df5a
PINCache: ce36ed282031b92fc7733ffe831f474ff80fddc2
PODFILE CHECKSUM: ff913a7083428b2ae8c026df0f355309ba034a5a
......
......@@ -15,7 +15,7 @@
<key>CFBundlePackageType</key>
<string>FMWK</string>
<key>CFBundleShortVersionString</key>
<string>1.3.1</string>
<string>1.4.0</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
......
<?xml version="1.0" encoding="UTF-8"?>
<Bucket
type = "0"
version = "2.0">
</Bucket>
<?xml version="1.0" encoding="UTF-8"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="13122.16" 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="14490.70" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" useSafeAreas="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
<device id="retina6_1" orientation="portrait">
<adaptation id="fullscreen"/>
</device>
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="13104.12"/>
<deployment identifier="iOS"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="14490.49"/>
<capability name="Safe area layout guides" minToolsVersion="9.0"/>
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
</dependencies>
......@@ -9,16 +13,60 @@
<!--View Controller-->
<scene sceneID="tne-QT-ifu">
<objects>
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
<viewController id="BYZ-38-t0r" customClass="ViewController" customModule="UIImageSizeTest" customModuleProvider="target" sceneMemberID="viewController">
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
<rect key="frame" x="0.0" y="0.0" width="414" height="896"/>
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
<subviews>
<button opaque="NO" contentMode="scaleToFill" ambiguous="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="UXb-qJ-ufE">
<rect key="frame" x="172" y="756" width="69" height="30"/>
<state key="normal" title="Download"/>
<connections>
<action selector="didTapDownload:" destination="BYZ-38-t0r" eventType="touchUpInside" id="chO-Ls-hp3"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" ambiguous="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="y0O-m5-APy">
<rect key="frame" x="188" y="794" width="36" height="30"/>
<state key="normal" title="Clear"/>
<connections>
<action selector="didTapClear:" destination="BYZ-38-t0r" eventType="touchUpInside" id="81B-pN-wGL"/>
</connections>
</button>
<button opaque="NO" contentMode="scaleToFill" ambiguous="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" buttonType="roundedRect" lineBreakMode="middleTruncation" translatesAutoresizingMaskIntoConstraints="NO" id="yFn-ha-DRf">
<rect key="frame" x="165" y="832" width="85" height="30"/>
<state key="normal" title="Clean cache">
<color key="titleColor" red="1" green="0.0" blue="0.0" alpha="1" colorSpace="calibratedRGB"/>
</state>
<connections>
<action selector="didTapCleanCache:" destination="BYZ-38-t0r" eventType="touchUpInside" id="KEJ-cL-7Cm"/>
</connections>
</button>
<imageView clipsSubviews="YES" userInteractionEnabled="NO" contentMode="scaleAspectFit" horizontalHuggingPriority="251" verticalHuggingPriority="251" translatesAutoresizingMaskIntoConstraints="NO" id="S19-Or-dl6">
<rect key="frame" x="20" y="44" width="374" height="704"/>
<color key="backgroundColor" red="0.94509803921568625" green="0.94509803921568625" blue="0.94509803921568625" alpha="1" colorSpace="calibratedRGB"/>
</imageView>
</subviews>
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
<constraints>
<constraint firstItem="y0O-m5-APy" firstAttribute="centerX" secondItem="UXb-qJ-ufE" secondAttribute="centerX" id="Cbq-SW-1As"/>
<constraint firstItem="yFn-ha-DRf" firstAttribute="bottom" secondItem="6Tk-OE-BBY" secondAttribute="bottom" id="MMO-hG-dZH"/>
<constraint firstItem="yFn-ha-DRf" firstAttribute="top" secondItem="y0O-m5-APy" secondAttribute="bottom" constant="8" id="aOk-6a-yCR"/>
<constraint firstItem="UXb-qJ-ufE" firstAttribute="top" secondItem="S19-Or-dl6" secondAttribute="bottom" constant="8" id="cN8-hp-hMg"/>
<constraint firstItem="S19-Or-dl6" firstAttribute="leading" secondItem="6Tk-OE-BBY" secondAttribute="leading" constant="20" id="egA-1K-9w2"/>
<constraint firstItem="6Tk-OE-BBY" firstAttribute="trailing" secondItem="S19-Or-dl6" secondAttribute="trailing" constant="20" id="ezS-T6-ima"/>
<constraint firstItem="S19-Or-dl6" firstAttribute="top" secondItem="6Tk-OE-BBY" secondAttribute="top" id="lWN-bO-IE9"/>
<constraint firstItem="yFn-ha-DRf" firstAttribute="centerX" secondItem="UXb-qJ-ufE" secondAttribute="centerX" id="p4f-kA-lBf"/>
<constraint firstItem="y0O-m5-APy" firstAttribute="top" secondItem="UXb-qJ-ufE" secondAttribute="bottom" constant="8" id="w0H-3C-ETb"/>
</constraints>
<viewLayoutGuide key="safeArea" id="6Tk-OE-BBY"/>
</view>
<connections>
<outlet property="imageView" destination="S19-Or-dl6" id="osi-7W-b7U"/>
</connections>
</viewController>
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
</objects>
<point key="canvasLocation" x="137.68115942028987" y="137.94642857142856"/>
</scene>
</scenes>
</document>
......@@ -7,12 +7,43 @@
//
import UIKit
import PINCache
import GeneralToolsFramework
class ViewController: UIViewController {
@IBOutlet weak var imageView: UIImageView!
var image: Image?
override func viewDidLoad() {
super.viewDidLoad()
// To download: https://solar-intranet.ams3.cdn.digitaloceanspaces.com/images/5d54f066248731.57114861.jpg
self.image = Image(url: "https://solar-intranet.ams3.cdn.digitaloceanspaces.com/images/5d54f066248731.57114861.jpg")
}
@IBAction func didTapDownload(_ sender: Any) {
self.image?.getImage(available: { image in
DispatchQueue.main.async {
self.imageView.image = image
}
}, onError: { _ in
DispatchQueue.main.async {
let alert = UIAlertController(title: "Error", message: "Error downloading image", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
})
}
@IBAction func didTapClear(_ sender: Any) {
self.imageView.image = nil
}
@IBAction func didTapCleanCache(_ sender: Any) {
PINCache.shared().removeAllObjects()
let alert = UIAlertController(title: "Cache", message: "Cache has been cleared.", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
self.present(alert, animated: true, completion: nil)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment