Skip to content

Instantly share code, notes, and snippets.

View juanarzola's full-sized avatar

Juan Arzola juanarzola

View GitHub Profile
@juanarzola
juanarzola / ModelContainer+runNonisolated.swift
Last active August 20, 2024 23:37
Convenience extension for running ModelActor code in the background
extension ModelContainer {
// non-throwing version
nonisolated func runNonisolated<ResultType, ActorType: InitWithModelContainer>(
action: @Sendable (_ actor: ActorType) async -> ResultType
) async -> ResultType {
let actor = ActorType(modelContainer: self)
let result = await action(actor)
return result
}
// throwing version
@juanarzola
juanarzola / swift-data-request.swift
Last active June 18, 2024 06:17
Proposed query API for SwiftData notifications (stream based)
// Our one super simple SwiftData Model
@Model
class Item {
...
}
// MARK: - TodayView
@MainActor
@juanarzola
juanarzola / updateLoop.swift
Last active June 8, 2024 17:31
ViewModel (or controller(!)) updateLoop pattern that keeps the model up-to-date in a task.
// MARK: - TodayView
@MainActor
struct TodayView: View {
@State private var viewModel = TodayViewModel()
@Environment(\.modelContext) private var modelContext
var body: some View {
HStack {
switch viewModel.content {
@juanarzola
juanarzola / SelectedBackgroundColorHackery.swift
Last active June 5, 2024 18:20
Workaround for lack of `listRowSelectedBackground` or some way of defining an adaptive background style with your own color
struct ContentView: View {
var body: some View {
List {
SomeRow()
// set the background of the row
.listRowBackground(ListRowBackground())
// .listRowSelectedBackground(ListRowSelectedBackground())
// Oh no, `listRowSelectedBackground` above is not a real API, so list cells don't highlight when selected anymore.
// What do we do now?
@juanarzola
juanarzola / SectionBuilderSample.swift
Last active June 5, 2024 18:21
An attempt to reduce unnecessary computations for section building in the body of a View. Ultimately I had to modify it (See comments)
// slow version
struct MyFancyList: View {
@Query(FetchDescriptors.fancyListItems) var items
var sections: [ListSection] = []
var body: some View {
// it's a bad idea to build sections here, as that can execute for all sorts of updates to the view (there's more going on in this view in the original code)
SomethingRenderingSections(ListSection.sections(for: items, editMode: editMode, searchText: searchText)
.fullScreenCover(isPresented: $someFullScreenThingVisible) { SomethingThatCovers() }
@juanarzola
juanarzola / Ents.lua
Last active June 5, 2024 18:22
For ScriptExtender with Baldur's Gate 3: Output all Entities that match a component name and display name (empty string matches all)
-- _Ents: Filters current entities in the engine, matching their component names and/or display names.
--
-- @param pattern: filter by entities with component names matching pattern. Empty string "" matches all.
-- @param displayNamePattern: filter by entities with displayName (where displayName is a heuristic determined
-- from various components) matching `displayNamePattern`. Empty string "" matches all.
-- @param withComponents: include component names in the output result
--
_Ents = function(pattern, displayNamePattern, withComponents)
local entities = Ext.Entity.GetAllEntities()
local result = {}
@juanarzola
juanarzola / ChangeType.cs
Last active October 6, 2016 08:44
UniRx observable wrappers for Entitas entity and group events
using System;
namespace Entitas {
/** Used in the *AnyChangeObservable methods to observe multiple change types */
[Flags]
public enum ChangeType : short{
Addition = 1 << 0,
Replacement = 1 << 1,
Removal = 1 << 2,
@juanarzola
juanarzola / gist:2161782
Created March 22, 2012 18:52
Evernote Food 1.0 sample ENML
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-export SYSTEM "http://xml.evernote.com/pub/evernote-export2.dtd">
<en-export export-date="20120322T185030Z" application="Evernote" version="Evernote Mac 3.0.6 (221382)">
<note><title>1.0 meal</title><content><![CDATA[<?xml version='1.0' encoding='utf-8'?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note style="margin:10px;background-color:rgb(86, 81, 78);font-size:13px;color:rgb(80, 80, 80);text-align:center;font-family:Helvetica;">
<div style="font-size:13px;color:rgb(80, 80, 80);text-align:center;font-family:Helvetica;x-evernote:food-meal;">
<div style="max-width:600px;padding-bottom:3.125%;background-color:#fff;box-shadow:0 3px 15px rgba(0,0,0,.5);-webkit-box-shadow:0 3px 15px rgba(0,0,0,.5);margin:20px auto;">
<div style="margin-left: 3.334%;text-align:left;">
<div style="padding-top:3.45%;padding-right:3.45%;">
@juanarzola
juanarzola / person.js
Created October 15, 2011 18:23
Common JSON data
{
"firstName": "John",
"middleName": "M",
"lastName": "Appleseed",
"age": 30,
"addresses":[
{
"streetAddress": "222 North Mountain Rd.",
"city": "Redwood City",
"state": "CA",
for(UILabel* label in self.allLabels){
[label setText:NSLocalizedString(label.text, @"")];
}