Skip to content

Instantly share code, notes, and snippets.

View Plnda's full-sized avatar

Jason Meulenhoff Plnda

View GitHub Profile
struct ParallaxModifier: ViewModifier {
@ObservedObject var manager: MotionManager = MotionManager.shared
var magnitude: Double
func body(content: Content) -> some View {
content
.offset(x: CGFloat(manager.roll * magnitude), y: CGFloat(manager.pitch * magnitude))
}
}
@Plnda
Plnda / MotionManager.swift
Last active September 1, 2020 11:12
Motion
class MotionManager: ObservableObject {
@Published var pitch: Double = 0.0
@Published var roll: Double = 0.0
private var manager: CMMotionManager
static var shared = MotionManager()
init() {
@Plnda
Plnda / Check.cs
Created August 12, 2020 09:08
Tilecheck
public string GetGroundTypeFromPosition(Vector3 position)
{
if(sortedLayers == null)
{
sortedLayers = layers
.ToArray()
.OrderByDescending(x => x.GetComponent<TilemapRenderer>().sortingOrder);
}
foreach(var tileMap in sortedLayers)
Dependencies {
Service { _ in Bar.init() }
Service { Foo(bar: $0.get()) }
Service(.global) { _ in ViewModel.init() }
}.build()
import UIKit
protocol WorkProtocol: class {
func work()
}
class ViewModel {
weak var delegate: WorkProtocol?
}
@propertyWrapper
struct Injected<Service> {
typealias DelayedInjection = () -> Service
var service: Service?
var delayed: DelayedInjection?
init() {
delayed = { Dependencies.main.resolve() }
public extension Dependencies {
/// Create a overridable main resolver
fileprivate static var main = Dependencies()
func get<Service>() -> Service {
return resolve()
}
/// Function builder that accepts a single or multiple services
/// Private extension
private extension Dependencies {
/// Resolve a serice based on its ObjectIdentifier
func resolve<Service>() -> Service {
var service = self.factories[ObjectIdentifier(Service.self)]!
guard let instance = service.instance, service.cycle == .global else {
public struct Service {
public enum LifeCycle {
case global
case oneOf
}
/// Holds the lifecycle of the current service
public var cycle: LifeCycle
/// Public class
public class Dependencies {
/// Will hold all our factories
public var factories: [ObjectIdentifier: Service] = [:]
/// Make sure that our init will stay private so they need to use the provider functionBuilder
private init() { }
/// Make sure that all the dependencies are removed when we deinit