Skip to content

Instantly share code, notes, and snippets.

View adri4silva's full-sized avatar
:octocat:

Adri Silva adri4silva

:octocat:
View GitHub Profile
@adri4silva
adri4silva / BetterXcodeJumpToCounterpartSwift.org
Last active November 19, 2019 09:13 — forked from danielmartin/BetterXcodeJumpToCounterpartSwift.org
Add support for a better Xcode's Jump to Next Counterpart in Swift

If you work on a Swift project that follows the Sensor architecture or similar, you may want to jump to counterpart in Xcode from your view to your model, and then to your view model. (ie. by using Ctrl+Cmd+Up and Ctrl+Cmd+Down).

You can do this in recent versions of Xcode by setting a configuration default.

From a terminal, just type this command and press Enter:

defaults write com.apple.dt.Xcode IDEAdditionalCounterpartSuffixes -array-add "Store" "View"
import RxSwift
enum RequestState {
case data
case empty
case loading
case error
}
extension ObservableType where E: RandomAccessCollection {
@adri4silva
adri4silva / Line.swift
Created December 5, 2018 12:32
Create a line of 1dp height on swift
// When creating lines with 1dp height, it's important to know that if you use a height value of 1 (CGFloat, Float..),
// that height won't look the same in all devices.
// The solution for this is to use the natural scale factor of the user's screen.
// You can access that value with the next code
let normalizedLineHeight = 1 / UIScreen.main.scale
@adri4silva
adri4silva / BiometricAuth.swift
Created September 21, 2018 12:11
Biometric authentication using RxSwift
enum AuthenticationError: Error {
case noPermissionError
case authenticationFailed
}
class BiometricAuth {
private static var context = LAContext()
private static let authPermissionReason = "Unlock the door"
/// If biometrics are available, use biometrics as default. Otherwise promt User/Password
private static let authenticationPolicy: LAPolicy = .deviceOwnerAuthentication
@adri4silva
adri4silva / Animation.swift
Created August 1, 2018 11:34
Twitter Tab Bar Icon Bounce Animation Swift
func animate(_ image: UIImageView) {
let bounceAnimation = CAKeyframeAnimation(keyPath: "transform.scale")
bounceAnimation.values = [1.0, 1.10, 0.9, 1.0] // Sinusoidal curve
bounceAnimation.duration = TimeInterval(0.35) // 0.35s
bounceAnimation.calculationMode = kCAAnimationCubic
image.layer.add(bounceAnimation, forKey: "bounceAnimation")
}
// Get the position of a given controller in the TabBarController's viewControllers array
guard let position = viewControllers!.index(of: controller) else { return }
// Get item index at position
@adri4silva
adri4silva / CountingLabel.swift
Created July 26, 2018 11:06
Label counter animation with RxSwift
class CountingLabel: UILabel {
// MARK: - DisposeBag
private var disposeBag = DisposeBag()
private var start: Int = 0
private var end: Int = 0
private var timer: Observable<Int>!
@adri4silva
adri4silva / RxCaptureMetadataOutputObjectsDelegateProxy.swift
Last active May 3, 2019 04:09
RxCaptureMetadataOutputObjectsDelegateProxy and Reactive extension to (_:didOutput:from:) method
import Foundation
import AVFoundation
import RxSwift
import RxCocoa
class RxCaptureMetadataOutputObjectsDelegateProxy:
DelegateProxy<AVCaptureMetadataOutput, AVCaptureMetadataOutputObjectsDelegate>,
DelegateProxyType,
AVCaptureMetadataOutputObjectsDelegate {
// Dada una matriz con String de colores, retornar un array con el color que mas se repite. En el caso de que sean varios colores retornar un array con los colores ordenados.
// Given an matrix color names as string, return an array with the most/s repeated color/s ordered by character
var colorsList = [["yellow", "red", "yellow"], ["red", "blue", "red"], ["yellow", "yellow", "red"]]
let counteredSet = colorsList
.flatMap { $0 }
.reduce(into: NSCountedSet()) { $0.add($1) }
let sorted = counteredSet
.allObjects
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "goToQR" {
let qRViewController = segue.destination as! QRViewController
qRViewController.delegate = self
}
}
@adri4silva
adri4silva / searchBarHide.swift
Created January 18, 2018 17:19
How to hide a searchBar on Swift
override func viewWillAppear(_ animated: Bool) {
tableView.contentOffset = CGPoint(x: 0, y: (tableView.tableHeaderView?.frame.size.height ?? 0))
}