Skip to content

Commit

Permalink
Merge pull request #972 from DeluxeAlonso/feature/code-cleanup
Browse files Browse the repository at this point in the history
Feature/code cleanup
  • Loading branch information
DeluxeAlonso authored Sep 10, 2024
2 parents d4a87e1 + ef26f72 commit b175af2
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 12 deletions.
4 changes: 4 additions & 0 deletions UpcomingMovies.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@
E2C42E1824C3F9BF0047A8EF /* ProfileMockFactory.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C42E1724C3F9BF0047A8EF /* ProfileMockFactory.swift */; };
E2C44C192616C6B0004B9C9B /* ToastSuccessConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C44C182616C6B0004B9C9B /* ToastSuccessConfiguration.swift */; };
E2C44C1F2616C6D0004B9C9B /* ToastFailureConfiguration.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C44C1E2616C6D0004B9C9B /* ToastFailureConfiguration.swift */; };
E2C75E5C2C900320004174BB /* SearchOptionsDataSourceTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C75E5B2C90031F004174BB /* SearchOptionsDataSourceTests.swift */; };
E2C8FFBF2C3F8AE500EB983D /* MovieSearchModelTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C8FFBE2C3F8AE500EB983D /* MovieSearchModelTests.swift */; };
E2C8FFC12C3F8B8A00EB983D /* GenreProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C8FFC02C3F8B8A00EB983D /* GenreProtocol.swift */; };
E2C951B42C4E1CF3005297D8 /* MockUserProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = E2C951B32C4E1CF3005297D8 /* MockUserProtocol.swift */; };
Expand Down Expand Up @@ -928,6 +929,7 @@
E2C42E1724C3F9BF0047A8EF /* ProfileMockFactory.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileMockFactory.swift; sourceTree = "<group>"; };
E2C44C182616C6B0004B9C9B /* ToastSuccessConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastSuccessConfiguration.swift; sourceTree = "<group>"; };
E2C44C1E2616C6D0004B9C9B /* ToastFailureConfiguration.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ToastFailureConfiguration.swift; sourceTree = "<group>"; };
E2C75E5B2C90031F004174BB /* SearchOptionsDataSourceTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchOptionsDataSourceTests.swift; sourceTree = "<group>"; };
E2C8FFBE2C3F8AE500EB983D /* MovieSearchModelTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MovieSearchModelTests.swift; sourceTree = "<group>"; };
E2C8FFC02C3F8B8A00EB983D /* GenreProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GenreProtocol.swift; sourceTree = "<group>"; };
E2C951B32C4E1CF3005297D8 /* MockUserProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockUserProtocol.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2322,6 +2324,7 @@
children = (
E279169D28F092360092B592 /* SearchOptionsViewModelTests.swift */,
E2D61C932C8581FF00339AD1 /* DefaultSearchOptionTests.swift */,
E2C75E5B2C90031F004174BB /* SearchOptionsDataSourceTests.swift */,
);
path = SearchOptions;
sourceTree = "<group>";
Expand Down Expand Up @@ -3331,6 +3334,7 @@
E2B2D2942BBD0A17000690AF /* MockMovieProtocol.swift in Sources */,
E2A991992A4C0C5F0006C9C3 /* MovieDetailPosterCoordinatorTests.swift in Sources */,
E26A81292A84A796002C7029 /* TestError.swift in Sources */,
E2C75E5C2C900320004174BB /* SearchOptionsDataSourceTests.swift in Sources */,
E2B8C49B28B738E2006A8ECC /* FavoritesSavedMoviesInteractorTests.swift in Sources */,
E2A958CC2A3677B70004FC5C /* AccountInteractorTests.swift in Sources */,
E2A958D52A367C970004FC5C /* MovieDetailOptionTests.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,22 @@ import UIKit

final class SearchOptionsDataSource: NSObject, UITableViewDataSource {

private var viewModel: SearchOptionsViewModelProtocol?
private let viewModel: SearchOptionsViewModelProtocol

init(viewModel: SearchOptionsViewModelProtocol?) {
init(viewModel: SearchOptionsViewModelProtocol) {
self.viewModel = viewModel
}

func numberOfSections(in tableView: UITableView) -> Int {
guard let viewModel = viewModel else { return 0 }
return viewModel.viewState.value.sections.count
viewModel.viewState.value.sections.count
}

func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
guard let viewModel = viewModel else { return nil }
let sections = viewModel.viewState.value.sections
return sections[section].title
}

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
guard let viewModel = viewModel else { return 0 }
switch viewModel.section(at: section) {
case .recentlyVisited:
return 1
Expand All @@ -40,7 +37,6 @@ final class SearchOptionsDataSource: NSObject, UITableViewDataSource {
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let viewModel = viewModel else { return UITableViewCell() }
switch viewModel.section(at: indexPath.section) {
case .recentlyVisited:
return recentlyVisitedDataSource(tableView, at: indexPath)
Expand All @@ -52,7 +48,6 @@ final class SearchOptionsDataSource: NSObject, UITableViewDataSource {
}

private func recentlyVisitedDataSource(_ tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
guard let viewModel = viewModel else { fatalError() }
let cell = tableView.dequeueReusableCell(with: RecentlyVisitedMoviesTableViewCell.self, for: indexPath)
cell.selectionStyle = .none
cell.delegate = self
Expand All @@ -62,13 +57,13 @@ final class SearchOptionsDataSource: NSObject, UITableViewDataSource {

private func defaultSearchOptionDataSource(_ tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(with: DefaultSearchOptionTableViewCell.self, for: indexPath)
cell.viewModel = viewModel?.defaultSearchOptionsCells[indexPath.row]
cell.viewModel = viewModel.defaultSearchOptionsCells[indexPath.row]
return cell
}

private func genreSearchOptionDataSource(_ tableView: UITableView, at indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(with: GenreSearchOptionTableViewCell.self, for: indexPath)
cell.viewModel = viewModel?.genreCells[indexPath.row]
cell.viewModel = viewModel.genreCells[indexPath.row]
return cell
}

Expand All @@ -80,7 +75,7 @@ extension SearchOptionsDataSource: RecentlyVisitedMoviesTableViewCellDelegate {

func recentlyVisitedMoviesTableViewCell(_ recentlyVisitedMoviesTableViewCell: RecentlyVisitedMoviesTableViewCell,
didSelectMovieAt indexPath: IndexPath) {
viewModel?.getRecentlyVisitedMovieSelection(by: indexPath.row)
viewModel.getRecentlyVisitedMovieSelection(by: indexPath.row)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ final class SearchOptionsViewController: UITableViewController, Storyboarded {
}

private func setupDataSource() {
dataSource = SearchOptionsDataSource(viewModel: viewModel)
dataSource = viewModel.flatMap { SearchOptionsDataSource(viewModel: $0) }
tableView.dataSource = dataSource
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// SearchOptionsDataSourceTests.swift
// UpcomingMoviesTests
//
// Created by Alonso on 9/09/24.
// Copyright © 2024 Alonso. All rights reserved.
//

import XCTest
@testable import UpcomingMovies

final class SearchOptionsDataSourceTests: XCTestCase {

private var dataSource: SearchOptionsDataSource!
private var viewModel: MockSearchOptionsViewModel!

override func setUpWithError() throws {
try super.setUpWithError()
viewModel = MockSearchOptionsViewModel()
dataSource = SearchOptionsDataSource(viewModel: viewModel)
}

override func tearDownWithError() throws {
dataSource = nil
viewModel = nil
try super.tearDownWithError()
}

}

0 comments on commit b175af2

Please sign in to comment.