Skip to content

Commit

Permalink
Merge pull request #6 from alvesmarcos/dev
Browse files Browse the repository at this point in the history
Add RxSwift
  • Loading branch information
alvesmarcos authored Oct 27, 2021
2 parents 30c53fe + 66f196c commit 5583584
Show file tree
Hide file tree
Showing 18 changed files with 137 additions and 223 deletions.
8 changes: 0 additions & 8 deletions GithubRepo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,11 @@
0DAFDE3326E71433009FF757 /* SearchViewController.xib in Resources */ = {isa = PBXBuildFile; fileRef = 0DAFDE3126E71433009FF757 /* SearchViewController.xib */; };
0DB25D6C271A420C00B2968C /* GithubFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D6B271A420C00B2968C /* GithubFetcher.swift */; };
0DB25D6E271A43E500B2968C /* RemoteGithubFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D6D271A43E500B2968C /* RemoteGithubFetcher.swift */; };
0DB25D70271A47D300B2968C /* MockGithubFetcher.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D6F271A47D300B2968C /* MockGithubFetcher.swift */; };
0DB25D73271A4DF500B2968C /* DataMapper.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D72271A4DF500B2968C /* DataMapper.swift */; };
0DB25D75271A517700B2968C /* RepositoryResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D74271A517700B2968C /* RepositoryResponse.swift */; };
0DB25D77271A519600B2968C /* OwnerResponse.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D76271A519600B2968C /* OwnerResponse.swift */; };
0DB25D7A271A5A1700B2968C /* GithubDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D79271A5A1700B2968C /* GithubDataSource.swift */; };
0DB25D7C271A5A8F00B2968C /* RemoteGithubDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D7B271A5A8F00B2968C /* RemoteGithubDataSource.swift */; };
0DB25D7E271A5BDE00B2968C /* LocalGithubDataSource.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D7D271A5BDE00B2968C /* LocalGithubDataSource.swift */; };
0DB25D81271A5F5500B2968C /* GithubRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D80271A5F5500B2968C /* GithubRepository.swift */; };
0DB25D83271A623A00B2968C /* GithubMainRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DB25D82271A623A00B2968C /* GithubMainRepository.swift */; };
0DC0572B2708F95A00EE6F5C /* UIColors+RandomColor.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0DC0572A2708F95A00EE6F5C /* UIColors+RandomColor.swift */; };
Expand Down Expand Up @@ -95,13 +93,11 @@
0DAFDE3126E71433009FF757 /* SearchViewController.xib */ = {isa = PBXFileReference; lastKnownFileType = file.xib; path = SearchViewController.xib; sourceTree = "<group>"; };
0DB25D6B271A420C00B2968C /* GithubFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GithubFetcher.swift; sourceTree = "<group>"; };
0DB25D6D271A43E500B2968C /* RemoteGithubFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteGithubFetcher.swift; sourceTree = "<group>"; };
0DB25D6F271A47D300B2968C /* MockGithubFetcher.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MockGithubFetcher.swift; sourceTree = "<group>"; };
0DB25D72271A4DF500B2968C /* DataMapper.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DataMapper.swift; sourceTree = "<group>"; };
0DB25D74271A517700B2968C /* RepositoryResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepositoryResponse.swift; sourceTree = "<group>"; };
0DB25D76271A519600B2968C /* OwnerResponse.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = OwnerResponse.swift; sourceTree = "<group>"; };
0DB25D79271A5A1700B2968C /* GithubDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GithubDataSource.swift; sourceTree = "<group>"; };
0DB25D7B271A5A8F00B2968C /* RemoteGithubDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RemoteGithubDataSource.swift; sourceTree = "<group>"; };
0DB25D7D271A5BDE00B2968C /* LocalGithubDataSource.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LocalGithubDataSource.swift; sourceTree = "<group>"; };
0DB25D80271A5F5500B2968C /* GithubRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GithubRepository.swift; sourceTree = "<group>"; };
0DB25D82271A623A00B2968C /* GithubMainRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GithubMainRepository.swift; sourceTree = "<group>"; };
0DC0572A2708F95A00EE6F5C /* UIColors+RandomColor.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "UIColors+RandomColor.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -311,7 +307,6 @@
children = (
0DB25D6B271A420C00B2968C /* GithubFetcher.swift */,
0DB25D6D271A43E500B2968C /* RemoteGithubFetcher.swift */,
0DB25D6F271A47D300B2968C /* MockGithubFetcher.swift */,
);
path = Services;
sourceTree = "<group>";
Expand All @@ -332,7 +327,6 @@
children = (
0DB25D79271A5A1700B2968C /* GithubDataSource.swift */,
0DB25D7B271A5A8F00B2968C /* RemoteGithubDataSource.swift */,
0DB25D7D271A5BDE00B2968C /* LocalGithubDataSource.swift */,
);
path = DataSource;
sourceTree = "<group>";
Expand Down Expand Up @@ -510,14 +504,12 @@
buildActionMask = 2147483647;
files = (
0DB25D73271A4DF500B2968C /* DataMapper.swift in Sources */,
0DB25D70271A47D300B2968C /* MockGithubFetcher.swift in Sources */,
0D5E5BB8271B98A5009CD366 /* Coordinator.swift in Sources */,
0DAFDE3226E71433009FF757 /* SearchViewController.swift in Sources */,
0D5C740B2707FAAC0052FADF /* Int+ConventIntoAFriendlyKMAbbr.swift in Sources */,
0D9A650E27050C420061CD8F /* Owner.swift in Sources */,
0D69A9ED271E3042004A973A /* SearchCoordinator.swift in Sources */,
0DB25D77271A519600B2968C /* OwnerResponse.swift in Sources */,
0DB25D7E271A5BDE00B2968C /* LocalGithubDataSource.swift in Sources */,
0DB25D6E271A43E500B2968C /* RemoteGithubFetcher.swift in Sources */,
0D9A6516270529090061CD8F /* RepositoryTableViewCell.swift in Sources */,
0DAFDE0D26E6EFC3009FF757 /* AppDelegate.swift in Sources */,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<key>GithubRepo.xcscheme_^#shared#^_</key>
<dict>
<key>orderHint</key>
<integer>4</integer>
<integer>8</integer>
</dict>
</dict>
</dict>
Expand Down
2 changes: 1 addition & 1 deletion GithubRepo/Application/AppCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit

class AppCoordinator: Coordinator {
var isCompleted: (() -> Void)?

var childCoordinators = [Coordinator]()

private let window: UIWindow
Expand Down
3 changes: 2 additions & 1 deletion GithubRepo/Data/DataSource/GithubDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
//

import Foundation
import RxSwift

protocol GithubDataSource {
func getRepositories(with query: String, completion: @escaping (Result<[Repository], Error>) -> Void)
func getRepositories(with query: String) -> Single<[Repository]>
}
28 changes: 0 additions & 28 deletions GithubRepo/Data/DataSource/LocalGithubDataSource.swift

This file was deleted.

14 changes: 5 additions & 9 deletions GithubRepo/Data/DataSource/RemoteGithubDataSource.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//

import Foundation
import RxSwift

class RemoteGithubDataSource: GithubDataSource {
private let service: GithubFetcher
Expand All @@ -14,15 +15,10 @@ class RemoteGithubDataSource: GithubDataSource {
self.service = service
}

func getRepositories(with query: String, completion: @escaping (Result<[Repository], Error>) -> Void) {
service.fetchRepositories(with: query) { response in
switch response {
case .success(let searchRepositoriesResponse):
let repositories = searchRepositoriesResponse.items.map { RepositoryResponseMapper.map($0) }
completion(.success(repositories))
case .failure(let error):
completion(.failure(error))
func getRepositories(with query: String) -> Single<[Repository]> {
return self.service.fetchRepositories(with: query)
.map { response in
response.1.items.map { RepositoryResponseMapper.map($0) }
}
}
}
}
52 changes: 23 additions & 29 deletions GithubRepo/Data/Repository/GithubMainRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,42 @@
//

import Foundation
import RxRelay
import RxSwift

class GithubMainRepository: GithubRepository {
private let dataSource: GithubDataSource
private(set) var repositories: [Repository]
private(set) var loading: Bool
private(set) var error: Bool
weak var delegate: GithubRepositoryDelegate?
private let disposeBag = DisposeBag()

let repositories: BehaviorRelay<[Repository]>
let state: BehaviorRelay<FetchState>

init(dataSource: GithubDataSource = RemoteGithubDataSource()) {
self.dataSource = dataSource
self.repositories = []
self.loading = false
self.error = false
self.repositories = BehaviorRelay<[Repository]>(value: [])
self.state = BehaviorRelay<FetchState>(value: .inital)
}

func handleChangeRepositories(_ repo: [Repository]) {
self.repositories = repo
delegate?.didChangeRepositories(repositories: repo)
}

func handleChangeLoading(_ loading: Bool) {
self.loading = loading
delegate?.didChangeLoading(loading: loading)
self.repositories.accept(repo)
}

func handleChangeError(_ error: Bool) {
self.error = error
delegate?.didChangeError(error: error)
func handleChangeState(_ state: FetchState) {
self.state.accept(state)
}

func fetchRepositories(with query: String) {
handleChangeLoading(true)
self.dataSource.getRepositories(with: query) { [weak self] response in
switch response {
case .success(let repositoriesResponse):
self?.handleChangeRepositories(repositoriesResponse)
self?.handleChangeLoading(false)
self?.handleChangeError(false)
case .failure:
self?.handleChangeLoading(false)
self?.handleChangeError(true)
}
}
self.handleChangeState(.loading)
self.dataSource.getRepositories(with: query)
.subscribe(
onSuccess: { [weak self] in
self?.handleChangeRepositories($0)
self?.handleChangeState($0.isEmpty ? .empty: .content)
},
onFailure: { [weak self] _ in
self?.handleChangeState(.error)
}
)
.disposed(by: disposeBag)
}
}
13 changes: 5 additions & 8 deletions GithubRepo/Data/Repository/GithubRepository.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@
//

import Foundation
import RxRelay

protocol GithubRepositoryDelegate: AnyObject {
func didChangeLoading(loading: Bool)
func didChangeRepositories(repositories: [Repository])
func didChangeError(error: Bool)
enum FetchState {
case loading, error, content, empty, inital
}

protocol GithubRepository {
var delegate: GithubRepositoryDelegate? { get set }
var repositories: [Repository] { get }
var loading: Bool { get }
var error: Bool { get }
var repositories: BehaviorRelay<[Repository]> { get }
var state: BehaviorRelay<FetchState> { get }

func fetchRepositories(with query: String)
}
3 changes: 2 additions & 1 deletion GithubRepo/Data/Services/GithubFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
//

import Foundation
import RxSwift

protocol GithubFetcher {
func fetchRepositories(with query: String, completion: @escaping(Result<SearchRepoResponse, Error>) -> Void)
func fetchRepositories(with query: String) -> Single<(HTTPURLResponse, SearchRepoResponse)>
}
44 changes: 0 additions & 44 deletions GithubRepo/Data/Services/MockGithubFetcher.swift

This file was deleted.

17 changes: 5 additions & 12 deletions GithubRepo/Data/Services/RemoteGithubFetcher.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@

import Alamofire
import Foundation
import RxAlamofire
import RxSwift

struct GithubFetcherConstants {
static let kUrl = "https://api.github.com/search/repositories"
}

struct RemoteGithubFetcher: GithubFetcher {
func fetchRepositories(with query: String, completion: @escaping(Result<SearchRepoResponse, Error>) -> Void) {
func fetchRepositories(with query: String) -> Single<(HTTPURLResponse, SearchRepoResponse)> {
guard let url = URL(string: GithubFetcherConstants.kUrl) else {
completion(.failure(URLError(.badURL)))
return
return Single.error(URLError(.badURL))
}
AF.request(url, method: .get, parameters: ["q": query])
.responseDecodable(of: SearchRepoResponse.self) { response in
switch response.result {
case .success(let data):
completion(.success(data))
case .failure(let error):
completion(.failure(error))
}
}
return RxAlamofire.requestDecodable(.get, url, parameters: ["q": query]).asSingle()
}
}
2 changes: 1 addition & 1 deletion GithubRepo/Scenes/About/AboutCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit

class AboutCoordinator: NavigationCoordinator {
var isCompleted: (() -> Void)?

var rootViewController: UINavigationController

var childCoordinators = [Coordinator]()
Expand Down
2 changes: 1 addition & 1 deletion GithubRepo/Scenes/Search/SearchCoordinator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import UIKit

class SearchCoordinator: NavigationCoordinator {
var isCompleted: (() -> Void)?

var rootViewController: UINavigationController

var childCoordinators = [Coordinator]()
Expand Down
Loading

0 comments on commit 5583584

Please # to comment.