No profile left behind

Adding details

Lates version of Find movies to watch introduced movie details presentation. This also includes actor profile pictures.

Find movies details page sample

These images are based on info from The Movie Database. When profile is show we do lazy loading of image using ImageProvide. This means either loading from local cache and presenting. Or retrieving it from remote source, caching for future use, and presenting.

But in some cases actor profile is missing image and I wanted to handle it by showing generated image with actor initials. Same way how The Movie Database does it. All this should ideally be done without changing existing presentation logic. I did not want to add additional code for handling generated images caching and presentation.

The interception

First step in achieving it was to introduced different profile image URL for missing ones. When TMDB was missing image, then actor object returned custom URL with following format:

profile://image/JT

Last characters are for actor name initials to be used in generated image.

ImageProvide is using the fetcher pattern where it requires that app injects logic for retrieving data from network. This gave me a perfect interception point for handling missing images.

import ImageProvide

class ImageFetch: RemoteFetch {
    func fetchImage(for ask: ImageAsk, completion: @escaping (Data?, URLResponse?, Error?) -> ()) {
        // ImageAsk contains requested URL
        guard /* check for profile scheme */ else {
            // not generated profile image URL, make remote request
            // .......
            return
        }
        
        DispatchQueue.global(qos: .background).async {
            // create image with initials
            // .......
            // finally return image data for caching and presenting in UI
            let data = UIImagePNGRepresentation(image)
            completion(data, nil, nil)
        }
    }
}

With minimal changes outside presentation logic I was able to leverage the usual pull/cache/present logic that was already available for other images.

Find movies profile details

Conclusion

When time permits, I do my best to come up with simplest and most elegant solution possible. By adding some code outside main application logic I was able to introduce new feature with minimal effort.

I may not always get there, but this time I do feel like I nailed it :)

Tags: ios, programming, design