Skip to content

Commit

Permalink
Refactor: Declare static constants or use enum value for setting fram…
Browse files Browse the repository at this point in the history
…es of View #26
  • Loading branch information
jinios authored and godrm committed May 14, 2018
1 parent 96a96ff commit 572e204
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 16 deletions.
6 changes: 3 additions & 3 deletions CardGameApp/CardGameApp/View/CardDeckView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class CardDeckView: UIView {

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.frame = CGRect(x: 0, y: 0, width: 414, height: 100)
self.frame = CGRect(x: 0, y: 0, width: ViewController.widthOfRootView, height: PositionY.bottom.value)
}

convenience init() {
self.init(frame: CGRect(x: 0, y: 0, width: 414, height: 100))
self.init(frame: CGRect(x: 0, y: 0, width: ViewController.widthOfRootView, height: PositionY.bottom.value))
self.deckManager = gameManager.getDeckDelegate()
}

Expand Down Expand Up @@ -83,7 +83,7 @@ class CardDeckView: UIView {
@objc func cardDoubleTapped(sender: UITapGestureRecognizer) {
if sender.state == .ended {
let deckview = sender.view?.superview as! CardDeckView
NotificationCenter.default.post(name: .doubleTappedOpenedDeck, object: self, userInfo: ["from": deckview])
NotificationCenter.default.post(name: .doubleTappedOpenedDeck, object: self, userInfo: [ViewController.fromViewKey: deckview])
}
}

Expand Down
16 changes: 9 additions & 7 deletions CardGameApp/CardGameApp/View/CardStacksView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import UIKit

class CardStacksView: UIView {
static let numberOfStacks: Int = 7
private var wholeStackManager: (CardStackDelegate & Stackable)!
private var oneStackViews = [OneStack]()

Expand All @@ -19,7 +20,7 @@ class CardStacksView: UIView {

convenience init() {
self.init(frame: CGRect(x: 0, y: PositionY.bottom.value,
width: 414, height: 736 - PositionY.bottom.value))
width: ViewController.widthOfRootView, height: ViewController.heightOfRootView - PositionY.bottom.value))
self.wholeStackManager = CardGameManager.shared().getWholeStackDelegate()
}

Expand All @@ -28,7 +29,7 @@ class CardStacksView: UIView {
}

func setup() {
for i in 0...6 {
for i in 0..<CardStacksView.numberOfStacks {
oneStackViews.append(OneStack(column: i, manager: wholeStackManager))
addSubview(oneStackViews[i])
oneStackViews[i].setup()
Expand All @@ -46,6 +47,7 @@ class CardStacksView: UIView {
}

class OneStack: UIView {

private var column: Int!
private var wholeStackManager: (CardStackDelegate & Stackable)!
private var stackManager: StackDelegate!
Expand All @@ -61,8 +63,8 @@ class OneStack: UIView {
convenience init(column: Int, manager: CardStackDelegate & Stackable) {
self.init(frame: CGRect(x: PositionX.allValues[column].value,
y: 0,
width: 414 / 7,
height: 736 - PositionY.bottom.value))
width: ViewController.widthOfRootView / CGFloat(CardStacksView.numberOfStacks),
height: ViewController.heightOfRootView - PositionY.bottom.value))
self.column = column
self.wholeStackManager = manager
self.stackManager = wholeStackManager.getStackDelegate(of: column)
Expand All @@ -72,8 +74,8 @@ class OneStack: UIView {
super.init(coder: aDecoder)
self.frame = CGRect(x: PositionX.allValues[column].value,
y: 0,
width: 414 / 7,
height: 736 - PositionY.bottom.value)
width: ViewController.widthOfRootView / CGFloat(CardStacksView.numberOfStacks),
height: ViewController.heightOfRootView - PositionY.bottom.value)
}

func setup() {
Expand Down Expand Up @@ -104,7 +106,7 @@ class OneStack: UIView {
@objc func cardDoubleTapped(sender: UITapGestureRecognizer) {
if sender.state == .ended {
let oneStackView = sender.view?.superview as! OneStack
NotificationCenter.default.post(name: .doubleTappedStack, object: self, userInfo: ["from": oneStackView])
NotificationCenter.default.post(name: .doubleTappedStack, object: self, userInfo: [ViewController.fromViewKey: oneStackView])
}
}

Expand Down
4 changes: 2 additions & 2 deletions CardGameApp/CardGameApp/View/FoundationView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@ class FoundationView: UIView {

required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
self.frame = CGRect(x: 0, y: 0, width: 414, height: 100)
self.frame = CGRect(x: 0, y: 0, width: ViewController.widthOfRootView, height: PositionY.bottom.value)
}

convenience init() {
self.init(frame: CGRect(x: 0, y: 0, width: 414, height: 100))
self.init(frame: CGRect(x: 0, y: 0, width: ViewController.widthOfRootView, height: PositionY.bottom.value))
self.foundationManager = gameManager.getFoundationDelegate()
}

Expand Down
11 changes: 7 additions & 4 deletions CardGameApp/CardGameApp/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,14 @@ class ViewController: UIViewController {
private var stackView: CardStacksView!
private var foundationView: FoundationView!

static let fromViewKey: String = "from"
static let widthOfRootView: CGFloat = 414
static let heightOfRootView: CGFloat = 736
static let spaceY: CGFloat = 15.0
static let widthDivider: CGFloat = 8
static let cardHeightRatio: CGFloat = 1.27
static let cardSize = CGSize(width: 414 / ViewController.widthDivider,
height: (414 / ViewController.widthDivider) * ViewController.cardHeightRatio)
static let cardSize = CGSize(width: widthOfRootView / widthDivider,
height: (widthOfRootView / widthDivider) * cardHeightRatio)

override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
Expand Down Expand Up @@ -107,7 +110,7 @@ class ViewController: UIViewController {
// from deck view to foundation || stack view
@objc func deckViewDidDoubleTap(notification: Notification) {
guard let userInfo = notification.userInfo else {return}
guard let from = userInfo["from"] else { return }
guard let from = userInfo[ViewController.fromViewKey] else { return }
guard (from as? CardDeckView) != nil else { return }
let targetCard = deckView.lastCardView!

Expand Down Expand Up @@ -148,7 +151,7 @@ class ViewController: UIViewController {

@objc func stackViewDidDoubleTap(notification: Notification) {
guard let userInfo = notification.userInfo else { return }
guard let from = userInfo["from"] else { return }
guard let from = userInfo[ViewController.fromViewKey] else { return }
guard let fromView = from as? OneStack else { return }
let fromIndex = fromView.getColumn()
let targetCard = fromView.lastCardView!
Expand Down

0 comments on commit 572e204

Please sign in to comment.