IG
Size: a a a
IG
ДБ
ДБ
ДБ
ДБ
ДБ
GZ
ДБ
KK
import UIKit
import Firebase
class RoomsAdminTableViewController: UITableViewController {
var reff : DatabaseReference?
var user: Users!
var rooms = Array<Rooms>()
@IBAction func addNewRooms(_ sender: UIBarButtonItem) {
var alertName : UITextField!
var alertPrice : UITextField!
let allertController = UIAlertController(title: "Введите название комнаты", message: nil, preferredStyle: .alert)
allertController.addTextField(configurationHandler: {
alertNameText in alertName = alertNameText
alertNameText.placeholder = "Название номера"
})
allertController.addTextField(configurationHandler: {
alertPriceText in alertPrice = alertPriceText
alertPriceText.placeholder = "Цена номера "
})
let alertAction = UIAlertAction(title: "Save", style: .default){[weak self] _ in
let roms = Rooms(title: alertName.text!, userId: (self?.user.email)!, price: alertPrice.text!)
if let title = roms.title {
let romsRef = self?.reff?.child(title.lowercased())
romsRef?.setValue(roms.convertToDictionary())
NotificationCenter.default.post(name: Notification.Name("Rooms"), object: roms)
}
}
allertController.addAction(alertAction)
present(allertController,animated: true,completion: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
guard let currentUser = Auth.auth().currentUser else {return}
user = Users(user: currentUser )
reff = Database.database().reference(withPath: "Admin").child(String(user.uid)).child("Rooms")
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
reff?.observe(.value, with: {[weak self] (snapshot) in
var _rooms = Array<Rooms>()
for item in snapshot.children {
let rooms = Rooms(snapshot: item as! DataSnapshot)
_rooms.append(rooms)
}
self?.rooms = _rooms
self?.tableView.reloadData()
})
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
reff?.removeAllObservers()
}
override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return rooms.count
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! RoomsAdminTableViewCell
let roomsTitle = rooms[indexPath.row].title
let roomsPrice = rooms[indexPath.row].price
cell.titleLabel.text = roomsTitle
cell.priceLabel.text = roomsPrice
return cell
}
}
import UIKit
import Firebase
class RoomsUserTableViewController: UITableViewController {
var ref : DatabaseReference?
var user: Users!
var rooms = Array<Rooms>()
override func viewDidLoad() {
super.viewDidLoad()
guard let currentUser = Auth.auth().currentUser else {return}
user = Users(user: currentUser )
ref = Database.database().reference(withPath: "users").child(String(user.uid)).child("rooms")
NotificationCenter.default.addObserver(self, selector: #selector(notificationFired(_:)), name: Notification.Name("Rooms"), object: nil)
//создали уведомление
}
@objc func notificationFired(_ notification:Notification){
if let rooms = notification.object as? Rooms {
self.rooms = [rooms]
KK
ТШ
AY
if #available(iOS 1
3.0, *) {} else {
ТШ
if #available(iOS 1
3.0, *) {} else {
AY
ДБ