Rename repo and app
This commit is contained in:
30
Caps/Extensions/Array+Extensions.swift
Normal file
30
Caps/Extensions/Array+Extensions.swift
Normal file
@ -0,0 +1,30 @@
|
||||
//
|
||||
// Array+Extensions.swift
|
||||
// CapCollector
|
||||
//
|
||||
// Created by Christoph on 12.05.20.
|
||||
// Copyright © 2020 CH. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Array {
|
||||
|
||||
func split(intoPartsOf maxElements: Int) -> [ArraySlice<Element>] {
|
||||
guard !isEmpty, maxElements > 0 else {
|
||||
return []
|
||||
}
|
||||
var result = [ArraySlice<Element>]()
|
||||
var currentIndex = 0
|
||||
while true {
|
||||
let nextIndex = currentIndex + maxElements
|
||||
if nextIndex >= count {
|
||||
result.append(self[currentIndex..<count])
|
||||
return result
|
||||
}
|
||||
result.append(self[currentIndex..<nextIndex])
|
||||
currentIndex += maxElements
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
33
Caps/Extensions/CGImagePropertyOrientation+Extensions.swift
Normal file
33
Caps/Extensions/CGImagePropertyOrientation+Extensions.swift
Normal file
@ -0,0 +1,33 @@
|
||||
//
|
||||
// CGImagePropertyOrientation+Extensions.swift
|
||||
// CapCollector
|
||||
//
|
||||
// Created by Christoph on 13.05.20.
|
||||
// Copyright © 2020 CH. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension CGImagePropertyOrientation {
|
||||
/**
|
||||
Converts a `UIImageOrientation` to a corresponding
|
||||
`CGImagePropertyOrientation`. The cases for each
|
||||
orientation are represented by different raw values.
|
||||
|
||||
- Tag: ConvertOrientation
|
||||
*/
|
||||
init(_ orientation: UIImage.Orientation) {
|
||||
switch orientation {
|
||||
case .up: self = .up
|
||||
case .upMirrored: self = .upMirrored
|
||||
case .down: self = .down
|
||||
case .downMirrored: self = .downMirrored
|
||||
case .left: self = .left
|
||||
case .leftMirrored: self = .leftMirrored
|
||||
case .right: self = .right
|
||||
case .rightMirrored: self = .rightMirrored
|
||||
@unknown default:
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
}
|
28
Caps/Extensions/DispatchGroup+Extensions.swift
Normal file
28
Caps/Extensions/DispatchGroup+Extensions.swift
Normal file
@ -0,0 +1,28 @@
|
||||
//
|
||||
// DispatchGroup+Extensions.swift
|
||||
// CapCollector
|
||||
//
|
||||
// Created by iMac on 13.01.21.
|
||||
// Copyright © 2021 CH. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension DispatchGroup {
|
||||
|
||||
typealias AsyncSuccessCallback = (Bool) -> Void
|
||||
|
||||
static func singleTask(timeout: TimeInterval = 30, _ block: (@escaping AsyncSuccessCallback) -> Void) -> Bool {
|
||||
let group = DispatchGroup()
|
||||
group.enter()
|
||||
var result = true
|
||||
block { success in
|
||||
result = success
|
||||
group.leave()
|
||||
}
|
||||
guard group.wait(timeout: .now() + timeout) == .success else {
|
||||
return false
|
||||
}
|
||||
return result
|
||||
}
|
||||
}
|
61
Caps/Extensions/UIAlertControllerExtensions.swift
Normal file
61
Caps/Extensions/UIAlertControllerExtensions.swift
Normal file
@ -0,0 +1,61 @@
|
||||
//
|
||||
// UIAlertControllerExtensions.swift
|
||||
// CapFinder
|
||||
//
|
||||
// Created by User on 23.03.18.
|
||||
// Copyright © 2018 User. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
extension UIAlertController {
|
||||
|
||||
private struct AssociatedKeys {
|
||||
static var blurStyleKey = "UIAlertController.blurStyleKey"
|
||||
}
|
||||
|
||||
public var blurStyle: UIBlurEffect.Style {
|
||||
get {
|
||||
return objc_getAssociatedObject(self, &AssociatedKeys.blurStyleKey) as? UIBlurEffect.Style ?? .extraLight
|
||||
} set (style) {
|
||||
objc_setAssociatedObject(self, &AssociatedKeys.blurStyleKey, style, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
|
||||
|
||||
view.setNeedsLayout()
|
||||
view.layoutIfNeeded()
|
||||
}
|
||||
}
|
||||
|
||||
public var cancelButtonColor: UIColor? {
|
||||
return blurStyle == .dark ? UIColor(red: 28.0/255.0, green: 28.0/255.0, blue: 28.0/255.0, alpha: 1.0) : nil
|
||||
}
|
||||
|
||||
private var visualEffectView: UIVisualEffectView? {
|
||||
if let presentationController = presentationController, presentationController.responds(to: Selector(("popoverView"))), let view = presentationController.value(forKey: "popoverView") as? UIView // We're on an iPad and visual effect view is in a different place.
|
||||
{
|
||||
return view.recursiveSubviews.compactMap({$0 as? UIVisualEffectView}).first
|
||||
}
|
||||
|
||||
return view.recursiveSubviews.compactMap({$0 as? UIVisualEffectView}).first
|
||||
}
|
||||
|
||||
private var cancelActionView: UIView? {
|
||||
return view.recursiveSubviews.compactMap({
|
||||
$0 as? UILabel}
|
||||
).first(where: {
|
||||
$0.text == actions.first(where: { $0.style == .cancel })?.title
|
||||
})?.superview?.superview
|
||||
}
|
||||
|
||||
public convenience init(title: String?, message: String?, preferredStyle: UIAlertController.Style, blurStyle: UIBlurEffect.Style) {
|
||||
self.init(title: title, message: message, preferredStyle: preferredStyle)
|
||||
self.blurStyle = blurStyle
|
||||
}
|
||||
|
||||
open override func viewWillLayoutSubviews() {
|
||||
super.viewWillLayoutSubviews()
|
||||
|
||||
visualEffectView?.effect = UIBlurEffect(style: blurStyle)
|
||||
cancelActionView?.backgroundColor = cancelButtonColor
|
||||
}
|
||||
}
|
22
Caps/Extensions/UIColor+Extensions.swift
Normal file
22
Caps/Extensions/UIColor+Extensions.swift
Normal file
@ -0,0 +1,22 @@
|
||||
//
|
||||
// UIColor+Extensions.swift
|
||||
// CapCollector
|
||||
//
|
||||
// Created by Christoph on 14.04.20.
|
||||
// Copyright © 2020 CH. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UIColor {
|
||||
|
||||
var rgb: (red: Double, green: Double, blue: Double) {
|
||||
var fRed: CGFloat = 0
|
||||
var fGreen: CGFloat = 0
|
||||
var fBlue: CGFloat = 0
|
||||
var fAlpha: CGFloat = 0
|
||||
getRed(&fRed, green: &fGreen, blue: &fBlue, alpha: &fAlpha)
|
||||
return (Double(fRed), Double(fGreen), Double(fBlue))
|
||||
}
|
||||
}
|
||||
|
161
Caps/Extensions/UIImage+Extensions.swift
Normal file
161
Caps/Extensions/UIImage+Extensions.swift
Normal file
@ -0,0 +1,161 @@
|
||||
//
|
||||
// UIImageExtensions.swift
|
||||
// CapFinder
|
||||
//
|
||||
// Created by User on 13.02.18.
|
||||
// Copyright © 2018 User. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
|
||||
extension UIImage {
|
||||
|
||||
static func templateImage(named: String) -> UIImage {
|
||||
return UIImage(named: named)!.withRenderingMode(.alwaysTemplate)
|
||||
}
|
||||
|
||||
/**
|
||||
Resize an image to the target size
|
||||
*/
|
||||
func resize(to targetSize: CGSize) -> UIImage {
|
||||
let rect = CGRect(x: 0, y: 0, width: targetSize.width, height: targetSize.height)
|
||||
|
||||
UIGraphicsBeginImageContextWithOptions(targetSize, false, 1.0)
|
||||
self.draw(in: rect)
|
||||
let newImage = UIGraphicsGetImageFromCurrentImageContext()
|
||||
UIGraphicsEndImageContext()
|
||||
return newImage!
|
||||
}
|
||||
|
||||
/**
|
||||
Crop an image to a square, centered around the middle of the frame
|
||||
- parameter factor: The crop factor of the resulting image
|
||||
- returns: The cropped image
|
||||
*/
|
||||
func crop(factor: CGFloat) -> UIImage {
|
||||
let width = self.size.width * factor
|
||||
return crop(to: width)
|
||||
}
|
||||
|
||||
/**
|
||||
Crop an image to a square, centered around the middle of the frame
|
||||
- parameter size: The height and width of the resulting image
|
||||
- returns: The cropped image
|
||||
*/
|
||||
func crop(to size: CGFloat) -> UIImage {
|
||||
var rect = CGRect(
|
||||
x: (self.size.height - size) / 2,
|
||||
y: (self.size.width - size) / 2,
|
||||
width: size,
|
||||
height: size)
|
||||
rect.origin.x *= scale
|
||||
rect.origin.y *= scale
|
||||
rect.size.width *= scale
|
||||
rect.size.height *= scale
|
||||
|
||||
let imageRef = cgImage!.cropping(to: rect)
|
||||
return UIImage(cgImage: imageRef!, scale: scale, orientation: imageOrientation)
|
||||
}
|
||||
|
||||
/// The (circular) image masked by a circle
|
||||
var circleMasked: UIImage? {
|
||||
let width = size.width
|
||||
let height = size.height
|
||||
let isPortrait = height > width
|
||||
let breadth = min(width, height)
|
||||
let breadthSize = CGSize(width: breadth, height: breadth)
|
||||
let breadthRect = CGRect(origin: .zero, size: breadthSize)
|
||||
UIGraphicsBeginImageContextWithOptions(breadthSize, false, scale)
|
||||
defer { UIGraphicsEndImageContext() }
|
||||
|
||||
let x = isPortrait ? 0 : floor((width - height) / 2)
|
||||
let y = isPortrait ? floor((height - width) / 2) : 0
|
||||
let rect = CGRect(origin: CGPoint( x: x, y: y), size: breadthSize)
|
||||
guard let cgImage = cgImage?.cropping(to: rect) else {
|
||||
return nil
|
||||
}
|
||||
UIBezierPath(ovalIn: breadthRect).addClip()
|
||||
UIImage(cgImage: cgImage, scale: 1, orientation: imageOrientation).draw(in: breadthRect)
|
||||
return UIGraphicsGetImageFromCurrentImageContext()
|
||||
}
|
||||
|
||||
var averageColor: UIColor? {
|
||||
let image = ciImage ?? CIImage(cgImage: cgImage!)
|
||||
return image.averageColor
|
||||
}
|
||||
}
|
||||
|
||||
extension CIImage {
|
||||
|
||||
func averageColor(context: CIContext) -> UIColor? {
|
||||
let extentVector = CIVector(
|
||||
x: extent.origin.x,
|
||||
y: extent.origin.y,
|
||||
z: extent.size.width,
|
||||
w: extent.size.height)
|
||||
|
||||
guard let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: self, kCIInputExtentKey: extentVector]) else {
|
||||
log("Failed to create filter")
|
||||
return nil
|
||||
}
|
||||
guard let outputImage = filter.outputImage else {
|
||||
log("Failed get filter output")
|
||||
return nil
|
||||
}
|
||||
|
||||
var bitmap = [UInt8](repeating: 0, count: 4)
|
||||
|
||||
context.render(outputImage, toBitmap: &bitmap, rowBytes: 4,
|
||||
bounds: CGRect(x: 0, y: 0, width: 1, height: 1),
|
||||
format: .RGBA8, colorSpace: nil)
|
||||
|
||||
return UIColor(
|
||||
red: saturate(bitmap[0]),
|
||||
green: saturate(bitmap[1]),
|
||||
blue: saturate(bitmap[2]),
|
||||
alpha: CGFloat(bitmap[3]) / 255)
|
||||
}
|
||||
|
||||
var averageColor: UIColor? {
|
||||
let extentVector = CIVector(
|
||||
x: extent.origin.x,
|
||||
y: extent.origin.y,
|
||||
z: extent.size.width,
|
||||
w: extent.size.height)
|
||||
|
||||
guard let filter = CIFilter(name: "CIAreaAverage", parameters: [kCIInputImageKey: self, kCIInputExtentKey: extentVector]) else {
|
||||
log("Failed to create filter")
|
||||
return nil
|
||||
}
|
||||
guard let outputImage = filter.outputImage else {
|
||||
log("Failed get filter output")
|
||||
return nil
|
||||
}
|
||||
|
||||
var bitmap = [UInt8](repeating: 0, count: 4)
|
||||
guard let null = kCFNull else {
|
||||
return nil
|
||||
}
|
||||
let context = CIContext(options: [.workingColorSpace: null])
|
||||
context.render(outputImage, toBitmap: &bitmap, rowBytes: 4,
|
||||
bounds: CGRect(x: 0, y: 0, width: 1, height: 1),
|
||||
format: .RGBA8, colorSpace: nil)
|
||||
|
||||
let color = UIColor(
|
||||
red: saturate(bitmap[0]),
|
||||
green: saturate(bitmap[1]),
|
||||
blue: saturate(bitmap[2]),
|
||||
alpha: CGFloat(bitmap[3]) / 255)
|
||||
|
||||
return color
|
||||
}
|
||||
}
|
||||
|
||||
/// Map expected range 75-200 to 0-255
|
||||
private func saturate(_ component: UInt8) -> CGFloat {
|
||||
return max(min(CGFloat(component) * 2 - 150, 255), 0) / 255
|
||||
}
|
||||
|
||||
extension CIImage: Logger { }
|
31
Caps/Extensions/UINavigationItem+Extensions.swift
Normal file
31
Caps/Extensions/UINavigationItem+Extensions.swift
Normal file
@ -0,0 +1,31 @@
|
||||
//
|
||||
// UINavigationItem+Extensions.swift
|
||||
// CapCollector
|
||||
//
|
||||
// Created by Christoph on 12.05.20.
|
||||
// Copyright © 2020 CH. All rights reserved.
|
||||
//
|
||||
|
||||
import UIKit
|
||||
|
||||
extension UINavigationItem {
|
||||
|
||||
func setTitle(_ title: String, subtitle: String) {
|
||||
let titleLabel = UILabel()
|
||||
titleLabel.text = title
|
||||
titleLabel.font = .systemFont(ofSize: 17.0)
|
||||
titleLabel.textColor = .black
|
||||
|
||||
let subtitleLabel = UILabel()
|
||||
subtitleLabel.text = subtitle
|
||||
subtitleLabel.font = .systemFont(ofSize: 12.0)
|
||||
subtitleLabel.textColor = .gray
|
||||
|
||||
let stackView = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel])
|
||||
stackView.distribution = .equalCentering
|
||||
stackView.alignment = .center
|
||||
stackView.axis = .vertical
|
||||
|
||||
self.titleView = stackView
|
||||
}
|
||||
}
|
23
Caps/Extensions/UIViewExtensions.swift
Normal file
23
Caps/Extensions/UIViewExtensions.swift
Normal file
@ -0,0 +1,23 @@
|
||||
//
|
||||
// UIViewExtensions.swift
|
||||
// CapFinder
|
||||
//
|
||||
// Created by User on 23.03.18.
|
||||
// Copyright © 2018 User. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
extension UIView {
|
||||
|
||||
var recursiveSubviews: [UIView] {
|
||||
var subviews = self.subviews.compactMap{ $0 }
|
||||
subviews.forEach { subviews.append(contentsOf: $0.recursiveSubviews) }
|
||||
return subviews
|
||||
}
|
||||
|
||||
func fromNib<T : UIView>() -> T { // 2
|
||||
return Bundle(for: type(of: self)).loadNibNamed(String(describing: type(of: self)), owner: self, options: nil)!.first! as! T
|
||||
}
|
||||
}
|
28
Caps/Extensions/ViewControllerExtensions.swift
Normal file
28
Caps/Extensions/ViewControllerExtensions.swift
Normal file
@ -0,0 +1,28 @@
|
||||
//
|
||||
// ViewControllerExtensions.swift
|
||||
// CapFinder
|
||||
//
|
||||
// Created by User on 18.03.18.
|
||||
// Copyright © 2018 User. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
|
||||
extension UIViewController {
|
||||
|
||||
// MARK: Alerts
|
||||
|
||||
/// Present an alert with a message to the user
|
||||
func showAlert(_ message: String, title: String = "Error") {
|
||||
let alertController = UIAlertController(
|
||||
title: title,
|
||||
message: message,
|
||||
preferredStyle: .alert)//,
|
||||
//blurStyle: .dark)
|
||||
|
||||
alertController.addAction(UIAlertAction(title: "OK", style: .cancel, handler: nil))
|
||||
|
||||
self.present(alertController, animated: true, completion: nil)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user