-
Notifications
You must be signed in to change notification settings - Fork 37
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 [RM79] Character List View Model #82
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// | ||
// CharacterCardStateFactory.swift | ||
// Rick And Morty | ||
// | ||
// Created by Scottie Gray on 2021-08-13. | ||
// Copyright © 2021 Novoda. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
final class CharacterCardStateFactory { | ||
private func getIsAlive(character: Character) -> Bool { | ||
if character.status == "Alive" { | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
|
||
private func getFirstEpisode(character: Character) -> String? { | ||
if let firstEpisode = character.episodeURLs.first { | ||
return firstEpisode | ||
} | ||
|
||
return nil | ||
} | ||
Comment on lines
+20
to
+26
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're returning nil this code doesn't do anything. you could just call |
||
|
||
func createCharacterCardState(from character: Character) -> CharacterCardState { | ||
let isAlive = getIsAlive(character: character) | ||
let firstEpisode = getFirstEpisode(character: character) ?? "Unknown" | ||
let characterCardState = CharacterCardState(id: character.id, name: character.name, imageURL: character.imageURL, isAlive: isAlive, species: character.species, lastLocation: character.lastLocation.name, firstEpisode: firstEpisode) | ||
|
||
return characterCardState | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// | ||
// CharacterListViewModel.swift | ||
// Rick And Morty | ||
// | ||
// Created by Scottie Gray on 2021-08-13. | ||
// Copyright © 2021 Novoda. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
final class CharacterListViewModel: ObservableObject { | ||
@Published var characterListViewState: CharacterListViewState = CharacterListViewState(characterCardStates: []) | ||
|
||
private let characterRepository: CharacterRepositoryProtocol = CharacterRepository() | ||
private let characterCardStateFactory = CharacterCardStateFactory() | ||
|
||
init() { | ||
loadCardStates() | ||
} | ||
|
||
func loadCardStates() { | ||
characterRepository.getCharacters { characters in | ||
for character in characters { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do you think about using |
||
let cardState = self.characterCardStateFactory.createCharacterCardState(from: character) | ||
self.characterListViewState.characterCardStates.append(cardState) | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ import Foundation | |
struct CharacterCardState { | ||
var id: Int | ||
var name: String | ||
var image: UIImage | ||
var imageURL: String | ||
var isAlive: Bool | ||
var species: String | ||
var lastLocation: String | ||
|
@@ -35,9 +35,9 @@ struct CharacterCard: View { | |
|
||
var body: some View { | ||
HStack(alignment: .top) { | ||
Image(uiImage: characterCardState.image) | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
//Image(uiImage: characterCardState.image) | ||
//.resizable() | ||
//.aspectRatio(contentMode: .fit) | ||
Comment on lines
+38
to
+40
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. They not to commit commented out code. |
||
|
||
VStack(alignment: .leading, spacing: constants.VSpacing) { | ||
VStack(alignment: .leading, spacing: constants.noSpacing) { | ||
|
@@ -81,11 +81,11 @@ struct CharacterTileView_Previews: PreviewProvider { | |
static var previews: some View { | ||
Group { | ||
VStack { | ||
CharacterCard(characterCardState: CharacterCardState(id: 2, name: "Morty", image: UIImage(named: "morty-image")!, isAlive: true, species: "Human", lastLocation: "Earth", firstEpisode: "Episode 1")) | ||
//CharacterCard(characterCardState: CharacterCardState(id: 2, name: "Morty", image: UIImage(named: "morty-image")!, isAlive: true, species: "Human", lastLocation: "Earth", firstEpisode: "Episode 1")) | ||
} | ||
.preferredColorScheme(.light) | ||
VStack { | ||
CharacterCard(characterCardState: CharacterCardState(id: 2, name: "Morty", image: UIImage(named: "morty-image")!, isAlive: true, species: "Human", lastLocation: "Earth", firstEpisode: "Episode 1")) | ||
//CharacterCard(characterCardState: CharacterCardState(id: 2, name: "Morty", image: UIImage(named: "morty-image")!, isAlive: true, species: "Human", lastLocation: "Earth", firstEpisode: "Episode 1")) | ||
} | ||
.preferredColorScheme(.dark) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I remember correctly there is also state
unknown
. is the bool correct choice here?