Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature [RM80] Character Repository #81

Merged
merged 6 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion Rick-and-Morty/Rick And Morty.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
17588BAC26C1750B008ECC31 /* Character.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17588BAB26C1750B008ECC31 /* Character.swift */; };
17588BB226C2B126008ECC31 /* RickAndMortyService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17588BB126C2B126008ECC31 /* RickAndMortyService.swift */; };
179D2D7626C3D24B008E1B3A /* RickAndMortyServiceProtocol.swift in Sources */ = {isa = PBXBuildFile; fileRef = 179D2D7526C3D24B008E1B3A /* RickAndMortyServiceProtocol.swift */; };
179D2D7826C3E456008E1B3A /* CharacterRepository.swift in Sources */ = {isa = PBXBuildFile; fileRef = 179D2D7726C3E456008E1B3A /* CharacterRepository.swift */; };
B811686D1CFF1C9900301A0A /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = B811686C1CFF1C9900301A0A /* AppDelegate.swift */; };
B81168761CFF1C9900301A0A /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = B81168751CFF1C9900301A0A /* Assets.xcassets */; };
B81168791CFF1C9900301A0A /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = B81168771CFF1C9900301A0A /* LaunchScreen.storyboard */; };
Expand All @@ -32,6 +33,7 @@
17588BAB26C1750B008ECC31 /* Character.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Character.swift; sourceTree = "<group>"; };
17588BB126C2B126008ECC31 /* RickAndMortyService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RickAndMortyService.swift; sourceTree = "<group>"; };
179D2D7526C3D24B008E1B3A /* RickAndMortyServiceProtocol.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RickAndMortyServiceProtocol.swift; sourceTree = "<group>"; };
179D2D7726C3E456008E1B3A /* CharacterRepository.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterRepository.swift; sourceTree = "<group>"; };
B81168691CFF1C9900301A0A /* Rick And Morty.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Rick And Morty.app"; sourceTree = BUILT_PRODUCTS_DIR; };
B811686C1CFF1C9900301A0A /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
B81168751CFF1C9900301A0A /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
Expand Down Expand Up @@ -77,6 +79,22 @@
path = Services;
sourceTree = "<group>";
};
179D2D7B26C40CF4008E1B3A /* Repositories */ = {
isa = PBXGroup;
children = (
179D2D7726C3E456008E1B3A /* CharacterRepository.swift */,
);
path = Repositories;
sourceTree = "<group>";
};
179D2D7C26C40CFF008E1B3A /* Views */ = {
isa = PBXGroup;
children = (
1711B39D26B1898100BE935B /* CharacterListView.swift */,
);
path = Views;
sourceTree = "<group>";
};
B81168601CFF1C9900301A0A = {
isa = PBXGroup;
children = (
Expand All @@ -101,7 +119,8 @@
17588BB026C2B112008ECC31 /* Services */,
17588BAA26C174FB008ECC31 /* Model */,
B811686C1CFF1C9900301A0A /* AppDelegate.swift */,
1711B39D26B1898100BE935B /* CharacterListView.swift */,
179D2D7C26C40CFF008E1B3A /* Views */,
179D2D7B26C40CF4008E1B3A /* Repositories */,
B81168751CFF1C9900301A0A /* Assets.xcassets */,
B81168771CFF1C9900301A0A /* LaunchScreen.storyboard */,
B811687A1CFF1C9900301A0A /* Info.plist */,
Expand Down Expand Up @@ -227,6 +246,7 @@
179D2D7626C3D24B008E1B3A /* RickAndMortyServiceProtocol.swift in Sources */,
1711B39E26B1898100BE935B /* CharacterListView.swift in Sources */,
B811686D1CFF1C9900301A0A /* AppDelegate.swift in Sources */,
179D2D7826C3E456008E1B3A /* CharacterRepository.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import Foundation

protocol CharacterRepositoryProtocol {
func getCharacters(completion: @escaping (([Character]) -> Void))
}

final class CharacterRepository: CharacterRepositoryProtocol {
private var characterPageURL: URL? = RickAndMortyService.baseURL.appendingPathComponent("character")

private let rickAndMortyService: RickAndMortyServiceProtocol = RickAndMortyService()

func getCharacters(completion: @escaping (([Character]) -> Void)) {

if let url = characterPageURL {
rickAndMortyService.fetchData(url: url) { (characterList: CharactersList) in
if let nextURLString = characterList.info.next {
self.characterPageURL = URL(string: nextURLString)!
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add another let to this if let to remove the ! in URL(string: nextURLString)!

} else {
self.characterPageURL = nil
}

completion(characterList.results)
} error: { error in
print(error.debugDescription)
return
}
}
}
}