<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ivan Chernoknizhnikov</title>
    <description>The latest articles on DEV Community by Ivan Chernoknizhnikov (@treamz).</description>
    <link>https://dev.to/treamz</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4079883%2Fadb7b964-1de8-4926-84e3-bb733feb631b.jpg</url>
      <title>DEV Community: Ivan Chernoknizhnikov</title>
      <link>https://dev.to/treamz</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/treamz"/>
    <language>en</language>
    <item>
      <title>Offline-first aquarium log in Flutter with Drift and Supabase</title>
      <dc:creator>Ivan Chernoknizhnikov</dc:creator>
      <pubDate>Sun, 29 Nov 2026 09:01:58 +0000</pubDate>
      <link>https://dev.to/treamz/offline-first-aquarium-log-in-flutter-with-drift-and-supabase-f8h</link>
      <guid>https://dev.to/treamz/offline-first-aquarium-log-in-flutter-with-drift-and-supabase-f8h</guid>
      <description>&lt;p&gt;This post is about the data model behind my aquarium app: readings where a&lt;br&gt;
blank field never turns into a zero, free ammonia worked out from pH and&lt;br&gt;
temperature, one stored unit per parameter, and a local Drift database that&lt;br&gt;
syncs to Supabase through an outbox. The app is&lt;br&gt;
&lt;a href="https://apps.apple.com/us/app/aquarium-log-tracker/id6804384133" rel="noopener noreferrer"&gt;Aquarium - Log Tracker&lt;/a&gt;,&lt;br&gt;
a free iPhone logbook for water tests, written in Flutter. What it does for&lt;br&gt;
fishkeepers is in &lt;a href="https://treamz.me/blog/aquarium-water-test-log-app" rel="noopener noreferrer"&gt;the user-facing post&lt;/a&gt;;&lt;br&gt;
this one is the code.&lt;/p&gt;
&lt;h2&gt;
  
  
  Blank is not zero
&lt;/h2&gt;

&lt;p&gt;A water test is a handful of numbers typed standing at a tank, often with wet&lt;br&gt;
hands, and nobody tests everything every time. So the first real decision was&lt;br&gt;
what an empty field means. The answer is nothing. There is no nullable value&lt;br&gt;
column: a parameter you didn't test produces no row at all.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;setting&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;settings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;_controllers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;setting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// An untouched field produces no reading at all. Never zero.&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;tryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;replaceAll&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;','&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'.'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;setState&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;_error&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'${setting.definition.displayName}: "$text" is not a number'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="n"&gt;entries&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ReadingDraft&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parameterKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;setting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;_units&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;setting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;read&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;unitsProvider&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unitFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;setting&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;definition&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The previous value sits in each field as &lt;code&gt;hintText&lt;/code&gt;, not as text, so it's a&lt;br&gt;
reference you can't save by accident.&lt;/p&gt;

&lt;p&gt;This matters most for rate of change. Each saved reading is compared with the&lt;br&gt;
latest reading of the same parameter, which is by construction the last time&lt;br&gt;
that thing was actually measured. The default reef rules allow alkalinity to&lt;br&gt;
move 0.5 dKH a day. Store a skipped test as zero and the next honest 8.4 dKH&lt;br&gt;
looks like the tank jumped 8.4 dKH overnight. Two related rules: no daily rate&lt;br&gt;
from readings less than 12 hours apart, because over an hour the difference is&lt;br&gt;
mostly the test kit, and no pH drift limit for planted tanks, where CO₂&lt;br&gt;
injection swings pH every day on purpose.&lt;/p&gt;

&lt;p&gt;Charts follow from the same model. Each parameter gets its own row and scale&lt;br&gt;
on a shared time axis, with points spaced by date and a dot on every real&lt;br&gt;
measurement. A month without a nitrate test is a long straight segment between&lt;br&gt;
two dots, not a dip to zero. That segment is still an interpolation the eye&lt;br&gt;
can read as data; making the dots obvious is the best answer I have.&lt;/p&gt;
&lt;h2&gt;
  
  
  Absence of a measurement is not a measurement
&lt;/h2&gt;

&lt;p&gt;Nulls do appear in one place: when rows are folded into per-session snapshots&lt;br&gt;
to work out where a new tank is in its nitrogen cycle. A snapshot holds&lt;br&gt;
nullable ammonia, nitrite, nitrate, pH, KH and temperature, and a null never&lt;br&gt;
counts as clear or present.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;ammoniaClear&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ammonia&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;ammonia&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nc"&gt;Cycling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;effectivelyZero&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;nitriteClear&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nitrite&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nitrite&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nc"&gt;Cycling&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;effectivelyZero&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;nitratePresent&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nitrate&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nitrate&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kt"&gt;bool&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;passesCompletionTest&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;ammoniaClear&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nitriteClear&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;nitratePresent&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// In Cycling.assess: the stage comes from the newest session that measured&lt;/span&gt;
&lt;span class="c1"&gt;// ammonia or nitrite, not simply the newest session.&lt;/span&gt;
&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;latest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;lastWhere&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ammonia&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nitrite&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="k"&gt;null&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;orElse&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A tank counts as cycled after two passing sessions in a row, because one clean&lt;br&gt;
result can be a misread. Nitrate is required so that "nothing was ever dosed"&lt;br&gt;
can't pass for "the bacteria are working". "Clear" means at or below 0.25 ppm,&lt;br&gt;
because my research notes flag a common hobby kit as reading about that on&lt;br&gt;
clean water.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;lastWhere&lt;/code&gt; exists because of a bug I found by looking at the screen, not&lt;br&gt;
with a test. After logging a single alkalinity reading, the cycling banner&lt;br&gt;
dropped back to "Setting up": the newest snapshot had no ammonia in it, and&lt;br&gt;
the code took that as the cycle going backwards. Stall detection had the other&lt;br&gt;
half of the bug. KH and pH are often tested on a different day from ammonia,&lt;br&gt;
so each check now uses the latest session that measured that particular thing.&lt;/p&gt;
&lt;h2&gt;
  
  
  Free ammonia from pH and temperature
&lt;/h2&gt;

&lt;p&gt;Kits report total ammonia. The seriously toxic part is un-ionised NH₃, and its&lt;br&gt;
share depends on pH and temperature, as the&lt;br&gt;
&lt;a href="https://www.epa.gov/caddis/ammonia" rel="noopener noreferrer"&gt;EPA's ammonia page&lt;/a&gt; explains. When the&lt;br&gt;
user has typed all three into the same session, the rest is arithmetic.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// pKa = 0.09018 + 2729.92 / T(K). At 25 C this gives ~9.25.&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;pKa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;temperatureC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="mf"&gt;0.09018&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;2729.92&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperatureC&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mf"&gt;273.15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;/// Fraction of total ammonia present as toxic, un-ionised NH3 (0..1).&lt;/span&gt;
&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;freeFraction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;ph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;temperatureC&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;pKa&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperatureC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;ph&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;freeAmmoniaPpm&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;totalAmmoniaPpm&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;ph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;required&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;temperatureC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;totalAmmoniaPpm&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;freeFraction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ph&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ph&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;temperatureC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;temperatureC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pKa expression is the same as in the Florida Department of Environmental&lt;br&gt;
Protection's&lt;br&gt;
&lt;a href="https://floridadep.gov/sites/default/files/unnh3sop.pdf" rel="noopener noreferrer"&gt;procedure for calculating un-ionized ammonia&lt;/a&gt;,&lt;br&gt;
which writes the constant as 0.0901821, credits Thurston et al., and gives it&lt;br&gt;
as valid from 0 to 50 °C and pH 6 to 10. At 25 °C it puts about 0.56% of total&lt;br&gt;
ammonia in the free form at pH 7 and 5.4% at pH 8, matching the table in that&lt;br&gt;
document. One test pins down why&lt;br&gt;
the feature exists: 0.5 ppm total ammonia is 0.0017 ppm free at pH 6.8 and&lt;br&gt;
24 °C, and 0.054 ppm at pH 8.3 and 26 °C. Same kit reading, about 33 times&lt;br&gt;
apart.&lt;/p&gt;

&lt;p&gt;The result appears only when the saved session has ammonia, pH and temperature&lt;br&gt;
together; it doesn't borrow last week's pH. The known limitation: the&lt;br&gt;
expression is for fresh water. The same document says the free fraction drops&lt;br&gt;
slightly as ionic strength rises and gives a separate correction for saline&lt;br&gt;
water, which the app doesn't apply, so in a reef tank it errs high. It&lt;br&gt;
calculates; it doesn't diagnose.&lt;/p&gt;
&lt;h2&gt;
  
  
  One stored unit per parameter
&lt;/h2&gt;

&lt;p&gt;Each reading keeps the value in the parameter's canonical unit and, beside it,&lt;br&gt;
exactly what the user typed and in which unit, so a conversion bug can't&lt;br&gt;
destroy the original observation. Nitrate is stored as the NO₃ ion, alkalinity&lt;br&gt;
in meq/L, salinity in ppt, temperature in °C. The display unit is a device&lt;br&gt;
setting, since it's about how this person reads their kit, not a fact about&lt;br&gt;
the tank. Grading uses the canonical value, so a reading in range stays in&lt;br&gt;
range whatever it's shown as.&lt;/p&gt;

&lt;p&gt;The ratios are where silent errors live. NO₃ is 4.427 times NO₃-N, so the same&lt;br&gt;
water reads 10 on one kit and 44 on another. Nitrite has its own ratio, 3.284.&lt;br&gt;
One meq/L is 2.8 dKH. An unknown unit throws instead of passing the number&lt;br&gt;
through, because an unconverted alkalinity is off by 2.8 times and still looks&lt;br&gt;
plausible on a chart. I caught myself quoting nitrate's 4.4 on the nitrite row,&lt;br&gt;
on a screen whose whole job is preventing unit mistakes. Both figures now come from&lt;br&gt;
constants pinned by tests.&lt;/p&gt;
&lt;h2&gt;
  
  
  Drift as the source of truth, with an outbox
&lt;/h2&gt;

&lt;p&gt;The UI reads only from Drift. Every write goes into its table and into an&lt;br&gt;
outbox table in the same transaction, so nothing appears on screen without&lt;br&gt;
also being queued for the server.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;transaction&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...the session row and its outbox entry first, then for each reading:&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;into&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_db&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readings&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;ReadingsCompanion&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;insert&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;tankId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;draft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tankId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;sessionId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Value&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;sessionId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;parameterKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parameterKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;valueCanonical&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;canonical&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;enteredValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;enteredUnit&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;entry&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;measuredAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;draft&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;measuredAt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;updatedAt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;stamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;_outbox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enqueue&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SyncEntity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;readings&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;entityId&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;op&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;SyncOp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upsert&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="n"&gt;payload&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="cm"&gt;/* the same row as JSON */&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The outbox stores the payload rather than re-reading the row later, so an&lt;br&gt;
edit can't change what an earlier queued write meant. IDs are UUID v7 made on&lt;br&gt;
the device, so a reading can reference a tank that hasn't synced yet. Deletes&lt;br&gt;
set &lt;code&gt;deleted_at&lt;/code&gt;, because a delta sync can't express a row that stopped&lt;br&gt;
existing.&lt;/p&gt;

&lt;p&gt;The sync engine is written against a small &lt;code&gt;SyncRemote&lt;/code&gt; interface and tested&lt;br&gt;
against a fake, on an in-memory SQLite database rather than a mock. It pushes&lt;br&gt;
before it pulls, parents before children. The pull asks for rows with&lt;br&gt;
&lt;code&gt;updated_at&lt;/code&gt; after the stored cursor minus two seconds, because a transaction&lt;br&gt;
that began before the cursor can commit after it. The cursor comes from server&lt;br&gt;
rows, stamped by a Postgres trigger, never from the device clock, and a pulled&lt;br&gt;
row is skipped while the same row has an unpushed local edit. Drift&lt;br&gt;
&lt;a href="https://drift.simonbinder.eu/guides/datetime-migrations/" rel="noopener noreferrer"&gt;reads DateTimes back in local time&lt;/a&gt;&lt;br&gt;
by default, so I set &lt;code&gt;store_date_time_values_as_text&lt;/code&gt;, which keeps UTC as UTC.&lt;/p&gt;

&lt;p&gt;What went wrong:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Against a project that took fourteen seconds to answer auth, eight changes
were dead-lettered and shown as refused by the server. They hadn't been.
Each timeout counted as an attempt across launches and resumes, and after
five the tank was abandoned; its parameter settings then pointed at a tank
the server had never seen, row level security refused them, and they went
too. Now being offline never uses up attempts, a pass stops before anything
depending on an abandoned row, and dead letters have a "Try again".&lt;/li&gt;
&lt;li&gt;Sync only ran while the account screen was open, because only that screen
watched the provider. It's now watched at the app root and invalidated on
resume.&lt;/li&gt;
&lt;li&gt;The tank screen kept showing "not tested yet" after a test was logged,
until a restart. The query watched only the settings table but attached the
last reading from another one; Drift's
&lt;a href="https://drift.simonbinder.eu/sql_api/custom_queries/" rel="noopener noreferrer"&gt;&lt;code&gt;readsFrom&lt;/code&gt;&lt;/a&gt; fixed
it. The tests missed it because they called &lt;code&gt;.first&lt;/code&gt; after writing. The new
one subscribes first.&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;
  
  
  Row level security, briefly
&lt;/h2&gt;

&lt;p&gt;Supabase only holds a copy for people who sign in, and row level security is&lt;br&gt;
the real boundary; the publishable key in the app authorises nothing on its&lt;br&gt;
own. Tanks carry an &lt;code&gt;owner_id&lt;/code&gt;, and a &lt;code&gt;tank_members&lt;/code&gt; table that every policy&lt;br&gt;
already reads through means sharing can come later without a migration. Every&lt;br&gt;
child table carries &lt;code&gt;tank_id&lt;/code&gt;, so one pair of policies covers all of them:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;do&lt;/span&gt; $$
&lt;span class="k"&gt;declare&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="kt"&gt;text&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;begin&lt;/span&gt;
  &lt;span class="n"&gt;foreach&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;
    &lt;span class="s1"&gt;'tank_parameter_config'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'test_sessions'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'readings'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'livestock'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="s1"&gt;'task_templates'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'task_events'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'journal_entries'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'photos'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s1"&gt;'cycle_logs'&lt;/span&gt;
  &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt;
    &lt;span class="n"&gt;execute&lt;/span&gt; &lt;span class="nf"&gt;format&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$f&lt;/span&gt;$
      &lt;span class="k"&gt;create&lt;/span&gt; &lt;span class="n"&gt;policy&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nv"&gt;$s_select&lt;/span&gt; &lt;span class="k"&gt;on&lt;/span&gt; &lt;span class="n"&gt;public&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="nv"&gt;$s&lt;/span&gt;
      &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="k"&gt;to&lt;/span&gt; &lt;span class="n"&gt;authenticated&lt;/span&gt;
      &lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;tank_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;any&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;select&lt;/span&gt; &lt;span class="n"&gt;private&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;accessible_tank_ids&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nv"&gt;$f&lt;/span&gt;$&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="c1"&gt;-- plus a matching %1$s_write policy using private.can_edit_tank(tank_id)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="n"&gt;loop&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt; $$&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The helpers are &lt;code&gt;security definer&lt;/code&gt; functions in a &lt;code&gt;private&lt;/code&gt; schema with an&lt;br&gt;
empty &lt;code&gt;search_path&lt;/code&gt;, revoked from the API roles, which also breaks the&lt;br&gt;
infinite recursion of a &lt;code&gt;tank_members&lt;/code&gt; policy that queries &lt;code&gt;tank_members&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;auth.uid()&lt;/code&gt; is wrapped as &lt;code&gt;(select auth.uid())&lt;/code&gt;, as the&lt;br&gt;
&lt;a href="https://supabase.com/docs/guides/database/postgres/row-level-security" rel="noopener noreferrer"&gt;Supabase RLS docs&lt;/a&gt;&lt;br&gt;
recommend. A pgTAP suite simulates two users by setting &lt;code&gt;request.jwt.claims&lt;/code&gt;,&lt;br&gt;
and an integration test runs the real sync code against a local Supabase stack&lt;br&gt;
and checks that a second account sees none of it.&lt;/p&gt;

&lt;p&gt;What the app does for the people who use it: &lt;a href="https://treamz.me/blog/aquarium-water-test-log-app" rel="noopener noreferrer"&gt;A free aquarium water test log app for iPhone&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How do you calculate free ammonia from pH and temperature?
&lt;/h3&gt;

&lt;p&gt;Compute pKa = 0.09018 + 2729.92 / T with T in kelvin, then the free fraction&lt;br&gt;
= 1 / (1 + 10^(pKa − pH)), and multiply total ammonia by it. At 25 °C pKa is&lt;br&gt;
about 9.25, so roughly 0.56% of total ammonia is un-ionised NH₃ at pH 7 and&lt;br&gt;
5.4% at pH 8. The expression is for fresh water; in seawater the real fraction&lt;br&gt;
is slightly lower.&lt;/p&gt;

&lt;h3&gt;
  
  
  Should an untested value be stored as null or zero?
&lt;/h3&gt;

&lt;p&gt;Neither. In Aquarium an untested parameter produces no reading row, and nulls&lt;br&gt;
appear only when rows are grouped into per-session views. Storing zero for&lt;br&gt;
"not tested" draws dips that never happened and makes rate-of-change checks&lt;br&gt;
compare against a value nobody measured.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you build offline-first sync with Drift and Supabase?
&lt;/h3&gt;

&lt;p&gt;Make Drift the only thing the UI reads, and write every change to its table&lt;br&gt;
and to an outbox table in one Drift transaction. A sync engine pushes the&lt;br&gt;
outbox parents-first, then pulls rows whose server-stamped &lt;code&gt;updated_at&lt;/code&gt; is&lt;br&gt;
newer than the last one it saw minus a short overlap, skipping rows with&lt;br&gt;
unpushed local edits. Client-generated UUIDs and &lt;code&gt;deleted_at&lt;/code&gt; tombstones let&lt;br&gt;
both sides agree on what changed.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does an app decide an aquarium is cycled?
&lt;/h3&gt;

&lt;p&gt;Aquarium groups readings into test sessions and looks for two sessions in a&lt;br&gt;
row where ammonia and nitrite are at or below 0.25 ppm and nitrate is present.&lt;br&gt;
One such session is shown as "confirming", because a single clean result can&lt;br&gt;
be a misread.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you write Supabase RLS policies for tables that belong to a parent row?
&lt;/h3&gt;

&lt;p&gt;Give every child table the parent's id, put the access check in a&lt;br&gt;
&lt;code&gt;security definer&lt;/code&gt; function in a schema the API can't reach, and generate the&lt;br&gt;
same select and write policy for each table in a loop. Wrap &lt;code&gt;auth.uid()&lt;/code&gt; as&lt;br&gt;
&lt;code&gt;(select auth.uid())&lt;/code&gt; and index the columns the policies filter on.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>supabase</category>
      <category>offlinefirst</category>
    </item>
    <item>
      <title>Mapping free street parking from open data with PostGIS and MapKit</title>
      <dc:creator>Ivan Chernoknizhnikov</dc:creator>
      <pubDate>Fri, 27 Nov 2026 09:01:51 +0000</pubDate>
      <link>https://dev.to/treamz/mapping-free-street-parking-from-open-data-with-postgis-and-mapkit-3dee</link>
      <guid>https://dev.to/treamz/mapping-free-street-parking-from-open-data-with-postgis-and-mapkit-3dee</guid>
      <description>&lt;p&gt;This post covers why OpenStreetMap gives kerb-level parking in only some&lt;br&gt;
cities, how I turn a council's paid streets into "likely free" kerbs with&lt;br&gt;
PostGIS, how that guess stays apart from real facts, and how SwiftUI's &lt;code&gt;Map&lt;/code&gt;&lt;br&gt;
draws a city's kerbs without stuttering. The app is Easy Find Parking, a free&lt;br&gt;
iPhone and iPad map of free street parking in Berlin, Madrid, Barcelona,&lt;br&gt;
València and Kraków,&lt;br&gt;
&lt;a href="https://apps.apple.com/us/app/easy-find-parking-europe/id6502238318" rel="noopener noreferrer"&gt;on the App Store&lt;/a&gt;;&lt;br&gt;
the user's side is in &lt;a href="https://treamz.me/blog/free-parking-app-europe" rel="noopener noreferrer"&gt;a separate post&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Server: Flask, PostGIS and a self-hosted ParkAPI. Phone: SwiftUI on iOS 17.&lt;/p&gt;
&lt;h2&gt;
  
  
  OpenStreetMap street parking is mostly a German dataset
&lt;/h2&gt;

&lt;p&gt;My first plan was OpenStreetMap everywhere. Kerbside parking is tagged on the&lt;br&gt;
street itself, with keys like &lt;code&gt;parking:left=lane&lt;/code&gt; and &lt;code&gt;parking:right:fee=no&lt;/code&gt;&lt;br&gt;
(see &lt;a href="https://wiki.openstreetmap.org/wiki/Street_parking" rel="noopener noreferrer"&gt;Street parking&lt;/a&gt; on&lt;br&gt;
the OSM wiki). When I measured in September 2026, this was the share of&lt;br&gt;
drivable streets carrying any &lt;code&gt;parking:*&lt;/code&gt; tag:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;City&lt;/th&gt;
&lt;th&gt;Streets with &lt;code&gt;parking:*&lt;/code&gt; tags&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Berlin&lt;/td&gt;
&lt;td&gt;99.9%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Hamburg&lt;/td&gt;
&lt;td&gt;54%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Warsaw&lt;/td&gt;
&lt;td&gt;26%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lyon&lt;/td&gt;
&lt;td&gt;22%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Milan&lt;/td&gt;
&lt;td&gt;11%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Paris&lt;/td&gt;
&lt;td&gt;10%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;València&lt;/td&gt;
&lt;td&gt;7%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Barcelona&lt;/td&gt;
&lt;td&gt;4%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lisbon&lt;/td&gt;
&lt;td&gt;0.9%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Fee sub-tags were on about a tenth of those. Berlin works: 64,229 kerb sides and&lt;br&gt;
48,567 car parks from one Geofabrik extract. Elsewhere a tag-driven layer would&lt;br&gt;
be mostly blank, which reads as "no parking here". So outside Germany the&lt;br&gt;
street layer comes from municipal open data, which the city actually enforces.&lt;/p&gt;

&lt;p&gt;Reading the tags has traps. The wiki deprecated the older &lt;code&gt;parking:lane=*&lt;/code&gt; and&lt;br&gt;
&lt;code&gt;parking:condition=*&lt;/code&gt; keys in December 2022 and asks for existing ones to be&lt;br&gt;
converted, so the normaliser reads both. &lt;code&gt;parking:left=no&lt;/code&gt; is a value, not an&lt;br&gt;
absence: 43% of Berlin's tagged sides. And without &lt;code&gt;closed_ways_are_polygons&lt;/code&gt;&lt;br&gt;
in its config, GDAL put closed &lt;code&gt;amenity=parking&lt;/code&gt; ways in the lines layer, where&lt;br&gt;
my highway filter dropped them: Berlin had 42 car-park polygons instead of&lt;br&gt;
63,294.&lt;/p&gt;
&lt;h2&gt;
  
  
  Deriving "likely free" from what a city charges for
&lt;/h2&gt;

&lt;p&gt;Councils publish what they charge for, because that's what they enforce.&lt;br&gt;
Barcelona's&lt;br&gt;
&lt;a href="https://opendata-ajuntament.barcelona.cat/data/dataset/trams-aparcament-superficie" rel="noopener noreferrer"&gt;surface parking dataset&lt;/a&gt;&lt;br&gt;
lists its blue, green and goods-delivery segments; Kraków publishes 23 paid-zone&lt;br&gt;
polygons and no kerbs. The complement is the free map: every drivable street&lt;br&gt;
inside the city boundary with nothing regulated near it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TEMP&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;regulated&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;geom&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;efp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;street_parking&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;city_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;'city'&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;fee_source&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'municipal_paid'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'municipal_free'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;geom&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;geography&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;efp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;paid_zone&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;city_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="s1"&gt;'city'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;-- Kraków's polygons&lt;/span&gt;

&lt;span class="c1"&gt;-- ...&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;osm_stage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;streets_REGIONTAG&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
&lt;span class="k"&gt;CROSS&lt;/span&gt; &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;VALUES&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'left'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'right'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="nf"&gt;sides&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;city_boundary&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;ST_Within&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;geom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;geom&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;regulated&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;
      &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nc"&gt;ST_DWithin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;efp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;kerb_offset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;geom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sides&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;side&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;geography&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;r&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;geom&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;clearance&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;highway&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'residential'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'living_street'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'unclassified'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'tertiary'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'tertiary_link'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="nb"&gt;lower&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;coalesce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;access&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;''&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;NOT&lt;/span&gt; &lt;span class="k"&gt;IN&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'private'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'no'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'customers'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three of these are there because a looser version published something wrong.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Metres, not degrees.&lt;/strong&gt; The first version buffered by 0.00025°. At&lt;br&gt;
Barcelona's latitude that's 27.8 m north–south but 20.8 m east–west, and 407&lt;br&gt;
streets 21–25 m from a paid kerb went out as free. Casting to &lt;code&gt;geography&lt;/code&gt; makes&lt;br&gt;
&lt;code&gt;ST_DWithin&lt;/code&gt; measure in metres; &lt;code&gt;:clearance&lt;/code&gt; is 25.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Test the kerb, not the centreline.&lt;/strong&gt; &lt;code&gt;efp.kerb_offset&lt;/code&gt; shifts the street 3 m&lt;br&gt;
to one side, working in EPSG:3857 and dividing the distance by cos(latitude) to&lt;br&gt;
undo Mercator's stretch. That line can sit closer to a paid kerb than the&lt;br&gt;
centreline, so it's the one tested.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Leave out roads with no parking.&lt;/strong&gt; The first run turned 17,275 regulated&lt;br&gt;
segments into 10,842 free kerb sides, and the map showed dashed "likely free"&lt;br&gt;
lines down Avinguda Diagonal. Arterials have no regulated segments because&lt;br&gt;
nobody parks on them, so "outside every paid zone" came out true. Limiting the&lt;br&gt;
rule to residential, unclassified and tertiary roads dropped 3,156 sides, 29%&lt;br&gt;
of the layer. Service roads stay out: they're car-park aisles and driveways as&lt;br&gt;
often as public kerb.&lt;/p&gt;

&lt;p&gt;Kraków's polygons feed the same rule: 21,255 free kerb sides, none within 25 m&lt;br&gt;
of a zone. A test samples 2,000 derived kerbs per city to assert&lt;br&gt;
it. Checking every row took over 150 seconds at 29,000 derived against&lt;br&gt;
105,000 regulated, and a check nobody waits for is a check nobody runs.&lt;/p&gt;
&lt;h2&gt;
  
  
  Free and likely free are different values
&lt;/h2&gt;

&lt;p&gt;The schema never stores a bare "free". Every row has a &lt;code&gt;fee_state&lt;/code&gt; (&lt;code&gt;FREE&lt;/code&gt;,&lt;br&gt;
&lt;code&gt;PAID&lt;/code&gt;, &lt;code&gt;CONDITIONAL&lt;/code&gt;, &lt;code&gt;UNKNOWN&lt;/code&gt;) and a &lt;code&gt;fee_source&lt;/code&gt; saying where it came from:&lt;br&gt;
&lt;code&gt;municipal_free&lt;/code&gt;, &lt;code&gt;osm_tag&lt;/code&gt;, &lt;code&gt;operator_feed&lt;/code&gt;, &lt;code&gt;municipal_paid_complement&lt;/code&gt; and&lt;br&gt;
&lt;code&gt;municipal_outside_zone&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The last one came from Madrid, whose SER map service publishes 87,614 kerb&lt;br&gt;
bands. 53,158 are grey, a colour missing from the service's own legend.&lt;br&gt;
Sampling 300 bands of each colour against the SER boundary settled it: every&lt;br&gt;
blue and green band tested fell inside, no grey one did. So grey bands are&lt;br&gt;
published kerbs outside the paid zone: stronger than an inference from nothing,&lt;br&gt;
weaker than the council saying "free". I first stored them as&lt;br&gt;
&lt;code&gt;municipal_paid_complement&lt;/code&gt;, and the overlap test failed, correctly: grey bands&lt;br&gt;
sit next to blue ones on the same street, which for a derived street would be a&lt;br&gt;
bug.&lt;/p&gt;

&lt;p&gt;One function turns the pair into a claim, ordered by trust: a current count, a&lt;br&gt;
published fact, a schedule, an inference, an admitted gap.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;feeState&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;feeSource&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;municipalFree&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;operatorFeed&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cityFree&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;osmTag&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;signpostedFree&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;municipalOutsideZone&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;municipalPaidComplement&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;// This is the inference, and it must never borrow a stronger phrasing.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;likelyFree&lt;/span&gt;
&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;free&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="c1"&gt;// A free claim with no provenance is not one we will repeat.&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;unknown&lt;/span&gt;
&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;break&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Charging hours the parser can't read resolve to "charged right now": wrong&lt;br&gt;
that way costs a walk, the other way a fine.&lt;/p&gt;

&lt;p&gt;On the first build the inferred layer was mint and read as the same free as&lt;br&gt;
the confirmed green. Now it's teal and dashed, which says "not solid" before&lt;br&gt;
anyone reads the legend:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nc"&gt;ForEach&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;streets&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;street&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt;
    &lt;span class="nc"&gt;MapPolyline&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;coordinates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;street&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coordinates&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;stroke&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;street&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;claim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;tint&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;opacity&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.85&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                &lt;span class="n"&gt;style&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;street&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;claim&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isInferred&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;inferredKerb&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kerb&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;extension&lt;/span&gt; &lt;span class="nc"&gt;StrokeStyle&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;kerb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StrokeStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;lineWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lineCap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;round&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lineJoin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;round&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;/// Dashed: derived from an absence of regulation, not from a sign.&lt;/span&gt;
    &lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;inferredKerb&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;StrokeStyle&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;lineWidth&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lineCap&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;butt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lineJoin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;round&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dash&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both hedged kinds draw the same dashed teal; the data keeps them apart.&lt;/p&gt;

&lt;h2&gt;
  
  
  Drawing kerbs with SwiftUI's Map
&lt;/h2&gt;

&lt;p&gt;That &lt;code&gt;ForEach&lt;/code&gt; is the whole renderer, and its cost is the count: a city-wide&lt;br&gt;
Barcelona viewport holds around 660 kerbs. The obvious fix is one&lt;br&gt;
&lt;code&gt;MKMultiPolyline&lt;/code&gt; per style, about four overlays, but &lt;code&gt;MapPolyline&lt;/code&gt; has no&lt;br&gt;
initialiser that takes one. So I rewrote the map as &lt;code&gt;MKMapView&lt;/code&gt; in a&lt;br&gt;
&lt;code&gt;UIViewRepresentable&lt;/code&gt;. It worked in the simulator and drew no tiles on a real&lt;br&gt;
device. I couldn't reproduce it, and a blank map is worse than a slow one, so I&lt;br&gt;
went back to SwiftUI's &lt;code&gt;Map&lt;/code&gt;. What keeps it smooth:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Measure first.&lt;/strong&gt; The database answered in 7.6 ms and the endpoint in under
half a second, so the stutter was on the phone.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fetch a ring, draw the screen.&lt;/strong&gt; The app fetches 30% of the span beyond
each edge, skips the request while the viewport stays inside it, and waits
250 ms for the camera to settle. Drawing is clipped to the viewport, about
60% fewer lines, and the array is reassigned only when visible IDs change:
an identical array still makes SwiftUI diff every overlay.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Slim payload.&lt;/strong&gt; Street name, zone, capacity and source were 65% of the
bytes and none is drawn, so the layer gets four fields per kerb and
coordinates rounded to six decimals, about 10 cm. A city-wide response went
from 575 KB to 359 KB.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Nearest first.&lt;/strong&gt; The query orders by &lt;code&gt;sp.geom &amp;lt;-&amp;gt; centre&lt;/code&gt; with a limit of
1,500, so truncation eats the ring, not the middle of the screen.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One feature from the live API:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"Feature"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"geometry"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"LineString"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"coordinates"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;2.174812&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;41.403032&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;2.174847&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;41.403005&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mf"&gt;2.174876&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;41.402983&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nl"&gt;"properties"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;"source_id"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"complement-es-barcelona-885702924-left"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"fee_state"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"FREE"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"fee_source"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"municipal_paid_complement"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nl"&gt;"opening_hours"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A cap of 400 drawn kerbs left the edges of a Barcelona screen bare, and a&lt;br&gt;
driver can't tell a capped edge from a street with no parking, so it went.&lt;/p&gt;

&lt;p&gt;València publishes individual spaces, 315,303 averaging 2.6 m, so 1,500&lt;br&gt;
features covered a few blocks. &lt;code&gt;ST_ClusterDBSCAN&lt;/code&gt;, partitioned by street, fee&lt;br&gt;
state and a grid of about 300 m, now merges adjacent bays into segments with a&lt;br&gt;
bay count: the same view went from 1,500 features and 453 KB to 582 and 135 KB.&lt;br&gt;
Without the grid, nameless paid bays chained into lines 25 km long. A polyline&lt;br&gt;
threaded through the bays zig-zagged across the street, since bays sit on both&lt;br&gt;
sides, so each cluster is drawn as the longest line across its convex hull.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv3ob1qzbxbcuy5nbtqql.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fv3ob1qzbxbcuy5nbtqql.jpg" title="València: each green line is a run of free bays merged into one segment" alt="The map in València at street level, with free kerbs drawn as solid green lines" width="644" height="1400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Zoomed out past 0.02° of span, the kerb layer switches off and the gateway&lt;br&gt;
counts kerbs in roughly an 8×8 grid over the viewport, drawn as circles.&lt;br&gt;
These aren't MapKit annotation clusters: the phone never downloads the kerbs at&lt;br&gt;
that zoom. Circle size is logarithmic, because counts span four orders of&lt;br&gt;
magnitude and linear sizing pinned nearly every circle to the maximum.&lt;/p&gt;
&lt;h2&gt;
  
  
  Live counts go stale after 15 minutes
&lt;/h2&gt;

&lt;p&gt;The old app read &lt;code&gt;api.parkendd.de&lt;/code&gt;, which had stopped updating. Now I run my&lt;br&gt;
own &lt;a href="https://github.com/ParkenDD/park-api-v3" rel="noopener noreferrer"&gt;ParkAPI v3&lt;/a&gt; (MIT), pulling&lt;br&gt;
realtime data every 300 seconds into the same Postgres as my schema, so the&lt;br&gt;
gateway joins its &lt;code&gt;parking_site&lt;/code&gt; table directly. The gateway decides freshness,&lt;br&gt;
so phone and server can't disagree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="no"&gt;STALE_AFTER&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;timedelta&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;minutes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;freshness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;updated_at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;str&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kc"&gt;None&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'none'&lt;/span&gt;
    &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;datetime&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;now&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;timezone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;utc&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;updated_at&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s1"&gt;'live'&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="no"&gt;STALE_AFTER&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s1"&gt;'stale'&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;rows&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'freshness'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;freshness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'realtime_updated_at'&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="c1"&gt;# Keep the number, drop the claim that it is current.&lt;/span&gt;
    &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'realtime_is_current'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s1"&gt;'freshness'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'live'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;ParkAPI's own public API drops realtime fields once they're too old. I keep the&lt;br&gt;
number and label it: a stale count shows grey as "N free when last seen", with&lt;br&gt;
its age. On 9 November 2026, 15 car parks within 2 km of central Basel had a&lt;br&gt;
current count.&lt;/p&gt;

&lt;p&gt;Indigo's feed reported zero occupancy for all 558 French car parks, which naive&lt;br&gt;
arithmetic shows as empty, so realtime is off for it. And 28% of upstream&lt;br&gt;
ParkAPI converters were already dead when I set it up, so each source's last&lt;br&gt;
success and error live in a table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Licences and gaps
&lt;/h2&gt;

&lt;p&gt;OpenStreetMap data is ODbL (© OpenStreetMap contributors). Barcelona and&lt;br&gt;
València publish under CC BY 4.0, Madrid and Kraków under their own open-data&lt;br&gt;
terms, the French national base and Indigo under ODbL. Derived layers credit&lt;br&gt;
both the council and OSM, and the app shows these credits behind its info&lt;br&gt;
button.&lt;/p&gt;

&lt;p&gt;Missing: an offline cache, address search, and Warsaw. Its city API timed out&lt;br&gt;
when I checked, and OSM had fee information on 0% of its central streets. Kerb&lt;br&gt;
lines show rules, not empty spaces.&lt;/p&gt;

&lt;p&gt;What the app does for the people who use it: &lt;a href="https://treamz.me/blog/free-parking-app-europe" rel="noopener noreferrer"&gt;Free parking app for Europe: Easy Find Parking for iPhone&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How complete is OpenStreetMap parking data in European cities?
&lt;/h3&gt;

&lt;p&gt;Very uneven. When I measured in September 2026, the share of drivable streets&lt;br&gt;
with any &lt;code&gt;parking:*&lt;/code&gt; tag was 99.9% in Berlin, 10% in Paris, 4% in Barcelona&lt;br&gt;
and 0.9% in Lisbon, and about a tenth of tagged streets had fee&lt;br&gt;
information. Outside Germany, municipal open data is usually the better source.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you find free street parking from a city's paid parking data?
&lt;/h3&gt;

&lt;p&gt;Take the OpenStreetMap road network inside the city boundary and drop any&lt;br&gt;
street side within a clearance (25 m in Easy Find Parking) of a published paid&lt;br&gt;
segment or zone polygon. Limit it to residential, unclassified and tertiary&lt;br&gt;
roads, or arterials come out free, and label the result "likely free".&lt;/p&gt;

&lt;h3&gt;
  
  
  Should I buffer in degrees or metres in PostGIS?
&lt;/h3&gt;

&lt;p&gt;Metres. On EPSG:4326 geometry, distances are in degrees, and a degree of&lt;br&gt;
longitude shrinks with latitude: 0.00025° is 27.8 m north–south but 20.8 m&lt;br&gt;
east–west in Barcelona. Cast to &lt;code&gt;geography&lt;/code&gt; and &lt;code&gt;ST_DWithin&lt;/code&gt; takes metres.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do I draw thousands of polylines in a SwiftUI Map without lag?
&lt;/h3&gt;

&lt;p&gt;SwiftUI's &lt;code&gt;Map&lt;/code&gt; needs one &lt;code&gt;MapPolyline&lt;/code&gt; per line and can't take an&lt;br&gt;
&lt;code&gt;MKMultiPolyline&lt;/code&gt;, so the line count is the cost. Fetch a padded area, skip&lt;br&gt;
requests while the viewport stays inside it, debounce camera changes by about&lt;br&gt;
250 ms, draw only what's visible and send only the fields you draw. Zoomed out,&lt;br&gt;
show area counts instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you know if a live car park count is still current?
&lt;/h3&gt;

&lt;p&gt;Compare its timestamp with the server's clock. Easy Find Parking treats a count&lt;br&gt;
older than 15 minutes as stale: it keeps the number but shows it as "free when&lt;br&gt;
last seen" with its age, never as spaces available now.&lt;/p&gt;

</description>
      <category>opendata</category>
      <category>postgis</category>
      <category>ios</category>
      <category>swift</category>
    </item>
    <item>
      <title>GTA 6 on a MacBook at 60 FPS, with no RTX and no DLSS</title>
      <dc:creator>Ivan Chernoknizhnikov</dc:creator>
      <pubDate>Tue, 24 Nov 2026 20:47:01 +0000</pubDate>
      <link>https://dev.to/treamz/gta-6-on-a-macbook-at-60-fps-with-no-rtx-and-no-dlss-4d8h</link>
      <guid>https://dev.to/treamz/gta-6-on-a-macbook-at-60-fps-with-no-rtx-and-no-dlss-4d8h</guid>
      <description>&lt;p&gt;GTA 6 runs at 30 FPS on PS5, and there's no PC version, so the "GTA 6 with DLSS 5" videos are the console's picture processed on a PC with an RTX card. I have a PS5 and a MacBook Pro with an M1 Max, so I built the Mac version of that setup: the PS5 goes into a €17 USB 3.0 capture card, and MetalGoose, an open-source Mac app, upscales the picture and generates a frame between every two real ones. The result is 60 FPS on the MacBook's screen, about 66 ms from the console's frame to the display.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://treamz.me/blog/gta-6-on-mac/30-vs-60.mp4" rel="noopener noreferrer"&gt;Watch the comparison video&lt;/a&gt;: the left half shows only the frames the PS5 sends, the right half the same moment with generated frames.&lt;/p&gt;

&lt;h2&gt;
  
  
  How I ended up in GTA 6 at all
&lt;/h2&gt;

&lt;p&gt;I wasn't planning to buy GTA 6. I barely played GTA V: I only ever went into GTA Online, and never really figured out how it works. I expected nothing from the sixth one. But my friends insisted it was worth it, so I bought it.&lt;/p&gt;

&lt;p&gt;From the first minutes it blew me away. I expected nothing and got a game that makes you stop and look around: the road, the water, the clouds, the way light falls on the cars. No game has surprised me like that in a long time.&lt;/p&gt;

&lt;p&gt;Then, like everyone else, I watched the videos and reels about "GTA 6 with DLSS 5": smooth motion, high FPS, everything flying on an RTX card. I wanted at least 60 frames. I don't have a PC for that, only a PS5 and a MacBook, so I had to make do with what I had.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why DLSS 5 doesn't help on a Mac
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;GTA 6 is out only on consoles&lt;/strong&gt;, and on PS5 (Pro included) it runs at 30 FPS. There's no 60 FPS mode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;DLSS only works on a PC with an NVIDIA card&lt;/strong&gt;, and only in games built with it. There's no PC version of GTA 6, so it can't have DLSS in the usual sense.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The videos are the PS5's picture sent to an RTX PC&lt;/strong&gt;, through a capture card or Remote Play. A tool there applies DLSS 5 to the finished video and DLSS frame generation adds frames in between. SmoovPlay is one such project. All of them need Windows and an NVIDIA card.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So real 60 FPS on a PS5 isn't possible anywhere. What you can get is 30 real frames with generated ones in between: smoother motion, but not more responsive controls. The question was whether a Mac can do the generating.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Mac equivalent: MetalGoose
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://github.com/Stallion77RepoOfficial/MetalGoose" rel="noopener noreferrer"&gt;MetalGoose&lt;/a&gt; is an open-source Apple Silicon app that captures a picture, upscales it with MetalFX Spatial (Apple's upscaler) and generates frames, either on the Neural Engine through VideoToolbox or on the GPU through MetalFX frame interpolation. It does on a Mac what SmoovPlay does on a PC.&lt;/p&gt;

&lt;p&gt;It's young and has no ready-made builds, so I built it from source and adapted it, working with Claude Code as an AI assistant. The first step was reading all of the code: the app asks for screen recording access, and I wanted to be sure it does nothing else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attempt 1: PS5 Remote Play
&lt;/h2&gt;

&lt;p&gt;The first setup cost nothing: the PS5 streams to the Mac through PS Remote Play, and MetalGoose captures the stream's window. It worked, but:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Remote Play's picture is poor&lt;/strong&gt;: heavy compression and smeared detail;&lt;/li&gt;
&lt;li&gt;the open-source client &lt;strong&gt;chiaki-ng&lt;/strong&gt; looks better, since you can choose the codec and bitrate, but it's still video compressed for the network;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;latency adds up&lt;/strong&gt;: the network stream, window capture and frame generation.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Good enough to test the idea, not to enjoy the game.&lt;/p&gt;

&lt;h2&gt;
  
  
  Attempt 2: a €17 capture card
&lt;/h2&gt;

&lt;p&gt;The right way is to take the PS5's picture over HDMI, uncompressed. I didn't want to pay for an Elgato just to experiment, so I ordered the cheapest decent card on Amazon: a SUOFEIXIANG for €17.69.&lt;/p&gt;

&lt;p&gt;What matters in these cards is the chip and the USB:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;an &lt;strong&gt;MS2130 chip and USB 3.0&lt;/strong&gt; deliver 1080p at 60 FPS uncompressed;&lt;/li&gt;
&lt;li&gt;cheap &lt;strong&gt;USB 2.0 cards (MS2109)&lt;/strong&gt; compress the picture to MJPEG, which costs both quality and latency;&lt;/li&gt;
&lt;li&gt;to check yours, open About This Mac → System Report → USB: it should say "Up to 5 Gb/s". Mine did.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;On the PS5:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Setting&lt;/th&gt;
&lt;th&gt;Value&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Resolution&lt;/td&gt;
&lt;td&gt;1080p (the card takes 4K only at 30 Hz)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;120 Hz output&lt;/td&gt;
&lt;td&gt;Off&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HDR&lt;/td&gt;
&lt;td&gt;Off&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;HDCP (Settings → System → HDMI)&lt;/td&gt;
&lt;td&gt;Off, or the card shows a black screen&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;h2&gt;
  
  
  What I changed in MetalGoose
&lt;/h2&gt;

&lt;p&gt;Out of the box MetalGoose captures only windows, so the card's picture first had to be shown in QuickTime and MetalGoose captured QuickTime's window. It worked, but it was an extra step and extra latency. So I added:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Direct capture card input.&lt;/strong&gt; Frames go from the card straight into processing, without QuickTime or window capture, and the card's sound goes straight to the Mac's speakers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A choice of frame generation engine&lt;/strong&gt;: Neural Engine or GPU (MetalFX), and a shimmer fix for MetalFX.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A 30 vs 60 comparison mode&lt;/strong&gt;: the screen splits in half, real frames only on the left, generated frames added on the right. It toggles live with ⌘⇧D.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Two problems turned up along the way:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The card ran at 20 FPS instead of 60.&lt;/strong&gt; macOS reset its frame rate when the capture session started. Setting it again once the session runs brought it to 60, and each frame now reaches MetalGoose about 13 ms after the card captured it instead of 60 to 75 ms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Repeated frames weren't recognised.&lt;/strong&gt; The game draws 30 frames while the PS5 sends a 60 Hz signal, so every frame arrives twice. The card copies it with a little noise, and MetalGoose took the copies for new frames and generated frames between near-identical pictures. Now a frame that differs from the last one only by noise (under 0.05% of pixels changed by more than 10 levels) counts as a repeat, and exactly 30 new frames a second go in.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Neural Engine or MetalFX
&lt;/h2&gt;

&lt;p&gt;The Neural Engine generates frames at 720p, so its in-between frames are softer. MetalFX works at the full 1080p and is noticeably sharper, but reflections shimmered with it: the reflection on a car's roof flickered on the generated half. Frame generation knows where the car moved, but the reflection on it moves its own way, so the in-between frame puts it in the wrong place, the next real frame puts it back, and that alternation 60 times a second shows as shimmer.&lt;/p&gt;

&lt;p&gt;So MetalGoose now checks MetalFX's output. Each generated pixel is compared with the 3×3 neighbourhood of the two real frames along the motion that was found, and a colour neither of them has is pulled back into their range, the way TAA in games rejects colours from previous frames that no longer belong. On test frames it didn't change the overall match with the real in-between frames (26.81 dB against 26.83 without it), and in the game the shimmer on reflections is almost gone. I play with MetalFX and this fix on.&lt;/p&gt;

&lt;h2&gt;
  
  
  The result
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;PS5 (1080p, 30 FPS) → HDMI → capture card (USB 3.0) → MetalGoose on a MacBook Pro M1 Max → 120 Hz screen&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;From the MetalGoose HUD while playing:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Capture&lt;/td&gt;
&lt;td&gt;29 FPS (every real frame of the game)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Output&lt;/td&gt;
&lt;td&gt;58 FPS&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Upscale&lt;/td&gt;
&lt;td&gt;1920×1080 → 3456×1944&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;GPU time&lt;/td&gt;
&lt;td&gt;7.3 ms per frame, 43% of the GPU&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Frame pacing&lt;/td&gt;
&lt;td&gt;97 out of 100, no dropped frames&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Console frame to screen&lt;/td&gt;
&lt;td&gt;about 66 ms&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;For comparison, 4× mode on the Neural Engine reached 120 FPS, but took about 107 ms from the card's frame to the screen. 2× on MetalFX is sharper and noticeably quicker.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://treamz.me/blog/gta-6-on-mac/30-vs-60-slow.mp4" rel="noopener noreferrer"&gt;The same scene four times slower: 30 FPS on the left, generated frames on the right (video)&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The downsides
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;It isn't real 60 FPS.&lt;/strong&gt; The game still computes 30 frames a second, and the rest are generated. Motion gets smoother; the controls don't.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;There's input lag.&lt;/strong&gt; An in-between frame needs the next real frame, so generation adds about one game frame. It's comfortable for driving and exploring; in gunfights you feel the difference from a TV.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Artifacts still happen&lt;/strong&gt; on transparent objects and the HUD, especially when the camera turns sharply.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No passthrough.&lt;/strong&gt; While the PS5 is connected to a card like this one, the TV gets nothing.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Can you play GTA 6 on a Mac?
&lt;/h3&gt;

&lt;p&gt;Not natively: GTA 6 is out only on PS5 and Xbox Series X|S. You can play the PS5 version on a Mac through PS Remote Play, or with an HDMI capture card, which gives an uncompressed picture and less latency.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can GTA 6 run at 60 FPS on a Mac?
&lt;/h3&gt;

&lt;p&gt;The game itself runs at 30 FPS on the console. On a Mac, MetalGoose can generate a frame between every two real ones and show 60 FPS; on a MacBook Pro M1 Max it takes 7.3 ms of GPU time per frame. Motion is smoother, but the game still responds at 30 FPS.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does DLSS 5 work on a Mac?
&lt;/h3&gt;

&lt;p&gt;No. DLSS needs an NVIDIA GPU. On Apple Silicon the closest tools are MetalFX upscaling and MetalFX or VideoToolbox frame interpolation, which is what MetalGoose uses.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which capture card works with a Mac for PS5?
&lt;/h3&gt;

&lt;p&gt;A UVC card with an MS2130 chip on USB 3.0 works with no drivers and delivers 1080p at 60 FPS uncompressed; mine cost €17.69. Turn off HDCP, HDR and 120 Hz output on the PS5 and set it to 1080p.&lt;/p&gt;

&lt;h3&gt;
  
  
  How much input lag does frame generation add?
&lt;/h3&gt;

&lt;p&gt;About one game frame, 33 ms at 30 FPS, because an in-between frame can only be made once the next real frame has arrived. In my setup the whole path from the console's frame to the MacBook's screen is about 66 ms.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Originally published at &lt;a href="https://treamz.me/blog/gta-6-on-mac" rel="noopener noreferrer"&gt;treamz.me&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>gaming</category>
      <category>macos</category>
      <category>apple</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Identifying card printings on device with Vision and OCR</title>
      <dc:creator>Ivan Chernoknizhnikov</dc:creator>
      <pubDate>Sun, 22 Nov 2026 10:24:57 +0000</pubDate>
      <link>https://dev.to/treamz/identifying-card-printings-on-device-with-vision-and-ocr-j5b</link>
      <guid>https://dev.to/treamz/identifying-card-printings-on-device-with-vision-and-ocr-j5b</guid>
      <description>&lt;p&gt;How the scanner in Foilcase works out which printing of a trading card it is looking at: Vision feature prints for the artwork, text recognition for the collector number, and a strict rule for when to ask instead of guessing. Plus the storage underneath it: an append-only ledger and one daily price file for everyone. Foilcase is my iPhone app for scanning and pricing cards from five trading card games (&lt;a href="https://apps.apple.com/us/app/foilcase-tcg-card-scanner/id6813665558" rel="noopener noreferrer"&gt;App Store&lt;/a&gt;); the &lt;a href="https://treamz.me/blog/trading-card-scanner-app" rel="noopener noreferrer"&gt;user-facing write-up&lt;/a&gt; covers what it does.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why artwork matching picks the wrong print
&lt;/h2&gt;

&lt;p&gt;Before release I built a harness on a Mac that calls the same Vision APIs as the app: 9,903 cards indexed, 400 queries, half drawn on purpose from collector numbers that more than one print shares. Each query was a synthetic degradation of an official card image (perspective, blur, glare, colour cast, JPEG).&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Sharp frame, 400 queries&lt;/th&gt;
&lt;th&gt;Correct&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Artwork, top-1&lt;/td&gt;
&lt;td&gt;87.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Artwork, top-1 on shared numbers&lt;/td&gt;
&lt;td&gt;78.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Artwork, top-5&lt;/td&gt;
&lt;td&gt;99.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Artwork, top-1 when given the correct number&lt;/td&gt;
&lt;td&gt;89.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The last row changed the design. Even with the right collector number, artwork matching picks the right print 89.5% of the time, because alternate arts and higher-rarity parallels often share one number and one illustration. No embedding separates two prints of one picture, so the app asks which print, as it asks whether a card is foil. And since five candidates nearly always include the right card, the five-card sheet is the main flow, not an error state.&lt;/p&gt;

&lt;p&gt;The embedding is &lt;a href="https://developer.apple.com/documentation/vision/vngenerateimagefeatureprintrequest" rel="noopener noreferrer"&gt;&lt;code&gt;VNGenerateImageFeaturePrintRequest&lt;/code&gt;&lt;/a&gt;, revision 2: 768 floats, about 9 ms an image. One option matters more than it looks:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;featurePrint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CIImage&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;VNGenerateImageFeaturePrintRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;imageCropAndScaleOption&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;scaleFill&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;VNImageRequestHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciImage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;observation&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;first&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Float&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;repeating&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;observation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;elementCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;withUnsafeMutableBytes&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;observation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copyBytes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;vector&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;artworkMatchingAvailable&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;probe&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;CIImage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;gray&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cropped&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;to&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CGRect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;y&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;featurePrint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;probe&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The default, &lt;code&gt;.centerCrop&lt;/code&gt;, cuts the top and bottom off a tall card: 82.4% against 93.2% for &lt;a href="https://developer.apple.com/documentation/vision/vnimagecropandscaleoption/scalefill" rel="noopener noreferrer"&gt;&lt;code&gt;.scaleFill&lt;/code&gt;&lt;/a&gt; on the same 1,295-card index. The probe exists because feature prints fail in the Simulator ("Failed to create espresso context"). The app checks once and falls back to the collector number alone, as it would on any device without a usable neural engine.&lt;/p&gt;

&lt;h2&gt;
  
  
  Distance is not a confidence score
&lt;/h2&gt;

&lt;p&gt;The obvious next step is a distance threshold. Measured, it says almost nothing: median distance was 0.450 when the top match was right and 0.420 when it was wrong. But distance does tell whether there is a card at all: photographed cards sat at a median of 0.459, a phone pointed at a laptop at 0.966. I found that out when the scanner, with no ceiling, turned a browser window into twenty-six confident entries. The ceiling is now 0.80, which keeps 99% of photographed cards and rejects 89% of everything else.&lt;/p&gt;

&lt;h2&gt;
  
  
  Reading the collector number
&lt;/h2&gt;

&lt;p&gt;The number is unambiguous whenever it is legible, so most of the work went into legibility.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Drop soft frames.&lt;/strong&gt; Between a handheld frame and a sharp one, the artwork gained 5.6 points and the collector number 46 (32.8% read against 78.5%). The gate is the variance of the Laplacian on a greyscale buffer of the rectified card. My first version chained Core Image filters, a Laplacian then a multiply blend, and returned negative variances: blend modes clamp and premultiply, they are not arithmetic.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't ask Vision to find the card.&lt;/strong&gt; On iOS, &lt;code&gt;VNDetectDocumentSegmentationRequest&lt;/code&gt; returned the card's text box at 0.96 confidence, and &lt;code&gt;VNDetectRectanglesRequest&lt;/code&gt; found artwork panels, never the border. So the scanner draws a card-shaped guide, tries three crops around it and keeps whichever embeds closest to the index.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Read in passes, at full size.&lt;/strong&gt; Downscaling crops is how my first benchmark reported an OCR accuracy of zero. &lt;a href="https://developer.apple.com/documentation/vision/vnrecognizetextrequest" rel="noopener noreferrer"&gt;&lt;code&gt;VNRecognizeTextRequest&lt;/code&gt;&lt;/a&gt; runs over the whole card, then the top and bottom 22% enlarged 2.5 times, then three corners at 4 times if nothing turned up:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;scan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CIImage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;into&lt;/span&gt; &lt;span class="n"&gt;found&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;inout&lt;/span&gt; &lt;span class="kt"&gt;Set&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;minimumTextHeight&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Float&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.008&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;VNRecognizeTextRequest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recognitionLevel&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;accurate&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;usesLanguageCorrection&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;   &lt;span class="c1"&gt;// these are codes, not words&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recognitionLanguages&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"en-US"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minimumTextHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;minimumTextHeight&lt;/span&gt;   &lt;span class="c1"&gt;// the default floor skips the number&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;VNImageRequestHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciImage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;image&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;options&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;perform&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;observation&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;results&lt;/span&gt; &lt;span class="o"&gt;??&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;candidate&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;observation&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;topCandidates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;found&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;formUnion&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;PrintedNumber&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;extract&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;candidate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The recognizer swaps look-alikes (S and 5, O and 0, B and 8), so readings are folded before comparing. That tolerance is what kept producing wrong additions. The fix was two keys: a loose one, padding removed, for &lt;em&gt;finding&lt;/em&gt; candidates, and a literal one, padding and variant marks kept, for &lt;em&gt;adding&lt;/em&gt; without asking. "45/195" fits a catalog "045/195", but it is also what "145/195" looks like with its first digit lost.&lt;/p&gt;

&lt;h2&gt;
  
  
  When the scanner adds and when it asks
&lt;/h2&gt;

&lt;p&gt;Add without asking only when the artwork's first choice (or a card it can't tell apart from it) and a reading that spells that card's number literally name the same card, and nothing the artwork can't rule out shares that number. Everything else is the sheet. Trimmed from &lt;code&gt;ScanPipeline.decide&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;blocks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;CardRef&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="nf"&gt;baseName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="nf"&gt;baseName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;   &lt;span class="c1"&gt;// same card, other print&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;margin&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distinctParallelMargin&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;mine&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;distance&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;theirs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;distances&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;theirs&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;mine&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;margin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;parallels&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;where&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;blocks&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ambiguous&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;offered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parallelPrints&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;artworkAgrees&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
    &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="nf"&gt;artworkTies&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;policy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minimumRelativeMargin&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;byNumber&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;numberMatched&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;artworkAgrees&lt;/span&gt;
    &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;spelled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;card&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;literals&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;unpadded&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;byNumber&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;ambiguous&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;candidates&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;ordered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;reason&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;numberMatched&lt;/span&gt; &lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;numberDisagrees&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lowConfidence&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;numbers&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;identified&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;winner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A different card sharing the number stops blocking only if the artwork puts it at least 15% farther away. &lt;code&gt;unpadded&lt;/code&gt; lets an old card printed "18/130" match a catalog "018/130", but only when it lands on the artwork's own first pick.&lt;/p&gt;

&lt;p&gt;The rule took three rounds of adversarial review to reach, each reported bug reproduced against the real catalogs. The first confirmed 33 defects; the second found 12 new ones, 11 introduced by the fixes. As my commit message put it, a comparison built to &lt;em&gt;find&lt;/em&gt; candidates was also allowed to &lt;em&gt;add&lt;/em&gt; one. Adding on artwork margin alone went too: a card missing from the index still has a nearest neighbour that can be clearly ahead and wrong.&lt;/p&gt;

&lt;p&gt;Before a silent add, a second text request also counts Han, kana and hangul characters, because artwork and number are identical across languages and each catalog holds one. English cards read 0 to 3, Japanese ones 37 to 188; the cut-off is 8.&lt;/p&gt;

&lt;p&gt;Then I measured on 529 real photographs of cards, labelled against the catalog, with all five games installed (62,331 cards in one index) and the app's recognition code running on a Mac. Every silent add was checked by eye:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;529 real photos&lt;/th&gt;
&lt;th&gt;Before&lt;/th&gt;
&lt;th&gt;After&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Added without asking, wrong card&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;td&gt;0&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Added without asking, right card&lt;/td&gt;
&lt;td&gt;21%&lt;/td&gt;
&lt;td&gt;42%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Right card added, or first in the sheet&lt;/td&gt;
&lt;td&gt;69%&lt;/td&gt;
&lt;td&gt;90%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Collector number read&lt;/td&gt;
&lt;td&gt;59%&lt;/td&gt;
&lt;td&gt;77%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The gains came from the photos: a picked photo now skips the quality gates, since no better frame is coming; the number is read from the loosest crop when the tightest cut it off; and each crop is also embedded slightly blurred, because a holo's sparkle pulled matches away from the flat scans in the index (right card first: 377 to 397). The live camera path is still unmeasured; for that, the app keeps an on-device calibration log of each attempt and the card the user actually picked.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keeping a 27,000-card index on the phone
&lt;/h2&gt;

&lt;p&gt;Each game's index downloads separately, quantised to int8 with one scale per pack. On queries perturbed to the distance a handheld photo sits at, int8 and float16 returned the same card 99.8% of the time, for half the bytes. There's no approximate nearest-neighbour library: in the Mac harness, a brute-force scan of the largest index, 26,978 vectors, takes 1.6 ms widened to &lt;code&gt;Float&lt;/code&gt;, but holds 83 MB. So the app memory-maps the int8 bytes and converts 512 rows at a time with &lt;a href="https://developer.apple.com/documentation/accelerate/vdsp_vflt8" rel="noopener noreferrer"&gt;&lt;code&gt;vDSP_vflt8&lt;/code&gt;&lt;/a&gt; and &lt;a href="https://developer.apple.com/documentation/accelerate/vdsp_distancesq" rel="noopener noreferrer"&gt;&lt;code&gt;vDSP_distancesq&lt;/code&gt;&lt;/a&gt;: 5.6 ms, nothing resident. The scan runs only on a steady frame, so four milliseconds is invisible and 83 MB isn't. A pack from a different Vision revision is refused; its distances live in another space.&lt;/p&gt;

&lt;p&gt;The index is built on a Mac and lags the catalog, so a clearly read number naming a card the artwork never offered goes to the top of the sheet, asked about and never added.&lt;/p&gt;

&lt;h2&gt;
  
  
  A ledger that never overwrites, and one price file for everyone
&lt;/h2&gt;

&lt;p&gt;The collection is an append-only &lt;code&gt;ledger&lt;/code&gt; table plus an &lt;code&gt;items&lt;/code&gt; table derived from it in the same transaction. Removal is a tombstone; &lt;code&gt;rebuild()&lt;/code&gt; replays the log. Replay needs an order every device agrees on, and the middle term of the sort key is a bug fix: the scanner adds a card and sets its finish within one millisecond, and a random event id sometimes sorted the edit first.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;/// Creation sorts before every edit to the same item at the same instant.&lt;/span&gt;
&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;replayRank&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;added&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;record&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;records&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;by&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;replayRank&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$0&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eventID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
                                 &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;event&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;replayRank&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;eventID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It paid off in October. The upstream catalog renamed sets, card ids include the set name, and 1,323 cards in one game got new ids overnight, 222 more across the others. Copies saved under old ids dropped off the collection screen. The fix is an alias table read in memory; the log stays as written.&lt;/p&gt;

&lt;p&gt;Prices are one binary file per game, the same bytes for every user, so a request says nothing about what anyone owns. Names are content-addressed and the manifest is written last, so a client never sees half an update. Prices are integer cents, with a sentinel so a $0.00 promo isn't an unpriced card. Each carries the day it was captured, and past &lt;code&gt;PriceProvenance.staleAfterDays&lt;/code&gt; (7) the portfolio headline says "These prices are 2 weeks old" instead of presenting an old total as current.&lt;/p&gt;

&lt;p&gt;What the app does for the people who use it: &lt;a href="https://treamz.me/blog/trading-card-scanner-app" rel="noopener noreferrer"&gt;Scan trading cards and see what they're worth with Foilcase&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How do you tell apart card printings that share the same artwork?
&lt;/h3&gt;

&lt;p&gt;Mostly not from the image: in my pre-release harness, artwork matching picked the right print 89.5% of the time even when given the correct collector number. Foilcase reads the number with Vision text recognition and, when other prints share it, shows the candidates and asks.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is VNGenerateImageFeaturePrintRequest good enough for on-device image recognition?
&lt;/h3&gt;

&lt;p&gt;For retrieval, yes, with no model to ship: the right card was in the top five 99.8% of the time on sharp synthetic frames of a 9,903-card index. Use &lt;code&gt;.scaleFill&lt;/code&gt;, and test on a device, because it doesn't run in the Simulator.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I need an approximate nearest neighbour library for on-device image search?
&lt;/h3&gt;

&lt;p&gt;Not at tens of thousands of vectors. A brute-force Accelerate scan over 26,978 feature prints took 1.6 ms on a Mac in Float, or 5.6 ms as memory-mapped int8, which avoids holding 83 MB in memory.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can embedding distance be used as a confidence score?
&lt;/h3&gt;

&lt;p&gt;Not between similar images: median distance was 0.450 for correct matches and 0.420 for wrong ones. It does separate cards (0.459) from non-cards (0.966), so Foilcase uses it only as a 0.80 ceiling.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you read small printed numbers with VNRecognizeTextRequest?
&lt;/h3&gt;

&lt;p&gt;Lower &lt;code&gt;minimumTextHeight&lt;/code&gt;, turn off language correction, never downscale the crop, and add passes over enlarged strips and corners. Rejecting blurry frames first matters most: it took collector-number accuracy from 32.8% to 78.5% in my harness.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why store a collection as an append-only ledger?
&lt;/h3&gt;

&lt;p&gt;Nothing is overwritten, so a mis-tap or a bug can't destroy data: removal is a tombstone and the item table is rebuilt from the log. Changed upstream ids resolve through an alias table at read time, without touching user data.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>computervision</category>
      <category>machinelearning</category>
    </item>
    <item>
      <title>Planning sourdough backwards in Swift, with quiet hours</title>
      <dc:creator>Ivan Chernoknizhnikov</dc:creator>
      <pubDate>Fri, 20 Nov 2026 09:01:35 +0000</pubDate>
      <link>https://dev.to/treamz/planning-sourdough-backwards-in-swift-with-quiet-hours-8de</link>
      <guid>https://dev.to/treamz/planning-sourdough-backwards-in-swift-with-quiet-hours-8de</guid>
      <description>&lt;p&gt;Proofday is an iPhone sourdough planner: you say when you want bread, and it&lt;br&gt;
works out when to feed the starter, mix, fold, shape and bake, adjusted for&lt;br&gt;
kitchen temperature and kept out of the hours you sleep. It's&lt;br&gt;
&lt;a href="https://apps.apple.com/us/app/proofday-sourdough-planner/id6826169140" rel="noopener noreferrer"&gt;on the App Store&lt;/a&gt;,&lt;br&gt;
and &lt;a href="https://treamz.me/blog/sourdough-schedule-planner-app" rel="noopener noreferrer"&gt;the post for bakers&lt;/a&gt;&lt;br&gt;
covers what it does. This one is about how: the fermentation model, the dynamic&lt;br&gt;
program that plans backwards, how it refuses an impossible time, and the iOS&lt;br&gt;
limits that shaped the reminders.&lt;/p&gt;

&lt;p&gt;The plans below come from running the app's planning package, a Swift package&lt;br&gt;
with no UI, on its Beginner Country Loaf recipe.&lt;/p&gt;
&lt;h2&gt;
  
  
  The fermentation model is one formula
&lt;/h2&gt;

&lt;p&gt;Proofday's model says every 7 °C doubles or halves fermentation time, relative&lt;br&gt;
to 24 °C:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// FermentationConfig.swift&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;referenceTemperatureC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;degreesPerDoubling&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;trustedTemperatureRangeC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ClosedRange&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt;

&lt;span class="c1"&gt;// FermentationModel.swift&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;temperatureFactor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;celsius&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;clamp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;temperatureC&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;celsius&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;pow&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;referenceTemperatureC&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;degreesPerDoubling&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Bulk is 5 hours at 24 °C with 20% levain, so 18 °C gives a factor of 1.81 and&lt;br&gt;
about 9 hours, and 30 °C gives 0.55 and 2.8 hours. Outside 16–30 °C the input is&lt;br&gt;
clamped and the plan carries a warning, so 34 °C plans exactly like 30 °C&lt;br&gt;
rather than trusting an exponential that far out.&lt;/p&gt;

&lt;p&gt;The model's other constants, down to the starter peak table (5 to 14 hours at&lt;br&gt;
24 °C, from 1:1:1 to 1:10:10 feeds), live in the same struct. Its doc comment&lt;br&gt;
calls them "starting heuristics, not lab science".&lt;/p&gt;
&lt;h2&gt;
  
  
  Learning from logged bakes
&lt;/h2&gt;

&lt;p&gt;When a starter peaks or a bulk ends, the app folds actual over predicted into a&lt;br&gt;
per-starter moving average:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;mutating&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;record&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;actual&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TimeInterval&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;baselinePrediction&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TimeInterval&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                            &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;FermentationConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;standard&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Bool&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;baselinePrediction&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;ratio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;actual&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="n"&gt;baselinePrediction&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;correctionSampleSanityRange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;contains&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ratio&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;alpha&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;correctionSmoothing&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;alpha&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;ratio&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;alpha&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
    &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;min&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;correctionRange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;lowerBound&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;config&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;correctionRange&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;upperBound&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;sampleCount&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;α is 0.3, the factor stays within 0.6–1.6, and a ratio outside 0.25–4 counts as&lt;br&gt;
a logging mistake. The part that matters is the argument name. Predictions are&lt;br&gt;
already multiplied by the factor, so a sample has to be compared with the&lt;br&gt;
&lt;em&gt;uncorrected&lt;/em&gt; baseline. Compare with the corrected one and the average settles&lt;br&gt;
at the square root of the true factor: a starter that really takes 1.44 times as&lt;br&gt;
long gets learned as 1.2. So each feeding and each plan stores its uncorrected&lt;br&gt;
estimate up front.&lt;/p&gt;
&lt;h2&gt;
  
  
  Planning backwards is a shortest path
&lt;/h2&gt;

&lt;p&gt;A recipe is a chain of typed steps. Each resolves into a minimum, ideal and&lt;br&gt;
maximum length, hands-on minutes, and the moments the baker acts: the step's&lt;br&gt;
start, each fold, and the oven preheat as a negative offset from the bake. Bulk&lt;br&gt;
doesn't flex at plan time; the aliquot jar decides when it really ends. The&lt;br&gt;
cold proof (8 to 24 hours), autolyse, bench rest, cooling and the levain do.&lt;/p&gt;

&lt;p&gt;Rounding every length to a 5-minute grid turns the search into a dynamic&lt;br&gt;
program. The state is "step i starts at time t", filled in from the ready time&lt;br&gt;
backwards:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nb"&gt;stride&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;from&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;through&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;by&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;levels&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sorted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;by&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;levels&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;vi&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;steps&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;enumerated&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="n"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;duration&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;earliestStart&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;violations&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;of&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;at&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;anchorExempt&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;hard&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="n"&gt;violationCost&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;existing&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;existing&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;cost&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;continue&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Node&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;cost&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;violations&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;node&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;violations&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;link&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;variant&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;vi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isEmpty&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;levels&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;next&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;levels[n]&lt;/code&gt; holds the ready time at cost zero. A variant is one layout of a&lt;br&gt;
step: a length, plus a feed ratio for the levain or a preheat length for the&lt;br&gt;
bake. Keeping only the cheapest path into each boundary is exact because a&lt;br&gt;
step's cost and quiet-hours check depend only on its own start and length. On a&lt;br&gt;
tie the later start wins, so you wait less.&lt;/p&gt;

&lt;p&gt;Quiet hours default to 23:00–07:00, and staying out of them has two shapes. An&lt;br&gt;
action is a point and may sit on the boundary, so a fold at 7:00 is fine. Hands-on minutes are a span and may not&lt;br&gt;
overlap at all, so a 15-minute shape has to start by 22:45.&lt;/p&gt;
&lt;h2&gt;
  
  
  Preferences are just prices
&lt;/h2&gt;

&lt;p&gt;There's no rule saying "stretch the cold proof first". There are costs:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;costPerMinute&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;switch&lt;/span&gt; &lt;span class="kc"&gt;self&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;coldProof&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.05&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;levain&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.15&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;autolyse&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rest&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.2&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;cool&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;roomProof&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.3&lt;/span&gt;
    &lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;benchRest&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mf"&gt;0.5&lt;/span&gt;
    &lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// FermentationConfig&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;feedRatioSwitchCost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;quietHoursViolationCost&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Double&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;10_000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A cold proof minute away from the ideal is the cheapest thing on the list, a&lt;br&gt;
third of a levain minute. A different feed ratio costs 20, as much as moving the&lt;br&gt;
cold proof 400 minutes. So a plan bridges the night with the fridge when it can,&lt;br&gt;
and reaches for a 1:5:5 or 1:10:10 overnight feed when that's cheaper.&lt;/p&gt;

&lt;p&gt;For the Beginner Country Loaf at 23 °C, ready Saturday at 10:00, the 12 steps&lt;br&gt;
expand to 182 variants, 70 of them for the levain alone:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Fri 07:10  levain       465 min  1:2:2
Fri 14:55  autolyse      60 min
Fri 15:55  mix           15 min
Fri 16:10  bulk         330 min  folds 16:40, 17:10, 17:40, 18:10
Fri 21:40  preshape       5 min
Fri 21:45  bench rest    30 min
Fri 22:15  shape         15 min
Fri 22:30  cold proof   555 min  (ideal 720)
Sat 07:45  bake          20 min  preheat from 07:00
Sat 08:05  lid off       25 min
Sat 08:30  cool          90 min  (ideal 120)
Sat 10:00  ready
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The preheat can't start before 7:00, which pins the bake and cuts cooling to 90&lt;br&gt;
minutes; the plan says so in a note. On my M1 Pro, in a release build, that&lt;br&gt;
takes about a millisecond. The app still solves in a detached task, since the&lt;br&gt;
setup screen re-plans on every input change.&lt;/p&gt;

&lt;p&gt;Rough edges: windows longer than two hours are searched in 15-minute&lt;br&gt;
steps, so the cold proof can't be 565 minutes and the levain starts at 7:10&lt;br&gt;
rather than 7:00. And the cold proof used to stop at 16 hours, the figure I&lt;br&gt;
started from, which made dinner-time bread impossible with the default quiet&lt;br&gt;
hours. It now goes to 24: ready at 19:00 on Saturday means shaping at 22:45 on&lt;br&gt;
Friday and a 17 h 15 min proof.&lt;/p&gt;
&lt;h2&gt;
  
  
  Saying no, with a reason and a better time
&lt;/h2&gt;

&lt;p&gt;When the hard solve fails, the planner works out why, in order: the target has&lt;br&gt;
passed, the step minimums don't fit, the target is inside quiet hours, or some&lt;br&gt;
action would be. For that last case it reruns the DP in soft mode, where a&lt;br&gt;
violation costs 10,000 instead of being forbidden, and reports the first action&lt;br&gt;
that lands in quiet hours. Then it walks a 15-minute grid outward,&lt;br&gt;
up to 48 hours each way, until a ready time solves.&lt;/p&gt;

&lt;p&gt;From the package:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Saturday 9:00, asked on Thursday:&lt;/strong&gt; the first violation is the preheat at
6:10 AM. Nearest: 9:30.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Saturday 10:00, asked Friday at 8 PM:&lt;/strong&gt; the bake needs at least 21 h 20 min.
Nearest: Sunday 9:30.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sunday 9:30, asked Saturday at noon:&lt;/strong&gt; shaping would land at 11:30 PM.
Nearest: Monday 9:30. With "My starter is already ripe" on, Sunday 9:30
works, shaping at 7:30 PM.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The third looked like a bug. It isn't: with a levain to build, the&lt;br&gt;
earliest Saturday shape is about half an hour too late, and mixing on Sunday&lt;br&gt;
morning puts the bread after 11 PM. These refusals took roughly 3 to 13 ms, mostly&lt;br&gt;
the search for alternatives.&lt;/p&gt;
&lt;h2&gt;
  
  
  Re-planning while the dough is out
&lt;/h2&gt;

&lt;p&gt;Done, +15 min and Need more time re-run the solver forwards from the current&lt;br&gt;
step, in soft mode, with one more price: each minute the ready time drifts from&lt;br&gt;
the promise costs 1, against 0.05 for a cold proof minute. In the plan above,&lt;br&gt;
asking for 30 more minutes of bulk at 9:30 PM keeps 10:00 and shortens the cold&lt;br&gt;
proof to 525 minutes. Asking for 45 can't be absorbed, because shaping would run&lt;br&gt;
from 23:00 to 23:15. The plan keeps 10:00 and flags that action as inside quiet&lt;br&gt;
hours rather than moving the bread.&lt;/p&gt;
&lt;h2&gt;
  
  
  Notifications, Live Activities and widgets
&lt;/h2&gt;

&lt;p&gt;Every pending action is a local notification, rescheduled after any change.&lt;br&gt;
The triggers are &lt;code&gt;UNTimeIntervalNotificationTrigger&lt;/code&gt;, elapsed seconds rather&lt;br&gt;
than calendar components, so a DST change can't move an absolute moment.&lt;br&gt;
A time-zone change re-solves the plan, since quiet hours follow the wall clock.&lt;br&gt;
Done, +15 min and Snooze 5 min carry no &lt;code&gt;.foreground&lt;/code&gt; option, so&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/usernotifications/declaring-your-actionable-notification-types" rel="noopener noreferrer"&gt;iOS hands them to the app in the background&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Live Activities were the awkward part. One&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/activitykit/displaying-live-data-with-live-activities" rel="noopener noreferrer"&gt;lasts up to eight hours, twelve on the Lock Screen&lt;/a&gt;,&lt;br&gt;
and &lt;a href="https://developer.apple.com/documentation/activitykit/activity/request(attributes:content:pushtype:)" rel="noopener noreferrer"&gt;&lt;code&gt;Activity.request&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
only works while the app is in the foreground. The Saturday plan runs almost 27&lt;br&gt;
hours. So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;startWindow&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;TimeInterval&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;3600&lt;/span&gt;

&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;sync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;planID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BakeSchedule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;presenter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ActionPresenter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Date&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;isComplete&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;state&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;presenter&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;presenter&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;end&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;planID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;planID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ActivityContent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;staleDate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addingTimeInterval&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;30&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;relevanceScore&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="kt"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;update&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;planID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;planID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="nc"&gt;ActivityAuthorizationInfo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;areActivitiesEnabled&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="n"&gt;state&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextDate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeIntervalSince&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="kt"&gt;Self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;startWindow&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="nc"&gt;Activity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;request&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="n"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BakeActivityAttributes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;planID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;planID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;recipeName&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;schedule&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recipeName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;content&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;pushType&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;sync&lt;/code&gt; runs after every plan change and whenever the app comes to the&lt;br&gt;
foreground. It updates a running activity, or starts one when the next action is&lt;br&gt;
under seven hours away, inside the eight-hour limit. There's no push server, so&lt;br&gt;
the content changes only when the app's code runs, but the countdown keeps&lt;br&gt;
moving because it's &lt;code&gt;Text(timerInterval:)&lt;/code&gt;, drawn by the system. The catch,&lt;br&gt;
stated in the user docs: it appears once you open the app within seven hours of&lt;br&gt;
a step. iOS 26 added&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/activitykit/activity/request(attributes:content:pushtype:style:alertconfiguration:start:)" rel="noopener noreferrer"&gt;a &lt;code&gt;request&lt;/code&gt; that takes a start date&lt;/a&gt;&lt;br&gt;
and starts the activity with the app in the background. Proofday supports iOS&lt;br&gt;
17 and doesn't use it.&lt;/p&gt;

&lt;p&gt;Its Done and +15 buttons are &lt;code&gt;LiveActivityIntent&lt;/code&gt;s, which&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/appintents/liveactivityintent" rel="noopener noreferrer"&gt;run in the app's process&lt;/a&gt;,&lt;br&gt;
through a static bridge the app delegate fills at launch. Under Swift 6,&lt;br&gt;
&lt;code&gt;Activity&lt;/code&gt; isn't &lt;code&gt;Sendable&lt;/code&gt;, so its handles stay inside &lt;code&gt;nonisolated&lt;/code&gt; helpers.&lt;/p&gt;

&lt;p&gt;Widgets never open SwiftData: the app writes a Codable snapshot of the next&lt;br&gt;
seven actions to the App Group, and the timeline adds an entry as each comes&lt;br&gt;
due, so the Next bake step widget advances on its own.&lt;/p&gt;

&lt;h2&gt;
  
  
  App Attest for the one server call
&lt;/h2&gt;

&lt;p&gt;Crumb Check, a photo analysis of the cut loaf, is the only server feature: a&lt;br&gt;
small proxy in front of a Claude model. Each install makes a key with&lt;br&gt;
&lt;code&gt;DCAppAttestService&lt;/code&gt; and has Apple attest it once. After that, every request carries an assertion over a single-use challenge plus&lt;br&gt;
the SHA-256 of the body, and the server checks signature, counter and app ID&lt;br&gt;
(&lt;a href="https://developer.apple.com/documentation/devicecheck/establishing-your-app-s-integrity" rel="noopener noreferrer"&gt;Apple's overview&lt;/a&gt;).&lt;br&gt;
It allows 30 checks per install per day and stores only public keys,&lt;br&gt;
challenges and counters.&lt;/p&gt;

&lt;p&gt;What the app does for the people who use it: &lt;a href="https://treamz.me/blog/sourdough-schedule-planner-app" rel="noopener noreferrer"&gt;Sourdough schedule planner that works back from bread time&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  How do you schedule tasks backwards from a deadline with time windows?
&lt;/h3&gt;

&lt;p&gt;Round durations to a grid and run a dynamic program from the deadline back:&lt;br&gt;
the state is "step i starts at time t", each step offers one choice per allowed&lt;br&gt;
length, choices that put an action in a blocked window are dropped, and the&lt;br&gt;
cheapest path into each boundary is kept. Proofday does this on a 5-minute grid.&lt;/p&gt;

&lt;h3&gt;
  
  
  How does Proofday estimate fermentation time at different temperatures?
&lt;/h3&gt;

&lt;p&gt;It multiplies a duration at 24 °C by 2^((24 − T) / 7), so every 7 °C cooler&lt;br&gt;
doubles it: a 5-hour bulk becomes about 9 hours at 18 °C. It's trusted between&lt;br&gt;
16 and 30 °C, and a per-starter factor learned from logged bakes scales it.&lt;/p&gt;

&lt;h3&gt;
  
  
  When can an iOS app start a Live Activity?
&lt;/h3&gt;

&lt;p&gt;With &lt;code&gt;Activity.request&lt;/code&gt;, only while the app is in the foreground, or from a&lt;br&gt;
&lt;code&gt;LiveActivityIntent&lt;/code&gt;. From iOS 26 an app can also schedule one for a start&lt;br&gt;
date. A Live Activity lasts up to eight hours, then can stay on the Lock Screen&lt;br&gt;
for up to four more.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you keep local notifications correct across daylight saving time?
&lt;/h3&gt;

&lt;p&gt;Use &lt;code&gt;UNTimeIntervalNotificationTrigger&lt;/code&gt; with the seconds until the absolute&lt;br&gt;
moment, not calendar components, so a clock change can't shift it. If times&lt;br&gt;
follow the wall clock, re-plan and reschedule on &lt;code&gt;NSSystemTimeZoneDidChange&lt;/code&gt;&lt;br&gt;
and &lt;code&gt;UIApplication.significantTimeChangeNotification&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  How do you learn a correction factor from logged data without bias?
&lt;/h3&gt;

&lt;p&gt;Average the ratio of actual duration to the uncorrected prediction and multiply&lt;br&gt;
predictions by the result. Averaging against the already-corrected prediction&lt;br&gt;
settles at the square root of the true factor. Proofday uses an exponential&lt;br&gt;
moving average with weight 0.3, clamped to 0.6–1.6.&lt;/p&gt;

&lt;h3&gt;
  
  
  What should a scheduler do when a deadline is impossible?
&lt;/h3&gt;

&lt;p&gt;Say why and offer the nearest time that works. Proofday finds the offending&lt;br&gt;
action by re-running its solver with quiet-hour violations penalised instead of&lt;br&gt;
forbidden, then searches a 15-minute grid up to 48 hours either side for the&lt;br&gt;
closest feasible ready time.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>algorithms</category>
      <category>mobile</category>
    </item>
    <item>
      <title>Counting reps with Vision and locking apps with Screen Time</title>
      <dc:creator>Ivan Chernoknizhnikov</dc:creator>
      <pubDate>Tue, 17 Nov 2026 17:38:14 +0000</pubDate>
      <link>https://dev.to/treamz/counting-reps-with-vision-and-locking-apps-with-screen-time-3m5p</link>
      <guid>https://dev.to/treamz/counting-reps-with-vision-and-locking-apps-with-screen-time-3m5p</guid>
      <description>&lt;p&gt;Pull to Refresh is my iPhone app that locks the apps you choose and unlocks them&lt;br&gt;
for exercise reps counted by the camera&lt;br&gt;
(&lt;a href="https://apps.apple.com/us/app/pull-to-refresh-screen-time/id6809189842" rel="noopener noreferrer"&gt;App Store&lt;/a&gt;).&lt;br&gt;
This is the developer side: counting reps from Vision joint positions, what&lt;br&gt;
Apple's Screen Time API lets a third-party app do, and the bugs that taught me&lt;br&gt;
both. What the app does for its users is in&lt;br&gt;
&lt;a href="https://treamz.me/blog/app-blocker-unlock-with-exercise" rel="noopener noreferrer"&gt;the user-facing post&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  One number per exercise, one state machine
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://developer.apple.com/documentation/vision/vndetecthumanbodyposerequest" rel="noopener noreferrer"&gt;&lt;code&gt;VNDetectHumanBodyPoseRequest&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
runs on every frame of a 1280×720 front-camera feed. I keep joints with a&lt;br&gt;
confidence of 0.3 or more and drop the frame; nothing is recorded.&lt;/p&gt;

&lt;p&gt;What kept the counter small: every exercise reduces a pose to one &lt;code&gt;Double&lt;/code&gt;,&lt;br&gt;
oriented so that low means contracted and high means extended. Push-ups, squats&lt;br&gt;
and sit-ups use a joint angle, averaged over both sides of the body when both&lt;br&gt;
are visible. Pull-ups and jumping jacks use the nose's height against the&lt;br&gt;
wrists, divided by torso length, so standing further back doesn't change the&lt;br&gt;
number.&lt;/p&gt;

&lt;p&gt;One state machine then scores all five; each exercise supplies two thresholds&lt;br&gt;
and a speed limit:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Exercise&lt;/th&gt;
&lt;th&gt;Signal&lt;/th&gt;
&lt;th&gt;Contracted&lt;/th&gt;
&lt;th&gt;Extended&lt;/th&gt;
&lt;th&gt;Min. gap&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Push-up&lt;/td&gt;
&lt;td&gt;elbow angle&lt;/td&gt;
&lt;td&gt;100°&lt;/td&gt;
&lt;td&gt;150°&lt;/td&gt;
&lt;td&gt;0.7 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Squat&lt;/td&gt;
&lt;td&gt;knee angle&lt;/td&gt;
&lt;td&gt;110°&lt;/td&gt;
&lt;td&gt;160°&lt;/td&gt;
&lt;td&gt;0.7 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Sit-up&lt;/td&gt;
&lt;td&gt;shoulder–hip–knee angle&lt;/td&gt;
&lt;td&gt;80°&lt;/td&gt;
&lt;td&gt;120°&lt;/td&gt;
&lt;td&gt;0.7 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Pull-up&lt;/td&gt;
&lt;td&gt;hands above nose, in torsos&lt;/td&gt;
&lt;td&gt;0.0&lt;/td&gt;
&lt;td&gt;0.35&lt;/td&gt;
&lt;td&gt;1.0 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Jumping jack&lt;/td&gt;
&lt;td&gt;nose above hands, in torsos&lt;/td&gt;
&lt;td&gt;0.0&lt;/td&gt;
&lt;td&gt;0.8&lt;/td&gt;
&lt;td&gt;0.4 s&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The gap between the thresholds is the hysteresis: the phase flips to&lt;br&gt;
&lt;code&gt;.contracted&lt;/code&gt; below &lt;code&gt;contracted&lt;/code&gt;, and a rep counts only on the way back above&lt;br&gt;
&lt;code&gt;extended&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;case&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;contracted&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extended&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;phase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;extended&lt;/span&gt;
        &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;sample&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;timestamp&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lastRepAt&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;timeIntervalSince&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;last&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;model&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;minRepInterval&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;formHint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Slow down — that one didn't count."&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;lastRepAt&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;now&lt;/span&gt;
        &lt;span class="n"&gt;reps&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;counted&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;reps&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The raw value is smoothed first (&lt;code&gt;smoothed + 0.35 * (raw - smoothed)&lt;/code&gt;), because&lt;br&gt;
joints jitter between frames. A third line, 35% of the way down, catches&lt;br&gt;
half-reps: dip past it and come back without reaching depth, and you get a&lt;br&gt;
hint to go deeper instead of silence. Up to eight frames of missing joints&lt;br&gt;
pass before the screen says it can't see you.&lt;/p&gt;

&lt;p&gt;The tests drive the counter with synthetic geometry: a pose builder that&lt;br&gt;
produces an exact elbow angle, swept through a cosine at 30 fps. Ten clean&lt;br&gt;
push-ups must give 10, half-reps 0 and a hint, and twenty 0.2-second cycles,&lt;br&gt;
someone shaking the phone, at most 4. Not zero: smoothing and the speed floor&lt;br&gt;
limit how fast reps pay, but nothing recognises shaking as such.&lt;/p&gt;

&lt;p&gt;One dead end. I saw zero reps on a screenshot from the start of a squat set,&lt;br&gt;
decided a knee angle can't be read head-on and moved squats to a side view.&lt;br&gt;
Later frames of that set were counting fine. I reverted it the same day and&lt;br&gt;
fixed the setup hint instead, which had been asking people to stand side-on in&lt;br&gt;
front of the selfie camera. Now every exercise opens on the front camera, so the&lt;br&gt;
person doing the reps can see the count.&lt;/p&gt;
&lt;h2&gt;
  
  
  Five targets and an App Group
&lt;/h2&gt;

&lt;p&gt;Screen Time work is split across processes that can't call each other. The app&lt;br&gt;
picks apps with &lt;code&gt;FamilyActivityPicker&lt;/code&gt; and gets opaque tokens, never names. A&lt;br&gt;
&lt;code&gt;DeviceActivityMonitor&lt;/code&gt; extension wakes when an interval starts or ends or a&lt;br&gt;
usage threshold is crossed. Two shield extensions draw the lock screen and&lt;br&gt;
handle its buttons. A&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/deviceactivity/deviceactivityreportextension" rel="noopener noreferrer"&gt;&lt;code&gt;DeviceActivityReport&lt;/code&gt; extension&lt;/a&gt;&lt;br&gt;
is the only process that sees per-app usage, and it can only render a SwiftUI&lt;br&gt;
view: the app shows a "minutes left" card without ever learning the numbers.&lt;/p&gt;

&lt;p&gt;They share state through App Group &lt;code&gt;UserDefaults&lt;/code&gt;, plus a Darwin notification to&lt;br&gt;
tell a running app something changed. Each rule has its own named&lt;br&gt;
&lt;code&gt;ManagedSettingsStore&lt;/code&gt;, so rules shield and lift independently. The monitor is&lt;br&gt;
handed nothing but an activity name, so the name carries the context:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="k"&gt;static&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;make&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ActivityKind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rule&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;DeviceActivityName&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;DeviceActivityName&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"\(kind.rawValue)|\(rule.uuidString)|\(slot)"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="n"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;ActivityKind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;rule&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;rawValue&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;split&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;separator&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;"|"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;omittingEmptySubsequences&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;ActivityKind&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rawValue&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;uuidString&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kt"&gt;String&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kt"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;parts&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;slot&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Two documented limits shape everything: an app and its extensions can monitor&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/deviceactivity/deviceactivitycenter/monitoringerror/excessiveactivities" rel="noopener noreferrer"&gt;twenty activities&lt;/a&gt;,&lt;br&gt;
and an interval can't be shorter than&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/deviceactivity/deviceactivitycenter/monitoringerror/intervaltooshort" rel="noopener noreferrer"&gt;fifteen minutes&lt;/a&gt;.&lt;br&gt;
Schedules repeat daily, not hourly, so "five minutes an hour" costs one&lt;br&gt;
registration per hour of its window. Windows are capped at 16 hours, and one&lt;br&gt;
slot always stays free for unlocks.&lt;/p&gt;
&lt;h2&gt;
  
  
  Callbacks are not transitions
&lt;/h2&gt;

&lt;p&gt;Up to 1.4 the code did the obvious thing: &lt;code&gt;intervalDidStart&lt;/code&gt; lifts the shield,&lt;br&gt;
the threshold puts it back. The docs for&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/deviceactivity/deviceactivitycenter/startmonitoring(_:during:events:)" rel="noopener noreferrer"&gt;&lt;code&gt;startMonitoring&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
say callbacks may arrive as soon as you call it if the interval is running.&lt;br&gt;
Saving a rule re-registered every budget, the registration echoed&lt;br&gt;
&lt;code&gt;intervalDidStart&lt;/code&gt;, and the shield came down. And unless an event opts into&lt;br&gt;
counting past activity, which needs iOS 17.4, it counts usage only from the&lt;br&gt;
moment it's registered, so a save handed a spent allowance back in full.&lt;br&gt;
Stopping an activity echoes &lt;code&gt;intervalDidEnd&lt;/code&gt;, so switching a schedule off&lt;br&gt;
mid-lock shielded the rule I'd just been told was off.&lt;/p&gt;

&lt;p&gt;The fix was to let no callback decide. Each one hands the rule, as it is now,&lt;br&gt;
to one &lt;code&gt;apply&lt;/code&gt;, which asks a pure policy function whether it should be open:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="kd"&gt;override&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="nf"&gt;intervalDidStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;activity&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;DeviceActivityName&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;super&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;intervalDidStart&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;activity&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;guard&lt;/span&gt; &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;decoded&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;activity&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;grant&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;kind&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;warning&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
          &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;rule&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;SharedState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;rule&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;decoded&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;rule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="c1"&gt;// A fresh allowance, a schedule's boundary, or registering this&lt;/span&gt;
    &lt;span class="c1"&gt;// activity echoing straight back. Lifting outright here is what let&lt;/span&gt;
    &lt;span class="c1"&gt;// every save reopen an allowance already spent.&lt;/span&gt;
    &lt;span class="nc"&gt;ShieldEngine&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;apply&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;rule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nc"&gt;StateBridge&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;postChanged&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That works because of a record: when a threshold fires, the monitor writes down&lt;br&gt;
that the allowance is spent for the current day or hour, so an echo finds the&lt;br&gt;
rule spent and keeps it shut. A save now registers only the rule saved, and&lt;br&gt;
only when something iOS enforces changed. From iOS 17.4, budgets pass&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/deviceactivity/deviceactivityevent/init(applications:categories:webdomains:threshold:includespastactivity:)" rel="noopener noreferrer"&gt;&lt;code&gt;includesPastActivity: true&lt;/code&gt;&lt;/a&gt;&lt;br&gt;
and count the whole day or hour however late they're registered.&lt;/p&gt;

&lt;p&gt;Earned time is a one-shot activity whose event threshold is the time bought.&lt;br&gt;
The threshold counts use of those apps, so putting the phone down doesn't burn&lt;br&gt;
paid minutes. The interval end is a backstop: four times the unlock, between 30&lt;br&gt;
minutes and 22 hours. It's registered before anything is stored or lifted; 1.4&lt;br&gt;
did it the other way round, so a refusal at the twenty-activity limit left the&lt;br&gt;
rule open with nothing to close it. And unlock records once lived in one array&lt;br&gt;
that three processes read and wrote whole, so one could resurrect an unlock&lt;br&gt;
another had just ended. Each record has its own key now.&lt;/p&gt;
&lt;h2&gt;
  
  
  From the shield back into the app
&lt;/h2&gt;

&lt;p&gt;The shield's button belongs to the shield action extension, and for years it&lt;br&gt;
couldn't launch its containing app. The only way back was a local notification,&lt;br&gt;
easy to miss with a Focus on. My first version posted it and returned &lt;code&gt;.close&lt;/code&gt;&lt;br&gt;
in the same breath; closing the shield lets iOS terminate the extension, and the&lt;br&gt;
notification sometimes went with it.&lt;/p&gt;

&lt;p&gt;iOS 26.5 added&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/managedsettings/shieldactionresponse/openparentalcontrolsapp" rel="noopener noreferrer"&gt;&lt;code&gt;ShieldActionResponse.openParentalControlsApp&lt;/code&gt;&lt;/a&gt;.&lt;br&gt;
Both paths:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="nc"&gt;SharedState&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;setPendingUnlock&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;targets&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;\&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="cp"&gt;#available(iOS 26.5, *) {&lt;/span&gt;
    &lt;span class="c1"&gt;// Straight into the workout screen, with the rule already chosen.&lt;/span&gt;
    &lt;span class="nf"&gt;completionHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;openParentalControlsApp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Closing the shield lets iOS terminate this extension, so the&lt;/span&gt;
&lt;span class="c1"&gt;// request has to be handed over before that happens.&lt;/span&gt;
&lt;span class="nc"&gt;Task&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;notify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;title&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ruleID&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;carriedRule&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;completionHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;close&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The pending unlock goes into the App Group first, so the app opens on the&lt;br&gt;
workout screen and spends the next set on that rule. With a minute already&lt;br&gt;
banked, the button just spends it. The new case needs the iOS&lt;br&gt;
26.5 SDK, so CI moved to macOS 26 runners. On older iOS with notifications off&lt;br&gt;
there's no way back, and the app warns about it.&lt;/p&gt;

&lt;p&gt;None of it ships without &lt;code&gt;com.apple.developer.family-controls&lt;/code&gt;. The development&lt;br&gt;
entitlement comes without asking and TestFlight worked with it; distribution is&lt;br&gt;
by request, and&lt;br&gt;
&lt;a href="https://developer.apple.com/documentation/familycontrols/requesting-the-family-controls-entitlement" rel="noopener noreferrer"&gt;Apple's page&lt;/a&gt;&lt;br&gt;
says to file the same request for every Screen Time extension.&lt;/p&gt;
&lt;h2&gt;
  
  
  Pricing a rep
&lt;/h2&gt;

&lt;p&gt;A clean rep earns 10 seconds for a jumping jack, 20 for a squat or sit-up, 30&lt;br&gt;
for a push-up and 90 for a pull-up. Steps move the rate (×1.15, ×1.3, ×1.6 at&lt;br&gt;
3,000, 8,000 and 15,000), each streak day adds 0.05 up to eight, and the total&lt;br&gt;
caps at ×2. The rate locks when a set starts.&lt;/p&gt;

&lt;p&gt;I started with "only reps buy time", and a test asserts no number of steps pays&lt;br&gt;
a second without a rep. Then a user asked for a way in for people who can't do a&lt;br&gt;
push-up today, so an opt-in mode now lets 1,000 steps buy 3 minutes, up to 30 a&lt;br&gt;
day, with steps dropped from the multiplier so one walk is paid once. HealthKit&lt;br&gt;
reports a running total, so what today already paid is subtracted, and a total&lt;br&gt;
that drops never takes minutes back.&lt;/p&gt;

&lt;p&gt;Steps typed into Health by hand don't count. My first predicate for that summed&lt;br&gt;
to zero for everyone:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Must be a negated match, not a "not equal to" match.&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;typedIn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;HKQuery&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;predicateForObjects&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="n"&gt;withMetadataKey&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HKMetadataKeyWasUserEntered&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;allowedValues&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="n"&gt;notTypedIn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NSCompoundPredicate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;notPredicateWithSubpredicate&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;typedIn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A not-equal match only considers samples that carry the key, and step samples&lt;br&gt;
from an iPhone or Watch don't.&lt;/p&gt;

&lt;p&gt;None of this is unbreakable: deleting the app removes every lock, and a camera&lt;br&gt;
can be fooled.&lt;/p&gt;

&lt;p&gt;What the app does for the people who use it: &lt;a href="https://treamz.me/blog/app-blocker-unlock-with-exercise" rel="noopener noreferrer"&gt;App blocker you unlock with push-ups: Pull to Refresh&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Can you count push-ups with Apple's Vision framework?
&lt;/h3&gt;

&lt;p&gt;Yes. &lt;code&gt;VNDetectHumanBodyPoseRequest&lt;/code&gt; returns shoulder, elbow and wrist positions&lt;br&gt;
per frame, and the elbow angle follows from them. Pull to Refresh counts a&lt;br&gt;
push-up, on the device, when the angle drops below 100° and comes back above&lt;br&gt;
150°, with smoothing and at least 0.7 seconds between reps.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can a Screen Time shield open the app that blocked it?
&lt;/h3&gt;

&lt;p&gt;From iOS 26.5, yes: a &lt;code&gt;ShieldActionDelegate&lt;/code&gt; can answer with&lt;br&gt;
&lt;code&gt;ShieldActionResponse.openParentalControlsApp&lt;/code&gt;. Before that, the fallback is a&lt;br&gt;
local notification, and the extension should wait until the request is added&lt;br&gt;
before calling its completion handler, or iOS may end it first.&lt;/p&gt;

&lt;h3&gt;
  
  
  How many DeviceActivity schedules can an iOS app monitor at once?
&lt;/h3&gt;

&lt;p&gt;Twenty, across the app and its extensions; past that, &lt;code&gt;startMonitoring&lt;/code&gt; throws&lt;br&gt;
&lt;code&gt;excessiveActivities&lt;/code&gt;. Each interval must last at least fifteen minutes, and since&lt;br&gt;
schedules repeat daily, an hourly allowance needs one activity per hour.&lt;/p&gt;

&lt;h3&gt;
  
  
  Can an iOS app read how long someone used another app?
&lt;/h3&gt;

&lt;p&gt;Not directly. Per-app usage goes only to a &lt;code&gt;DeviceActivityReport&lt;/code&gt; extension,&lt;br&gt;
which can render a SwiftUI view from it but can't pass the numbers back. Apps&lt;br&gt;
arrive as opaque tokens, not names.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does the Screen Time API work in the iOS Simulator?
&lt;/h3&gt;

&lt;p&gt;No. FamilyControls, ManagedSettings and DeviceActivity have no Simulator&lt;br&gt;
implementation, so authorization fails and no shield appears. You need a&lt;br&gt;
physical iPhone.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do Screen Time extensions need their own Family Controls entitlement request?
&lt;/h3&gt;

&lt;p&gt;Yes. Apple's documentation says to submit the same distribution request for&lt;br&gt;
each Screen Time extension: Device Activity Monitor, Device Activity Report,&lt;br&gt;
Shield Action and Shield Configuration.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>computervision</category>
      <category>showdev</category>
    </item>
    <item>
      <title>How to play animated SVG in Flutter (flutter_svg won't)</title>
      <dc:creator>Ivan Chernoknizhnikov</dc:creator>
      <pubDate>Wed, 11 Nov 2026 20:32:14 +0000</pubDate>
      <link>https://dev.to/treamz/how-to-play-animated-svg-in-flutter-fluttersvg-wont-4m1p</link>
      <guid>https://dev.to/treamz/how-to-play-animated-svg-in-flutter-fluttersvg-wont-4m1p</guid>
      <description>&lt;p&gt;&lt;strong&gt;Short answer:&lt;/strong&gt; flutter_svg doesn't play SVG animations: it skips SMIL&lt;br&gt;
elements like &lt;code&gt;&amp;lt;animate&amp;gt;&lt;/code&gt; and ignores &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt;, so the file shows as a still&lt;br&gt;
picture. To play the animation that's already in the SVG, add&lt;br&gt;
&lt;a href="https://pub.dev/packages/svg_animate" rel="noopener noreferrer"&gt;svg_animate&lt;/a&gt; and use&lt;br&gt;
&lt;code&gt;AnimatedSvgPicture&lt;/code&gt; where you'd use &lt;code&gt;SvgPicture&lt;/code&gt;. It takes the same arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;flutter pub add svg_animate
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="nc"&gt;AnimatedSvgPicture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'assets/spinner.svg'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It plays SMIL, CSS &lt;code&gt;@keyframes&lt;/code&gt; and CSS motion paths, in pure Dart. The rest of&lt;br&gt;
this post is how it works, the three bugs real files exposed, and how it&lt;br&gt;
compares with Lottie, Rive and the other SVG packages.&lt;/p&gt;

&lt;p&gt;Our designer sent me a folder of animated SVGs. A spinner, a progress bar, a&lt;br&gt;
few icons. Nice stuff, all of it animated right there in the file with plain&lt;br&gt;
CSS and SMIL.&lt;/p&gt;

&lt;p&gt;I dropped them into flutter_svg and got static pictures.&lt;/p&gt;

&lt;p&gt;I spent an evening convinced I'd broken something. Wrong widget, wrong asset&lt;br&gt;
path, something dumb. Turns out no: flutter_svg just skips &lt;code&gt;&amp;lt;animate&amp;gt;&lt;/code&gt;. And it&lt;br&gt;
ignores &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; too, which is worse, because that means a stylesheet-driven&lt;br&gt;
SVG doesn't even render with the right colors, let alone move.&lt;/p&gt;

&lt;p&gt;The usual answer is "convert it to Lottie". I didn't want to go back to the&lt;br&gt;
designer and ask him to rebuild everything in After Effects when the animation&lt;br&gt;
was already sitting in the file he'd sent me. So I wrote svg_animate.&lt;/p&gt;
&lt;h2&gt;
  
  
  How svg_animate works: a flipbook of static SVGs
&lt;/h2&gt;

&lt;p&gt;flutter_svg draws SVGs by compiling them into a binary format with&lt;br&gt;
&lt;code&gt;vector_graphics_compiler&lt;/code&gt;, then replaying that. That compiler is good. I had no&lt;br&gt;
interest in writing a renderer.&lt;/p&gt;

&lt;p&gt;So: don't. Take the animated document, work out what every animated attribute&lt;br&gt;
is at time &lt;em&gt;t&lt;/em&gt;, write those values back into the markup, and hand the result to&lt;br&gt;
the existing compiler. Do that once per frame ahead of time, then flip through&lt;br&gt;
the compiled frames like a flipbook.&lt;/p&gt;

&lt;p&gt;A frame costs exactly what a static SVG costs to draw, because it &lt;em&gt;is&lt;/em&gt; a static&lt;br&gt;
SVG. You pay at load instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="nc"&gt;AnimatedSvgPicture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'assets/spinner.svg'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same arguments as &lt;code&gt;SvgPicture&lt;/code&gt;. Reuses flutter_svg's &lt;code&gt;SvgTheme&lt;/code&gt; and&lt;br&gt;
&lt;code&gt;ColorMapper&lt;/code&gt; so nothing in an existing app breaks. If the file has no animation&lt;br&gt;
in it, it renders like a normal &lt;code&gt;SvgPicture&lt;/code&gt; and doesn't even start a ticker.&lt;/p&gt;

&lt;p&gt;Two evenings later the spinner was spinning. I felt great about myself for&lt;br&gt;
roughly a week.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bug 1: an SVGator banner that rendered blank
&lt;/h2&gt;

&lt;p&gt;A 450×450 promo banner exported from SVGator. Coins, sparkles, a train, a&lt;br&gt;
"BUY BONUS" badge. 90 KB of markup with five WebP images base64'd into it.&lt;/p&gt;

&lt;p&gt;It rendered as nothing at all. Not "wrong" — a blank box.&lt;/p&gt;

&lt;p&gt;Here's where I want to be honest about how I debugged it, because I got it wrong&lt;br&gt;
first. I built a minimal test case: a tiny SVG with one &lt;code&gt;&amp;lt;image&amp;gt;&lt;/code&gt; in it,&lt;br&gt;
referenced through &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt;. Nothing drew. I tried the same file through plain&lt;br&gt;
flutter_svg. Nothing drew there either. Great, I thought, images just don't work&lt;br&gt;
on this pipeline, not my problem.&lt;/p&gt;

&lt;p&gt;That conclusion was garbage. I'd hand-rolled the test PNG as a base64 string and&lt;br&gt;
it was truncated — zlib couldn't even decompress it. I'd proven nothing except&lt;br&gt;
that a broken PNG doesn't render.&lt;/p&gt;

&lt;p&gt;Generated a real one. Ran it again. Images render fine. &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt; pointing at an&lt;br&gt;
image renders too, just at the wrong size.&lt;/p&gt;

&lt;p&gt;So back to the real file, this time properly: feed the raw SVG straight into&lt;br&gt;
&lt;code&gt;vector_graphics_compiler&lt;/code&gt;, no code of mine involved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Failed assertion: 'width &amp;gt; 0 &amp;amp;&amp;amp; height &amp;gt; 0'  (writeDrawImage)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It dies in the compiler. Before any animation logic. &lt;code&gt;SvgPicture&lt;/code&gt; would have&lt;br&gt;
shown the same blank box.&lt;/p&gt;

&lt;p&gt;Bisecting the file by deleting things: strip the 19 &lt;code&gt;&amp;lt;use href="#img"&amp;gt;&lt;/code&gt;&lt;br&gt;
references and it compiles. The compiler loses an image's declared width and&lt;br&gt;
height when the image is reached through a reference, gets zero, and then&lt;br&gt;
refuses to draw something of zero size — killing the whole picture rather than&lt;br&gt;
that one element.&lt;/p&gt;

&lt;p&gt;Fix on my side: expand a &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt; that points at an &lt;code&gt;&amp;lt;image&amp;gt;&lt;/code&gt; into the image&lt;br&gt;
itself before compiling. Only images — a &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt; on a shape is fine, and&lt;br&gt;
copying those would just bloat the document.&lt;/p&gt;

&lt;p&gt;The banner rendered. Coins and all.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bug 2: slow frames and 27.5 MB of memory
&lt;/h2&gt;

&lt;p&gt;Every compiled frame carries the images inside it, because each frame is a&lt;br&gt;
self-contained picture. 82% of each 96 KB frame was image payload. At the&lt;br&gt;
default 60 fps that's 300 frames and &lt;strong&gt;27.5 MB&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Worse than the memory: changing frame cost &lt;strong&gt;6.8 ms&lt;/strong&gt;. The frame budget at 60fps&lt;br&gt;
is 16.7 ms. That's 40% of it, just to decode, on an M-series Mac. On a mid-range&lt;br&gt;
phone it would have blown straight through.&lt;/p&gt;

&lt;p&gt;Two fixes, and I'm not proud of the first one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The image cache.&lt;/strong&gt; I went reading how vector_graphics decodes images and found&lt;br&gt;
this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;FlutterVectorGraphicsListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;hashCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;_createImageKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;imageId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;imageId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It keys Flutter's global image cache by the hash code of the loader you hand it.&lt;br&gt;
And mine was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;get&lt;/span&gt; &lt;span class="n"&gt;hashCode&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;identityHashCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frames&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;frameIndex&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;frameIndex&lt;/code&gt; in there. Every single frame opened its own namespace in the cache&lt;br&gt;
and re-decoded the same five bitmaps. I had disabled a cache that was sitting&lt;br&gt;
right there, with my own line of code, and then measured the result and blamed&lt;br&gt;
the architecture.&lt;/p&gt;

&lt;p&gt;Dropped &lt;code&gt;frameIndex&lt;/code&gt;. Frames are still unequal to each other — that's what &lt;code&gt;==&lt;/code&gt;&lt;br&gt;
is for — and two unequal objects sharing a hash code is perfectly legal Dart.&lt;br&gt;
Here it's the whole point. 6.8 ms → 1.4 ms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The duplicated bytes.&lt;/strong&gt; For memory I nearly went and wrote a parser for the&lt;br&gt;
binary format to find and hoist the image section. Then I realised I didn't need&lt;br&gt;
to understand the format at all: consecutive frames of one animation are the&lt;br&gt;
same document with a few numbers changed, and the encoder puts everything the&lt;br&gt;
picture embeds at the front. So just compare the frames byte by byte and find&lt;br&gt;
the longest run they all start with.&lt;/p&gt;

&lt;p&gt;On the banner that run is 78,772 bytes. The compiler reports 78,538 bytes of&lt;br&gt;
images. It found them, without knowing what it was looking at.&lt;/p&gt;

&lt;p&gt;Store that once, keep only the tails, glue a frame back together when it's&lt;br&gt;
asked for. 27.5 MB → 5.1 MB, and reassembling costs about 0.1 ms.&lt;/p&gt;

&lt;p&gt;I like this one because it doesn't depend on the encoder's layout. If the format&lt;br&gt;
ever stops front-loading resources, the shared run just gets shorter and nothing&lt;br&gt;
breaks.&lt;/p&gt;
&lt;h2&gt;
  
  
  Bug 3: a CSS spinner with no 0% keyframe
&lt;/h2&gt;

&lt;p&gt;Last one, and it's the most embarrassing, because it's the most common CSS&lt;br&gt;
animation on earth:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="nt"&gt;spin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;360deg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No &lt;code&gt;0%&lt;/code&gt;. My code filled the missing end from the element's own &lt;code&gt;transform&lt;/code&gt;&lt;br&gt;
attribute, and when the element didn't have one, it fell back to... the other&lt;br&gt;
keyframe. So both ends said 360 and the thing sat perfectly still while&lt;br&gt;
everything around it animated.&lt;/p&gt;

&lt;p&gt;The missing end has to come from the value the property has &lt;em&gt;outside&lt;/em&gt; the&lt;br&gt;
animation — no rotation for a transform, fully opaque for an opacity. With one&lt;br&gt;
wrinkle: you can't substitute an empty transform, because two transform lists&lt;br&gt;
only interpolate if they're built the same way. The stand-in has to keep the&lt;br&gt;
shape: &lt;code&gt;translate(0 0)&lt;/code&gt; against a translate, &lt;code&gt;scale(1)&lt;/code&gt; against a scale.&lt;/p&gt;
&lt;h2&gt;
  
  
  What svg_animate doesn't support
&lt;/h2&gt;

&lt;p&gt;Filters. At all. &lt;code&gt;vector_graphics&lt;/code&gt; drops &lt;code&gt;&amp;lt;filter&amp;gt;&lt;/code&gt; entirely, so drop shadows&lt;br&gt;
and glows just don't happen — the shapes draw, the effect doesn't. Same for&lt;br&gt;
&lt;code&gt;mix-blend-mode: plus-lighter&lt;/code&gt;, which is exactly what editors reach for to make&lt;br&gt;
something glow.&lt;/p&gt;

&lt;p&gt;Also no &lt;code&gt;d&lt;/code&gt; morphing, no &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;, no &lt;code&gt;:hover&lt;/code&gt;, no event-based &lt;code&gt;begin&lt;/code&gt;. If you&lt;br&gt;
need any of that, &lt;a href="https://pub.dev/packages/full_svg_flutter" rel="noopener noreferrer"&gt;full_svg_flutter&lt;/a&gt;&lt;br&gt;
covers a lot more of the format. It's a good package. It also bundles a&lt;br&gt;
JavaScript runtime, which is a fair price for what it does and not one I wanted&lt;br&gt;
to pay for a spinner.&lt;/p&gt;

&lt;p&gt;Mine adds two pure-Dart packages, both already in flutter_svg's dependency tree.&lt;/p&gt;
&lt;h2&gt;
  
  
  svg_animate vs Lottie, Rive and other Flutter SVG packages
&lt;/h2&gt;

&lt;p&gt;"Animated SVG" means different things in different packages. Only some of them&lt;br&gt;
play the animation that's already written into the file:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Package&lt;/th&gt;
&lt;th&gt;What it animates&lt;/th&gt;
&lt;th&gt;Plays the animation already in the SVG&lt;/th&gt;
&lt;th&gt;Notes&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;svg_animate&lt;/td&gt;
&lt;td&gt;SMIL, CSS &lt;code&gt;@keyframes&lt;/code&gt;, CSS motion paths&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Pure Dart, on flutter_svg's own compiler; no filters&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;flutter_svg&lt;/td&gt;
&lt;td&gt;Nothing: draws a still picture&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Skips &lt;code&gt;&amp;lt;animate&amp;gt;&lt;/code&gt;, ignores &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;lottie&lt;/td&gt;
&lt;td&gt;Lottie JSON files&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;The animation has to be rebuilt in After Effects and exported as Lottie&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;rive&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;.riv&lt;/code&gt; files from the Rive editor&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;The animation has to be rebuilt in Rive&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;anim_svg&lt;/td&gt;
&lt;td&gt;SMIL and CSS &lt;code&gt;@keyframes&lt;/code&gt;, converted to Lottie at runtime&lt;/td&gt;
&lt;td&gt;Yes, except SVGator's script-driven exports&lt;/td&gt;
&lt;td&gt;Renders with the native thorvg engine through FFI&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;full_svg_flutter&lt;/td&gt;
&lt;td&gt;Much more of SVG&lt;/td&gt;
&lt;td&gt;Yes&lt;/td&gt;
&lt;td&gt;Bundles a JavaScript runtime&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;animated_svg&lt;/td&gt;
&lt;td&gt;A transition between two SVG images, such as play to pause&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;For icon toggles, not animated files&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;drawing_animation&lt;/td&gt;
&lt;td&gt;A line-drawing effect on &lt;code&gt;&amp;lt;path&amp;gt;&lt;/code&gt; strokes&lt;/td&gt;
&lt;td&gt;No&lt;/td&gt;
&lt;td&gt;Draws paths progressively&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If your designer already animated the file, in SVGator, by hand, or anywhere&lt;br&gt;
else that writes SMIL or CSS, use a package from the "Yes" rows. If you're&lt;br&gt;
starting from scratch and want a timeline editor, Lottie or Rive is the better&lt;br&gt;
tool.&lt;/p&gt;
&lt;h2&gt;
  
  
  Install svg_animate from pub.dev
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pub.dev/packages/svg_animate" rel="noopener noreferrer"&gt;pub.dev/packages/svg_animate&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SMIL, CSS &lt;code&gt;@keyframes&lt;/code&gt;, CSS motion paths (which is how SVGator writes every&lt;br&gt;
movement, so its exports work), play/pause/seek through a controller. It's one&lt;br&gt;
of the &lt;a href="https://treamz.me/projects/#svg-animate" rel="noopener noreferrer"&gt;projects I maintain&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you've got an SVG that renders wrong, send it. Every single thing in this&lt;br&gt;
post came from someone's file not working.&lt;/p&gt;
&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;
&lt;h3&gt;
  
  
  Does flutter_svg support animated SVGs?
&lt;/h3&gt;

&lt;p&gt;No. flutter_svg draws an SVG as a still picture: SMIL elements such as&lt;br&gt;
&lt;code&gt;&amp;lt;animate&amp;gt;&lt;/code&gt; are skipped, and &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; blocks are ignored entirely, so CSS&lt;br&gt;
&lt;code&gt;@keyframes&lt;/code&gt; don't play and colours set in a stylesheet don't apply either.&lt;/p&gt;
&lt;h3&gt;
  
  
  How do I play an animated SVG in Flutter?
&lt;/h3&gt;

&lt;p&gt;Add svg_animate with &lt;code&gt;flutter pub add svg_animate&lt;/code&gt; and use&lt;br&gt;
&lt;code&gt;AnimatedSvgPicture&lt;/code&gt; where you would use &lt;code&gt;SvgPicture&lt;/code&gt;. It takes the same&lt;br&gt;
arguments:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="nc"&gt;AnimatedSvgPicture&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;'assets/spinner.svg'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It plays the SMIL and CSS keyframe animations in the file, and an&lt;br&gt;
&lt;code&gt;AnimatedSvgController&lt;/code&gt; adds play, pause and seek. A file with no animation in&lt;br&gt;
it renders like a normal &lt;code&gt;SvgPicture&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Do I need Lottie to animate an SVG in Flutter?
&lt;/h3&gt;

&lt;p&gt;Not when the animation is already in the SVG. Files written with SMIL or CSS&lt;br&gt;
&lt;code&gt;@keyframes&lt;/code&gt;, including exports from SVGator, play as they are with&lt;br&gt;
svg_animate, with no round trip through After Effects.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's the difference between svg_animate and animated_svg?
&lt;/h3&gt;

&lt;p&gt;They solve different problems. svg_animate plays the animation inside an SVG&lt;br&gt;
file: SMIL elements and CSS &lt;code&gt;@keyframes&lt;/code&gt;. animated_svg doesn't read animation&lt;br&gt;
from the file; it transitions between two separate SVG images, such as a play&lt;br&gt;
icon turning into a pause icon.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does svg_animate work with SVGator exports?
&lt;/h3&gt;

&lt;p&gt;Yes, with SVGator's CSS export: SVGator writes movement as CSS motion paths,&lt;br&gt;
which svg_animate plays, and the image-heavy banner in this post is an SVGator&lt;br&gt;
export. SVGator's JavaScript export depends on an embedded script, and&lt;br&gt;
svg_animate doesn't run &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which SVG animation features does svg_animate support?
&lt;/h3&gt;

&lt;p&gt;SMIL (&lt;code&gt;&amp;lt;animate&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;animateTransform&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;animateMotion&amp;gt;&lt;/code&gt; and &lt;code&gt;&amp;lt;set&amp;gt;&lt;/code&gt;), CSS&lt;br&gt;
&lt;code&gt;@keyframes&lt;/code&gt; and CSS motion paths. It interpolates numbers, colours, lengths&lt;br&gt;
and transform lists, and it loads SVGs from assets, the network, files, memory&lt;br&gt;
or a string.&lt;/p&gt;

&lt;h3&gt;
  
  
  What doesn't svg_animate support?
&lt;/h3&gt;

&lt;p&gt;SVG filters, so no drop shadows or glows; &lt;code&gt;mix-blend-mode: plus-lighter&lt;/code&gt;;&lt;br&gt;
morphing a path's &lt;code&gt;d&lt;/code&gt; attribute; &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;; &lt;code&gt;:hover&lt;/code&gt;; and event-based &lt;code&gt;begin&lt;/code&gt;.&lt;br&gt;
For those, &lt;a href="https://pub.dev/packages/full_svg_flutter" rel="noopener noreferrer"&gt;full_svg_flutter&lt;/a&gt; covers&lt;br&gt;
more of the format, at the cost of bundling a JavaScript runtime.&lt;/p&gt;

&lt;h3&gt;
  
  
  Is svg_animate fast enough for production?
&lt;/h3&gt;

&lt;p&gt;Every frame is compiled ahead of time and costs the same to draw as a static&lt;br&gt;
SVG. On the image-heavy banner in this post, switching frames went from 6.8 ms&lt;br&gt;
to 1.4 ms once decoded images were cached, and memory went from 27.5 MB to&lt;br&gt;
5.1 MB once the bytes every frame shares were stored only once.&lt;/p&gt;

&lt;h3&gt;
  
  
  Which platforms does svg_animate run on?
&lt;/h3&gt;

&lt;p&gt;Android, iOS, macOS, Windows and Linux.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>svg</category>
      <category>opensource</category>
    </item>
    <item>
      <title>flutter_svg doesn’t animate. so I wrote the part that does.</title>
      <dc:creator>Ivan Chernoknizhnikov</dc:creator>
      <pubDate>Thu, 20 Aug 2026 15:47:52 +0000</pubDate>
      <link>https://dev.to/treamz/fluttersvg-doesnt-animate-so-i-wrote-the-part-that-does-3kjg</link>
      <guid>https://dev.to/treamz/fluttersvg-doesnt-animate-so-i-wrote-the-part-that-does-3kjg</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fotb4yzfov2528lf3lwrq.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fotb4yzfov2528lf3lwrq.gif" alt=" " width="300" height="300"&gt;&lt;/a&gt;&lt;br&gt;
Our designer sent me a folder of animated SVGs. A spinner, a progress bar, a&lt;br&gt;
few icons. Nice stuff, all of it animated right there in the file with plain&lt;br&gt;
CSS and SMIL.&lt;/p&gt;

&lt;p&gt;I dropped them into flutter_svg and got static pictures.&lt;/p&gt;

&lt;p&gt;I spent an evening convinced I'd broken something. Wrong widget, wrong asset&lt;br&gt;
path, something dumb. Turns out no: flutter_svg just skips &lt;code&gt;&amp;lt;animate&amp;gt;&lt;/code&gt;. And it&lt;br&gt;
ignores &lt;code&gt;&amp;lt;style&amp;gt;&lt;/code&gt; too, which is worse, because that means a stylesheet-driven&lt;br&gt;
SVG doesn't even render with the right colors, let alone move.&lt;/p&gt;

&lt;p&gt;The usual answer is "convert it to Lottie". I didn't want to go back to the&lt;br&gt;
designer and ask him to rebuild everything in After Effects when the animation&lt;br&gt;
was already sitting in the file he'd sent me. So I wrote svg_animate.&lt;/p&gt;
&lt;h2&gt;
  
  
  the idea is embarrassingly simple
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F59hn734000rao3c513wj.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2F59hn734000rao3c513wj.webp" alt=" " width="800" height="231"&gt;&lt;/a&gt;&lt;br&gt;
flutter_svg draws SVGs by compiling them into a binary format with&lt;br&gt;
&lt;code&gt;vector_graphics_compiler&lt;/code&gt;, then replaying that. That compiler is good. I had no&lt;br&gt;
interest in writing a renderer.&lt;/p&gt;

&lt;p&gt;So: don't. Take the animated document, work out what every animated attribute&lt;br&gt;
is at time &lt;em&gt;t&lt;/em&gt;, write those values back into the markup, and hand the result to&lt;br&gt;
the existing compiler. Do that once per frame ahead of time, then flip through&lt;br&gt;
the compiled frames like a flipbook.&lt;/p&gt;

&lt;p&gt;A frame costs exactly what a static SVG costs to draw, because it &lt;em&gt;is&lt;/em&gt; a static&lt;br&gt;
SVG. You pay at load instead.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="n"&gt;AnimatedSvgPicture&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asset&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;'assets/spinner.svg'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nl"&gt;width:&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Same arguments as &lt;code&gt;SvgPicture&lt;/code&gt;. Reuses flutter_svg's &lt;code&gt;SvgTheme&lt;/code&gt; and&lt;br&gt;
&lt;code&gt;ColorMapper&lt;/code&gt; so nothing in an existing app breaks. If the file has no animation&lt;br&gt;
in it, it renders like a normal &lt;code&gt;SvgPicture&lt;/code&gt; and doesn't even start a ticker.&lt;/p&gt;

&lt;p&gt;Two evenings later the spinner was spinning. I felt great about myself for&lt;br&gt;
roughly a week.&lt;/p&gt;
&lt;h2&gt;
  
  
  then someone gave me a real file
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs9qjhhxz56mniq68bym5.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fs9qjhhxz56mniq68bym5.webp" alt=" " width="450" height="450"&gt;&lt;/a&gt;&lt;br&gt;
A 450×450 promo banner exported from SVGator. Coins, sparkles, a train, a&lt;br&gt;
"BUY BONUS" badge. 90 KB of markup with five WebP images base64'd into it.&lt;/p&gt;

&lt;p&gt;It rendered as nothing at all. Not "wrong" — a blank box.&lt;/p&gt;

&lt;p&gt;Here's where I want to be honest about how I debugged it, because I got it wrong&lt;br&gt;
first. I built a minimal test case: a tiny SVG with one &lt;code&gt;&amp;lt;image&amp;gt;&lt;/code&gt; in it,&lt;br&gt;
referenced through &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt;. Nothing drew. I tried the same file through plain&lt;br&gt;
flutter_svg. Nothing drew there either. Great, I thought, images just don't work&lt;br&gt;
on this pipeline, not my problem.&lt;/p&gt;

&lt;p&gt;That conclusion was garbage. I'd hand-rolled the test PNG as a base64 string and&lt;br&gt;
it was truncated — zlib couldn't even decompress it. I'd proven nothing except&lt;br&gt;
that a broken PNG doesn't render.&lt;/p&gt;

&lt;p&gt;Generated a real one. Ran it again. Images render fine. &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt; pointing at an&lt;br&gt;
image renders too, just at the wrong size.&lt;/p&gt;

&lt;p&gt;So back to the real file, this time properly: feed the raw SVG straight into&lt;br&gt;
&lt;code&gt;vector_graphics_compiler&lt;/code&gt;, no code of mine involved.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Failed assertion: 'width &amp;gt; 0 &amp;amp;&amp;amp; height &amp;gt; 0'  (writeDrawImage)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It dies in the compiler. Before any animation logic. &lt;code&gt;SvgPicture&lt;/code&gt; would have&lt;br&gt;
shown the same blank box.&lt;/p&gt;

&lt;p&gt;Bisecting the file by deleting things: strip the 19 &lt;code&gt;&amp;lt;use href="#img"&amp;gt;&lt;/code&gt;&lt;br&gt;
references and it compiles. The compiler loses an image's declared width and&lt;br&gt;
height when the image is reached through a reference, gets zero, and then&lt;br&gt;
refuses to draw something of zero size — killing the whole picture rather than&lt;br&gt;
that one element.&lt;/p&gt;

&lt;p&gt;Fix on my side: expand a &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt; that points at an &lt;code&gt;&amp;lt;image&amp;gt;&lt;/code&gt; into the image&lt;br&gt;
itself before compiling. Only images — a &lt;code&gt;&amp;lt;use&amp;gt;&lt;/code&gt; on a shape is fine, and&lt;br&gt;
copying those would just bloat the document.&lt;/p&gt;

&lt;p&gt;The banner rendered. Coins and all.&lt;/p&gt;
&lt;h2&gt;
  
  
  and then it was slow
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy2njaelynha3ihk47maq.webp" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fy2njaelynha3ihk47maq.webp" alt=" " width="800" height="293"&gt;&lt;/a&gt;&lt;br&gt;
Every compiled frame carries the images inside it, because each frame is a&lt;br&gt;
self-contained picture. 82% of each 96 KB frame was image payload. At the&lt;br&gt;
default 60 fps that's 300 frames and &lt;strong&gt;27.5 MB&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Worse than the memory: changing frame cost &lt;strong&gt;6.8 ms&lt;/strong&gt;. The frame budget at 60fps&lt;br&gt;
is 16.7 ms. That's 40% of it, just to decode, on an M-series Mac. On a mid-range&lt;br&gt;
phone it would have blown straight through.&lt;/p&gt;

&lt;p&gt;Two fixes, and I'm not proud of the first one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The image cache.&lt;/strong&gt; I went reading how vector_graphics decodes images and found&lt;br&gt;
this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="n"&gt;listener&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;FlutterVectorGraphicsListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nl"&gt;id:&lt;/span&gt; &lt;span class="n"&gt;loader&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hashCode&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...);&lt;/span&gt;
&lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;_createImageKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;imageId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;imageId&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;format&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It keys Flutter's global image cache by the hash code of the loader you hand it.&lt;br&gt;
And mine was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight dart"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="kd"&gt;get&lt;/span&gt; &lt;span class="n"&gt;hashCode&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; &lt;span class="kt"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;identityHashCode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;frames&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;frameIndex&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;frameIndex&lt;/code&gt; in there. Every single frame opened its own namespace in the cache&lt;br&gt;
and re-decoded the same five bitmaps. I had disabled a cache that was sitting&lt;br&gt;
right there, with my own line of code, and then measured the result and blamed&lt;br&gt;
the architecture.&lt;/p&gt;

&lt;p&gt;Dropped &lt;code&gt;frameIndex&lt;/code&gt;. Frames are still unequal to each other — that's what &lt;code&gt;==&lt;/code&gt;&lt;br&gt;
is for — and two unequal objects sharing a hash code is perfectly legal Dart.&lt;br&gt;
Here it's the whole point. 6.8 ms → 1.4 ms.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The duplicated bytes.&lt;/strong&gt; For memory I nearly went and wrote a parser for the&lt;br&gt;
binary format to find and hoist the image section. Then I realised I didn't need&lt;br&gt;
to understand the format at all: consecutive frames of one animation are the&lt;br&gt;
same document with a few numbers changed, and the encoder puts everything the&lt;br&gt;
picture embeds at the front. So just compare the frames byte by byte and find&lt;br&gt;
the longest run they all start with.&lt;/p&gt;

&lt;p&gt;On the banner that run is 78,772 bytes. The compiler reports 78,538 bytes of&lt;br&gt;
images. It found them, without knowing what it was looking at.&lt;/p&gt;

&lt;p&gt;Store that once, keep only the tails, glue a frame back together when it's&lt;br&gt;
asked for. 27.5 MB → 5.1 MB, and reassembling costs about 0.1 ms.&lt;/p&gt;

&lt;p&gt;I like this one because it doesn't depend on the encoder's layout. If the format&lt;br&gt;
ever stops front-loading resources, the shared run just gets shorter and nothing&lt;br&gt;
breaks.&lt;/p&gt;
&lt;h2&gt;
  
  
  the spinner that wouldn't spin
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdugn8qlkac0lzv19pmmm.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Farticles%2Fdugn8qlkac0lzv19pmmm.gif" alt=" " width="480" height="240"&gt;&lt;/a&gt;&lt;br&gt;
Last one, and it's the most embarrassing, because it's the most common CSS&lt;br&gt;
animation on earth:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="k"&gt;@keyframes&lt;/span&gt; &lt;span class="n"&gt;spin&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nt"&gt;to&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nl"&gt;transform&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;360deg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No &lt;code&gt;0%&lt;/code&gt;. My code filled the missing end from the element's own &lt;code&gt;transform&lt;/code&gt;&lt;br&gt;
attribute, and when the element didn't have one, it fell back to... the other&lt;br&gt;
keyframe. So both ends said 360 and the thing sat perfectly still while&lt;br&gt;
everything around it animated.&lt;/p&gt;

&lt;p&gt;The missing end has to come from the value the property has &lt;em&gt;outside&lt;/em&gt; the&lt;br&gt;
animation — no rotation for a transform, fully opaque for an opacity. With one&lt;br&gt;
wrinkle: you can't substitute an empty transform, because two transform lists&lt;br&gt;
only interpolate if they're built the same way. The stand-in has to keep the&lt;br&gt;
shape: &lt;code&gt;translate(0 0)&lt;/code&gt; against a translate, &lt;code&gt;scale(1)&lt;/code&gt; against a scale.&lt;/p&gt;

&lt;h2&gt;
  
  
  what it doesn't do
&lt;/h2&gt;

&lt;p&gt;Filters. At all. &lt;code&gt;vector_graphics&lt;/code&gt; drops &lt;code&gt;&amp;lt;filter&amp;gt;&lt;/code&gt; entirely, so drop shadows&lt;br&gt;
and glows just don't happen — the shapes draw, the effect doesn't. Same for&lt;br&gt;
&lt;code&gt;mix-blend-mode: plus-lighter&lt;/code&gt;, which is exactly what editors reach for to make&lt;br&gt;
something glow.&lt;/p&gt;

&lt;p&gt;Also no &lt;code&gt;d&lt;/code&gt; morphing, no &lt;code&gt;&amp;lt;script&amp;gt;&lt;/code&gt;, no &lt;code&gt;:hover&lt;/code&gt;, no event-based &lt;code&gt;begin&lt;/code&gt;. If you&lt;br&gt;
need any of that, &lt;a href="https://pub.dev/packages/full_svg_flutter" rel="noopener noreferrer"&gt;full_svg_flutter&lt;/a&gt;&lt;br&gt;
covers a lot more of the format. It's a good package. It also bundles a&lt;br&gt;
JavaScript runtime, which is a fair price for what it does and not one I wanted&lt;br&gt;
to pay for a spinner.&lt;/p&gt;

&lt;p&gt;Mine adds two pure-Dart packages, both already in flutter_svg's dependency tree.&lt;/p&gt;

&lt;h2&gt;
  
  
  it's on pub
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://pub.dev/packages/svg_animate" rel="noopener noreferrer"&gt;pub.dev/packages/svg_animate&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;SMIL, CSS &lt;code&gt;@keyframes&lt;/code&gt;, CSS motion paths (which is how SVGator writes every&lt;br&gt;
movement, so its exports work), play/pause/seek through a controller.&lt;/p&gt;

&lt;p&gt;If you've got an SVG that renders wrong, send it. Every single thing in this&lt;br&gt;
post came from someone's file not working.&lt;/p&gt;

</description>
      <category>flutter</category>
      <category>dart</category>
      <category>fluttersvg</category>
      <category>animation</category>
    </item>
  </channel>
</rss>
