Skip to content

Instantly share code, notes, and snippets.

@l-bimba
Created June 28, 2017 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save l-bimba/2afddec5a4d2d3b48b519afcf111a30c to your computer and use it in GitHub Desktop.
Save l-bimba/2afddec5a4d2d3b48b519afcf111a30c to your computer and use it in GitHub Desktop.
Data
//
// ViewController.swift
// ShopPeer
//
// Created by Lukas Bimba on 6/3/17.
// Copyright © 2017 Lukas Bimba. All rights reserved.
//
import UIKit
import SnapKit
import CoreLocation
import MapKit
import Firebase
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var firstName: UITextField!
@IBOutlet weak var lastName: UITextField!
@IBOutlet weak var companyPosition: UITextField!
@IBOutlet weak var emailTextField: UITextField!
@IBOutlet weak var passwordTextField: UITextField!
@IBOutlet weak var businessName: UITextField!
@IBOutlet weak var businessStreet: UITextField!
@IBOutlet weak var businessCity: UITextField!
@IBOutlet weak var businessState: UITextField!
@IBOutlet weak var businessZIP: UITextField!
@IBOutlet weak var businessCountry: UITextField!
@IBOutlet weak var businessPhone: UITextField!
@IBOutlet weak var businessWebsite: UITextField!
@IBOutlet weak var businessLatitude: UITextField!
@IBOutlet weak var businessLongitude: UITextField!
// Social Media
@IBOutlet weak var facebookUrl: UITextField!
@IBOutlet weak var twitterUrl: UITextField!
@IBOutlet weak var instagramUrl: UITextField!
@IBOutlet weak var googleUrl: UITextField!
@IBOutlet weak var yelpUrl: UITextField!
@IBOutlet weak var foursquareUrl: UITextField!
@IBOutlet weak var snapchatUrl: UITextField!
@IBOutlet weak var facebookButton: UIButton!
@IBOutlet weak var twitterButton: UIButton!
@IBOutlet weak var instagramButton: UIButton!
@IBOutlet weak var googleButton: UIButton!
@IBOutlet weak var yelpButton: UIButton!
@IBOutlet weak var foursquareButton: UIButton!
@IBOutlet weak var snapchatButton: UIButton!
let isApproved = "Pending"
let picker = LocationPicker()
var ref: FIRDatabaseReference!
var user: FIRUser!
override func viewDidLoad() {
super.viewDidLoad()
ref = FIRDatabase.database().reference()
user = FIRAuth.auth()?.currentUser
self.firstName.delegate = self
self.lastName.delegate = self
self.companyPosition.delegate = self
self.emailTextField.delegate = self
self.passwordTextField.delegate = self
self.businessName.delegate = self
self.businessStreet.delegate = self
self.businessCity.delegate = self
self.businessState.delegate = self
self.businessZIP.delegate = self
self.businessCountry.delegate = self
self.businessPhone.delegate = self
self.businessWebsite.delegate = self
self.businessLatitude.delegate = self
self.businessLongitude.delegate = self
/*
self.facebookUrl.delegate = self
self.twitterUrl.delegate = self
self.instagramUrl.delegate = self
self.googleUrl.delegate = self
self.yelpUrl.delegate = self
self.foursquareUrl.delegate = self
self.snapchatUrl.delegate = self
*/
// Do any additional setup after loading the view.
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
picker.delegate = self
}
@IBAction func selectLocation(_ sender: Any) {
present(picker, animated: true, completion: nil)
}
@IBAction func createAccount(_ sender: Any) {
let email = self.emailTextField.text!
let password = self.passwordTextField.text!
if firstName.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter Your First Name.")
}
if lastName.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter Your Last Name.")
}
if companyPosition.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter Your Company Position.")
}
if businessName.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business Name.")
}
if businessStreet.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business's Street.")
}
if businessCity.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The City.")
}
if businessState.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The State.")
}
if businessZIP.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The ZIP.")
}
if businessPhone.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business's Phone.")
}
if businessWebsite.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business Website.")
}
if businessLatitude.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business Latitude.")
}
if businessLongitude.text == "" {
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "Please Enter The Business Longitude.")
}
if email != "" && password != "" && self.firstName.text != "" && self.lastName.text != "" && self.companyPosition.text != "" && self.businessName.text != "" && self.businessStreet.text != "" && self.businessCity.text != "" && self.businessState.text != "" && self.businessZIP.text! != "" && self.businessPhone.text != "" && self.businessWebsite.text != "" && self.businessLatitude.text != "" && self.businessLongitude.text != "" {
self.ref.child("Businesses").queryOrdered(byChild: "businessLatitude").queryEqual(toValue: self.businessLatitude.text!).observeSingleEvent(of: .value, with: { (snapshot) in
if(snapshot.value! is NSNull){
print("Latitude doesn't exists")
CommonUtils.sharedUtils.showProgress(self.view, label: "Registering...")
FIRAuth.auth()?.createUser(withEmail: email, password: password, completion: { (user, error) in
if error == nil {
FIREmailPasswordAuthProvider.credential(withEmail: email, password: password)
self.ref.child("Businesses").child(user!.uid).setValue(["firstName":self.firstName.text!,"lastName":self.lastName.text!, "companyPosition":self.companyPosition.text!,"businessName":self.businessName.text!, "businessStreet":self.businessStreet.text!, "businessCity":self.businessCity.text!, "businessState":self.businessState.text!, "businessZIP":self.businessZIP.text!, "businessPhone":self.businessPhone.text!, "businessWebsite":self.businessWebsite.text!,"businessLatitude":self.businessLatitude.text!, "businessLongitude":self.businessLongitude.text!, "approvalStatus":self.isApproved, "email":email ])
CommonUtils.sharedUtils.hideProgress()
let photoViewController = self.storyboard?.instantiateViewController(withIdentifier: "BusinessProfile")
self.navigationController?.pushViewController(photoViewController!, animated: true)
} else {
DispatchQueue.main.async(execute: {() -> Void in
CommonUtils.sharedUtils.hideProgress()
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: (error?.localizedDescription)!)
})
}
})
}
else{
print("Latitude exists")
CommonUtils.sharedUtils.hideProgress()
CommonUtils.sharedUtils.showAlert(self, title: "Error", message: "This Place Is Already Registered")
}
})
}
else {
let alert = UIAlertController(title: "Error", message: "Enter email & password!", preferredStyle: .alert)
let action = UIAlertAction(title: "OK", style: .default, handler: nil)
alert.addAction(action)
}
}
// Hide Keyboard when User Touches Outside of Keyboard
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
self.view.endEditing(true)
}
// Hide Keybaord when User Taps Return Key
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
if textField == firstName {
firstName.resignFirstResponder()
lastName.becomeFirstResponder()
}
if textField == lastName {
lastName.resignFirstResponder()
companyPosition.becomeFirstResponder()
}
if textField == companyPosition {
companyPosition.resignFirstResponder()
emailTextField.becomeFirstResponder()
}
if textField == emailTextField {
emailTextField.resignFirstResponder()
passwordTextField.becomeFirstResponder()
}
passwordTextField.resignFirstResponder()
businessName.resignFirstResponder()
businessStreet.resignFirstResponder()
businessCity.resignFirstResponder()
businessState.resignFirstResponder()
businessZIP.resignFirstResponder()
businessCountry.resignFirstResponder()
businessPhone.resignFirstResponder()
businessWebsite.resignFirstResponder()
businessLatitude.resignFirstResponder()
businessLongitude.resignFirstResponder()
/*
facebookUrl.resignFirstResponder()
twitterUrl.resignFirstResponder()
instagramUrl.resignFirstResponder()
googleUrl.resignFirstResponder()
yelpUrl.resignFirstResponder()
foursquareUrl.resignFirstResponder()
snapchatUrl.resignFirstResponder()
*/
return(true)
}
}
extension ViewController: LocationPickerDelegate {
func locationPicker(_ locationPicker: LocationPicker, didSelectLocation location: Location) {
print(location)
businessName.text = location.name
businessStreet.text = location.streetAddress
businessCity.text = location.city
businessState.text = location.state
businessZIP.text = location.postalCode
businessCountry.text = location.country
businessPhone.text = location.phoneNumber
businessWebsite.text = location.website
businessLatitude.text = location.coordinate.latitude.description
businessLongitude.text = location.coordinate.longitude.description
}
func locationPickerWillClose(_ locationPicker: LocationPicker) {
print("Closing")
}
func locationPickerDidClose(_ locationPicker: LocationPicker) {
print("Closed")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment