New app published: Karbs

Some months ago I published a new iOS app: Karbs. It’s a carbs-counting application for patients with diabetes.

Why a carbs-counting app?

My wife has diabetes since she was a child. She used the app Notes from Apple to keep a list of carbs counting for the meals she usually eats, including meals cooked at home and ordered at restaurants.

She tried multiple applications offering databases of nutritional information for common products but her experience was not very good with them. Although some calculations were pretty close, she was not getting her diabetes properly under control as the carbs counting was not properly done.

That was the main reason why she started creating her own list, to count carbs quickly but also precisely. I wanted to help her by building an app for that purpose.

What is carbs counting? Why is it needed?

So you’ve probably seen a nutrition facts table in all the food you buy from the supermarket:

Carbs counting is the operation of calculating how many carbohydrates are in a portion of a food you are going to eat. In this particular example, if you eat the recommended portion (110g), there are 23g of carbs.

This calculation is needed for patients with type-1 diabetes because the amount of insulin they need to inject depends on two things:

  • the amount of carbs you are about to eat,
  • and the amount of insulin you need for 1g of carbohydrates.

Based on those two factors, type 1 patients with diabetes can calculate the amount of insulin to inject before each meal. For example, considering 23g or carbs and a ratio of 2 IU per 10g, they would inject 2,3 IU of insulin.

It’s a simple calculation that even children can do mentally, but it becomes more complicated if your ratio is different depending on the time of the day, whether you have your period or not, and a lot of different life factors (such as stress or anxiety).

For those patients, a Bolus Calculator usage is recommended to keep diabetes under control, as it keeps track of different ratios and active insulin in the body from previous meals. The most important part is to count carbs properly.

What does the app look like?

I wanted it to be as simple as possible so I divided it into two tabs:

  • List of meals: where you count carbs before eating
  • List of food: where you keep track of the nutrition facts for each food based on the regular amount you eat for them

This way, users just need to add their food in advance and then the counting is really simple: they just add items from their list and they see the calculation instantly.

Since my wife uses the Bolus Calculator from the mySugr app and importing data from Apple Health is a supported feature, I also implemented data export.

How was it built?

I wanted to take this opportunity to build a new application from scratch to try the new Observation tools from The Composable Architecture (TCA), introduced in version 1.7.0.

It was super easy to implement as I only had to create SwiftUI views and reducers for meals and food lists, meal counting, and item details. The data bindings between the views and the reducers are like black magic with the integration of the Observation framework into the TCA library.

The most complex part I had to implement was persisting the data, for which I decided to choose a simple file in the app’s sandbox.

One of the biggest advantages of using TCA was that all the data modeling was implemented using structs. This allowed me to offer the user the possibility of modifying the amount of carbs for a food item without affecting the previous meal calculations including that same food, for example, if the user decides to start buying it from another supermarket and nutrition facts are slightly different.

The Composable Architecture is such an incredible implementation of the unidirectional data flow paradigm, taking SwiftUI apps to the next level. It only took me four days to have my app implemented and published, including creating a promotional website and professional AppStore screenshots which I will explain in another article.