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 [RM75] Character Card #76

Merged
merged 4 commits into from
Aug 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 13 additions & 1 deletion Rick-and-Morty/Rick And Morty.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Begin PBXBuildFile section */
1711B39E26B1898100BE935B /* CharacterListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1711B39D26B1898100BE935B /* CharacterListView.swift */; };
17588BAC26C1750B008ECC31 /* Character.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17588BAB26C1750B008ECC31 /* Character.swift */; };
17588BAF26C273BB008ECC31 /* CharacterCard.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17588BAE26C273BB008ECC31 /* CharacterCard.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 @@ -28,6 +29,7 @@
/* Begin PBXFileReference section */
1711B39D26B1898100BE935B /* CharacterListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterListView.swift; sourceTree = "<group>"; };
17588BAB26C1750B008ECC31 /* Character.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Character.swift; sourceTree = "<group>"; };
17588BAE26C273BB008ECC31 /* CharacterCard.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCard.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 @@ -64,6 +66,15 @@
path = Model;
sourceTree = "<group>";
};
17588BAD26C273A2008ECC31 /* Views */ = {
isa = PBXGroup;
children = (
1711B39D26B1898100BE935B /* CharacterListView.swift */,
17588BAE26C273BB008ECC31 /* CharacterCard.swift */,
);
path = Views;
sourceTree = "<group>";
};
B81168601CFF1C9900301A0A = {
isa = PBXGroup;
children = (
Expand All @@ -85,9 +96,9 @@
B811686B1CFF1C9900301A0A /* Rick And Morty */ = {
isa = PBXGroup;
children = (
17588BAD26C273A2008ECC31 /* Views */,
17588BAA26C174FB008ECC31 /* Model */,
B811686C1CFF1C9900301A0A /* AppDelegate.swift */,
1711B39D26B1898100BE935B /* CharacterListView.swift */,
B81168751CFF1C9900301A0A /* Assets.xcassets */,
B81168771CFF1C9900301A0A /* LaunchScreen.storyboard */,
B811687A1CFF1C9900301A0A /* Info.plist */,
Expand Down Expand Up @@ -208,6 +219,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
17588BAF26C273BB008ECC31 /* CharacterCard.swift in Sources */,
17588BAC26C1750B008ECC31 /* Character.swift in Sources */,
1711B39E26B1898100BE935B /* CharacterListView.swift in Sources */,
B811686D1CFF1C9900301A0A /* AppDelegate.swift in Sources */,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"images" : [
{
"filename" : "2.jpeg",
"idiom" : "universal",
"scale" : "1x"
},
{
"idiom" : "universal",
"scale" : "2x"
},
{
"idiom" : "universal",
"scale" : "3x"
}
],
"info" : {
"author" : "xcode",
"version" : 1
}
}
92 changes: 92 additions & 0 deletions Rick-and-Morty/Rick And Morty/Views/CharacterCard.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
//
// CharacterTileView.swift
// Rick And Morty
//
// Created by Scottie Gray on 2021-08-10.
// Copyright © 2021 Novoda. All rights reserved.
//

import SwiftUI
import Foundation

struct CharacterCardState {
var id: Int
var name: String
var image: UIImage
var isAlive: Bool
var species: String
var lastLocation: String
var firstEpisode: String
}

struct CharacterCard: View {
var characterCardState: CharacterCardState

private let cornerRadius: CGFloat = 10
private let noSpacing: CGFloat = 0
private let VSpacing: CGFloat = 10
Copy link
Contributor

Choose a reason for hiding this comment

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

Id probably extract these into their own private constants struct. Something we do on Glovo which i quite like, except they use an enum which i dont like.

private struct Constants {
    let cornerRadius: CGFloat = 10
    let noSpacing: CGFloat = 0
    let VSpacing: CGFloat = 10
}

then id add the other magic numbers dotted around to it too like on line 43, 67 &70


var body: some View {
HStack(alignment: .top) {
Image(uiImage: characterCardState.image)
.resizable()
.aspectRatio(contentMode: .fit)

VStack(alignment: .leading, spacing: VSpacing) {
VStack(alignment: .leading, spacing: noSpacing) {
Text(characterCardState.name)
.font(.title)
.fontWeight(.black)
HStack {
Circle()
.foregroundColor(characterCardState.isAlive ? .green : .red)
.frame(width: 10, height: 10)
Text(characterCardState.isAlive ? "Alive - \(characterCardState.species)" : "Dead - \(characterCardState.species)")
}
}

VStack(alignment: .leading, spacing: noSpacing) {
Text("Last known location:")
.font(.caption)
.foregroundColor(.secondary)
Text(characterCardState.lastLocation)
}

VStack(alignment: .leading, spacing: noSpacing) {
Text("First seen in:")
.font(.caption)
.foregroundColor(.secondary)
Text(characterCardState.firstEpisode)
Copy link
Contributor

Choose a reason for hiding this comment

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

The duplication here could be extracted to a function. I havent played with SwiftUI to know enough but roughly along the lines of:

func characterDetailsVStack(pretext: String, details: String) -> VStack {
    return VStack(alignment: .leading, spacing: noSpacing) {
                    Text(pretext)
                        .font(.caption)
                        .foregroundColor(.secondary)
                    Text(details)
}

then called:

    characterDetailsVStack(pretext: "Last known location:", details: characterCardState.lastLocation)
    characterDetailsVStack(pretext: "First seen in:", details: characterCardState.firstEpisode)

Copy link
Contributor

Choose a reason for hiding this comment

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

Id maybe even consider putting those strings as constants too, or prep them for localisation 🤷

}
}
Spacer()
}
.cornerRadius(cornerRadius)
.overlay(
RoundedRectangle(cornerRadius: cornerRadius)
.stroke(Color(.lightGray), lineWidth: 0.25)
)
.padding()
.frame(height: 200)
}
}

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: false, 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: false, 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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Theres duplication here, try and use my above example to see what functions you can create for the VStack and CharacterCard duplications :)


}
}
}