Skip to content

Instantly share code, notes, and snippets.

View fmo91's full-sized avatar

Fernando Martín Ortiz fmo91

View GitHub Profile
@fmo91
fmo91 / NetworkTask.swift
Created February 12, 2017 06:45
Sample Task subclass for network operations
//
// NetworkTask.swift
//
// Created by Fernando Ortiz on 2/11/17.
//
import Foundation
import RxSwift
class NetworkTask<Input: Request, Output>: Task<Input, Output> {
@fmo91
fmo91 / UIImage+Compression.swift
Created February 14, 2017 13:34
UIImage extension with compression methods
//
// UIImage+Compression.swift
//
// Created by Fernando on 14/2/17.
// Copyright © 2017 Fernando Martín Ortiz. All rights reserved.
//
import UIKit
/// Intended to be used in UIImage size methods
@fmo91
fmo91 / ReusableViewEnum.swift
Last active August 16, 2017 20:39
Protocol extension to help using UITableView protocols in a better way.
protocol ReusableViewEnum {}
extension ReusableViewEnum where Self: RawRepresentable, Self.RawValue == Int {
static var all: [Self] {
var index = 0
var allItems = [Self]()
while let item = Self(rawValue: index) {
allItems.append(item)
index += 1
}
@fmo91
fmo91 / AnalyticsService.swift
Last active February 23, 2017 02:15
An abstraction over Analytics SDKs in iOS
public protocol AnalyticsService {
/**
1. INITIALIZATION
This will be called within AppDelegate application:didFinishLaunchingWithOptions: method
In this method, you should initialize the SDK.
*/
func initialize(application: UIApplication, launchOptions: [UIApplicationLaunchOptionsKey: Any]?)
/**
@fmo91
fmo91 / Event.swift
Created February 23, 2017 03:23
A sample list of events for analytics tracking
import Foundation
enum Event: Int {
// Here is the list of events that you
// want to track
case loginWithEmail = 0
case loginWithFacebook
case loginWithGoogle
case registerWithEmail
@fmo91
fmo91 / MixpanelAnalyticsService.swift
Last active February 23, 2017 03:40
Sample Mixpanel analytics wrapper
import Foundation
import Mixpanel
final class MixpanelAnalyticsService: AnalyticsService {
// Account credentials
private static let TOKEN = "Your token"
// Initializes Mixpanel analytics when app launches
func initialize(application: UIApplication, launchOptions: [UIApplicationLaunchOptionsKey: Any]?) {
@fmo91
fmo91 / AnalyticsManager.swift
Created February 23, 2017 03:44
Sample implementation of AnalyticsManager. See the blogpost in Medium
import Foundation
final class AnalyticsManager: AnalyticsService {
// MARK: - Properties -
// The list of services added to this class as observers.
internal private(set) var services = [AnalyticsService]()
// MARK: - Singleton -
static let instance = AnalyticsManager()
@fmo91
fmo91 / AnalyticsManagerAppDelegate.swift
Created February 23, 2017 04:09
Sample initialization for Analytics services layer.
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
let cleverTapAnalyticsService: CleverTapAnalyticsService = CleverTapAnalyticsService()
let mixpanelAnalyticsService: MixpanelAnalyticsService = MixpanelAnalyticsService()
AnalyticsManager.instance.add(service: cleverTapAnalyticsService)
AnalyticsManager.instance.add(service: mixpanelAnalyticsService)
@fmo91
fmo91 / Array+GroupBy.swift
Created February 23, 2017 04:58
Simple array extension with a grouped(by:) implementation
import Foundation
extension Array {
func grouped<T>(by criteria: (Element) -> T) -> [T: [Element]] {
var groups = [T: [Element]]()
for element in self {
let key = criteria(element)
if groups.keys.contains(key) == false {
groups[key] = [Element]()
}
@fmo91
fmo91 / LoggerApplicationService.swift
Created February 27, 2017 01:39
Sample ApplicationService implementation
import Foundation
import PluggableApplicationDelegate
final class LoggerApplicationService: NSObject, ApplicationService {
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey : Any]? = nil) -> Bool {
print("It has started!")
return true
}
func applicationDidEnterBackground(_ application: UIApplication) {