Skip to content

Instantly share code, notes, and snippets.

View aclima93's full-sized avatar
💭
Including Bugs

António Lima aclima93

💭
Including Bugs
View GitHub Profile
@aclima93
aclima93 / swiftui-state.markdown
Created March 10, 2023 09:56 — forked from gdavis/swiftui-state.markdown
Swift UI State Management

Swift UI State Management

Storing state for Child View Models

SwiftUI is all about state, and maintaining that state can be rather difficult when you have more than a few properties stored as @State for your view. It is common practice to use a view model to store the state outside of the view, since our views are constantly recreated as our state changes. Keeping what state you want in memory can provide a challenge, much more so than it may seem at first glance.

With SwiftUI 2, using @StateObject will keep a view model from being recreated with each rendering of a view's body. If the parent view is creating the view model, and then passing that down to the child view, that view model will also be recreated with each rendering of that parent view body.

struct ParentView: View {
  var body: some View {
@aclima93
aclima93 / git-pr-title.sh
Last active August 22, 2023 16:38
✨ pretty print ✨ the PR title from current git branch
#! /bin/bash
# datetime
datetime=$(date +%d/%m/%Y)
# git branch name
branch_name=$(git rev-parse --abbrev-ref HEAD)
# for branches that follow the `<dir>/<ticket|NOJIRA>-<title>` naming convention and
# regex `(?<branch_dir>(\w+\/)+)?(?<ticket>(\w+-\d+)|(NOJIRA))[-//](?<title>(\w+-?)+)`
@aclima93
aclima93 / LeakCheckTestCase.swift
Last active August 10, 2021 08:28
Automated detection of memory leaks in Unit Tests
class LeakCheckTestCase: XCTestCase {
private var excludedProperties = [String: Any]()
private var weakReferences = NSMapTable<NSString, AnyObject>.weakToWeakObjects()
// MARK: - SetUp
override func setUpWithError() throws {
try super.setUpWithError()
@aclima93
aclima93 / SwiftConfigurationGenerator.swift
Created August 6, 2020 00:02
// A build phase script for fetching, validating and generating a Swift wrapper over configuration files in iOS projects
#!/usr/bin/env xcrun --sdk macosx swift
// A build phase script for fetching, validating and generating a Swift wrapper over configuration files in iOS projects
// Source: https://github.com/pgorzelany/SwiftConfiguration
import Foundation
public struct ParsedArguments {
public let configurationPlistFilePath: String
public let outputFilePath: String
}
#!/usr/bin/env bash
# default values
SRC=${1:-_site/}
DST=${2:-docs/}
USER_PAGE=${3:-../aclima93.github.io/}
BLOG_PAGE=${3:-../Blog/docs/}
# copy the contents from $SRC (_site/) to $DST (doc/) and serve that over at github-pages
rm -rf $DST;

Keybase proof

I hereby claim:

  • I am aclima93 on github.
  • I am aclima93 (https://keybase.io/aclima93) on keybase.
  • I have a public key ASDVtyOtAN-HfNRgZVWoyoP053JtAG1EMvqctW440zMDzAo

To claim this, I am signing this object:

@aclima93
aclima93 / add_poi_to_ar_scene.swift
Created July 10, 2018 17:06
ARKit & CoreLocation + POI + AR Scene
func addPOIToARScene(_ poi: PointOfInterest) {
// create node
let location = CLLocation(latitude: poi.latitude, longitude: poi.longitude)
let text = poi.title
let annotationNode = LocationTextAnnotationNode(location: location, image: UIImage(named: "LocationMarker")!, text: text)
// add node to AR scene
sceneLocationView.addLocationNodeWithConfirmedLocation(locationNode: annotationNode)
}
@aclima93
aclima93 / LocationTextAnnotationNode.swift
Last active July 10, 2018 17:11
ARKit & CoreLocation + POI + AR Scene
open class LocationTextAnnotationNode: LocationNode {
// image and text that are displayed by the child nodes
public let image: UIImage
public let text: String
// child nodes
public let imageAnnotationNode: SCNNode
public let textAnnotationNode: SCNNode
@aclima93
aclima93 / nearby_pois.swift
Last active August 21, 2018 14:57
ARKit & CoreLocation + Getting Nearby POIs
var locationManager: CLLocationManager!
var latestLocation: CLLocation?
var pois: [PointOfInterest]!
func getPOIs() {
// formulate natural language query for nearby POIs
let request = MKLocalSearch.Request()
request.naturalLanguageQuery = "Restaurants" // or any other thing you might be interested in
request.region = MKCoordinateRegion(center: (latestLocation?.coordinate)!,
@aclima93
aclima93 / ar_poi_selection.swift
Last active August 21, 2018 14:58
ARKit & CoreLocation + POI + AR Interaction
var sceneLocationView: SceneLocationView!
var pois: [PointOfInterest]!
var locationAnnotationNode2POI: [LocationTextAnnotationNode: PointOfInterest]!
var selectedPOI: PointOfInterest?
func setupARScene() {
// create AR scene
sceneLocationView = SceneLocationView()
view.addSubview(sceneLocationView)