From af55595a0efb796c44bfdcfca123487125c9a3d1 Mon Sep 17 00:00:00 2001 From: swg99 <87419041+swg99@users.noreply.github.com> Date: Fri, 6 Aug 2021 11:24:03 +0100 Subject: [PATCH] Short Description in Cells Short descriptions are shown in cells instead of full descriptions if they are available. --- Rick-and-Morty/Rick And Morty/Models/Character.swift | 4 ++++ Rick-and-Morty/Rick And Morty/Models/Morty.swift | 2 +- .../Rick And Morty/Views/CharacterCell.swift | 11 ++++++++++- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/Rick-and-Morty/Rick And Morty/Models/Character.swift b/Rick-and-Morty/Rick And Morty/Models/Character.swift index de37e38..c0925fb 100644 --- a/Rick-and-Morty/Rick And Morty/Models/Character.swift +++ b/Rick-and-Morty/Rick And Morty/Models/Character.swift @@ -15,6 +15,10 @@ protocol Character { var description: String { get } } +protocol ShortCharacterDescription { + var shortDescription: String { get } +} + extension Character { var id: UUID { UUID() } } diff --git a/Rick-and-Morty/Rick And Morty/Models/Morty.swift b/Rick-and-Morty/Rick And Morty/Models/Morty.swift index 69c5d2a..a18b676 100644 --- a/Rick-and-Morty/Rick And Morty/Models/Morty.swift +++ b/Rick-and-Morty/Rick And Morty/Models/Morty.swift @@ -1,6 +1,6 @@ import Foundation -struct Morty: Character { +struct Morty: Character, ShortCharacterDescription { let name: String let image: String let shortDescription: String diff --git a/Rick-and-Morty/Rick And Morty/Views/CharacterCell.swift b/Rick-and-Morty/Rick And Morty/Views/CharacterCell.swift index 47c1c62..56812a5 100644 --- a/Rick-and-Morty/Rick And Morty/Views/CharacterCell.swift +++ b/Rick-and-Morty/Rick And Morty/Views/CharacterCell.swift @@ -26,17 +26,26 @@ struct CharacterCell: View { VStack(alignment: .leading, spacing: 8) { Text(character.name) - Text(character.description) + Text(description(for: character)) } if imagePosition == .right { CharacterCellImage(character: character) } + Spacer() } .padding() } .buttonStyle(PlainButtonStyle()) } + + func description(for character: Character) -> String { + if let c = character as? ShortCharacterDescription { + return c.shortDescription + } + + return character.description + } } struct CharacterCellImage: View {