Skip to content

Instantly share code, notes, and snippets.

View axelrivera's full-sized avatar

Axel Rivera axelrivera

View GitHub Profile
@pteasima
pteasima / OnScroll.swift
Last active December 16, 2020 17:35
SwiftUI onScroll
import SwiftUI
import Combine
struct OnScroll: ViewModifier {
@Binding var offset: CGFloat
//we can have a version with a closure instead of the binding, but that triggers an infinite loop if content depends on the same Store
// var onOffset: (CGFloat) -> ()
func body(content: Content) -> some View {
return VStack {
@BasThomas
BasThomas / moving-average.swift
Last active April 4, 2023 10:16
Calculate a moving average in Swift (Swift 4.1)
extension Collection where Element == Int, Index == Int {
/// Calculates a moving average.
/// - Parameter period: the period to calculate averages for.
/// - Warning: the supplied `period` must be larger than 1.
/// - Warning: the supplied `period` should not exceed the collection's `count`.
/// - Returns: a dictionary of indexes and averages.
func movingAverage(period: Int) -> [Int: Float] {
precondition(period > 1)
precondition(count > period)
@khorbushko
khorbushko / PHPhotoLibrary+SaveImage
Created December 29, 2016 09:27
PHPhotoLibrary+SaveImage - save image with Photos Framework swift 3
import UIKit
import Photos
extension PHPhotoLibrary {
// MARK: - PHPhotoLibrary+SaveImage
// MARK: - Public
func savePhoto(image:UIImage, albumName:String, completion:((PHAsset?)->())? = nil) {
func save() {
@klaas
klaas / UIView+Orientation.swift
Last active January 31, 2016 21:29
View orientation
// Swift version of: https://gist.github.com/smileyborg/a5d1355773ad2ba6bb1e
public enum ViewOrientation {
case Portrait
case Landscape
}
extension UIView {
public class func viewOrientationForSize(size:CGSize) -> ViewOrientation {
return (size.width > size.height) ? .Landscape : .Portrait
@schickling
schickling / Rakefile
Last active January 31, 2024 23:00
Activerecord without Rails
require "active_record"
namespace :db do
db_config = YAML::load(File.open('config/database.yml'))
db_config_admin = db_config.merge({'database' => 'postgres', 'schema_search_path' => 'public'})
desc "Create the database"
task :create do
ActiveRecord::Base.establish_connection(db_config_admin)
@andreyvit
andreyvit / tmux.md
Created June 13, 2012 03:41
tmux cheatsheet

tmux cheat sheet

(C-x means ctrl+x, M-x means alt+x)

Prefix key

The default prefix is C-b. If you (or your muscle memory) prefer C-a, you need to add this to ~/.tmux.conf:

remap prefix to Control + a

@evanbeard
evanbeard / registrations_controller.rb
Created May 11, 2012 19:53 — forked from jwo/registrations_controller.rb
API JSON authentication with Devise
class Api::RegistrationsController < Api::BaseController
respond_to :json
def create
user = User.new(params[:user])
if user.save
render :json=> user.as_json(:auth_token=>user.authentication_token, :email=>user.email), :status=>201
return
else
@jwo
jwo / registrations_controller.rb
Created September 30, 2011 23:11
API JSON authentication with Devise
class Api::RegistrationsController < Api::BaseController
respond_to :json
def create
user = User.new(params[:user])
if user.save
render :json=> user.as_json(:auth_token=>user.authentication_token, :email=>user.email), :status=>201
return
else