Skip to content

Commit

Permalink
Implemented updating cards.
Browse files Browse the repository at this point in the history
  • Loading branch information
fakerabbit committed Mar 6, 2017
1 parent 1fd87ad commit 823b2b7
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 18 deletions.
16 changes: 16 additions & 0 deletions Memoria.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@
ACB57EBE1E6CC8AA0079978B /* CardsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ACB57EBD1E6CC8AA0079978B /* CardsView.swift */; };
ACEFC5681E6DD3D900525B20 /* CardsCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = ACEFC5671E6DD3D900525B20 /* CardsCell.swift */; };
ACEFC56A1E6DE8F800525B20 /* Switch.swift in Sources */ = {isa = PBXBuildFile; fileRef = ACEFC5691E6DE8F800525B20 /* Switch.swift */; };
ACEFC56D1E6E0FF700525B20 /* CardVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = ACEFC56C1E6E0FF700525B20 /* CardVC.swift */; };
ACEFC56F1E6E0FFF00525B20 /* CardView.swift in Sources */ = {isa = PBXBuildFile; fileRef = ACEFC56E1E6E0FFF00525B20 /* CardView.swift */; };
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
Expand Down Expand Up @@ -70,6 +72,8 @@
ACB57EBD1E6CC8AA0079978B /* CardsView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardsView.swift; sourceTree = "<group>"; };
ACEFC5671E6DD3D900525B20 /* CardsCell.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardsCell.swift; sourceTree = "<group>"; };
ACEFC5691E6DE8F800525B20 /* Switch.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = Switch.swift; sourceTree = "<group>"; };
ACEFC56C1E6E0FF700525B20 /* CardVC.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardVC.swift; sourceTree = "<group>"; };
ACEFC56E1E6E0FFF00525B20 /* CardView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CardView.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -153,6 +157,7 @@
children = (
AC7CDB691E67C7B60025FEFE /* Assets */,
AC7CDB711E67CC340025FEFE /* Core */,
ACEFC56B1E6E0FD400525B20 /* Edit Card */,
ACB57EBA1E6CB58C0079978B /* Cards Screen */,
AC2566411E6A064F0056895D /* Create Card */,
AC7CDB701E67CC1A0025FEFE /* Learn Screen */,
Expand All @@ -176,6 +181,15 @@
name = "Cards Screen";
sourceTree = "<group>";
};
ACEFC56B1E6E0FD400525B20 /* Edit Card */ = {
isa = PBXGroup;
children = (
ACEFC56C1E6E0FF700525B20 /* CardVC.swift */,
ACEFC56E1E6E0FFF00525B20 /* CardView.swift */,
);
name = "Edit Card";
sourceTree = "<group>";
};
/* End PBXGroup section */

/* Begin PBXNativeTarget section */
Expand Down Expand Up @@ -260,6 +274,7 @@
AC9CBB4D1E64E4EA00C634F3 /* MemoriaVC.swift in Sources */,
AC5692D71E691A6E004EFC73 /* CircleBtn.swift in Sources */,
AC2566401E69E5910056895D /* CircleTextView.swift in Sources */,
ACEFC56D1E6E0FF700525B20 /* CardVC.swift in Sources */,
ACEFC56A1E6DE8F800525B20 /* Switch.swift in Sources */,
AC9CBB4F1E64E4EA00C634F3 /* LearnVC.swift in Sources */,
ACB57EBC1E6CC8950079978B /* CardsVC.swift in Sources */,
Expand All @@ -270,6 +285,7 @@
AC34D5C41E68B4DB00120C62 /* RoundTextView.swift in Sources */,
AC9CBB391E64E2F800C634F3 /* Memoria.xcdatamodeld in Sources */,
AC2566451E6A06760056895D /* CreateCardView.swift in Sources */,
ACEFC56F1E6E0FFF00525B20 /* CardView.swift in Sources */,
AC9CBB311E64E2F700C634F3 /* AppDelegate.swift in Sources */,
AC34D5C61E68E61300120C62 /* Extensions.swift in Sources */,
AC9CBB501E64E4EA00C634F3 /* LearnView.swift in Sources */,
Expand Down
116 changes: 116 additions & 0 deletions Memoria/CardVC.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
//
// CardVC.swift
// Memoria
//
// Created by Mirko Justiniano on 3/6/17.
// Copyright © 2017 MM. All rights reserved.
//

import Foundation
import UIKit

class CardVC: UIViewController {

var nav:NavController?
var card:Card!

lazy var cardView:CardView! = {
let frame = UIScreen.main.bounds
let v = CardView(frame: frame)
return v
}()

// MARK:- View methods

override func loadView() {
super.loadView()
self.view = self.cardView
}

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
cardView.backgroundAnim()
cardView.onClose = { [weak self] view in
self?.dismiss(animated: true, completion: nil)
}
cardView.card.qTfld.text = card.question
cardView.card.aTfld.text = card.answer
cardView.card.catBtn.title = card.category
cardView.card.createBtn.addTarget(self, action: #selector(onUpdateCard(_:)), for: .touchUpInside)
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

// MARK:- Private

func onUpdateCard(_ sender : UIButton) {

if (cardView.card.qTfld.text?.characters.count)! <= 1 {
let action = UIAlertAction(title: "Ok", style: .destructive, handler: {(alert: UIAlertAction!) in
self.cardView.card.qTfld.becomeFirstResponder()
})
let controller = UIAlertController(title: "Please add a Question", message: nil, preferredStyle: .alert)
controller.addAction(action)
self.present(controller, animated: true, completion: nil)
}
else if (cardView.card.aTfld.text?.characters.count)! <= 1 {
let action = UIAlertAction(title: "Ok", style: .destructive, handler: {(alert: UIAlertAction!) in
self.cardView.card.aTfld.becomeFirstResponder()
})
let controller = UIAlertController(title: "Please add an Answer", message: nil, preferredStyle: .alert)
controller.addAction(action)
self.present(controller, animated: true, completion: nil)
}
else if cardView.card.catTfld.isHidden == false {

if (cardView.card.catTfld.text?.characters.count)! <= 1 {
let action = UIAlertAction(title: "Ok", style: .destructive, handler: {(alert: UIAlertAction!) in
self.cardView.card.catTfld.becomeFirstResponder()
})
let controller = UIAlertController(title: "Please add a Category", message: nil, preferredStyle: .alert)
controller.addAction(action)
self.present(controller, animated: true, completion: nil)
}
else {
let category:Category = Category(name: cardView.card.catTfld.text, width: 0, active: true)
DataMgr.sharedInstance.saveCategory(category: category) { [weak self] category in
let newCard = Card(id: (self?.card.id)!, question: (self?.cardView.card.qTfld.text!)!, answer: self?.cardView.card.aTfld.text, category: category.name, active: (self?.card.active)!)
DataMgr.sharedInstance.updateCard(card: newCard) { [weak self] cardUpdated in
if cardUpdated == nil {
let action = UIAlertAction(title: "Ok", style: .destructive, handler: {(alert: UIAlertAction!) in
})
let controller = UIAlertController(title: "An error occurred :(", message: "Please try again", preferredStyle: .alert)
controller.addAction(action)
self?.present(controller, animated: true, completion: nil)
}
else {
self?.dismiss(animated: true, completion: nil)
}
}
}
}
}
else {
let category:Category = Category(name: cardView.card.catBtn.title, width: 0, active: true)
DataMgr.sharedInstance.saveCategory(category: category) { [weak self] category in
let newCard = Card(id: (self?.card.id)!, question: (self?.cardView.card.qTfld.text!)!, answer: self?.cardView.card.aTfld.text, category: category.name, active: (self?.card.active)!)
DataMgr.sharedInstance.updateCard(card: newCard) { [weak self] cardUpdated in
if cardUpdated == nil {
let action = UIAlertAction(title: "Ok", style: .destructive, handler: {(alert: UIAlertAction!) in
})
let controller = UIAlertController(title: "An error occurred :(", message: "Please try again", preferredStyle: .alert)
controller.addAction(action)
self?.present(controller, animated: true, completion: nil)
}
else {
self?.dismiss(animated: true, completion: nil)
}
}
}
}
}
}
Loading

0 comments on commit 823b2b7

Please sign in to comment.