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 [RM67] Detail View Short Description #69

Merged
merged 3 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions Rick-and-Morty/Rick And Morty/Models/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ protocol Character {
var description: String { get }
}

protocol ShortCharacterDescription {
var shortDescription: String { get }
}

extension Character {
var id: UUID { UUID() }
}
2 changes: 1 addition & 1 deletion Rick-and-Morty/Rick And Morty/Models/Morty.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Foundation

struct Morty: Character {
struct Morty: Character, ShortCharacterDescription {
let name: String
let image: String
let shortDescription: String
Expand Down
11 changes: 10 additions & 1 deletion Rick-and-Morty/Rick And Morty/Views/CharacterCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,26 @@ struct CharacterCell: View {

VStack(alignment: .leading, spacing: 8) {
Text(character.name)
Text(character.description)
description(for: character)
}

if imagePosition == .right {
CharacterCellImage(character: character)
}
Spacer()
}
.padding()
}
.buttonStyle(PlainButtonStyle())
}

func description(for character: Character) -> Text {
if let c = character as? ShortCharacterDescription {
return Text(c.shortDescription)
}

return Text(character.description)
}
}

struct CharacterCellImage: View {
Expand Down
17 changes: 13 additions & 4 deletions Rick-and-Morty/Rick And Morty/Views/CharacterDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,14 @@ struct CharacterDetailView: View {
.resizable()
.scaledToFit()
.frame(maxHeight: 200)
HStack {

VStack(alignment: .leading, spacing: 12) {
if let c = character as? ShortCharacterDescription {
Text(c.shortDescription)
}
Comment on lines +21 to +24

Choose a reason for hiding this comment

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

This if this is a duplicated logic, IF there would be 3rd way of describing character, like fullDescription You would have fix this in 2 places in code. If the application would have more then those two, would you put that logic everywhere?

Text(character.description)
Spacer()
}

Spacer()
}
.padding()
Expand All @@ -30,7 +34,12 @@ struct CharacterDetailView: View {

struct CharacterDetailView_Previews: PreviewProvider {
static var previews: some View {
CharacterDetailView(character: ricks[0])
CharacterDetailView(character: morties[0])
NavigationView {

Choose a reason for hiding this comment

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

I like the usage of previews! :)

CharacterDetailView(character: ricks[0])
}
NavigationView {
CharacterDetailView(character: morties[2])

}
}
}