import UIKit
import FirebaseDatabase
import Firebase
import UserNotifications
class addReminderVC: UIViewController {
var ref : DatabaseReference!
// var reminders = ReminderClass
var post : String = “”
var myList : [String] =
@IBOutlet weak var reminderTextField: UITextField!
@IBOutlet weak var timePicker: UIDatePicker!
@IBOutlet weak var tableview: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
// self.reminderTextField.delegate = (self as! UITextFieldDelegate)
//set current date/time as minimum date for picker
timePicker.minimumDate = NSDate() as Date
timePicker.locale = NSLocale.current
// ref = Database.database().reference()
DatabaseService.shared.remindersReference.observe(.childAdded, with: { (snapshot) in
if let item = snapshot.value
{
self.myList.append(item as! String )
self.tableview.reloadData()
}
})
/*DatabaseService.shared.remindersReference.observe(DataEventType.value, with: { (snapshot) in
print(snapshot)
guard let reminderSnapshot = reminderSnapshot(with: snapshot)
else {
print("hhhh")
return}
self.reminders = reminderSnapshot.reminders
self.reminders.sort(by: { $0.date.compare($1.date) == .orderedDescending})
self.tableview.reloadData()
})*/
// Do any additional setup after loading the view.
}
@IBAction func Save(_ sender: Any) {
// let dateString = String(describing: Date())
let rtf = reminderTextField.text
let parameter = [ "message" : rtf ]
//ref.child("Reminder").childByAutoId().setValue(parameter)
DatabaseService.shared.remindersReference.setValue(parameter)
}
// MARK: - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
let center = UNUserNotificationCenter.current()
center.requestAuthorization(options: [.alert , .sound]){
(granted , error) in
}
let name = reminderTextField.text
var time = timePicker.date
let timeInterval = floor(time.timeIntervalSinceReferenceDate/60) * 60
// build notification
let notification = UNMutableNotificationContent()
notification.title = "تنبيه"
notification.body = "لا تنسى \((name)!)!"
// notification. = time
time = NSDate(timeIntervalSinceReferenceDate: timeInterval) as Date
let dateComponent = Calendar.current.dateComponents([.year , .month , .day, .hour, .minute,.second], from: time)
let trigger = UNCalendarNotificationTrigger (dateMatching: dateComponent, repeats: false)
let uuidString = UUID().uuidString
let request = UNNotificationRequest(identifier: uuidString, content: notification, trigger: trigger)
center.add(request) {
(error) in
}
}
}
extension addReminderVC: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return myList.count
// return reminders.count
}
func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell (style: .default, reuseIdentifier: "cell")
cell.textLabel?.text = myList[indexPath.row]
// cell.detailTextLabel?.text = reminders[indexPath.row].username
return cell
}
}