Skip to content

Commit

Permalink
Merge pull request #52 from novoda/feature/RM-49-convert-viewControll…
Browse files Browse the repository at this point in the history
…ers-swiftui

Feature/[RM-49] - Convert view controllers swiftui
  • Loading branch information
swg99 authored Jul 28, 2021
2 parents 49c3065 + 9abe216 commit a13f1bb
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Rick-and-Morty/Rick And Morty.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
1711B39C26B16D6200BE935B /* CharactersView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1711B39B26B16D6200BE935B /* CharactersView.swift */; };
17CAB4FA26A824470048F2F1 /* CharacterCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17CAB4F926A824470048F2F1 /* CharacterCell.swift */; };
17F1E38226A18A54009C1CDB /* CharacterCollectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17F1E38126A18A54009C1CDB /* CharacterCollectionViewController.swift */; };
17F1E38426A18EA8009C1CDB /* TabBarController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 17F1E38326A18EA8009C1CDB /* TabBarController.swift */; };
Expand All @@ -33,6 +34,7 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1711B39B26B16D6200BE935B /* CharactersView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharactersView.swift; sourceTree = "<group>"; };
17CAB4F926A824470048F2F1 /* CharacterCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCell.swift; sourceTree = "<group>"; };
17F1E38126A18A54009C1CDB /* CharacterCollectionViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CharacterCollectionViewController.swift; sourceTree = "<group>"; };
17F1E38326A18EA8009C1CDB /* TabBarController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TabBarController.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -95,6 +97,7 @@
children = (
17F1E38926A1D877009C1CDB /* CharacterCollectionViewCell.swift */,
17CAB4F926A824470048F2F1 /* CharacterCell.swift */,
1711B39B26B16D6200BE935B /* CharactersView.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -252,6 +255,7 @@
17CAB4FA26A824470048F2F1 /* CharacterCell.swift in Sources */,
17F1E38426A18EA8009C1CDB /* TabBarController.swift in Sources */,
B81168931CFF234900301A0A /* Rick.swift in Sources */,
1711B39C26B16D6200BE935B /* CharactersView.swift in Sources */,
17F1E38626A1AF6A009C1CDB /* Character.swift in Sources */,
B81168951CFF235600301A0A /* Morty.swift in Sources */,
17F1E38A26A1D877009C1CDB /* CharacterCollectionViewCell.swift in Sources */,
Expand Down
5 changes: 5 additions & 0 deletions Rick-and-Morty/Rick And Morty/Models/Character.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@
import Foundation

protocol Character {
var id: UUID { get }
var name: String { get }
var image: String { get }
var description: String { get }
}

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

struct Rick: Character {
struct Rick: Character {
let name: String
let image: String
let description: String
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
import UIKit
import SwiftUI

class TabBarController: UITabBarController, UITabBarControllerDelegate {

override func viewDidLoad() {
// Rick tab index: 0
let rickNavController = self.viewControllers![0] as! UINavigationController
let rickViewController = rickNavController.topViewController as! CharacterCollectionViewController
rickViewController.charactersTitle = "Rick"
rickViewController.characters = ricks

// Morty tab index: 1
let mortyNavController = self.viewControllers![1] as! UINavigationController
let mortyViewController = mortyNavController.topViewController as! CharacterCollectionViewController
mortyViewController.charactersTitle = "Morty"
mortyViewController.characters = morties
let rickViewController = UIHostingController(rootView: CharactersView(characters: ricks, title: "Ricks"))
rickViewController.tabBarItem = UITabBarItem(title: "Ricks", image: UIImage(named: "rick-icon"), tag: 1)

let mortyViewController = UIHostingController(rootView: CharactersView(characters: morties, title: "Morties"))
mortyViewController.tabBarItem = UITabBarItem(title: "Morties", image: UIImage(named: "morty-icon"), tag: 2)

self.setViewControllers([rickViewController, mortyViewController], animated: false)
}
}
30 changes: 30 additions & 0 deletions Rick-and-Morty/Rick And Morty/Views/CharactersView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import SwiftUI

struct CharactersView: View {
let characters: [Character]
let title: String

var body: some View {
NavigationView {
ScrollView(.vertical) {
VStack(alignment: .leading) {
ForEach(characters, id: \.id) { character in
CharacterCell(character: character, imagePosition: getImagePosition(character: character))
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: 120)
}
}
}
.navigationBarTitle(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")
}
}

0 comments on commit a13f1bb

Please sign in to comment.