Skip to content

Instantly share code, notes, and snippets.

View levantAJ's full-sized avatar
💭
🌵🌵🌵

levantAJ levantAJ

💭
🌵🌵🌵
View GitHub Profile
@levantAJ
levantAJ / ExifOrientation.swift
Last active April 12, 2017 09:48
Exif Orientation from current device orientation
var exifOrientation: Int {
let exifOrientation: DeviceOrientation
enum DeviceOrientation: Int {
case top0ColLeft = 1
case top0ColRight = 2
case bottom0ColRight = 3
case bottom0ColLeft = 4
case left0ColTop = 5
case right0ColTop = 6
case right0ColBottom = 7
extension Date {
var timeAgoSimple: String {
let deltaSeconds = fabs(timeIntervalSince(Date()))
let deltaMinutes = deltaSeconds / 60.0
if deltaSeconds < 60 {
return "\(Int(deltaSeconds))s"
} else if deltaMinutes < 60 {
return "\(Int(deltaMinutes))m"
} else if deltaMinutes < (24 * 60) {
@levantAJ
levantAJ / UIViewController.swift
Created May 11, 2017 10:28
Load list of segues in UIViewController
import UIKit
extension UIViewController {
var segueIdentifiers: [String] {
return (self.value(forKey: "storyboardSegueTemplates") as? [AnyObject])?.flatMap({ $0.value(forKey: "identifier") as? String }) ?? []
}
}
@levantAJ
levantAJ / ViewController.swift
Created May 26, 2017 07:30
Hide status bar with animation
class ViewController: UIViewController {
var statusBarHidden: Bool = false {
didSet {
UIView.animate(withDuration: 0.25) { [weak self] in
self?.setNeedsStatusBarAppearanceUpdate()
}
}
}
override var prefersStatusBarHidden: Bool {
@levantAJ
levantAJ / Debouncer.swift
Last active May 26, 2017 10:10
Remove continuously sequences within a delaying time
//
// A -> B -> C -> D
// `keepFirst`: A
// `keepLast`: D
//
enum DebouncerType {
case keepFirst
case keepLast
}
@levantAJ
levantAJ / Dispatcher.m
Created December 11, 2017 01:19
GCD queues and dispatch method were extracted to properties to facilitate unit testing
#import <XCTest/XCTest.h>
#import "Dispatcher.h"
#import <ReactiveObjC/ReactiveObjC.h>
@interface Dispatcher ()
@property(nonatomic, assign) void (*dispatchMethod)(dispatch_queue_t, dispatch_block_t);
@property(nonatomic, strong) dispatch_queue_t queue;
@end
- (void)applicationDidEnterBackground:(UIApplication *)application {
@weakify(self);
self.backgroundTaskIdentifier = [application beginBackgroundTaskWithExpirationHandler:^{
@strongify(self);
[application endBackgroundTask:self.backgroundTaskIdentifier];
self.backgroundTaskIdentifier = UIBackgroundTaskInvalid;
}];
}
- (void)testApplicationDidEnterBackgroundExpirationHandler {
@levantAJ
levantAJ / CombinationsTest.m
Created February 5, 2018 08:16
Combination Tests
- (void)testCacheSnapshotCombinations {
for (int i = 0; i < 32; i++) {
@autoreleasepool {
//Given:
UIImage *snapshotImage = [UIImage new];
id image = OCMClassMock([UIImage class]);
OCMStub([image imageForView:OCMOCK_ANY]).andReturn(snapshotImage);
if (i & (1 << 0)) {
self.sut.bounds = CGRectMake(0, 0, 123, 456);
@levantAJ
levantAJ / MockStaticVariable
Created February 27, 2018 02:54
How to mock a static variable
With my case how I mock the static variable and I hope this can help you, here is my workaround:
For example, I have a static `count` value in production code:
```
@implementation MyClass
static int _count = 0;
- (int)viewDidAppearCount {
//
// ColdStartEventManagerTest.m
// InteractorTest
//
// Created by BBM on 28/3/18.
//
#import "ColdStartEventManager.h"
#import "AhoyReporterProtocol.h"