From f39678fa8ba76f86367ee83e4017f3b543d3e964 Mon Sep 17 00:00:00 2001 From: JuneBuug Date: Tue, 29 Aug 2017 10:26:33 +0900 Subject: [PATCH] =?UTF-8?q?170829=20-=20user=20singleton=20=EC=B2=98?= =?UTF-8?q?=EB=A6=AC=20=EB=B0=8F=20=EA=B5=AC=EA=B8=80=20=EB=A1=9C=EA=B7=B8?= =?UTF-8?q?=EC=9D=B8=20=EC=8B=9C=20=EC=98=A4=EB=A5=98=20=ED=95=B4=EA=B2=B0?= =?UTF-8?q?=20#207?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../pyunrihae/pyunrihae/AppDelegate.swift | 30 +++++++++------- iOSProject/pyunrihae/pyunrihae/Button.swift | 35 +++++++++---------- .../pyunrihae/pyunrihae/DataManager.swift | 11 +++--- .../pyunrihae/LikeProductViewController.swift | 4 +-- .../pyunrihae/LoginViewController.swift | 6 ++-- .../pyunrihae/MainViewController.swift | 4 +-- .../pyunrihae/MypageViewController.swift | 32 ++++++++--------- .../ProductDetailViewController.swift | 25 +++++++------ .../pyunrihae/ReviewViewController.swift | 4 +-- .../pyunrihae/SignupViewController.swift | 3 +- .../pyunrihae/TabBarViewController.swift | 4 +-- .../UpdateNicknameViewController.swift | 10 +++--- iOSProject/pyunrihae/pyunrihae/User.swift | 4 ++- .../WritingReviewViewController.swift | 8 ++--- 14 files changed, 91 insertions(+), 89 deletions(-) diff --git a/iOSProject/pyunrihae/pyunrihae/AppDelegate.swift b/iOSProject/pyunrihae/pyunrihae/AppDelegate.swift index 0fd33207..e8e5cd38 100644 --- a/iOSProject/pyunrihae/pyunrihae/AppDelegate.swift +++ b/iOSProject/pyunrihae/pyunrihae/AppDelegate.swift @@ -12,7 +12,6 @@ import GoogleSignIn @UIApplicationMain class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate{ var window: UIWindow? - var user: User? var productList : [Product] = [] let category = ["전체","도시락","김밥","베이커리","라면","식품","스낵","아이스크림","음료"] let priceLevelList = ["비싸다","비싼편","적당","싼편","싸다"] @@ -25,11 +24,11 @@ class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate{ GIDSignIn.sharedInstance().clientID = FirebaseApp.app()?.options.clientID GIDSignIn.sharedInstance().delegate = self - let handle = Auth.auth().addStateDidChangeListener { (auth, user) in + _ = Auth.auth().addStateDidChangeListener { (auth, user) in if user == nil { do { try Auth.auth().signOut() - self.user = User.init() + User.sharedInstance = User.init() NotificationCenter.default.post(name: NSNotification.Name("userLogined"), object: nil) } catch let signOutError as NSError { print ("Error signing out: %@", signOutError) @@ -38,7 +37,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate{ } // 오프라인 일 때 기기에 저장 - Database.database().isPersistenceEnabled = true +// Database.database().isPersistenceEnabled = true return true } func applicationWillResignActive(_ application: UIApplication) { @@ -80,17 +79,22 @@ class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate{ // ... Auth.auth().signIn(with: credential) { (user, error) in - if let error = error { - // ... + if error != nil { + // 에러 발생하면 return } - // User is signed in - // ... - let user_instance = User.init(id: (user?.uid)!, email: (user?.email)!, nickname: (user?.displayName)!) - DataManager.saveUser(user: user_instance) - self.user = user_instance - NotificationCenter.default.post(name: NSNotification.Name("userLogined"), object: nil) + DataManager.getUserFromUID(uid: (user?.uid)!, completion: { (resultUser) in + if resultUser.email != "" { // 이전에 연결된 게 있으면 + User.sharedInstance = resultUser + NotificationCenter.default.post(name: NSNotification.Name("userLogined"), object: nil) + }else{ // 새로 가입한 거라면 + let user_instance = User.init(id: (user!.uid), email: (user?.email)!, nickname: (user?.displayName)!) + DataManager.saveUser(user: user_instance) + } + }) + + } } @@ -103,7 +107,7 @@ class AppDelegate: UIResponder, UIApplicationDelegate,GIDSignInDelegate{ let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() - self.user = User.init() + User.sharedInstance = User.init() NotificationCenter.default.post(name: NSNotification.Name("userLogined"), object: nil) } catch let signOutError as NSError { print ("Error signing out: %@", signOutError) diff --git a/iOSProject/pyunrihae/pyunrihae/Button.swift b/iOSProject/pyunrihae/pyunrihae/Button.swift index a1e90c5a..4d691033 100644 --- a/iOSProject/pyunrihae/pyunrihae/Button.swift +++ b/iOSProject/pyunrihae/pyunrihae/Button.swift @@ -1,3 +1,4 @@ + // // Button.swift // pyunrihae @@ -93,14 +94,13 @@ class Button{ return categoryBtns } static func didPressUsefulBtn(sender: UIButton, reviewId: String, usefulNumLabel: UILabel, badNumLabel: UILabel, usefulBtn: UIButton, badBtn: UIButton, reviewList: [Review]){ - let appdelegate = UIApplication.shared.delegate as! AppDelegate - var reviewStatus = appdelegate.user?.review_like_list[reviewId] - let uid = appdelegate.user?.id + var reviewStatus = User.sharedInstance.review_like_list[reviewId] + let uid = User.sharedInstance.id if reviewStatus == nil { //유용해요 누른적이 없는 리뷰 var useful = Int(usefulNumLabel.text!) useful = useful! + 1 usefulNumLabel.text = String(describing: useful!) - DataManager.updateUsefulReview(id: reviewId, uid: uid!) + DataManager.updateUsefulReview(id: reviewId, uid: uid) reviewStatus = 1 Button.makeBorder(btn: usefulBtn) Button.deleteBorder(btn: badBtn) @@ -110,7 +110,7 @@ class Button{ var useful = Int(usefulNumLabel.text!) useful = useful! - 1 usefulNumLabel.text = String(describing: useful!) - DataManager.updateCancleReview(id: reviewId, uid: uid!) + DataManager.updateCancleReview(id: reviewId, uid: uid) reviewStatus = 0 Button.deleteBorder(btn: usefulBtn) Button.deleteBorder(btn: badBtn) @@ -123,7 +123,7 @@ class Button{ var bad = Int(badNumLabel.text!) bad = bad! - 1 badNumLabel.text = String(describing: bad!) - DataManager.updateUsefulReview(id: reviewId, uid: uid!) + DataManager.updateUsefulReview(id: reviewId, uid: uid) reviewStatus = 1 Button.makeBorder(btn: usefulBtn) Button.deleteBorder(btn: badBtn) @@ -133,14 +133,14 @@ class Button{ var useful = Int(usefulNumLabel.text!) useful = useful! + 1 usefulNumLabel.text = String(describing: useful!) - DataManager.updateUsefulReview(id: reviewId, uid: uid!) + DataManager.updateUsefulReview(id: reviewId, uid: uid) reviewStatus = 1 Button.makeBorder(btn: usefulBtn) Button.deleteBorder(btn: badBtn) usefulNumLabel.textColor = UIColor.red badNumLabel.textColor = UIColor.lightGray } - appdelegate.user?.review_like_list[reviewId] = reviewStatus + User.sharedInstance.review_like_list[reviewId] = reviewStatus for i in 0.. Void in // 업로드된 이미지 url 받아오기 if (error != nil) { // 없으면 ""로 저장 update["p_image"] = imgURL @@ -474,7 +474,7 @@ class DataManager{ } } }) - } else { + } else { // 이미지가 없는 경우 update["p_image"] = imgURL id.updateChildValues(update) NotificationCenter.default.post(name: NSNotification.Name("reviewUpload"), object: self) @@ -528,6 +528,7 @@ class DataManager{ let localRef = ref.child("user") localRef.child(user.id).setValue(["id": user.id, "email" : user.email, "nickname" : user.nickname, "review_like_list": user.review_like_list, "product_review_list" : user.product_review_list, "wish_product_list": user.wish_product_list]) } + /* * 마이페이지 화면 @@ -596,7 +597,7 @@ class DataManager{ contentBuilder.desc = review.comment - if review.product_image != nil && review.product_image != "" { + if review.product_image != "" { contentBuilder.imageURL = URL.init(string : review.product_image)! }else{ contentBuilder.imageURL = URL.init(string : "https://s3.ap-northeast-2.amazonaws.com/pyunrihae/Group%402x.png")! diff --git a/iOSProject/pyunrihae/pyunrihae/LikeProductViewController.swift b/iOSProject/pyunrihae/pyunrihae/LikeProductViewController.swift index 8a0d456a..9d3bb458 100644 --- a/iOSProject/pyunrihae/pyunrihae/LikeProductViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/LikeProductViewController.swift @@ -28,9 +28,9 @@ class LikeProductViewController: UIViewController { } func getLikeProductList(){ likeProductList = [] - if (appdelegate.user?.wish_product_list)! != [] && appdelegate.user?.wish_product_list != nil{ + if (User.sharedInstance.wish_product_list) != [] { for product in appdelegate.productList { - for id in (appdelegate.user?.wish_product_list)! { + for id in (User.sharedInstance.wish_product_list) { if product.id == id { likeProductList.append(product) } diff --git a/iOSProject/pyunrihae/pyunrihae/LoginViewController.swift b/iOSProject/pyunrihae/pyunrihae/LoginViewController.swift index c72e75a0..240adfb7 100644 --- a/iOSProject/pyunrihae/pyunrihae/LoginViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/LoginViewController.swift @@ -46,16 +46,16 @@ class LoginViewController: UIViewController { self.alertLabel.alpha = 0 }) }else{ - let appDelegate = UIApplication.shared.delegate as! AppDelegate + _ = UIApplication.shared.delegate as! AppDelegate if Auth.auth().currentUser != nil { DataManager.getUserFromUID(uid: (Auth.auth().currentUser?.uid)!, completion: { (user) in - appDelegate.user = user + User.sharedInstance = user self.dismiss(animated: true, completion: { NotificationCenter.default.post(name: NSNotification.Name("userLogined"), object: nil) }) }) } else { - appDelegate.user = User() + User.sharedInstance = User() } } } diff --git a/iOSProject/pyunrihae/pyunrihae/MainViewController.swift b/iOSProject/pyunrihae/pyunrihae/MainViewController.swift index b5ee7b59..349576d3 100644 --- a/iOSProject/pyunrihae/pyunrihae/MainViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/MainViewController.swift @@ -337,7 +337,7 @@ class MainViewController: UIViewController { } } func didPressUsefulBtn(sender: UIButton) { //유용해요 버튼 누르기 - if appdelegate.user?.email == "" { + if User.sharedInstance.email == "" { let alert = UIAlertController(title: "로그인 후 이용해주세요!", message: "", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) self.present(alert, animated: true, completion: nil) @@ -346,7 +346,7 @@ class MainViewController: UIViewController { } } func didPressBadBtn(sender: UIButton) { //별로에요 버튼 누르기 - if appdelegate.user?.email == "" { + if User.sharedInstance.email == "" { let alert = UIAlertController(title: "로그인 후 이용해주세요!", message: "", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) self.present(alert, animated: true, completion: nil) diff --git a/iOSProject/pyunrihae/pyunrihae/MypageViewController.swift b/iOSProject/pyunrihae/pyunrihae/MypageViewController.swift index 5e62d3c3..7f40f9fc 100644 --- a/iOSProject/pyunrihae/pyunrihae/MypageViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/MypageViewController.swift @@ -45,13 +45,12 @@ class MypageViewController: UIViewController, FusumaDelegate{ } } func checkUserLogin(){ - let appDelegate = UIApplication.shared.delegate as! AppDelegate - let user = appDelegate.user - if user?.email != "" { - if user?.user_profile != nil { + let user = User.sharedInstance + if user.email != "" { + if user.user_profile != "" { // userImage.af_setImage(withURL: URL(string: (user?.user_profile)!)!) // userImage.image = userImage.image?.af_imageRoundedIntoCircle() - let url = URL(string: (user?.user_profile)!)! + let url = URL(string: (user.user_profile))! let placeholderImage = UIImage(named: "ic_user")! let filter = AspectScaledToFillSizeCircleFilter( size: userImage.frame.size @@ -64,8 +63,8 @@ class MypageViewController: UIViewController, FusumaDelegate{ }else{ userImage.image = UIImage(named: "user_default.png") } - nickNameLabel.text = user?.nickname - emailLabel.text = user?.email + nickNameLabel.text = user.nickname + emailLabel.text = user.email labelList[2] = "로그아웃" DispatchQueue.main.async { self.moreImg.isHidden = false @@ -84,12 +83,11 @@ class MypageViewController: UIViewController, FusumaDelegate{ } } func setLogined(){ - let appDelegate = UIApplication.shared.delegate as! AppDelegate - let currentUser = appDelegate.user - if currentUser?.email != "" { + let currentUser = User.sharedInstance + if currentUser.email != "" { DispatchQueue.main.async{ - self.nickNameLabel.text = currentUser?.nickname - self.emailLabel.text = currentUser?.email + self.nickNameLabel.text = currentUser.nickname + self.emailLabel.text = currentUser.email self.labelList[2] = "로그아웃" self.tableView.reloadData() } @@ -127,7 +125,7 @@ class MypageViewController: UIViewController, FusumaDelegate{ } } func profileTapped(){ - if appDelegate.user?.email != "" { + if User.sharedInstance.email != "" { let fusuma = FusumaViewController() fusuma.delegate = self fusuma.cropHeightRatio = 0.7 @@ -138,10 +136,10 @@ class MypageViewController: UIViewController, FusumaDelegate{ } } func fusumaImageSelected(_ image: UIImage, source: FusumaMode) { - if appDelegate.user?.email != "" { + if User.sharedInstance.email != "" { userImage.image = image userImage.image = userImage.image?.af_imageRoundedIntoCircle() - DataManager.updateUserProfile(user: appDelegate.user!, profile: image) + DataManager.updateUserProfile(user: User.sharedInstance, profile: image) } } func fusumaMultipleImageSelected(_ images: [UIImage], source: FusumaMode) { @@ -183,7 +181,7 @@ extension MypageViewController: UITableViewDataSource, UITableViewDelegate { } func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { if indexPath.row == 0 { // 내가 찜한 상품을 눌렀을 때 - if appDelegate.user?.email == "" { // 로그인 된 상태가 아니면 + if User.sharedInstance.email == "" { // 로그인 된 상태가 아니면 let alertController = UIAlertController(title: "알림", message: "내가 찜한 상품은 로그인 뒤 이용가능합니다.", preferredStyle: UIAlertControllerStyle.alert) let DestructiveAction = UIAlertAction(title: "취소", style: UIAlertActionStyle.destructive) { (result : UIAlertAction) -> Void in alertController.dismiss(animated: false, completion: nil) @@ -220,7 +218,7 @@ extension MypageViewController: UITableViewDataSource, UITableViewDelegate { let firebaseAuth = Auth.auth() do { try firebaseAuth.signOut() - self.appDelegate.user = User() + User.sharedInstance = User() self.setLogined() self.showAlertIfLogined(bool: false) } catch let signOutError as NSError { diff --git a/iOSProject/pyunrihae/pyunrihae/ProductDetailViewController.swift b/iOSProject/pyunrihae/pyunrihae/ProductDetailViewController.swift index 9f94fd94..48dcfdd6 100644 --- a/iOSProject/pyunrihae/pyunrihae/ProductDetailViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/ProductDetailViewController.swift @@ -27,9 +27,8 @@ class ProductDetailViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() writingReviewBtn.isHidden = true - let user = appdelegate.user - if user?.email != "" { - if (user?.wish_product_list.contains(SelectedProduct.foodId))!{ + if User.sharedInstance.email != "" { + if (User.sharedInstance.wish_product_list.contains(SelectedProduct.foodId)){ wishBtn.setBackgroundImage(UIImage(named: "ic_like_filled.png"), for: .normal) } else { wishBtn.setBackgroundImage(UIImage(named: "ic_like.png"), for: .normal) @@ -90,9 +89,9 @@ class ProductDetailViewController: UIViewController { close() } @IBAction func tabWishBtn(_ sender: UIButton) { - let appDelegate = UIApplication.shared.delegate as! AppDelegate - let user = appDelegate.user - if user?.email != "" { + + let user = User.sharedInstance + if user.email != "" { uploadingView.isHidden = false if wishBtn.backgroundImage(for: .normal) == UIImage(named: "ic_like.png") { alertMessageLabel.text = "위시리스트에 추가되었습니다!" @@ -103,7 +102,7 @@ class ProductDetailViewController: UIViewController { wishBtn.setBackgroundImage(UIImage(named: "ic_like.png"), for: .normal) reviewUpload() } - DataManager.updateWishList(id: SelectedProduct.foodId, uid: (user?.id)!) + DataManager.updateWishList(id: SelectedProduct.foodId, uid: (user.id)) } else { let alert = UIAlertController(title: "로그인 후 이용해주세요!", message: "", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) @@ -121,7 +120,7 @@ class ProductDetailViewController: UIViewController { } } func didPressUsefulBtn(sender: UIButton) { //유용해요 버튼 누르기 - if appdelegate.user?.email == "" { + if User.sharedInstance.email == "" { let alert = UIAlertController(title: "로그인 후 이용해주세요!", message: "", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) self.present(alert, animated: true, completion: nil) @@ -130,7 +129,7 @@ class ProductDetailViewController: UIViewController { } } func didPressBadBtn(sender: UIButton) { //별로에요 버튼 누르기 - if appdelegate.user?.email == "" { + if User.sharedInstance.email == "" { let alert = UIAlertController(title: "로그인 후 이용해주세요!", message: "", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) self.present(alert, animated: true, completion: nil) @@ -195,14 +194,14 @@ class ProductDetailViewController: UIViewController { } } } else if segue.destination is WritingReviewViewController { // 리뷰 작성 화면 - let appDelegate = UIApplication.shared.delegate as! AppDelegate - let user = appDelegate.user - if user?.email == "" { + + let user = User.sharedInstance + if user.email == "" { let alert = UIAlertController(title: "로그인 후 이용해주세요!", message: "", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) self.present(alert, animated: true, completion: nil) } - if (user?.product_review_list.contains(SelectedProduct.foodId))!{ + if (user.product_review_list.contains(SelectedProduct.foodId)){ let alert = UIAlertController(title: "이미 리뷰한 상품입니다 :)", message: "", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) self.present(alert, animated: true, completion: nil) diff --git a/iOSProject/pyunrihae/pyunrihae/ReviewViewController.swift b/iOSProject/pyunrihae/pyunrihae/ReviewViewController.swift index ad85259d..e861d140 100644 --- a/iOSProject/pyunrihae/pyunrihae/ReviewViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/ReviewViewController.swift @@ -280,7 +280,7 @@ extension ReviewViewController: UITableViewDataSource, UITableViewDelegate { // } } func didPressUsefulBtn(sender: UIButton) { //유용해요 버튼 누르기 - if appdelegate.user?.email == "" { + if User.sharedInstance.email == "" { let alert = UIAlertController(title: "로그인 후 이용해주세요!", message: "", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) self.present(alert, animated: true, completion: nil) @@ -292,7 +292,7 @@ extension ReviewViewController: UITableViewDataSource, UITableViewDelegate { // } } func didPressBadBtn(sender: UIButton) { //별로에요 버튼 누르기 - if appdelegate.user?.email == "" { + if User.sharedInstance.email == "" { let alert = UIAlertController(title: "로그인 후 이용해주세요!", message: "", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) self.present(alert, animated: true, completion: nil) diff --git a/iOSProject/pyunrihae/pyunrihae/SignupViewController.swift b/iOSProject/pyunrihae/pyunrihae/SignupViewController.swift index da2a0a72..0e33a927 100644 --- a/iOSProject/pyunrihae/pyunrihae/SignupViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/SignupViewController.swift @@ -97,8 +97,7 @@ class SignupViewController: UIViewController, UITextFieldDelegate{ }) }else{ self.dismiss(animated: true, completion: { - let appDelegate = UIApplication.shared.delegate as! AppDelegate - appDelegate.user? = user_instance + User.sharedInstance = user_instance NotificationCenter.default.post(name: NSNotification.Name("userLogined"), object: nil) }) } diff --git a/iOSProject/pyunrihae/pyunrihae/TabBarViewController.swift b/iOSProject/pyunrihae/pyunrihae/TabBarViewController.swift index c37ccd01..81b0fd9c 100644 --- a/iOSProject/pyunrihae/pyunrihae/TabBarViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/TabBarViewController.swift @@ -67,10 +67,10 @@ class TabBarViewController: UIViewController,NVActivityIndicatorViewable { didPressTabBtn(tabBtns[selectedTabIndex]) if Auth.auth().currentUser != nil { DataManager.getUserFromUID(uid: (Auth.auth().currentUser?.uid)!, completion: { (user) in - self.appdelegate.user = user + User.sharedInstance = user }) } else { - appdelegate.user = User() + User.sharedInstance = User() } } override func didReceiveMemoryWarning() { diff --git a/iOSProject/pyunrihae/pyunrihae/UpdateNicknameViewController.swift b/iOSProject/pyunrihae/pyunrihae/UpdateNicknameViewController.swift index d37a643e..7ccff943 100644 --- a/iOSProject/pyunrihae/pyunrihae/UpdateNicknameViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/UpdateNicknameViewController.swift @@ -25,7 +25,7 @@ class UpdateNicknameViewController: UIViewController { dismiss(animated: true, completion: nil) } @IBAction func onNickNameTextChanged(_ sender: Any) { - if (nickNameTextField.text?.characters.count)! > 0 { + if (nickNameTextField.text?.characters.count)! > 0 && (nickNameTextField.text?.characters.count)! <= 10{ completeBtn.isEnabled = true }else{ completeBtn.isEnabled = false @@ -33,17 +33,17 @@ class UpdateNicknameViewController: UIViewController { } @IBAction func onTouchCompleteBtn(_ sender: Any) { if completeBtn.isEnabled { - if appdelegate.user != nil { + if User.sharedInstance.email != "" { // 로그인 되어있으면 if (nickNameTextField.text?.characters.count)! > 10 { - let alert = UIAlertController(title: "알림", message: "10자 이내로 닉네임을 설정해주세요!", preferredStyle: UIAlertControllerStyle.alert) + let alert = UIAlertController(title: "알림", message: "닉네임을 10자 이내로 설정해주세요!", preferredStyle: UIAlertControllerStyle.alert) alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.cancel, handler: nil)) self.present(alert, animated: true, completion: nil) } else { - DataManager.updateUserNickname(user: appdelegate.user!, nickname: nickNameTextField.text!) + DataManager.updateUserNickname(user: User.sharedInstance, nickname: nickNameTextField.text!) self.dismiss(animated: false, completion: nil) NotificationCenter.default.post(name: NSNotification.Name("userLogined"), object: nil) } - } + } // 비회원이면 동작 안함 } } func dismissKeyboard() { diff --git a/iOSProject/pyunrihae/pyunrihae/User.swift b/iOSProject/pyunrihae/pyunrihae/User.swift index af11a9af..53b72eeb 100644 --- a/iOSProject/pyunrihae/pyunrihae/User.swift +++ b/iOSProject/pyunrihae/pyunrihae/User.swift @@ -8,14 +8,16 @@ import Foundation import FirebaseDatabase class User { + static var sharedInstance = User() + var id : String // FirebaseAuth의 UID var email : String // 이메일 var nickname : String // 닉네임 -// var password : String 비밀번호는 저장할 수 없음. var user_profile : String // 유저 프로필 사진 url var review_like_list : [String : Int] // 유용해요 / 별로에요 누른 리뷰id : 숫자 (+1는 유용해요, -1는 별로에요) var product_review_list : [String] // 리뷰를 쓴 상품id 리스트 var wish_product_list : [String] // 즐겨찾기한 상품id 리스트 + init(){ id = "" email = "" diff --git a/iOSProject/pyunrihae/pyunrihae/WritingReviewViewController.swift b/iOSProject/pyunrihae/pyunrihae/WritingReviewViewController.swift index 8f5c934e..e98021dd 100644 --- a/iOSProject/pyunrihae/pyunrihae/WritingReviewViewController.swift +++ b/iOSProject/pyunrihae/pyunrihae/WritingReviewViewController.swift @@ -78,8 +78,8 @@ class WritingReviewViewController: UIViewController, FusumaDelegate{ } @IBAction func tabCompleteBtn(_ sender: UIButton) { var user_image = "" - if appdelegate.user?.user_profile != "" { - user_image = (appdelegate.user?.user_profile)! + if User.sharedInstance.user_profile != "" { + user_image = (User.sharedInstance.user_profile) }else{ user_image = "http://item.kakaocdn.net/dw/4407092.title.png" } @@ -91,8 +91,8 @@ class WritingReviewViewController: UIViewController, FusumaDelegate{ } else { self.navigationController?.popToRootViewController(animated: true) NotificationCenter.default.post(name: NSNotification.Name("startUploading"), object: self) - appdelegate.user?.product_review_list.append(SelectedProduct.foodId) - DataManager.updateReviewList(id: SelectedProduct.foodId, uid: (appdelegate.user?.id)!) + User.sharedInstance.product_review_list.append(SelectedProduct.foodId) + DataManager.updateReviewList(id: SelectedProduct.foodId, uid: (User.sharedInstance.id)) DataManager.getProductById(id: SelectedProduct.foodId) { (product) in DataManager.getUserFromUID(uid: (Auth.auth().currentUser?.uid)!, completion: { (user) in let userNickName = user.nickname