-
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 [RM61] Created and Implemented CharactersViewModel #65
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4ff360d
Implemented CharactersViewModel
swg99 b0afe66
Created ContentViewModel
swg99 96f8de8
Refactored ContentViewModel file
swg99 29899cd
Refactored characterviewstate
swg99 b8b6abe
Merge branch 'scottie-main' into feature/rm-61-CharactersViewModel
swg99 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
Rick-and-Morty/Rick And Morty/Factories/CharacterViewStateFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import Foundation | ||
|
||
final class CharacterViewStateFactory { | ||
func createCharacterViewState(from character: Character) -> CharacterViewState { | ||
let shortDescription = getShortDescription(for: character) | ||
let imagePosition = getImagePosition(for: character) | ||
|
||
return CharacterViewState(character: character, name: character.name, description: character.description, shortDescription: shortDescription, imageName: character.image, imagePosition: imagePosition) | ||
} | ||
|
||
private func getShortDescription(for character: Character) -> String? { | ||
if let character = character as? Morty { | ||
return character.shortDescription | ||
} | ||
|
||
return nil | ||
} | ||
|
||
private func getImagePosition(for character: Character) -> CharacterImagePosition { | ||
if character is Morty { | ||
return .left | ||
} else { | ||
return .right | ||
} | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
Rick-and-Morty/Rick And Morty/Factories/CharactersViewStateFactory.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Foundation | ||
|
||
final class CharactersViewStateFactory { | ||
func createCharactersViewState(characterType: CharacterType, characterViewStates: [CharacterViewState]) -> CharactersViewState { | ||
let title = getTitle(for: characterType) | ||
let iconName = getIconName(for: characterType) | ||
|
||
return CharactersViewState(title: title, iconName: iconName, characterViewStates: characterViewStates) | ||
} | ||
|
||
private func getTitle(for characterType: CharacterType) -> String { | ||
return characterType == .rick ? "Ricks" : "Morties" | ||
} | ||
|
||
private func getIconName(for characterType: CharacterType) -> String { | ||
return characterType == .rick ? "rick-icon" : "morty-icon" | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
Rick-and-Morty/Rick And Morty/ViewModels/CharactersViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import Foundation | ||
|
||
final class CharactersViewModel: ObservableObject { | ||
let characterType: CharacterType | ||
|
||
@Published var characters: [Character] = [] | ||
var title: String = "" | ||
var imagePosition: CharacterImagePosition = .right | ||
|
||
init(characterType: CharacterType) { | ||
self.characterType = characterType | ||
self.title = getTitle() | ||
self.imagePosition = getImagePosition() | ||
|
||
self.characters = fetchCharacters() | ||
} | ||
|
||
private func fetchCharacters() -> [Character] { | ||
return characterType == .rick ? ricks : morties | ||
} | ||
|
||
private func getTitle() -> String { | ||
return characterType == .rick ? "Ricks" : "Morties" | ||
} | ||
|
||
private func getImagePosition() -> CharacterImagePosition { | ||
return characterType == .rick ? .right : .left | ||
} | ||
} | ||
|
||
extension CharactersViewModel { | ||
enum CharacterType { | ||
case rick | ||
case morty | ||
} | ||
} | ||
61 changes: 61 additions & 0 deletions
61
Rick-and-Morty/Rick And Morty/ViewModels/ContentViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import Foundation | ||
|
||
final class ContentViewModel: ObservableObject { | ||
@Published var charatersViewStates: [CharactersViewState] = [] | ||
|
||
private let charactersManager = CharactersManager() | ||
private let characterViewStateFactory = CharacterViewStateFactory() | ||
private let charactersViewStateFactory = CharactersViewStateFactory() | ||
|
||
init() { | ||
self.charatersViewStates = getCharactersViewStates() | ||
} | ||
|
||
private func getCharactersViewStates() -> [CharactersViewState] { | ||
var charactersViewStates: [CharactersViewState] = [] | ||
|
||
for characterType in CharacterType.allCases { | ||
let characters = charactersManager.fetchCharacters(characterType: characterType) | ||
let characterViewStates = getCharacterViewStates(from: characters) | ||
|
||
let charactersViewState = charactersViewStateFactory.createCharactersViewState(characterType: characterType, characterViewStates: characterViewStates) | ||
|
||
charactersViewStates.append(charactersViewState) | ||
} | ||
|
||
return charactersViewStates | ||
} | ||
|
||
private func getCharacterViewStates(from characters: [Character]) -> [CharacterViewState] { | ||
var characterViewStates: [CharacterViewState] = [] | ||
|
||
for character in characters { | ||
let characterViewState = characterViewStateFactory.createCharacterViewState(from: character) | ||
characterViewStates.append(characterViewState) | ||
} | ||
|
||
return characterViewStates | ||
} | ||
} | ||
|
||
struct CharactersViewState: Identifiable { | ||
let id = UUID() | ||
let title: String | ||
let iconName: String | ||
let characterViewStates: [CharacterViewState] | ||
} | ||
|
||
struct CharacterViewState: Identifiable { | ||
let id = UUID() | ||
let character: Character | ||
let name: String | ||
let description: String | ||
let shortDescription: String? | ||
let imageName: String | ||
let imagePosition: CharacterImagePosition | ||
} | ||
|
||
enum CharacterType: CaseIterable { | ||
case rick | ||
case morty | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,28 @@ | ||
import SwiftUI | ||
|
||
struct CharactersView: View { | ||
let characters: [Character] | ||
let title: String | ||
let charactersViewState: CharactersViewState | ||
|
||
var body: some View { | ||
NavigationView { | ||
ScrollView(.vertical) { | ||
VStack(alignment: .leading) { | ||
ForEach(characters, id: \.id) { character in | ||
CharacterCell(character: character, imagePosition: getImagePosition(character: character)) | ||
ForEach(charactersViewState.characterViewStates) { characterViewState in | ||
CharacterCell(characterViewState: characterViewState) | ||
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 120) | ||
} | ||
} | ||
} | ||
.navigationBarTitle(title) | ||
.navigationBarTitle(charactersViewState.title) | ||
} | ||
} | ||
|
||
func getImagePosition(character: Character) -> CharacterImagePosition { | ||
character is Rick ? .right : .left | ||
} | ||
} | ||
|
||
/* | ||
struct CharactersView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
CharactersView(characters: ricks, title: "Ricks") | ||
//CharactersView(viewModel: CharactersViewModel(characterType: .rick)) | ||
//CharactersView(viewModel: CharactersViewModel(characterType: .morty)) | ||
} | ||
} | ||
*/ | ||
Comment on lines
+21
to
+28
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. This should be removed. there is no need for commented out code. You have a repo to check for old versions if you really need to :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import Foundation | ||
|
||
final class CharactersManager { | ||
func fetchCharacters(characterType: CharacterType) -> [Character] { | ||
return characterType == .rick ? ricks : morties | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Interesting: this is violating the Open Closed principle :P
You would have to modify the ViewModel if you want to add
Summer
orJerry
Why don't you define a struct instead?
If you feed your ViewModel with an array of this model, you won't need to take decisions anymore inside your ViewModel ;)
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.
I created a CharacterViewState and a CharactersViewState which holds an array of CharacterViewState's (naming could probably be better).
A CharactersViewState is made for each CharacterType in the new ContentViewModel. I don't think there is a need for a CharactersViewModel anymore. Check out my new commit, it should make more sense.