Skip to content

Instantly share code, notes, and snippets.

@glm4
glm4 / MessageBubbleShape.swift
Created April 2, 2024 17:46
Chat Message Bubbles Styles using SwiftUI Shapes
//
// MessageBubblePath.swift
// Sandbox
//
// Created by German on 2/4/24.
//
import SwiftUI
internal enum BubbleStyle: String, CaseIterable {
@glm4
glm4 / DynamicColorSchemeModifier.swift
Created February 23, 2024 15:45
Dynamic Appearance mode
// 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
//
// PlaceholderTextView.swift
// talkative-iOS
//
// Created by German Lopez on 6/6/16.
// Copyright © 2016 Rootstrap Inc. All rights reserved.
//
import UIKit
//
// 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
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
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
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
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
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
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 {