Skip to content

Instantly share code, notes, and snippets.

View alexbaramilis's full-sized avatar

Alexandros Baramilis alexbaramilis

View GitHub Profile
@alexbaramilis
alexbaramilis / updateContentViewHeight.swift
Last active December 5, 2018 18:38
Update Content View Height
override func viewWillLayoutSubviews() {
super.viewWillLayoutSubviews()
contentViewHeight.constant = view.bounds.height > 504 ? view.bounds.height : 504
}
@alexbaramilis
alexbaramilis / namespacing1.swift
Last active December 5, 2018 19:32
Namespacing with enums 1
struct API1 {
static let BaseURL = "api.airvisual.com/v2/nearest_city?lat={{LATITUDE}}&lon={{LONGITUDE}}&key={{YOUR_API_KEY}}"
static let Key = "your_api_key"
}
enum API2 {
static let BaseURL = "api.airvisual.com/v2/nearest_city?lat={{LATITUDE}}&lon={{LONGITUDE}}&key={{YOUR_API_KEY}}"
static let Key = "your_api_key"
}
@alexbaramilis
alexbaramilis / namespacing2.swift
Created December 5, 2018 19:39
Namespacing with enums 2
enum API {
enum AirVisual {
static let BaseURL = "api.airvisual.com/v2/nearest_city?lat={{LATITUDE}}&lon={{LONGITUDE}}&key={{YOUR_API_KEY}}"
static let Key = "you_api_key"
}
enum PropellerAir {
static let BaseURL = "https://open.propellerhealth.com/prod/forecast?latitude={{LATITUDE}}&longitude={{LONGITUDE}}"
}
}
@alexbaramilis
alexbaramilis / dependecyInjectionStoryboard.swift
Last active December 6, 2018 18:24
Dependency Injection with Storyboard
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func applicationDidFinishLaunching(_ application: UIApplication) {
if let navigationController = window?.rootViewController as? UINavigationController,
let breatherViewController = navigationController.topViewController as? BreatherViewController {
// Property and initialiser injection in one line
breatherViewController.dataSource = BreatherDataSource(with: NetworkClient())
}
@alexbaramilis
alexbaramilis / TappableButtonInPlaygroundWithRxCocoa.swift
Created April 1, 2019 13:27
Playground with tappable button bound with RxCocoa
import UIKit
import PlaygroundSupport
import RxSwift
import RxCocoa
let signUpButton = UIButton()
signUpButton.setTitle("Tap me!", for: .normal)
signUpButton.sizeToFit()
let didTapSignUp = PublishSubject<Void>()
@alexbaramilis
alexbaramilis / String+indicesOfNumbers.swift
Last active April 16, 2019 15:55
Swift String Extension for finding the indices of all the numbers in a string.
import Foundation
extension String {
/// Returns the indices of all the numbers in the string.
///
/// If a number is directly preceded by a dot/comma, the index of the dot/comma
/// will also be returned.
/// This accounts for potential numbers with decimal or thousands separators.
var indicesOfNumbers: [Int] {
var indices = [Int]()
@alexbaramilis
alexbaramilis / UILabel+setAttributedTextWithSubscripts.swift
Last active April 16, 2019 15:55
Swift UILabel Extension for setting attributedText with specified character indices in subscript.
import UIKit
extension UILabel {
/// Sets the attributedText property of UILabel with an attributed string
/// that displays the characters of the text at the given indices in subscript.
func setAttributedTextWithSubscripts(text: String, indicesOfSubscripts: [Int]) {
let font = self.font!
let subscriptFont = font.withSize(font.pointSize * 0.7)
let subscriptOffset = -font.pointSize * 0.3
let attributedString = NSMutableAttributedString(string: text,
@alexbaramilis
alexbaramilis / CityConditions.swift
Last active April 16, 2019 16:38
Breather models.
struct CityConditions {
let city: String
let weather: Weather
let pollution: Pollution
let asthma: Asthma
}
struct Weather {
let timestamp: String // timestamp: ex. "2018-12-04T18:00:00.000Z" (ISO 8601) (Z stands for UTC)
let iconCode: String // weather icon code: ex. "10n"
@alexbaramilis
alexbaramilis / CityConditionsSampleData.swift
Last active April 16, 2019 16:38
Breather models sample data extension.
extension CityConditions {
static func sampleData() -> CityConditions {
let weather = Weather(timestamp: "2019-04-16T11:00:00.000Z",
iconCode: "01d",
temperature: 5,
humidity: 36,
pressure: 1015,
windSpeed: 9.8,
windDirection: 300)
let pollution = Pollution(timestamp: "2019-04-16T08:00:00.000Z",
@alexbaramilis
alexbaramilis / PopulatingUIwithSampleData.swift
Last active April 16, 2019 13:53
Populating the Breather UI with sample data.
// MARK: - Properties
private let data = CityConditions.sampleData()
// MARK: - Actions
@IBAction func aqiStandardSegmentedControlValueChanged(_ sender: UISegmentedControl) {
updateAirQualityUI()
}