quis

2 items

What I did differently.md

Palette.md

What I did differently

The Power of Hindsight

Typically, when I complete a project, I write a list of what I would do differently if I could do it again, but that time rarely comes. Palette 1.0 was the first iOS app I made as a portfolio piece after learning how to build an app 6 years ago. At the time, I’d only been a SWE for 2 years, so I was unaware of many iOS and general engineering best practices. This meant I made many mistakes that I didn’t even know were mistakes and shipped it immediately. Getting the time and opportunity to apply everything I’ve learned over my 5 years at Square to my passion project was a blessing because I felt like I was rewriting Palette the “right” way (5 years from now I will be rewriting this again, saying something similar).

UIKit vs SwiftUI

I learned iOS development in 2019, when Swift was only 5 years old, and SwiftUI had literally just been released. At the time, SwiftUI was so new that there was almost no educational material, and I doubted big tech companies would pivot to it soon (correct). That led me to learn iOS Development with UIKit and Swift until I was forced to learn SwiftUI on the job (it took 6 years to get there).

Palette 1.0 leveraged UIKit to the fullest, using a UICollectionView for the landing page and the Mood home screen. We also used a UITableView for the list of saved songs. During the first couple of months of Palette 1.0 development, I used Interface Builder (IB). As the number of screens increased, I pivoted to programmatic UI with AutoLayout (TAMIC, my beloved).

The rewrite was my chance to go all in on SwiftUI, so I did a full rewrite: a new Xcode project from scratch, not a migration. I used SwiftUI for about a year at Block, where I migrated old screens to SwiftUI and built a few new screens in our in-house SwiftUI wrapper (Workflow SwiftUI), so it wasn’t completely new to me. One huge difference I think folks forget to mention is that UIKit was imperative, and SwiftUI meant adopting a declarative paradigm. Luckily, at Square we had our own declarative framework called Workflow, so I was very familiar with this change.

The home screen in 1.0 was a massive UIViewController that held all the Moods, initialized and laid out the UICollectionView, added it as a subview, and set itself as the delegate. Looking back, the file was a mess, but I’m not sure whether that was a UIKit problem or just an inexperienced iOS developer problem.

Now our Landing Page is a view that takes in a LandingPageView model, which manages the state of the user’s moods and relays that information back to the view for UI updates. This creates a clear separation of concerns, making the code much more readable, mockable, and testable. Palette 1.0 had no tests.

Local vs Server data

Palette 1.0 used Google Firebase as our backend, which handled auth and all moods and mood tracks shared across users. Songs users saved to their Palette library were stored only locally in a Realm database, so this data was lost if the user deleted the app or tried signing into Palette on a new device. This was confusing because we let users create accounts and sign in to Palette, but we didn’t store anything in their Firebase accounts. We used Firebase only to record which user uploaded a specific song to a mood.

Palette 2.0 shifts in the opposite direction: we store all of the user’s moods, mood collections, and taste profiles in our Supabase database. This lets users have the same experience across devices, even if they delete the app. I learned this pattern after my first month at Square in 2021. We had sellers who used multiple devices to manage their catalog. This would be impossible if their catalogs didn’t sync with the server; I have no idea how someone would run a business like that. The stakes were much lower in Palette, but I would hate for a power user to curate 20+ moods and lose them all because their phone was stolen, broken, or they had to delete Palette. This introduces the risk of stale data, but I assumed the typical Palette user isn’t running multiple sessions on different devices. If that does happen, we have session idempotency keys to ensure we write only one to Supabase at a time.

Concurrency: Swift Concurrency vs PromiseKit

Before learning Swift, I was a big fan of JavaScript, and that was my only baseline for concurrency. During my Swift course, I learned only the basics of Swift and little about concurrency, so my only frame of reference was JavaScript Promises. I remember looking up how to make a Promise in Swift, found PromiseKit, and felt at home.

private func createAppleMusicUser (devToken: [String:String]) -> Promise<AppleMusicUser>{
    return Promise {
        seal in
        guard let developerToken = devToken[AppleMusicUtils.TOKEN] else {
            seal.reject(PaletteError.invalidCredentials(message: "Invalid Apple Music Developer Token"))
            return
        }
        self.serviceController.requestUserToken(forDeveloperToken: developerToken) { (token, error) in
            if let userToken = token {
                // ...
                        seal.fulfill(AppleMusicUser.init(sessionTokenId: userToken, storefrontId: storefrontId, devToken: devToken))

Here is an example of how PromiseKit was used to get an Apple Music user with a dev token. This was also before MusicKit, which explains the hoops we needed to jump through.

Rewriting Palette in SwiftUI meant leveraging Swift Concurrency’s async/await, which was much more readable and easier to understand. The rewrite used MusicKit, so we actually don’t have a Swift Concurrency equivalent of this logic since MusicKit handles it, but if we did, it would look something like:

private func createAppleMusicUser(devToken: [String: String]) async throws -> AppleMusicUser {
    guard let developerToken = devToken[AppleMusicUtils.TOKEN] else {
        throw PaletteError.invalidCredentials(message: "Invalid Apple Music Developer Token")
    }
    let userToken = try await serviceController.requestUserToken(forDeveloperToken: developerToken)
    // storefront request, then:
    return AppleMusicUser(sessionTokenId: userToken, storefrontId: storefrontId, devToken: devToken)
}

Supporting low connectivity

This time around, one of my top priorities was responsiveness and making the app feel as smooth and quick as possible. I didn’t want Palette to feel like an indie app. Palette 1.0 had spirit, but it felt like an Indie developer’s first iOS app. There were flickering images, awkward loading experiences, and sometimes incredibly long waits when off Wi-Fi.

Instagram was my main inspiration for responsiveness. Every time I opened it, a new feed was ready to go, without a loading spinner or progress bar. Palette has only 2 intentional loading moments: building your Palette and building your Discovery session. These can take 2-6s, and during those moments we use loading milestones to show the user what’s happening and avoid making the app look frozen. Early in development, we built every Discovery session from scratch, which meant an intentional loading moment happened every time a user wanted to discover new songs, and that was no bueno. To fix this, I kicked off a discovery queue pre-build as soon as you finished your current session. This meant you only had to wait to start your next session if the pre-build failed. The lesson here was that we cannot avoid some work, but we can hide it behind other screens and moments to make these transitions feel seamless.

With that lesson in mind, I began tackling other loading moments I thought were unnecessary. In Palette 1.0’s discovery mode, we fetched the next track in the queue as needed. At home with fast Wi-Fi, this experience felt seamless, so I did the same for Palette 2.0. Then, while testing Palette, I went upstairs where the internet is weaker and noticed I spent much more time waiting for the next track to load during a discovery session. It made me wonder: if a user is sitting there listening to a 30s song preview, why do they then have to wait another 2-4s for the next song to load when the internet is slow? Discovery sessions give us a 30s window to prefetch the next songs while the user listens to the current song; we should use that moment. Prefetching the next couple of songs while the user listens to their current preview helped us remove almost all loading from the Discovery experience. I say almost because if they skip quickly before the preload finishes, they will see the loading spinner.

Closing

I wrote Palette 1.0 as an engineer 2 years into my career, fresh off an intro-to-Swift and iOS development course. I was so new I didn’t know what I didn’t know. Last week, I released Palette 2.0 into the App Store as a Senior iOS Engineer who spent the last 5 years learning exactly that. I still have much to learn, and I hope to come back to this post in 5 years laughing about what I thought I knew.