Skip to content

Instantly share code, notes, and snippets.

View Athosone's full-sized avatar

WERCK Ayrton Athosone

  • Lyon, France
View GitHub Profile
@Athosone
Athosone / RxSwift+fromAsync.swift
Last active October 2, 2018 17:36
RxSwift+fromAsync example
// Consider this function:
func fetchPost(by postId: Int, completion: @escaping (Post?, Error?) -> Void) {
...
}
// Using our extension to use an observable
// fetchPostObservable looks like: fetchPostObservable(postId) -> Observable<Post>
let fetchPostObservable = Observable.fromAsync(postService.getPost(postId:completion:))
// Now to fetch a post you just have to call it just like you would call any function which returns an Observable:
fecthPostObservable(10).subscribe(onNext: { (post) in
@Athosone
Athosone / ForEachCurry.swift
Created October 2, 2018 16:04
Curry example - ForEach
enum LogLevel: String {
case debug
case error
}
func logMessage(level: LogLevel, message: String) {
print("[\(level)] \(message)")
}
// Without Currying
@Athosone
Athosone / CurryRxSwift.swift
Last active September 19, 2018 15:19
Example of rx extension
extension Observable {
// MARK: For completionBlock(Element, Error) template methods
public static func fromAsync(_ asyncRequest: @escaping (@escaping (Element?, Error?) -> Void) -> Void) -> Observable<Element> {
return Observable.create { (o) -> Disposable in
asyncRequest { res, error in
if let err = error {
o.onError(err)
return
@Athosone
Athosone / LogMessageCurried.swift
Last active September 19, 2018 14:20
Debug Log message
// We apply curry to the log message func
let logCurried = curry(logMessage)
// logCurried can be represented like this: logCurried -> (LogLevel) -> (String) -> Void
let debug = logCurried(.debug)
// debug can be represented like this: debug -> (String) -> Void
debug("Log debug")
@Athosone
Athosone / LogMessage.swift
Created September 19, 2018 14:03
Curry-Log example
enum LogLevel: String {
case debug
case error
}
func logMessage(level: LogLevel, message: String) {
print("[\(level)] \(message)")
}
@Athosone
Athosone / curry.swift
Created September 19, 2018 14:00
Curry function
func curry<A, B, C>(_ fn: @escaping (A, B) -> C) -> (A) -> (B) -> C {
return { (a: A) in
return { (b: B) in
return fn(a, b)
}
}
}
@Athosone
Athosone / UICollectionView+DynamicCellSize.swift
Created April 4, 2017 10:28
Calculate size of uicollectionview
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
guard let dummyCell: OABSubCategoryMenuCell = Bundle.main.loadNibNamed("OABSubCategoryMenuCell", owner: self, options: nil)?.first as? OABSubCategoryMenuCell else {
return CGSize.zero
}
dummyCell.setContent(subCat: self.subCategories[indexPath.row], isSelected: false)
dummyCell.setNeedsLayout()
dummyCell.layoutIfNeeded()
let size: CGSize = dummyCell.contentView.systemLayoutSizeFitting(UILayoutFittingCompressedSize)
return size
}
: public enum BundleData
{
ItemPosition,
UserName
}
public static class Extensions
{
public static int GetBundleData(this Intent intent, BundleData data, int defaultValue)
=> intent.Extras.GetInt(data.ToString(), defaultValue);
//
// THTriangleView.swift
// thirsty
//
// Created by Werck Ayrton on 23/06/2015.
// Copyright (c) 2015 Nyu Web Developpement. All rights reserved.
//
import UIKit
//
// RatingSession.swift
//
//
// Created by Werck Ayrton on 19/01/2016.
//
import UIKit