Categories
Software

Flipping #*&$%@! Cards with SwiftUI

Despite the salty title this was a very low stress investigation. I wanted to find a way to use animation to flip my flash cards. There are many great sources to do something like this:

struct ContentView: View {
    @State var angle: CGFloat = 0    
    var body: some View {
        VStack {
            Text("contentText: frontText")
                .padding(40)
                .background(.green)
                .rotation3DEffect(.degrees(angle), axis: (x: 0.0, y: 1.0, z: 0.0))
                .animation(.default, value: angle)
            Button("Flip") {
                angle += 180
            }
        }
    }
}

Tapping the button in the above app will flip the card, and show an empty green rectangle. So how would I go about showing different (dynamic) content on the ‘back’ of the card? The idea I’m currently going with is to create a front content view and a back contentView. The back contentView begins pre-rotated, so that when the parent view gets rotated it ends up being the correct orientation,

        VStack {
            ZStack {
                FlippableContent(contentText: frontText)
                FlippableContent(contentText: rearText)
                    .rotation3DEffect(
                        .degrees(180), axis: axis)
            }
            .rotation3DEffect(.degrees(angle), axis: axis)
            .animation(.default, value: angle)
            Button("Flip") {
                angle += 180
            }
        }
...
struct FlippableContent: View {
    let contentText: String
    var body: some View {
        VStack {
            Text(contentText)
        }
        .frame(width: 300, height: 250)
        .background(.green)
    }
}

This is close, but not quite right. It always only ever shows the FlippableContent view showing rearText (sometimes forwards, sometimes reverse). I experimented with different angles on the FlippableContents, however I’m not entirely clear on exactly how multiple 3d rotation effects get combined. Not my circus, not my monkeys I guess. Tho I’m definitely a bit curious…

To fix my problem, I’ve created logic that creates different opacity values for the front and back content of the card.

extension CGFloat {
    var rearOpacity: CGFloat {
        return (self / 180).truncatingRemainder(dividingBy: 2)
    }
    var frontOpacity: CGFloat {
        return 1 - rearOpacity
    }
}

And these new functions get used in opacity modifiers in the ZStack.

            ZStack {
                FlippableContent(contentText: frontText)
                    .opacity(angle.frontOpacity)
                FlippableContent(contentText: rearText)
                    .rotation3DEffect(
                        .degrees(180), axis: axis)
                    .opacity(angle.rearOpacity)
            }

Now the view gets initialized with angle at zero, which shows the front text. When the user flips the card, angle gets increased by 180 degrees. This animates through the rotation, and hides the front face, and shows the rear face. On the next flip, the rear face gets hidden and the front face gets shown.

Categories
Software

AppStorage part 2

Turns out this topic needs more than just one post. In part 1, I described how I was able to use AppStorage for an enum with associated values.

Here I will discuss a challenge I encountered when attempting to build the View to enable users to change their value for the languageChoice enum.

enum LanguageChoice: Equatable, Codable {
    case all
    case oneToOne(Language, Language)
    case oneToAll(Language)
    case allToOne(Language)
    case symmetricSubset(Set<Language>)

Step 1, create a version of LanguageChoice with no associated values:

    enum SimpleLanguageChoice: String, CaseIterable {
        case all
        case oneToOne
        case oneToAll
        case allToOne
        case symmetricSubset
    }

Step 2, create a state variable using the new enum type and bind it to a picker:

    @State var languageChoice: SimpleLanguageChoice = .all
    var body: some View {
        VStack {
            Picker("Languages Choice", selection: $languageChoice) {
                ForEach(SimpleLanguageChoice.allCases, id: \.self) {
                    Text($0.rawValue)
                        .tag($0)
                }
            }
        }
    }

Step 3, add UI below the picker to display the appropriate controls to go with the value of languageChoice.

    @ViewBuilder var bottomStuff: some View {
        switch languageChoice {
        case .all:
            EmptyView()
        case .oneToAll:
            Picker("Ask in", selection: $language) {
                ForEach(Language.allCases, id: \.self) {
                    Text($0.rawValue)
                        .tag($0)
                }
            }
     // et cetera

Step 4, add an AppStorage var to get the persisted LanguageChoice value and use it to configure the Picker UI.

    @AppStorage("languageChoice") var persistedLanguageChoice: LanguageChoice = .all
    @State var languageChoice: SimpleLanguageChoice

init() {
     languageChoice = SimpleLanguageChoice.simpleChoice(for: persistedLanguageChoice)
}

Insert record scratch sound here! This code created the following compiler error:
'self' used before all stored properties are initialized

I wanted to use the persisted languageChoice to set up the UI to allow users to choose a new languageChoice. But doing this required reading persisted languageChoice, which was not allowed. Alas. So to get things to work I needed to set a default value for languageChoice in the declaration and then update the value in init. Just do this, right?

    @State var languageChoice: SimpleLanguageChoice = .all
    init() {
        languageChoice = SimpleLanguageChoice.simpleChoice(for: persistedLanguageChoice)

Nope. If you set an initial value for a State variable in the declaration, updating it in init gets more complicated. You need to do this:

    init() {
        _languageChoice = State(initialValue: SimpleLanguageChoice.simpleChoice(for: persistedLanguageChoice)

Categories
Software

SwiftUI AppStorage wrinkles

The SwiftUI property wrapper AppStorage is a great way to cut down on boiler plate code needed to manage/access UserDefaults. At the risk of sounding like I’m looking a gift horse in the mouth, I feel compelled to describe a couple of challenges I encountered while using AppStorage in my current project, the language flash cards app.

AppStorage for enums with associated values

The type I wanted to persist using AppStorage was a fairly gnarly enum with a variety of associated values. It is the value that persists the users preferences for languages to use for asking and answering. See this blog post for more details on how this enum came to be.

enum LanguageChoice: Equatable, Codable {
    case all
    case oneToOne(Language, Language)
    case oneToAll(Language)
    case allToOne(Language)
    case symmetricSubset(Set<Language>)
}

Out of the box, AppStorage only works with the most basic of types. (Int, String, Bool, etc.) There are several great resources on how to extend AppStorage support to basic structs. If you google “AppStorage RawRepresentable” you’ll be away to the races in no time. The tl;dr version is: implement
public init?(rawValue: String)
and
public var rawValue: String

My first inclination was to use JSONDecoder() in init and JSONEncoder() in rawValue. Unfortunately JSONEncoder() was calling rawValue, which was calling JSONEncoder() etc.

So instead I defined a custom scheme to encode (and decode) the LanguageChoice enum. This required a fair bit of custom code (see below) and allowed LanguageChoice to work with @AppStorage. But I wasn’t out of the woods yet. There was still more work to be done, which I’ll describe in a future post.

Here is how I chose to conform to RawRepresentable.

    public init?(rawValue: String) {
        let components = rawValue.components(separatedBy: ":")
        guard let first = components.first else {
            return nil
        }
        switch first {
        case "all":
            self = .all
        case "oneToOne":
            guard components.indices.contains(2),
                  let lang1 = Language.init(rawValue: components[1]),
                  let lang2 = Language.init(rawValue: components[2]) else {
                return nil
            }
            self = .oneToOne(lang1, lang2)
        case "oneToAll":
            guard components.indices.contains(1),
                  let lang = Language.init(rawValue: components[1]) else {
                return nil
            }
            self = .oneToAll(lang)
        case "allToOne":
            guard components.indices.contains(1),
                  let lang = Language.init(rawValue: components[1]) else {
                return nil
            }
            self = .allToOne(lang)
        case "symmetricSubset":
            guard components.indices.contains(1) else {
                return nil
            }
            let languageComponents = components[1].components(separatedBy: ",")
            let languages: Set<Language> = Set(languageComponents.compactMap { Language(rawValue: $0) } )
            self = .symmetricSubset(languages)
        default:
            return nil
        }
    }

    public var rawValue: String {
        let data = try? JSONEncoder().encode(self)
        print("data: \(data)")
        let result: String
        switch self {
        case .all:
            result = "all"
        case .oneToOne(let language, let language2):
            result = "oneToOne:\(language.rawValue):\(language2.rawValue)"
        case .oneToAll(let language):
            result = "oneToAll:\(language.rawValue)"
        case .allToOne(let language):
            result = "allToOne:\(language.rawValue)"
        case .symmetricSubset(let set):
            let setRawValue = set.sorted().reduce("") {
                $0 + $1.rawValue + ","
            }
            
            result = "symmetricSubset:\(setRawValue)"
        }
        return result
    }

Unit tests for this code have been left as an exercise for the student.

Categories
Software

Binding to a Dictionary

SwiftUI is mostly awesome, but sometimes in the corners things get a bit messy. In my recent work on the multi-lingual flash cards app, I encountered one such corner.

I am planning to include languages that use writing systems other than the good ol’ Western Alphabet. These include:

  • ਜਪਾਨੀ (Japanese)
  • 旁遮普语 (Punjabi)
  • китайський (Chinese)
  • ウクライナ語 (Ukrainian)

Just for fun, the above list is: Japanese (in Punjabi), Punjabi (in Chinese), Chinese (in Ukrainian), and Ukrainian (in Japanese)

When using the app, I want to give users the choice to see these words in their native script or in the ‘Roman’ alphabet. But I didn’t immediately how to implement this ‘feature.’

I saw two challenges:

  1. How to store the differently scripted versions of the same language
  2. How to map user preferences to the list of languages to use when quizzing users.

Problem #1: Storing the Languages

I ended up creating multiple localizations for each languages. For Japanese I used ‘ja’ and ‘ja-JP.’ The ‘ja’ localization stores the kana (and possibly kanji) version of the flash card content. The ‘ja-JP’ localization stores the romaji version of the content.

To be honest, I don’t LOVE this implementation option, but I really didn’t see anything better. Try not to judge me too harshly!

Problem #2: Mapping the user preferences to the languages list

Thanks to my solution to problem #1, the internal list of available languages will now look something like: en, fr, ja, ja-JP. But we never want to show users this list. Instead we will want to show them either ja or ja-JP. Depending on a user’s preferences, their language list will either be: en, fr, ja OR en, fr, ja-JP.

For each language with local script or western/roman alphabet options, the user will set a bool preference value. The bool preference values will be used to create a set of excluded languages. Here is the logic for the case where Japanese is the only multi-script language.

    var scriptExcludedLanguages: [Language] {
        let ja: Language = useNativeScript ? .ja_roman : .ja_nonRoman
        return [ja]
    }

The app can then remove the scriptExcludedLanguages to generate the list of languages available to the current user with the following code:

    var allLanguages: Set<Language> {
        let result = Language.allLanguages.subtracting(scriptExcludedLanguages)
        return result
    }

In the course of implementing this feature, I uncovered one other piece that ended up being non-obvious.

Problem #3 Binding the UI and UserDefaults for the Dictionary of Bools

First I defined the type:
typealias ScriptPickers = [String: Bool]
And then in the picker view added the following AppStorage property:
    @AppStorage("scriptPickers") var scriptPickers:  ScriptPickers = ScriptPickers.defaultDictionary
This lead to the following cryptic compiler error:
No exact matches in call to initializer 
It turned out the fix for this was to make ScriptPickers conform to RawRepresentable. Here’s what that looks like:

extension ScriptPickers: RawRepresentable where Key == String, Value == Bool {
    public init?(rawValue: String) {
        guard let data = rawValue.data(using: .utf8),  
            let result = try? JSONDecoder().decode(ScriptPickers.self, from: data)
        else {
            return nil
        }
        self = result
    }

    public var rawValue: String {
        guard let data = try? JSONEncoder().encode(self),   
              let result = String(data: data, encoding: .utf8) 
        else {
            return "{}"  // empty Dictionary respresented as String
        }
        return result
    }

}
// hat tip: actw https://stackoverflow.com/questions/65382590/how-to-use-appstorage-for-a-dictionary-of-strings-string-string 

But there was still the need to map the UI toggles to the ScriptPickers dictionary. Each language toggle needs a binding to the corresponding entry in dictionary. Here is the basic structure for doing that.

struct ContentView: View {
    
    @AppStorage("scriptPickers") var scriptPickers:  ScriptPickers = ScriptPickers.defaultDictionary

    var body: some View {
        VStack {
            ForEach(scriptPickers.keys.sorted(), id: \.self) { key in
                Toggle(key, isOn: binding(for: key))
            }
        }
        .padding()
    }
    private func binding(for key: String) -> Binding<Bool> {
        return .init(
            get: { self.scriptPickers[key, default: false] },
            set: { self.scriptPickers[key] = $0 })
    }

}

I am no noticing that some languages don’t just have roman and non-roman options. Punjabi for example can be expressed in Gurmukhi, Shamukhi, and the roman alphabet. Japanese can be expressed in Kanji (pictograms), Kana (non-Roman alphabets), and Romaji (using the Roman alphabet.)
So in the future, it feels like this code may need to be extended. Also, it feels like the languages should be expressed as an enum, rather than as strings.

Categories
Software

Swift Dictionaries and the Codable Protocol

(Not a book by JK Rowling, tho I’m sure many people would purchase and read Harry Potter and the Codable Protocol)

I’m working on a project where I need to write code to convert the following JSON into a structure:

{  "en" : { "stringUnit" : {
              "state" : "translated",
              "value" : "Cat" } },
    "fr" : { "stringUnit" : {
              "state" : "translated",
              "value" : "Chat" } },
    "ja" : { "stringUnit" : {
              "state" : "translated",
              "value" : "ねこ" } }
}

To accomplish this, I created the following model objects:

typealias LocalizationsDict = [String: Localization]

struct Localization: Codable, Equatable {
    let stringUnit: StringUnit
}

struct StringUnit: Codable, Equatable {
    let state: String
    let value: String
}

The code to decode a LocalizationsDict looked like this:

        let languages: LocalizationsDict = try JSONDecoder().decode(LocalizationsDict.self, from: data)

Everything was working wonderfully. But then I got greedy. (queue the jump scare music.) I wanted to see if the keys could be an enum, instead of a String. I felt this would make the code safer, and would mean languages could be types using autocomplete. (The jury may still be out on whether necessity or laziness is truly the mother of invention.)

Here was the code needed to create the Language enum:

enum Language: String, Codable {
    case en = "en"
    case fr = "fr"
    case ja = "ja"
}

Unfortunately…in order for a Swift dictionary to conform to Codable, its key type must be either String or Int. For a Swift dictionary with any other type of key, decode will assume the JSON will be represented as an array, where the first item is the first key, the second item is the first value, third item is the second key, etc.
Thank you Apple Dev forums, yet again. https://developer.apple.com/forums/thread/747665

This would mean the JSON would need to be stored like this:

[  "en", { "stringUnit" : {
              "state" : "translated",
              "value" : "Cat" } },
    "fr", { "stringUnit" : {
              "state" : "translated",
              "value" : "Chat" } },
    "ja", { "stringUnit" : {
              "state" : "translated",
              "value" : "ねこ" } }
}

Sadly, in this situation, the JSON was being generated by the metaphorical ‘somebody else’ and I didn’t have the option to change the format of my incoming JSON.

Instead I added a step when decoding this type of Dictionary. Here’s a code snippet:

typealias LocalizationsDict = [Language: Localization]
typealias LocalizationsDict2 = [String: Localization]

    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        let stringKeyedLocalizations = try container.decode(LocalizationsDict2.self, forKey: .localizations)
        var enumKeyedLocalizations: LocalizationsDict = [:]
        for (key, value) in stringKeyedLocalizations {
            if let enumKey = Language(rawValue: key) {
                enumKeyedLocalizations[enumKey] = value
            }
        }
        self.localizations = enumKeyedLocalizations
    }

The details of what’s happening are as follows:

  1. Decode the JSON to a [String: Localization] dictionary
  2. Create an empty [Language: Localization] dictionary
  3. Iterate through the [String: Localization] dictionary
  4. For each entry, verify it’s possible to create a valid enum value from the language string. (eg map "en" to Language.en
  5. For each entry store the value in the [Language: Localization] dictionary, using the enum key generated in the previous step.

This works, but I thought this seemed like an interesting shortcoming in Swift.

Categories
Software

Zoom Burst Instructions

Here are my instructions for how to open Zoom Burst. (or any iOS Photo Editing Extension)

In order to effectively use Zoom Burst, you should understand the principles behind the app. Zoom Burst starts with the original photo serving as the base layer of a composition. It also allows you to make the following adjustments:

  1. Select the blend mode used to compose the layers in the composition (CIColorDodgeBlendMode works best for night photographs)
  2. Specify the amount of scaling used. This determines how much the lights expand or pop.
  3. Specify the amount of rotation used.
  4. Adjust the number of layers. This reduces the gaps between the steps.

    Or if you’d prefer, here’s a video of Zoom Burst in action.

    You can download Zoom Burst from the App Store.

    Categories
    Software

    How to Open a Photo Editing Extension

    Until very recently I had never used a Photo Editing Extension. But now that I’ve built Zoom Burst, I wanted describe the steps involved in opening a photo editing extension.

    Step 1. Open the photos app and select a photo

    Step 2. Tap edit.

    Step 3. Tap the circle with three dots. (in the top right corner of the screen)

    Step 4. Select the editing extension of your choice. (eg ZoomBurst)

    You can download Zoom Burst from the App Store.

    Categories
    Software

    ZoomBurst is fun!

    I’ve been enjoying playing with a Zoom Burst new photo editing extension that I’ve been building. It lets me add zoom burst effects to photos. If you don’t know what zoom burst is, I recommend you read this great article by Dahlia Ambrose. Or if you’d prefer, Wikipedia offers a great introduction. Before creating my Zoom Burst photo editing extension you needed an SLR (digital or otherwise) and a zoom lens to create zoom burst photos.

    The procedure was:

    1. Set your shutter speed to 1/60th of a second or longer
    2. Press the shutter release in concert with changing the zoom focal length of the lens

    In particular I really enjoyed creating zoom burst photos of lights at night. In early December, it felt like any time I saw Christmas lights, I wished I could create a zoom burst photo. Alas.

    Now thanks to the miracle of Core Image, I’m able to convert this:

    Into this:

    I have been focussing my initial Zoom Burst efforts on getting it to work with photos of lights at night. I’m cautiously optimistic I’ll be able to extend it to also work with non-night photos. In the meantime, here are a few other photos before and after applying Zoom Burst.

    Before

    After

    I think the controls in the Zoom Burst extension are fairly easy to figure out, but I’ve created a short tutorial, that is available here.

    I had a bit of trouble organically discovering how to use a Photo Editing Extension. To prevent you from fumbling around like me, here is a list of instructions to open/use a Photo Editing Extension.

    And here is a link to the app store:

    Categories
    Software

    SwiftUI Being a bit Quirky

    The Ecstasy and the Agony of Making Software

    I recently wanted to create some content that would sometimes display in an HStack and sometimes in a VStack. The content was the same in both cases, so I wanted to avoid doing:

    if goVertical {
        VStack {
            thing1
            thing2
        }
    } else {
        HStack {
            thing1
            thing2
        }
    }

    Fortunately, I was able to use a ViewBuilder to contain the contents of the stacks.

    @ViewBuilder var stackContents: some View {
        thing1
        thing2
    }
    
    ...
    
    if goVertical {
        VStack {
            contents
                .padding(.horizontal)
        }
    } else {
        HStack {
            contents
        }
    }

    So far so good. (Aside: Am I the only one who didn’t know @ViewBuilder can be used to return a list of SwiftUI views?!) However I eventually got myself into a bit of trouble with my stack contents in an interesting way. Here are the details.

    First you need to know that thing1 is an image and thing 2 is a VStack of sliders, steppers and such. Here’s how the view looks in portrait and landscape.

    BUT… since I will be adding more sliders, steppers and such, I moved them into a ScrollView. But (as you can see on the right) when I did this, The ScrollView took more height than it needed and short changed the Image.

    My first thought was in use a layoutPriority modifier on the image, but this had no effect, so I removed it. Next, I tried adding background modifiers to the VStack contents, and that proved to be very illuminating. (see below)

    Here is the code with the background modifiers:

            VStack {
                contents
                    .background(.red)
                    .padding(.horizontal)
                    .background(.blue)
            }
            .background(.yellow)

    Applying horizontal padding to contents did not apply it once, it (presumably) iterated through all the items in contents and applied the padding to each item in contents.

    I moved the padding (and background modifiers to apply to the VStack, instead of contents

            VStack {
                contents
            }
            .background(.red)
            .padding(.horizontal)
            .background(.blue)

    Great, this looks more like what I was expecting with the padding. But the image is still narrower.

    So I re-added the layoutPriority modifier to the image, and that did the trick. I still don’t understand why the ScrollView ‘steals’ vertical space from the Image. (And maybe one day I’ll better understand what causes me to anthropomorphize SW modules like layout engines.)

    The interesting thing for me in this issue was how doing something incorrectly (ie applying the horizontal padding to the contents of the VStack, rather than to the VStack) worked correctly at first, but when the butterfly flapped its wings (ie one of the elements in contents was wrapped in a ScrollView) the wheels fell off.

    Encountering and eventually overcoming this type of problem is a big part of the ecstasy and the agony of writing software.

    Categories
    Haiku

    Haiku for an Old House

    Tearing down the house
    Big yellow excavator
    Eating bite by bite

    One house to the west
    A new foundation in place
    Waiting for the top

    Gonna miss the house
    Next door. Small quaint and frumpy
    Great people in there