Advertisement
Guest User

Untitled

a guest
Aug 25th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.31 KB | None | 0 0
  1. import UIKit
  2. import FirebaseDatabase
  3. import FirebaseAuth
  4.  
  5. class LogInController: UIViewController {
  6.  
  7. var ref: DatabaseReference!
  8.  
  9.  
  10. @IBOutlet weak var emailField: UITextField!
  11. @IBOutlet weak var passwordField: UITextField!
  12.  
  13.  
  14. func placeholders() {
  15. emailField.placeholder = "Enter Email"
  16. passwordField.placeholder = "Enter Password"
  17. }
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24. @IBAction func loginButton(_ sender: AnyObject) {
  25.  
  26. Auth.auth().signIn(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
  27.  
  28.  
  29.  
  30.  
  31. let userID: String = (user?.uid)!
  32. let userEmail: String = self.emailField.text!
  33.  
  34. self.ref.child("Users").childByAutoId().setValue(userEmail + " " + userID)
  35.  
  36.  
  37.  
  38.  
  39. if error != nil {
  40. print(error?.localizedDescription as Any)
  41. }
  42.  
  43. else {
  44. print("User logged in with UserID of: " + (user?.uid)!)
  45. }
  46. })
  47. performSegue(withIdentifier: "signedIn", sender: self)
  48.  
  49.  
  50. }
  51.  
  52.  
  53.  
  54. @IBAction func signoutButton(_ sender: Any) {
  55. print("User has logged out...")
  56. try! Auth.auth().signOut()
  57. }
  58.  
  59.  
  60.  
  61. @IBAction func registerButton(_ sender: AnyObject) {
  62.  
  63. Auth.auth().createUser(withEmail: emailField.text!, password: passwordField.text!, completion: { (user, error) in
  64.  
  65. if error != nil {
  66. print(error?.localizedDescription as Any)
  67. return
  68. }
  69.  
  70. print("User created with UserId of: " + (user?.uid)!)
  71.  
  72.  
  73. })
  74. }
  75.  
  76.  
  77.  
  78. override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
  79. if let flavorsVC = segue.destination as? FlavorsController {
  80. flavorsVC.ref = ref
  81. let userEmail = emailField.text
  82. flavorsVC.email = userEmail!
  83.  
  84. }
  85. }
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. override func viewDidLoad() {
  94. super.viewDidLoad()
  95.  
  96.  
  97. placeholders()
  98. ref = Database.database().reference()
  99.  
  100. }
  101.  
  102. import UIKit
  103. import FirebaseAuth
  104. import Firebase
  105. import FirebaseDatabase
  106.  
  107. class FlavorsController: UIViewController, UITableViewDelegate, UITableViewDataSource {
  108.  
  109. var bookieFlavors = ["Chocolate Chip", "Sugar w/o icing", "Sugar w/ icing", "Peanut Butter", "Honey", "Shortbread", "Ginger", "Double Chocolate", "Macadamie Nut", "Oatmeal Raisin", "Snickerdoodle"]
  110. var amount = [Int]()
  111. var bookieTotal = Int()
  112. var ref: DatabaseReference!
  113. var flavorRef: DatabaseReference!
  114. var email = String()
  115.  
  116.  
  117.  
  118.  
  119.  
  120. override func viewDidLoad() {
  121. super.viewDidLoad()
  122. for _ in self.bookieFlavors {
  123. self.amount.append(0)
  124. }
  125. flavorTable.delegate = self
  126. flavorTable.dataSource = self
  127.  
  128. //database references
  129. ref = Database.database().reference()
  130. flavorRef = Database.database().reference()
  131.  
  132.  
  133.  
  134. }
  135.  
  136. func emptyAmount(_ sender: UIButton) {
  137. print("Button Held, Amount Emptied")
  138. self.amount[sender.tag] = self.amount[sender.tag] - (self.amount[sender.tag] + 1)
  139. let cell = self.flavorTable.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? FlavorTableCell
  140. cell?.bookieAmount.text = "= (self.amount[sender.tag])"
  141.  
  142. }
  143.  
  144.  
  145. @IBAction func bookieButton(_ sender: UIButton) {
  146.  
  147. self.amount[sender.tag] = self.amount[sender.tag] + 1
  148. let cell = self.flavorTable.cellForRow(at: IndexPath(row: sender.tag, section: 0)) as? FlavorTableCell
  149. cell?.bookieAmount.text = "= (self.amount[sender.tag])"
  150. // print(amount[sender.tag])
  151.  
  152. self.bookieTotal = amount.reduce(0, +)
  153. print(bookieTotal)
  154. }
  155.  
  156.  
  157.  
  158.  
  159. @IBOutlet weak var flavorTable: UITableView!
  160.  
  161.  
  162. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
  163. {
  164. return bookieFlavors.count
  165.  
  166. }
  167.  
  168. func numberOfSections(in tableView: UITableView) -> Int {
  169. return 1
  170. }
  171.  
  172. public func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell
  173. {
  174. let cell = tableView.dequeueReusableCell(withIdentifier: "cell") as! FlavorTableCell
  175.  
  176.  
  177. //flavor label configuration
  178. cell.flavorLabel.text = bookieFlavors[indexPath.row]
  179.  
  180. //amount configuration
  181. cell.bookieAmount.text = "= (self.amount[indexPath.row])"
  182. cell.bookieButton.tag = indexPath.row
  183. cell.bookieButton.addTarget(self, action: #selector(bookieButton(_:)), for: .touchUpInside)
  184. cell.bookieButton.addTarget(self, action: #selector(emptyAmount(_:)), for: .touchDownRepeat)
  185.  
  186. return cell
  187.  
  188. }
  189.  
  190. @IBAction func registerBookieAmount(_ sender: Any) {
  191. print(bookieTotal)
  192.  
  193. let amount: Int = bookieTotal
  194. let user = Auth.auth().currentUser
  195.  
  196. if ((user) != nil) {
  197.  
  198.  
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement