Skip to content

Instantly share code, notes, and snippets.

@d-sea
Created May 27, 2019 00:45
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 d-sea/d2a54bd2a4809c24d5ceb758fb83204c to your computer and use it in GitHub Desktop.
Save d-sea/d2a54bd2a4809c24d5ceb758fb83204c to your computer and use it in GitHub Desktop.
Firebase FIrestore addDocuments GeoPoint data in Fureka
//
// FormViewController.swift
// Futsal-Kakekomi
//
// Created by Hiro Fukami on 2019/05/01.
// Copyright © 2019 Hiro Fukami. All rights reserved.
//
import UIKit
import Eureka
import Firebase
import MapKit
class MyFormViewController: FormViewController {
@IBOutlet weak var cancelButton: UIBarButtonItem!
@IBAction func actionCancelButton(_ sender: Any) {
self.dismiss(animated: true, completion: nil)
}
var latitude: Double = 0.0
var longitude: Double = 0.0
var receivedCourtName = "" {
didSet {
self.form.rowBy(tag: "court")!.updateCell()
}
}
var receivedCourtAddress = "" {
didSet {
CLGeocoder().geocodeAddressString(receivedCourtAddress) { placemarks, error in
if let lat = placemarks?.first?.location?.coordinate.latitude {
self.latitude = lat
}
if let lng = placemarks?.first?.location?.coordinate.longitude {
self.longitude = lng
}
}
}
}
override func viewDidLoad() {
super.viewDidLoad()
+++ Section("場所")
<<< LabelRow("court") {
$0.title = "コート名"
$0.add(rule: RuleRequired())
$0.validationOptions = .validatesOnChangeAfterBlurred
$0.onCellSelection(self.courtTapped)
}
.cellUpdate { cell, row in
if self.receivedCourtName != "" {
row.value = self.receivedCourtName
}
if !row.isValid {
cell.textLabel?.textColor = .red
}
}
+++ Section("メモ")
<<< TextAreaRow("memo") { row in
row.placeholder = "レベルや金額など参加者に伝えたいことをお書きください。"
row.add(rule: RuleRequired())
row.validationOptions = .validatesOnChangeAfterBlurred
}
.cellUpdate { cell, row in
if !row.isValid {
cell.textLabel?.textColor = .red
}
}
+++ Section()
<<< ButtonRow("register") {
$0.title = "登録"
$0.onCellSelection(self.buttonTapped)
}
}
func courtTapped(cell: LabelCellOf<String>, row: LabelRow) {
performSegue(withIdentifier: "locationSearch", sender: nil)
}
func buttonTapped(cell: ButtonCellOf<String>, row: ButtonRow) {
let errors = form.validate()
guard errors.isEmpty else {
let alert = UIAlertController(title: "エラー", message: "入力していない項目があります。", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
present(alert, animated: true, completion: nil)
print("validate errors:", errors)
return
}
let values = form.values()
// Rowの値を取得する
let memo = values["memo"] as! String
// ログインユーザー情報
let currentUser = Auth.auth().currentUser!
let db = Firestore.firestore()
var ref: DocumentReference? = nil
ref = db.collection("events").addDocument(data: [
"point": GeoPoint(latitude: self.latitude, longitude: self.longitude),
"memo": memo,
"createdAt": FieldValue.serverTimestamp(),
"ownerName": currentUser.displayName!,
"ownnerID": currentUser.uid
]) { err in
if let err = err {
let alert = UIAlertController(title: "エラー", message: "データが保存できませんでした。申し訳ございませんが、しばらくたってから入力してください。", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
print("Error adding document: \(err)")
} else {
print("Document added with ID: \(ref!.documentID)")
self.dismiss(animated: true, completion: nil)
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment