Skip to content

Instantly share code, notes, and snippets.

@glm4
glm4 / DynamicColorSchemeModifier.swift
Created February 23, 2024 15:45
Dynamic Appearance mode
View DynamicColorSchemeModifier.swift
// DynamicColorSchemeModifier.swift
import Foundation
import SwiftUI
struct DynamicColorSchemeModifier: ViewModifier {
@EnvironmentObject private var settingsStore: SettingsStore
// Allows to manage colorScheme independently for any view
@State private var colorScheme: ColorScheme?
@glm4
glm4 / PlaceholderTextView.swift
Created September 6, 2022 14:02
UITextView with placeholder capabilities
View PlaceholderTextView.swift
//
// PlaceholderTextView.swift
// talkative-iOS
//
// Created by German Lopez on 6/6/16.
// Copyright © 2016 Rootstrap Inc. All rights reserved.
//
import UIKit
View AppDelegate.swift
//
// AppDelegate.swift
// Adds observer to detect changes in the *rootViewController* of the main window.
//
import UIKit
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
@glm4
glm4 / lambda_function.rb
Last active September 16, 2019 16:29
AWS Lambda function to reboot EC2 instances. Ruby 2.5 Runtime. Execution Role needs: Allow: ec2:RebootInstances, Allow: ec2:DescribeInstances, Allow: ec2:DescribeTags
View lambda_function.rb
require 'aws-sdk-ec2'
# You can customize the lambda function in AWS console to use ENV variables or to receive
# the region, tags or any identification for your EC2 instances in the event message.
def lambda_handler(event:, context:)
ec2 = Aws::EC2::Resource.new(region: 'us-east-2')
instances = ec2.instances(filters: [
{
name: "tag:Role",
values: ["API"],
@glm4
glm4 / Gradients.swift
Created May 7, 2018 18:52
Swift helper methods to add gradients in views and/or layers
View Gradients.swift
import Foundation
import UIKit
public enum GradientDirection: Double {
case topToBottom = 90.0
case bottomToTop = 270.0
case leftToRight = 180.0
case rightToLeft = 0.0
}
@glm4
glm4 / EmailValidatorStringExtension.swift
Created May 7, 2018 18:47
Swift String Extension to validate emails conforming RFC 5322 Internet Message format
View EmailValidatorStringExtension.swift
extension String {
//Regex fulfill RFC 5322 Internet Message format
func isEmailFormatted() -> Bool {
let predicate = NSPredicate(format: "SELF MATCHES %@", "[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+(\\.[A-Za-z0-9!#$%&'*+/=?^_`{|}~-]+)*@([A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?\\.)+[A-Za-z0-9]([A-Za-z0-9-]*[A-Za-z0-9])?")
return predicate.evaluate(with: self)
}
}
@glm4
glm4 / CALayerExtension.swift
Created May 7, 2018 18:45
Helper methods to control CALayer i.e Interactive transitions
View CALayerExtension.swift
import Foundation
extension CALayer {
func pause() -> TimeInterval {
if #available(iOS 11, *) {
let pausedTime = convertTime(CACurrentMediaTime(), from: nil)
speed = 0
timeOffset = pausedTime
return pausedTime
@glm4
glm4 / UIScrollViewExtension.swift
Last active May 7, 2018 18:42
UIScrollView scrolling events detection
View UIScrollViewExtension.swift
extension UIScrollView {
//MARK: Normal Scrolling
func didScrollBeyondTop(withThreshold minimum: CGFloat = 0) -> Bool {
return contentOffset.y < -minimum
}
func didScrollBeyondBottom(withThreshold minimum: CGFloat = 0) -> Bool {
if contentOffset.y <= 0 { return false } //Fixes UIRefreshControll initial state
@glm4
glm4 / UIScrollViewExtension.swift
Created May 7, 2018 18:36
Swift UIScrollView focus on specific subview
View UIScrollViewExtension.swift
extension UIScrollView {
func scroll(toSubview view: UIView, animated: Bool = true) {
guard view.isDescendant(of: self) else { return }
let visibleRect = CGRect(origin: contentOffset,
size: frame.size)
let realRect = view.convert(view.frame, to: self
//View is out of scroll visible area
if !visibleRect.contains(realRect) || realRect.maxY <= 0 {