Skip to content

Instantly share code, notes, and snippets.

struct Parent: View {
@State var count: Int = 0
var body: some View {
return VStack {
ImageLoadingView(url: URL(string: "https://github.com/recurser/exif-orientation-examples/blob/9c4ccfaea6bfd434ac1c4bb0750ac6fc5848a5f4/Landscape_8.jpg?raw=true")!)
Text("counter: \(count)")
}
.onAppear {
Timer.scheduledTimer(withTimeInterval: 5, repeats: true) { (timer) in
self.count += 1
@davbeck
davbeck / DisplayLink.swift
Created July 20, 2019 18:42
A Combine Publisher version of CADisplayLink.
/// The result of a binary search.
struct BinarySearchResult<Index> {
/// The index for the searched key.
///
/// Use this to either insert new values while maintaining the sort order of the collection, or to lookup the value if `isPresent` is true.
var index: Index
/// Whether the key was found in the collection.
///
/// When performing a binary search, even if the value is not found in the collection, an index is returned which can be used to insert a new item at the correct location.
// adapted from https://github.com/raywenderlich/swift-algorithm-club/tree/master/Binary%20Search
/// The result of a binary search.
struct BinarySearchResult<Index> {
/// The index for the searched key.
///
/// Use this to either insert new values while maintaining the sort order of the collection, or to lookup the value if `isPresent` is true.
var index: Index
/// Whether the key was found in the collection.
@davbeck
davbeck / README.md
Created June 19, 2019 22:07
A wrapper for UICollectionViewDiffableDataSource that also handles updates to data

UICollectionViewDiffableDataSource does an excellent job of handling changes to data and updating the items accordingly. However, there seems to be no good way to handle updates to existing items. If you follow the samples that Apple provides and define Equatable and Hashable to use an id instead of the complete models value, value changes won't cause the collection view to update those cells. If you leave Equatable as it should be and have it compare all values of a model, the cell will update, but it will completely replace the old cell, causing an undesirable "flash".

UICollectionViewComparableDataSource wraps a UICollectionViewDiffableDataSource and will check for items that have been updated, but not removed or added.

This allows us to make updates to a cell without completely reloading it. This is especially usefull if your cells have some sort of temporary state.

This is just an expirement. Use at your own risk.

import Foundation
import SQLite3
public enum SQLiteError: Error, LocalizedError {
case sqlite(code: Int32, message: String?)
case invalidDatabase
case invalidStatement
case invalidUTF8String
@davbeck
davbeck / JWT.swift
Last active March 1, 2019 21:47
A basic JWT parser for Swift that doesn't do any validation.
import Foundation
extension Data {
/// JWT strips the padding off the end of the base64 string
fileprivate init?(jwtPart: String) {
var paddingCount = 4 - jwtPart.count % 4
if paddingCount == 4 {
paddingCount = 0
}
// original:
Query("SELECT * FROM example WHERE e_uuid = $1;", idA)
// or
Query("SELECT * FROM example WHERE e_uuid = $1;", [idA])
// manual interpolation test:
var query = Query(stringInterpolation: {
var temp = Query.StringInterpolation(totalLiteralSize: 40, interpolationCount: 1)
temp.appendLiteral("SELECT * FROM example WHERE e_uuid = ")
temp.appendInterpolation(idA)
!function(t){var n={};function r(e){if(n[e])return n[e].exports;var i=n[e]={i:e,l:!1,exports:{}};return t[e].call(i.exports,i,i.exports,r),i.l=!0,i.exports}r.m=t,r.c=n,r.d=function(t,n,e){r.o(t,n)||Object.defineProperty(t,n,{configurable:!1,enumerable:!0,get:e})},r.n=function(t){var n=t&&t.__esModule?function(){return t.default}:function(){return t};return r.d(n,"a",n),n},r.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},r.p="",r(r.s=125)}([function(t,n,r){var e=r(2),i=r(21),o=r(12),u=r(13),c=r(18),a=function(t,n,r){var f,s,l,h,v=t&a.F,p=t&a.G,d=t&a.S,g=t&a.P,y=t&a.B,m=p?e:d?e[n]||(e[n]={}):(e[n]||{}).prototype,b=p?i:i[n]||(i[n]={}),x=b.prototype||(b.prototype={});for(f in p&&(r=n),r)l=((s=!v&&m&&void 0!==m[f])?m:r)[f],h=y&&s?c(l,e):g&&"function"==typeof l?c(Function.call,l):l,m&&u(m,f,l,t&a.U),b[f]!=l&&o(b,f,h),g&&x[f]!=l&&(x[f]=l)};e.core=i,a.F=1,a.G=2,a.S=4,a.P=8,a.B=16,a.W=32,a.U=64,a.R=128,t.exports=a},function(t,n,r){var e=r(4);t.exports=function(t){if(!e(t))throw TypeError(t+" is not
import Foundation
import JavaScriptCore
/// Used to lookup our Bundle.
private class MomentBundleClass: NSObject {}
/// A wrapper around a moment.js object.
public struct Moment {