Skip to content

Commit

Permalink
Revert "Adopt diffable data source for tracks"
Browse files Browse the repository at this point in the history
This reverts commit 9015ce7.
  • Loading branch information
mttcrsp committed Jan 8, 2025
1 parent 9015ce7 commit 08ac59c
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 124 deletions.
98 changes: 70 additions & 28 deletions App/Sources/Controllers/SearchController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ final class SearchController: UISplitViewController {
tracksViewController.favoritesDelegate = self
tracksViewController.indexDataSource = self
tracksViewController.indexDelegate = self
tracksViewController.dataSource = self
tracksViewController.delegate = self
self.tracksViewController = tracksViewController

Expand All @@ -79,17 +80,56 @@ final class SearchController: UISplitViewController {
viewControllers.append(makeWelcomeViewController())
}

reloadTracks(animated: false)
reloadTracks()
observer = dependencies.favoritesService.addObserverForTracks { [weak self] in
self?.reloadTracks(animated: true)
self?.reloadTracks()
}
}

private func reloadTracks(animated: Bool) {
private func reloadTracks() {
dependencies.tracksService.loadConfiguration { [weak self] tracksConfiguration in
guard let self else { return }
self?.tracksLoadingDidSucceed(tracksConfiguration)
}
}

private func tracksLoadingDidSucceed(_ tracksConfiguration: TracksConfiguration) {
guard let currentTracksConfiguration = self.tracksConfiguration else {
self.tracksConfiguration = tracksConfiguration
tracksViewController?.reloadData()
return
}

let oldNonFavoriteTracks = currentTracksConfiguration.filteredTracks[selectedFilter]?.map(\.name) ?? []
let newNonFavoriteTracks = tracksConfiguration.filteredTracks[selectedFilter]?.map(\.name) ?? []
guard oldNonFavoriteTracks == newNonFavoriteTracks else {
self.tracksConfiguration = tracksConfiguration
tracksViewController?.reloadData()
return
}

var oldIdentifiers = currentTracksConfiguration.filteredFavoriteTracks[selectedFilter]?.map(\.name) ?? []
let newIdentifiers = tracksConfiguration.filteredFavoriteTracks[selectedFilter]?.map(\.name) ?? []
let deletesIdentifiers = Set(oldIdentifiers).subtracting(Set(newIdentifiers))
let insertsIdentifiers = Set(newIdentifiers).subtracting(Set(oldIdentifiers))

tracksViewController?.performBatchUpdates {
switch (oldIdentifiers.isEmpty, newIdentifiers.isEmpty) {
case (true, false): tracksViewController?.insertFavoritesSection()
case (false, true): tracksViewController?.deleteFavoritesSection()
default: break
}

for (index, track) in oldIdentifiers.enumerated().reversed() where deletesIdentifiers.contains(track) {
tracksViewController?.deleteFavorite(at: index)
oldIdentifiers.remove(at: index)
}

for (index, track) in newIdentifiers.enumerated() where insertsIdentifiers.contains(track) {
tracksViewController?.insertFavorite(at: index)
oldIdentifiers.insert(track, at: index)
}

self.tracksConfiguration = tracksConfiguration
self.tracksViewController?.setSections(tracksSections, animatingDifferences: animated)
}
}
}
Expand All @@ -105,7 +145,7 @@ extension SearchController: UISplitViewControllerDelegate {
}
}

extension SearchController: TracksViewControllerDelegate {
extension SearchController: TracksViewControllerDataSource, TracksViewControllerDelegate {
private var filteredTracks: [Track] {
tracksConfiguration?.filteredTracks[selectedFilter] ?? []
}
Expand All @@ -118,25 +158,28 @@ extension SearchController: TracksViewControllerDelegate {
!filteredFavoriteTracks.isEmpty
}

var tracksSections: [TracksSection] {
var sections: [TracksSection] = []
if hasFavoriteTracks {
sections.append(
.init(
title: L10n.Search.Filter.favorites,
accessibilityIdentifier: "favorites",
tracks: filteredFavoriteTracks
)
)
}
sections.append(
.init(
title: selectedFilter.title,
accessibilityIdentifier: selectedFilter.accessibilityIdentifier,
tracks: filteredTracks
)
)
return sections
private func isFavoriteSection(_ section: Int) -> Bool {
section == 0 && hasFavoriteTracks
}

func numberOfSections(in _: TracksViewController) -> Int {
hasFavoriteTracks ? 2 : 1
}

func tracksViewController(_: TracksViewController, titleForSectionAt section: Int) -> String? {
isFavoriteSection(section) ? L10n.Search.Filter.favorites : selectedFilter.title
}

func tracksViewController(_: TracksViewController, accessibilityIdentifierForSectionAt section: Int) -> String? {
isFavoriteSection(section) ? "favorites" : selectedFilter.accessibilityIdentifier
}

func tracksViewController(_: TracksViewController, numberOfTracksIn section: Int) -> Int {
isFavoriteSection(section) ? filteredFavoriteTracks.count : filteredTracks.count
}

func tracksViewController(_: TracksViewController, trackAt indexPath: IndexPath) -> Track {
isFavoriteSection(indexPath.section) ? filteredFavoriteTracks[indexPath.row] : filteredTracks[indexPath.row]
}

func tracksViewController(_ tracksViewController: TracksViewController, didSelect track: Track) {
Expand Down Expand Up @@ -174,9 +217,8 @@ extension SearchController: TracksViewControllerDelegate {
let filters = tracksConfiguration?.filters ?? []
for filter in filters where filter != selectedFilter {
let actionHandler: (UIAlertAction) -> Void = { [weak self] _ in
guard let self else { return }
self.selectedFilter = filter
self.tracksViewController?.setSections(tracksSections, animatingDifferences: false)
self?.selectedFilter = filter
self?.tracksViewController?.reloadData()
}
let action = UIAlertAction(title: filter.title, style: .default, handler: actionHandler)
alertController.addAction(action)
Expand Down
19 changes: 17 additions & 2 deletions App/Sources/Controllers/YearController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ final class YearController: TracksViewController {
private weak var eventsViewController: EventsViewController?
private var searchController: UISearchController?

private var tracks: [Track] = []
private var events: [Event] = []
var results: [Event] = []

Expand All @@ -32,6 +33,7 @@ final class YearController: TracksViewController {
view.backgroundColor = .systemGroupedBackground

delegate = self
dataSource = self
definesPresentationContext = true

let resultsViewController = EventsViewController(style: .grouped)
Expand All @@ -52,14 +54,27 @@ final class YearController: TracksViewController {
case let .failure(error):
self.didError?(self, error)
case let .success(tracks):
self.setSections([.init(tracks: tracks)])
self.tracks = tracks
self.reloadData()
}
}
}
}
}

extension YearController: TracksViewControllerDelegate {
extension YearController: TracksViewControllerDataSource, TracksViewControllerDelegate {
func numberOfSections(in _: TracksViewController) -> Int {
1
}

func tracksViewController(_: TracksViewController, numberOfTracksIn _: Int) -> Int {
tracks.count
}

func tracksViewController(_: TracksViewController, trackAt indexPath: IndexPath) -> Track {
tracks[indexPath.row]
}

func tracksViewController(_ tracksViewController: TracksViewController, didSelect track: Track) {
let eventsViewController = EventsViewController(style: .grouped)
eventsViewController.title = track.formattedName
Expand Down
6 changes: 0 additions & 6 deletions App/Sources/Models/Track.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ extension Track: Equatable {
}
}

extension Track: Hashable {
func hash(into hasher: inout Hasher) {
hasher.combine(name)
}
}

extension Track {
var formattedName: String {
TrackFormatter().formattedName(from: name)
Expand Down
Loading

0 comments on commit 08ac59c

Please sign in to comment.