diff --git a/iphone/Maps/Classes/CarPlay/Template Builders/ListTemplateBuilder.swift b/iphone/Maps/Classes/CarPlay/Template Builders/ListTemplateBuilder.swift index 5874d7d43..7b0f58148 100644 --- a/iphone/Maps/Classes/CarPlay/Template Builders/ListTemplateBuilder.swift +++ b/iphone/Maps/Classes/CarPlay/Template Builders/ListTemplateBuilder.swift @@ -13,7 +13,7 @@ final class ListTemplateBuilder { case search } - // MARK: - CPListTemplate bilder + // MARK: - CPListTemplate builder class func buildListTemplate(for type: ListTemplateType) -> CPListTemplate { var title = "" var trailingNavigationBarButtons = [CPBarButton]() @@ -40,26 +40,27 @@ final class ListTemplateBuilder { case .bookmarks(let category): title = category.title } - let template = CPListTemplate(title: title, sections: []) + + let sections = buildSectionsForType(type) + let template = CPListTemplate(title: title, sections: sections) template.trailingNavigationBarButtons = trailingNavigationBarButtons - obtainResources(for: type, template: template) return template } - - private class func obtainResources(for type: ListTemplateType, template: CPListTemplate) { + + private class func buildSectionsForType(_ type: ListTemplateType) -> [CPListSection] { switch type { case .history: - obtainHistory(template: template) + return buildHistorySections() case .bookmarks(let category): - obtainBookmarks(template: template, categoryId: category.categoryId) + return buildBookmarksSections(categoryId: category.categoryId) case .bookmarkLists: - obtainCategories(template: template) + return buildBookmarkListsSections() case .searchResults(let results): - convertSearchResults(results, template: template) + return buildSearchResultsSections(results) } } - - private class func obtainHistory(template: CPListTemplate) { + + private class func buildHistorySections() -> [CPListSection] { let searchQueries = FrameworkHelper.obtainLastSearchQueries() let items = searchQueries.map({ (text) -> CPListItem in let item = CPListItem(text: text, detailText: nil, image: UIImage(named: "recent")) @@ -67,11 +68,10 @@ final class ListTemplateBuilder { metadata: nil) return item }) - let section = CPListSection(items: items) - template.updateSections([section]) + return [CPListSection(items: items)] } - - private class func obtainCategories(template: CPListTemplate) { + + private class func buildBookmarkListsSections() -> [CPListSection] { let bookmarkManager = BookmarksManager.shared() let categories = bookmarkManager.sortedUserCategories() let items: [CPListItem] = categories.compactMap({ category in @@ -82,11 +82,10 @@ final class ListTemplateBuilder { metadata: CategoryInfo(category: category)) return item }) - let section = CPListSection(items: items) - template.updateSections([section]) + return [CPListSection(items: items)] } - - private class func obtainBookmarks(template: CPListTemplate, categoryId: MWMMarkGroupID) { + + private class func buildBookmarksSections(categoryId: MWMMarkGroupID) -> [CPListSection] { let bookmarkManager = BookmarksManager.shared() let bookmarks = bookmarkManager.bookmarks(forCategory: categoryId) var items = bookmarks.map({ (bookmark) -> CPListItem in @@ -103,11 +102,10 @@ final class ListTemplateBuilder { cropWarning.isEnabled = false items.append(cropWarning) } - let section = CPListSection(items: items) - template.updateSections([section]) + return [CPListSection(items: items)] } - - private class func convertSearchResults(_ results: [MWMCarPlaySearchResultObject], template: CPListTemplate) { + + private class func buildSearchResultsSections(_ results: [MWMCarPlaySearchResultObject]) -> [CPListSection] { var items = [CPListItem]() for object in results { let item = CPListItem(text: object.title, detailText: object.address) @@ -115,10 +113,10 @@ final class ListTemplateBuilder { metadata: SearchResultInfo(originalRow: object.originalRow)) items.append(item) } - let section = CPListSection(items: items) - template.updateSections([section]) + return [CPListSection(items: items)] } - + + // MARK: - CPBarButton builder private class func buildBarButton(type: BarButtonType, action: ((CPBarButton) -> Void)?) -> CPBarButton { switch type {