SOLID Photo Gallery in Swift 4

Goal of this exercise is to improve my code design. I am hearing a lot about SOLID design principles and “uncle Bob”, so I decided to understand its ideas. This exercise is my attempt to implement this ideas on practice in simple and modern Swift application. Although SOLID principles explained with C++ examples, its good Object Oriented Design principles can be applied to Swift.

As mentioned by Robert C. Martin before, this principles is only recommendations and is not a law. Sometimes it is better to violate this good design principles but only when necessary. I also have to mentions that this is only mine interpretation, I can be wrong. Please write to me if you have any feedback.


Applications accesses Flickr API and displays image gallery, displays meta data for each image.

To satisfy acceptance criteria we need to display collection of photos, be able to show all metadata for each photo and perform actions on each photo. I designed 3 screens and to implement them we will need following:

  1. PhotosCollectionViewController
  2. PhotoViewController
  3. MetadataViewController

Let’s assume, later we will need to add sorting feature for the next version of application.

Naive Design.

For getting photos from Flickr singe service class is created. This class sends data to PhotosCollectionViewController.

For the UI we use single Storyboard file.

Good Model Design.

What makes design good or bad? How we can judge it? There’s 3 warnings signs in which we can evaluate design quality. This was described in The Dependency Inversion Principle.

  1. Code that hard to change.
  2. Every change breaks code in unexpected place.
  3. Code can’t be reused.

In order to create reusable design we need to think what parts of application can be changed in the future and what kind of change we should be ready for. We can improve naive design in many ways. Let’s focus on model design and UI design separately and start with model.

Lets say everything user can’t see is part of the model. First decision that I made is to write separate NetworkService protocol. Swift protocol itself can have only method definition, not implementation. I extended NetworkService and chose to use Apple URLSession to request data with status and optional error object.

Let’s imagine this source code need to be used in another project which relies on third party Alamofire library. In this case, developer need to replace NetworkService  extension. If developer keeps same method definition, good news, PhotosCollectionViewController will not need to change. Job well done.

Another struct is required to specify request path, I called it FlickrService. It has endpoint path and data type parameter. I also created FlickrDataModel struct. In this struct I added method to parse data into Flickr model taking advantage of Swift 4 Standard Library with JSONDecoder. I chose to use nested Decodable Structs to mirror data model from an Flickr endpoint.

Unlike naive model, in this design I separated Flickr related logic and networking logic into separate abstractions. This enables easy code reuse, and also allows easier refactoring of one part of application without breaking other part.

PhotosCollectionViewController communicates with lower level networking module through higher level FlickrService class which conforms to abstract NetworkService protocol and can perform request. I believe this is good example of The Dependency Inversion Principle. Higher level abstractions doesn’t depend on lower level implementation.

The rest of application.

So far I spend most of my attention into PhotosCollectionViewController. There’s 2 other goals:

  1. View controller for displaying photo in full screen and performing some actions: sharing photo, opening original Flickr web page.
  2. Displaying metadata in full screen.

This can be done with 2 or 1 view controller. Whenever Photo metadata changes, this view controllers and UI would need to change as well. I chose to split metadata viewer and full screen photo view into 2 different view controllers. I created 2 Storyboard files and took advantage of Storyboard References. That’s better than using single Storyboard file, now engineers can work simultaneously. Storyboard itself allows us to separate view logic from business logic. This allows changes in UI design and layout without need to change view controller code.

I assume one can say that this design violates The Dependency Inversion Principle — changes in Photo object model, replacement it with another object, or adding new data source types, require changes in UI and controllers. For this simple exercise I chose to keep UI static. We can’t be ready to every possible change and need to think carefully what areas of application can change. Because this is photo browsing app I keep core UI functionality together. I believe it is important to understand purpose of the application, in our case it is displaying photos with meta data. We should design our code to be ready for additional features and changes in implementation. In case when core purpose changes, it is better to start from scratch.

Separating User Interface.

Well done, version one finished. I am satisfied with design and core requirements. What if we would like to implement sort function? It can be done inside the same collection view controller. But this would be bad design and eventually lead to well known Massive View Controller problem. What if we want to reuse sorting module or change its implementation? The Dependency Inversion Principle states that UI should not be connected with business logic directly, UI should communicate through higher level abstractions. Ideally, sorting feature should have its own MVC.

To fix this, we can create SortOptions struct which defines SortType enum and its order. It also contains text to be displayed in UI. We use UISegmentedControl, I consider it as low lever class which should know nothing about business logic. We extend this control with SortControlextension which would take SortOptions and would configure UISegmentedControl. to display appropriate number of segments with appropriate labels.

I included SortOptions struct into PhotosCollectionViewController which is fine but I didn’t connect UISegmentedControl directly to PhotosCollectionViewController like it’s done in naive model. Instead, I created SortViewController and took advantage of Cocoa Touch Container View. This allows me to move UISegmentedControl into separate Storyboard file, meaning if UI changes, PhotosCollectionViewController will work the same, only SortViewController would require change. As long as our collection view conforms to SortableCollection protocol and communicates through SortViewController, design does not violates The Dependency Inversion Principle.


Xcode project.

Project is available on GitHub.

Let Safari Be Different. Case Against Favicons.

Idea of why Safari team should change its original design and add favicons getting big traction among Daring Fireball users. Jhon Gruber wrote that most of his readers think that Safari team should include favicons in its browser. I have different opinion.

Safari should stay as it is, tabs should not have colourful favicons. Option in Preferences is not a good solution neither. Safari design should not follow common pattern where each browser has favicons in its tabs. It doesn’t have tabs on the top and it shouldn’t have favicons either. Here’s why.

I believe Safari designed to be minimal for multiple good reasons. It’s not hard to include them from technical perspective, this is clear design choice.

Why Safari worse with favicons.

Let’s think why Apple designers decided not to include favicons into Safari. I can speculate on many reasons. First that’s coming into my mind is content. Apple wants us to focus on the material. Colourful icons row is another distraction which gets in our way when we reading article or navigating the page. Aesthetics important here. Safari looks nicer, favicons small and noisy. Favicons look cheap. Safari looks professional, it makes website stand out, it doesn’t compete with its content.

Two main arguments from favicon supporters are:

  1. Favicons help to identify tab quickly, find the right tab without need to click or read.
  2. Favicons allow to fit more recognisable tabs in a single window.

I could agree with first argument, it is harder to find the right tab in Safari. But only when people use Safari like they would use Chrome. I believe Safari was created with different workflow in mind.

Web browser is main research tool for me. My goal finding information and returning into apps. My work starts with new Safari window and search query. Then reading and opening new tabs. When need to go back, not trying to use mouse — using hot key command + shift + { to go back, and command + shift + } to go forward. It feels faster than trying to find the right tab, I can see entire pages. I also use command + 1, 2, 3… to open first, second and third tabs and so on.

At some point I need to find particular tab and that’s when I use View > Show All Tabs, another muscle memory combination: command + shift + \. It also works well with iPad and iPhone. Each tab presented as small preview.

Safari window with 6 tabs visible as small page previews.

Safari tab preview is better representation of web pages and its content, from here we can find the right page and close pages we don’t need.

Okay, let’s imagine I had Chrome, then favicons would not help me, some of tabs are from the same domain, which means I have to use keyboard anyway. When you need to click, you need to read title and Safari even has more space to fit it.

The second argument with many tabs in single window is also not a problem. I open new Safari window whenever I have new task at a hand and “Show All Tabs” can help too. Normally I have more than 5 Safari windows open, and this number increases over time. Opening multiple windows allows me to separate my tasks. I close entire windows with one click after task is done.

From what I saw, some Chrome users maximise browser window, this is not how I use Safari, I have many narrow windows so I can see more than one page at a time and hide sidebar distractions behind the scroll area. This also means that I only have tabs I need for a given task.

Option in Preferences is bad solution.

Now answering your possible question, why don’t add option to the Preferences. If Apple designers would ever decide to add the option, it will be enabled by default. Most of people don’t have time and energy to go through settings and fiddle with checkboxes — nerds love it, most of the people don’t. App should be configured in a right way from the start. Hence if Apple designers think that favicons is the right way, they would ship new version with favicons and the will be no option to disable them. Because when you do need to open Preferences, less checkboxes means faster navigation, so there’s no need to search for a toggle.

What if they do it.

I realise that how I use web browser might be not the right way for some people. I see more people use web applications instead of native apps and keep only one maximised window. I spend more time in apps and I use Dock more than I use tab picker. Safari is just another app for me and I don’t do everything there. For math or weather queries Spotlight feels better, I don’t listen to music on YouTube, I use Dictionary app for looking up a definition, I use Xcode documentation window and so on… Safari is just another app for me.

People can change, designers change their opinion. It can happen. It will make me feel sad. Not as much sad as spacial Finder though.

Expectations from High Sierra.

I was pleased to watch High Sierra announcement during WWDC by Craig Federighi. Technology and Refinements was two things given most attention. As software engineer this words is music to my ears. I felt excitement. This is it, this is Mountain Lion moment once again!

All of my work is done on the Mac, I use it every day. macOS has great foundation, unlimited and unrestricted potential. Combined with years of iterative improvements and innovation, we get capable, reliable OS.

It seams like Craig and Phil continue to do excellent job. People will benefit if macOS continue to improve. I am happy that’s there’s wise executives and engineers at Apple who care about Mac and its software. Many people who create amazing things and continue to rely on macOS as their main working horse will benefit from refinements and performance improvements.

Besides Metal 2, APFS, new image and video formats, one thing mentioned by Jhon Siracusa during ATP caught my attention — new initiative to rewrite open source Dynamic linker in order to make apps launch faster. Goal is to reduce time from start moment to main function call. There’s WWDC session. It looks like low level risky work which can be done by few people. Wish them success.

Repairing first generation Apple Watch.

I heard strange pop noise when I took my watch from its charging dock. Then reboot cycle began and I left them on the desk for a night. On next morning, thought of software issue, decided to deal with it later, I put the watch on and couldn’t get touch screen to respond. I could unlock the watch with iPhone. Then on my commute to work reboot cycle began again. I was able to touch in with Apple Pay and I couldn’t touch out with “Card Unavailable” message. I got into the office and while lifting left arm to press door button I heard another short rattling sound coming from my wrist. I shook my wrist, and Apple Watch screen opened like a little door, rattling on thin wires strip. Came back to my desk and feeling upset I put the watch away, gently wrapped it to apply pressure from top and bottom with paper tissues and duck tape, then put them in black spring loaded glasses case to apply even more pressure and security. I thought of it as little coffin and felt sad.

Been busy with work, one or two weeks later, I went online to research the issue. This thread on Apple forum was most popular and gave me little hope it was factory issue. We wouldn’t be able to afford $200 repair cost. I bought them more than 2 years ago and was mostly happy with it, planned to use Apple Watch for another 2.

Next day I had scheduled call and friendly person suggested that the reason is battery which is expanded after large amount of charging cycles. He suggested to bring watch for repair or send him a picture. I regret not taking a picture when screen went loose.

With little hope I went to Apple Store Kingston, there I explained my problem. Apple Store employee couldn’t pick up screen with his nails and screen started to recognise touch input again. Been tightly sealed for a week, it could reverse the damage. It is not safe to use device with damaged battery. Another reason keeping device in a case was fear of fire or even explosion.

I signed estimated £165.83 repair cost, with single part which is entire watch body without band. I was told I don’t need to pay anything right now. He also told me they had replacement program but only for the back of the watch coming loose.

4 working days later I received “Your product is ready for pickup” letter. My hope grew stronger still I couldn’t know what to expect, I could get a bill or my broken watch back but I got small cardboard box, had to sign another waver with the same £165.83 price and £0 due amount. I was asked do I want to open the box right there or home, I choose latter, oh boy what a surprise I got.

Grey box with another white box inside. Sealed 38mm Apple Watch body.

To my delight, Apple sent Apple Watch Series 1, 2016 model with updated dual core CPU straight from China. It was mentioned in email but I wanted to check SN and engraving to be sure. Excelent customer service on every level.

Low Brightness Bug in iOS 10.2.1

10.3 Update.

I need to mention, the main issue is now resolved. People which couldn't see without maximum brightness not suffering any more. From what I found, problem caused by energy saving behaviour and it's also exist in 10.3, however maximum brightness cap is removed.

10.2.1 Problem.

With iOS 10.2.1 update Apple engineers fixed lots of security problems. I was impressed with the list of fixes and the fact that Apple credited some people and organisations which helped to discover the problems.

I went to Twitter to find out how update is doing, as I sometimes do, and found one complaint from Tim Carr about brightness issues on his iPhone 6. First I was sceptical because I read release notes prior and I couldn't make a connection between patching core iOS security and breaking brightness.

Turns out there's real bug which locks maximum iPhone 6 and 6s[^2] brightness at certain battery levels. Tim Carr is registered blind and having maximum screen brightness at all time is very important for him and many other users with visual disabilities. Quick search brought this iMore forum thread which proves that's it's not isolated problem.

Two iPhone 6 phones. Left is without latest update. Right is with latest update. Left screen is much brighter.
Two iPhone 6 phones. Left is without latest update. Right is with latest update. Both set to full brightness. Image from Tim Carr's tweet.

We tried to troubleshoot it and came to a conclusion that's issue was in iOS not his iPhone. I hoped that Apple Support could help and encouraged Tim to talk to them. That's didn't help at all. Tim tried to reach them in many ways — through Twitter and phone calls and couldn't get any solution. In his desperation he even tweeted to Tim Cook:[^1]

and recieved this poor response:

It would be much better if Apple could admit their fault and inform their support team. I bet some iPhone 6 owners talking to Apple Support right now and getting standard responses to restore their devices. Apple Support at this moment has no idea about the problem and taking every enquiry through the standard path when it could be just a single reply: *Thank you, we working on a fix, I'm meanwhile please try to turn Zoom on and off.*

Instead they making it worse:

As Tim found out, there's a workaround. I mentioned it on the top. I also submitted a radar #30612785. Let's hope it's going to be fixed.

[^1]: Note Apple Support monitors tweets to Tim Cook.
[^2]: I couldn't reproduce issue on my 6s which means not all devices affected.

The State of iBooks in Early 2017. ⚓️

Michael E. Cohen from TidBITS wrote amazing post revealing details about current iBooks state. Even found “irritating” Truncated Serif bug I never noticed before. Michael also like me noticed that iBooks sync ignores books added not from the iBooks Store.

His conclusion:

iBooks is not quite as unreliable and confusing as it was when I wrote about it last year, but neither has it improved nearly as much as loyal iBooks users deserve. Moreover, what little support documentation Apple provides is sketchy and inaccurate, leaving the impression that even the support and documentation departments within Apple are ignoring iBooks.

I wrote about iBooks stagnation 2 weeks ago, glad I’m not the only one concerned about its future.

Black Wallpaper.

My iPhone Home Screen.

All my iOS wallpapers are black. I am using simple black wallpaper for iPhone more than 3 years and now also trying it on iPad. Black wallpaper has following perks.

  • Less visual noise on the Home Screen.
  • App icons easier to recognise.
  • Matches nicely with black bezel.1
  • You not getting tired looking at it.

Note original iPhone had black wallpaper.

How to Make One.

Unfortunately Apple doesn’t include it with iOS. To make your own you can use Pixelmator app.

  1. Open Pixelmator app.
  2. Tap on the + icon to Create Image.
  3. Select Custom…
  4. You will get empty canvas with your device screen size. Tap Create.
  5. Tap on the + icon, select Layers control. Pixelmator Layers.
  6. Select black rectangle.
  7. Tap Activity Sheet button and save image to Photos.
  8. Open Photos, tap on the Activity Sheet icon and tap Use as Wallpaper.
  1. My iPad and iPhone are black. 

Backing up iBooks, DRM and Region Switching.

There’s many readers on the App Store but iBooks was always my favourite. It was one of the first applications developed by Apple for iPad. It is the app which convinced me to purchase my first iPad 7 years ago, primarily for reading books and PDF documents.

There’s many things to admire about iBooks:

  • Beautiful shelf view with covers, colourful and crisp.
  • Stunning page turning animation.
  • Auto-Night Theme.
  • Bookmarks and Notes sync.
  • Built-in iBooks Store.
  • Beautiful typography and tasteful layout.

Obviously nothing is perfect and there’s several aspects of iBooks which needs to be improved.


Other issue is lack of Activity Sheet. I prefer to keep an extra backup on top of iCloud for all my books. It is easier to import book but it is impossible to export it. Currently to backup files from iBooks Mac is necessary.

Sync was always a mystery for me. Generally it is very robust and fast. There’s very interesting aspects of it. For example, I can make a note then remove book from all devices and then add it one year laters and my note will be there waiting for me. Notes and Bookmarks sync is great and works everywhere. However, file sync only works for purchased books and PDF. And this leads me to DRM (digital rights management).

Because Mac is required to create a backup file for books purchased from iBooks Store, I don’t recommend to anyone to buy from iBooks Store if they don’t have access to Mac.

Problem of DRM.

Everything purchased from iBooks is protected by publishers which means I’m as copy owner don’t have control. Not every online book store is like this. You can get DRM free books from Humble Bundle, Oreilly and Image Comics which I wrote about. I applaud to their initiative. I don’t want my library to be forever locked inside of iBooks. It is important to have freedom to use any reader you like in case Apple goes out of books business or someone makes better reading app. As iPad user I appreciate that I can add any DRM free book into iBooks and read it there. I don’t appreciate the fact that non iBooks books not syncing (luckily reading position, notes and bookmarks do), especially when I can sync PDF files easily.

Moving Region.

One other thing I hate about DRM is region locking. For instance, if I purchase book from UK Store and than move to United States Store it is my responsibility to have an extra copy somewhere because Apple would not allow to re-download it again from US Store.1 That’s why I always have an external folder in iCloud Drive with my library.

I noticed very curious thing when adding books I purchased from US Store into iBooks where I logged in with UK account. Books can be added very easy and Apple does not prevent me to read them. Apple will even sync this books across all my devices. However when I “remove the download”, iBooks wouldn’t allow me to download it again.

General Stability.

Sometimes I get error message. I don’t remember details but it is obvious that ePub format needs to be improved, especially, error handling. There’s nothing worse than get your book corrupted when you don’t have an internet connection.

Update on Publishing.

Just about an hour ago Dimsumthinking wrote excellent blog post about iBooks Author and how Apple abandoned its development. I am mostly agree2 with the post and I hope Apple will continue investing into iBooks Author especially knowing how much worse Amazon tools are. This is missed opportunity for Apple to capture more creative publishers.

  1. The same is true for movies, TV shows and music. 

  2. Dimsumthinking mentioned that he is appreciating ability to email his customers who acquired book through Gum Road. I do understand benefits but as consumer I also value the fact that iBooks Store doesn’t disclose my email address. 

Text Selection on iPad.

Finding myself working from iPad more and more, including this blog, I wanted to share few tips on typing.

At first I would always connect my Applet Wireless Keyboard before working on long text however recently I found myself using on screen keyboard more. There’s many advantages when using software keyboard:

  1. I can start immediately.
  2. I can work on any surface at any place.
  3. Quick Type suggestions easier to reach.
  4. When I need to be quiet, I can disable sound.
  5. When sound is on it is superior experience to mechanical sound because different keys produce different sound.

Typing on glass is entirely different experience. However I don’t think it is lesser experience, moreover I think MacBook keyboard will be replaced with touch screen eventually. I am finding typing on glass easier and easier. You don’t need to press on keys and I like it. The same reason I prefer keyboards with less travel. I think it’s a direction we going to. It just a matter of practice and you can be as much productive as with hardware keyboard.

There’s disadvantages also - less screen space which we can’t do anything about and text selection problem. Classic iOS magnifier glass selection is accessible but not fast enough. New 3D Touch gesture is amazing on iPhone but doesn’t work on iPad. iOS 10 introduced trackpad mode for iPad but I always couldn’t understand how to select text with it. Here’s some things I’ve learned from iOS 10

  1. Pan with 2 fingers to move a cursor. That’s easy to do, reliable1 and faster than using magnifying glass.
  2. 2 fingers tap anywhere on the keyboard surface to select a word.
  3. 2 fingers double tap to select entire sentence.
  4. When text is selected, try to pan with 2 fingers to any direction to change selection.

The last 3 tips is amazing and makes typing on iPad much more enjoyable. They were not obvious for me.

You also can double tap on the text to select a word. It’s useful when keyboard is not present.

  1. I think something changed in the latest iOS update and my problem when I would select text accidentally while moving cursor with 2 fingers is fixed. It just doesn’t happen any more. This is great improvement. 

Page 2 of 8