Search code examples
swiftmodel-view-controlleruisearchcontroller

How to properly clean up my code using MVC?


Basically, I have the following code in a TableViewController which is basically repeated in another CollectionViewController except for a few extra lines:

  func configureSearchController() {
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Albums"
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
    definesPresentationContext = true
    searchController.searchBar.delegate = self
  }

In order to clean up the code in both my controllers, I wanted to move these methods to a new file, like so:

class SearchBarManager: UIViewController {

func configureAlbumSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Albums"
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
    definesPresentationContext = true
}

func configurePhotoSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
    searchController.obscuresBackgroundDuringPresentation = false
    searchController.searchBar.placeholder = "Search Photos"
    navigationItem.searchController = searchController
    navigationItem.hidesSearchBarWhenScrolling = false
    definesPresentationContext = true
    searchController.searchBar.scopeButtonTitles = ["1 Column", "2 Columns", "3 Columns"]
    searchController.searchBar.selectedScopeButtonIndex = 2
}}

Is this the proper way to do it? I feel like there is a simpler way to simplify what I'm trying to accomplish, but I'm not sure. Thanks in advance!


Solution

  • Keep your code clean is very important, it enables you to reuse code. To achieve this you could use some common design patterns like Strategy and you could use protocol-oriented programming.

    I will leave some sources so you can go deeper into how to achieve this by using SOLID principles: https://medium.com/ios-expert-series-or-interview-series/solid-design-principle-using-swift-34bb1731cfb3

    What you need in this case is a simple abstraction, you need to create a function that is able to configure any SearchController. I will give you an example, but don't let this example to limit you, there are many other (and better ways) to achieve this.

     class SearchBarManager: UIViewController {
    
    private func configureSearchController(searchController: UISearchController, navigationItem: UINavigationItem, title: String, scopeButtonTitles: [String] = [], selectedScopeButtonIndex: Int = 0) {
        searchController.obscuresBackgroundDuringPresentation = false
        searchController.searchBar.placeholder = title
        navigationItem.searchController = searchController
        navigationItem.hidesSearchBarWhenScrolling = false
        definesPresentationContext = true
        searchController.searchBar.scopeButtonTitles = scopeButtonTitles
        searchController.searchBar.selectedScopeButtonIndex = selectedScopeButtonIndex
    }
    
    
    func configureAlbumSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
        configureSearchController(searchController: searchController, navigationItem: navigationItem, title: "Search Albums")
    }
    
    func configurePhotoSearchController(_ searchController: UISearchController, _ navigationItem: UINavigationItem) {
        configureSearchController(searchController: searchController, navigationItem: navigationItem, title: "Search Photos", scopeButtonTitles: ["1 Column", "2 Columns", "3 Columns"], selectedScopeButtonIndex: 2)
    }
    
    }