Skip to content

Instantly share code, notes, and snippets.

View bmoliveira's full-sized avatar
🏠

Bruno Oliveira bmoliveira

🏠
View GitHub Profile
@bmoliveira
bmoliveira / Android GCM Token fetcher
Last active August 29, 2015 14:22
Android Helper to get GCM Token from Play services, with fluent api callback registration
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.support.annotation.Nullable;
import android.util.Log;
import android.util.Pair;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.gcm.GoogleCloudMessaging;
@bmoliveira
bmoliveira / Hackaroos
Created July 2, 2015 16:36
Hacks to get keyboard and status_bar
static NSString *encodeText(NSString *string, int key)
{
NSMutableString *result = [[NSMutableString alloc] init];
for (int i = 0; i < [string length]; i++)
{
unichar c = [string characterAtIndex:i];
c += key;
[result appendString:[NSString stringWithCharacters:&c length:1]];
}
return result;
@bmoliveira
bmoliveira / Reusable.swift
Last active May 10, 2016 10:35
Reusable Protocol
protocol ReusableCell: AnyObject {
static func reuseIdentifier() -> String?
static func uiNibFromClass() -> UINib?
}
@bmoliveira
bmoliveira / ReusableUICollectionViewCell.swift
Last active May 10, 2016 10:29
Reusable UICollectionViewCell
extension ReusableCell where Self: UICollectionViewCell {
static func uiNibFromClass() -> UINib? {
return UINib(nibName: Self.className, bundle: nil)
}
static func reuseIdentifier() -> String? {
return Self.className
}
}
@bmoliveira
bmoliveira / UICollectionView+Reusable.swift
Last active May 10, 2016 14:23
UICollectionView reusable
extension UICollectionView {
func registerCell<T: ReusableCell>(cellType: T.Type) {
if let identifier = T.reuseIdentifier() {
self.registerNib(T.uiNibFromClass(), forCellWithReuseIdentifier: identifier)
}
}
func dequeueReusableCell<T: UICollectionViewCell>(index: Int) -> T {
return dequeueReusableCell(NSIndexPath(forItem: index, inSection: 0))
@bmoliveira
bmoliveira / ExampleCell+ExampleViewController.swift
Last active May 10, 2016 10:36
ExampleCell + ExampleViewController
class ExampleCell: UICollectionViewCell, ReusableCell {
func updateCellUI() {
print("Here you should do something")
}
}
class ExampleViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
@bmoliveira
bmoliveira / String+Localizable.swift
Last active May 10, 2016 10:38
String Localization extension
public extension String {
/**
Swift 2 friendly localization syntax, replaces NSLocalizedString
- Returns: The localized string.
*/
func localized() -> String {
if let path = NSBundle.mainBundle().pathForResource(Localize.currentLanguage(), ofType: "lproj"), bundle = NSBundle(path: path) {
return bundle.localizedStringForKey(self, value: nil, table: nil)
}
else if let path = NSBundle.mainBundle().pathForResource(LCLBaseBundle, ofType: "lproj"), bundle = NSBundle(path: path) {
public class Localize: NSObject {
/**
List available languages
- Returns: Array of available languages.
*/
public class func availableLanguages() -> [String] {
return NSBundle.mainBundle().localizations
}
/**
class ExampleViewController: UIViewController {
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
title = "Example title".localized()
collectionView.registerCell(ExampleCell)
}
}
Localization files example:
protocol SegueHandlerType {
associatedtype SegueIdentifier: RawRepresentable
}
// notice the cool use of where here to narrow down
// what could use these methods.
extension SegueHandlerType where Self: UIViewController,
SegueIdentifier.RawValue == String
{