vendoo_v1.0/Vendoo/NotificationsViewController.swift

167 lines
7.3 KiB
Swift

//
// NotificationsViewController.swift
// Vendoo
//
// Created by Okechi Onyeje on 11/2/16.
// Copyright © 2016 Okechi Onyeje. All rights reserved.
//
import UIKit
class NotificationsViewController: UIViewController {
var notifications: [Dictionary<String, AnyObject>]! = (NSUserDefaults.standardUserDefaults().objectForKey("notifications") == nil ? [] : (NSUserDefaults.standardUserDefaults().objectForKey("notifications") as! [Dictionary<String, AnyObject>]))
var fireBase = FirebaseManager()
@IBOutlet weak var notificationTable: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
self.notificationTable.dataSource = self
self.notificationTable.delegate = self
// Do any additional setup after loading the view.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
/*
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
// Get the new view controller using segue.destinationViewController.
// Pass the selected object to the new view controller.
}
*/
}
extension NotificationsViewController: UITableViewDelegate {
func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
var notification = self.notifications[indexPath.row]
switch ( notification["network"] as! String) {
case "facebook":
let alert = UIAlertController(title: "New Comment from \(notification["senderName"] as! String)", message: (notification["commentMessage"] as! String), preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Mark as Dismissed", style: .Default, handler: {
(action) -> Void in
notification["seen"] = true
self.notifications[indexPath.row] = notification
self.fireBase.ref.child("Users/\(self.fireBase.user_email)/notifications/\(notification["UUID"] as! String)").setValue(notification, withCompletionBlock: {
(error, reference) -> Void in
if error == nil {
self.notifications = self.notifications.filter({$0["seen"] as! Bool == false})
NSUserDefaults.standardUserDefaults().setObject(self.notifications, forKey: "notifications")
self.notificationTable.reloadData()
alert.dismissViewControllerAnimated(true, completion: nil)
}
})
}))
alert.addAction(UIAlertAction(title: "OK", style: .Cancel, handler: {(action) -> Void in}))
self.presentViewController(alert, animated: true, completion: nil)
break
case "ebay":
let alert = UIAlertController(title: "New Notification", message: "Would you like to dismiss this notification?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Mark as Dismissed", style: .Default, handler: {
(action) -> Void in
notification["seen"] = true
self.notifications[indexPath.row] = notification
self.fireBase.ref.child("Users/\(self.fireBase.user_email)/notifications/\(notification["UUID"] as! String)").setValue(notification, withCompletionBlock: {
(error, reference) -> Void in
if error == nil {
self.notifications = self.notifications.filter({$0["seen"] as! Bool == false})
NSUserDefaults.standardUserDefaults().setObject(self.notifications, forKey: "notifications")
self.notificationTable.reloadData()
alert.dismissViewControllerAnimated(true, completion: nil)
}
})
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action) -> Void in}))
self.presentViewController(alert, animated: true, completion: nil)
break
case "etsy":
let alert = UIAlertController(title: "New Notification", message: "Would you like to dismiss this notification?", preferredStyle: .Alert)
alert.addAction(UIAlertAction(title: "Mark as Dismissed", style: .Default, handler: {
(action) -> Void in
notification["seen"] = true
self.notifications[indexPath.row] = notification
self.fireBase.ref.child("Users/\(self.fireBase.user_email)/notifications/\(notification["UUID"] as! String)").setValue(notification, withCompletionBlock: {
(error, reference) -> Void in
if error == nil {
self.notifications = self.notifications.filter({$0["seen"] as! Bool == false})
NSUserDefaults.standardUserDefaults().setObject(self.notifications, forKey: "notifications")
self.notificationTable.reloadData()
alert.dismissViewControllerAnimated(true, completion: nil)
}
})
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: {(action) -> Void in}))
self.presentViewController(alert, animated: true, completion: nil)
break
case "amazon":
break
default:
break
}
}
}
extension NotificationsViewController: UITableViewDataSource {
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell: NotificationTableViewCell! = self.notificationTable.dequeueReusableCellWithIdentifier("notification_cell") as! NotificationTableViewCell
let notification = self.notifications[indexPath.row]
switch ( notification["network"] as! String) {
case "facebook":
cell.message.text = notification["notification"] as! String
cell.network.image = UIImage(named: "facebook_icon")
break
case "ebay":
cell.message.text = notification["message"] as! String
cell.network.image = UIImage(named: "ebay_icon")
break
case "etsy":
cell.message.text = notification["message"] as! String
cell.network.image = UIImage(named: "etsy_icon")
break
case "amazon":
break
default:
break
}
return cell
}
func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return self.notifications.count
}
}
extension NotificationsViewController /*IBoutlets*/ {
@IBAction func dismissNotifications(sender: AnyObject){
let vc = self.storyboard?.instantiateViewControllerWithIdentifier("Reveal View Controller")
self.presentViewController(vc!, animated: false, completion: nil)
}
}