Skip to content

Instantly share code, notes, and snippets.

@d-sea
Created July 1, 2019 23:53
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/78810b5812486d28d2a172c0ff26fdfb to your computer and use it in GitHub Desktop.
Save d-sea/78810b5812486d28d2a172c0ff26fdfb to your computer and use it in GitHub Desktop.
//
// MyPageTableViewController.swift
// Futsal-Kakekomi
//
// Created by Hiro Fukami on 2019/05/03.
// Copyright © 2019 Hiro Fukami. All rights reserved.
//
import UIKit
import Firebase
import FirebaseUI
class MyPageTableViewController: UITableViewController, FUIAuthDelegate {
var authUI: FUIAuth { get { return FUIAuth.defaultAuthUI()!}}
var handle: AuthStateDidChangeListenerHandle!
let providers: [FUIAuthProvider] = [
FUIGoogleAuth(),
FUIFacebookAuth(),
// FUITwitterAuth() // 動かなかったので除外
FUIEmailAuth()
]
// Google ログインと Facebook ログインのフローの結果を処理するハンドラ
func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey : Any]) -> Bool {
let sourceApplication = options[UIApplication.OpenURLOptionsKey.sourceApplication] as! String?
if FUIAuth.defaultAuthUI()?.handleOpen(url, sourceApplication: sourceApplication) ?? false {
return true
}
// other URL handling goes here.
return false
}
let sectionTitle: NSArray = ["", "", "", "", ""]
let array1_login = ["ユーザー名", "募集中", "申込済"]
let array1_logout = ["ログイン"]
let array2 = ["ガイド", "お問い合わせ・ご要望"]
let array3 = ["利用規約", "プライバシーポリシー"]
var login_status = false {
didSet {
tableView.reloadData()
}
}
override func viewDidLoad() {
super.viewDidLoad()
self.authUI.delegate = self
self.authUI.providers = providers
handle = Auth.auth().addStateDidChangeListener { (auth, user) in
if user != nil {
self.login_status = true
} else {
self.login_status = false
}
}
}
// 認証画面から離れたときに呼ばれる(キャンセルボタン押下含む)
public func authUI(_ authUI: FUIAuth, didSignInWith user: User?, error: Error?){
// 認証に成功した場合
if error == nil {
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
let alert = UIAlertController(title: "ログインしました。", message: "", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
} else {
// キャンセルボタンを押されたときは何も出さないようにしたい。
// DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
// let alert = UIAlertController(title: "認証失敗", message: "ログインに失敗しました。申し訳ございませんが、しばらくたってからし再度お試しください。", preferredStyle: .alert)
// alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
// self.present(alert, animated: true, completion: nil)
// }
}
}
override func numberOfSections(in tableView: UITableView) -> Int {
return sectionTitle.count
}
// Sectioのタイトル
override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
return sectionTitle[section] as? String
}
override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if section == 0 {
if login_status {
return array1_login.count
} else {
return array1_logout.count
}
}
else if section == 1 {
return array2.count
}
else if section == 2 {
if login_status {
return 1
} else {
return 0
}
}
else if section == 3 {
return array3.count
}
else if section == 4 {
return 1
}
else{
return 0
}
}
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
switch indexPath.section {
case 0:
cell.detailTextLabel!.text = ""
if login_status {
cell.textLabel!.text = array1_login[indexPath.row]
if indexPath.row == 0 {
let currentUser = Auth.auth().currentUser!
cell.detailTextLabel!.text = currentUser.displayName
} else {
cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
}
} else {
cell.textLabel!.text = array1_logout[indexPath.row]
}
case 1:
cell.textLabel!.text = array2[indexPath.row]
cell.detailTextLabel!.text = ""
cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
case 2:
if login_status {
cell.textLabel!.text = "ログアウト"
cell.textLabel!.textColor = UIColor.blue
}
cell.detailTextLabel!.text = ""
case 3:
cell.textLabel!.text = array3[indexPath.row]
cell.detailTextLabel!.text = ""
cell.accessoryType = UITableViewCell.AccessoryType.disclosureIndicator
case 4:
cell.textLabel!.text = "バージョン情報"
cell.detailTextLabel!.text = "1.0.0"
cell.selectionStyle = .none
default:
cell.textLabel!.text = ""
cell.detailTextLabel!.text = ""
cell.selectionStyle = .none
}
return cell
}
override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// ログイン
if indexPath.section == 0 && indexPath.row == 0 && !login_status {
let authViewController = self.authUI.authViewController()
// FirebaseUIのViewの表示
self.present(authViewController, animated: true, completion: nil)
tableView.deselectRow(at: indexPath, animated: true)
// ログアウト
} else if indexPath.section == 2 && indexPath.row == 0 && login_status {
do {
// ログアウト
try authUI.signOut()
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
let alert = UIAlertController(title: "ログアウトしました。", message: "", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
self.present(alert, animated: true, completion: nil)
}
} catch let error as NSError {
// error
print(error)
}
// 募集中
} else if indexPath.section == 0 && indexPath.row == 1 && login_status {
// 対象Rowの非選択処理
tableView.deselectRow(at: indexPath, animated: true)
// Segueを使った画面遷移を行う関数
performSegue(withIdentifier: "eventList", sender: "owner")
// 申込済
} else if indexPath.section == 0 && indexPath.row == 2 && login_status {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "eventList", sender: "entrant")
} else if indexPath.section == 1 && indexPath.row == 0 {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "webGuide", sender: nil)
} else if indexPath.section == 1 && indexPath.row == 1 {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "webForm", sender: nil)
} else if indexPath.section == 3 && indexPath.row == 0 {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "webTerms", sender: nil)
} else if indexPath.section == 3 && indexPath.row == 1 {
tableView.deselectRow(at: indexPath, animated: true)
performSegue(withIdentifier: "webPrivacy", sender: nil)
}
}
// 遷移先のViewControllerにデータを渡す関数
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if sender != nil {
let vc = segue.destination as! MyPageEventListTableViewController
vc.queryOrder = sender as? String
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment