From ca9b3c5bea3b8059be9da3d3521763810758e17b Mon Sep 17 00:00:00 2001 From: ysh Date: Mon, 31 Oct 2022 21:37:13 +0900 Subject: [PATCH] Refactoring. Add global constant properties --- .../Instagram.xcodeproj/project.pbxproj | 4 ++++ .../Instagram/Instagram/API/AuthService.swift | 8 +++----- .../Instagram/Instagram/API/UserService.swift | 12 +++++------- .../Instagram/Instagram/Utils/Constants.swift | 17 +++++++++++++++++ .../Instagram/Instagram/Utils/Extensions.swift | 3 --- 5 files changed, 29 insertions(+), 15 deletions(-) create mode 100644 Clone App/Instagram/Instagram/Utils/Constants.swift diff --git a/Clone App/Instagram/Instagram.xcodeproj/project.pbxproj b/Clone App/Instagram/Instagram.xcodeproj/project.pbxproj index dad4a85a..bfb32f84 100644 --- a/Clone App/Instagram/Instagram.xcodeproj/project.pbxproj +++ b/Clone App/Instagram/Instagram.xcodeproj/project.pbxproj @@ -35,6 +35,7 @@ 15ACA73D290D80040011E665 /* AuthenticationViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15ACA73B290D80030011E665 /* AuthenticationViewModel.swift */; }; 15ACA73E290D80040011E665 /* UserInfoModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15ACA73C290D80040011E665 /* UserInfoModel.swift */; }; 15C1848A290FF465004EF35F /* UserService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15C18489290FF465004EF35F /* UserService.swift */; }; + 15C1848C290FF76E004EF35F /* Constants.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15C1848B290FF76E004EF35F /* Constants.swift */; }; 81BBDDEDACB63985F43988AC /* Pods_Instagram.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B288DF44C633C67CA7F5E28 /* Pods_Instagram.framework */; }; /* End PBXBuildFile section */ @@ -69,6 +70,7 @@ 15ACA73B290D80030011E665 /* AuthenticationViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthenticationViewModel.swift; sourceTree = ""; }; 15ACA73C290D80040011E665 /* UserInfoModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserInfoModel.swift; sourceTree = ""; }; 15C18489290FF465004EF35F /* UserService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserService.swift; sourceTree = ""; }; + 15C1848B290FF76E004EF35F /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = ""; }; 5B288DF44C633C67CA7F5E28 /* Pods_Instagram.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Instagram.framework; sourceTree = BUILT_PRODUCTS_DIR; }; A04279BD0172B03AD23CDDBC /* Pods-Instagram.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Instagram.release.xcconfig"; path = "Target Support Files/Pods-Instagram/Pods-Instagram.release.xcconfig"; sourceTree = ""; }; BDAA046A9094EA0B1DC591AA /* Pods-Instagram.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Instagram.debug.xcconfig"; path = "Target Support Files/Pods-Instagram/Pods-Instagram.debug.xcconfig"; sourceTree = ""; }; @@ -200,6 +202,7 @@ isa = PBXGroup; children = ( 15ACA734290D79FA0011E665 /* Extensions.swift */, + 15C1848B290FF76E004EF35F /* Constants.swift */, ); path = Utils; sourceTree = ""; @@ -345,6 +348,7 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 15C1848C290FF76E004EF35F /* Constants.swift in Sources */, 15ACA71B290D79250011E665 /* LoginController.swift in Sources */, 15ACA711290D77960011E665 /* ProfileController.swift in Sources */, 156E22ED290D6EBD0026B0C3 /* ViewController.swift in Sources */, diff --git a/Clone App/Instagram/Instagram/API/AuthService.swift b/Clone App/Instagram/Instagram/API/AuthService.swift index 61c0b8a4..f19b715b 100644 --- a/Clone App/Instagram/Instagram/API/AuthService.swift +++ b/Clone App/Instagram/Instagram/API/AuthService.swift @@ -8,25 +8,23 @@ import Firebase import FirebaseFirestore -let firestoreUsers = "users" - struct AuthService { static func handleIsLoginAccount(email: String, pw: String, completion: @escaping (AuthDataResult?,Error?)-> Void) { - Auth.auth().signIn(withEmail: email, password: pw, completion: completion) + AUTH.signIn(withEmail: email, password: pw, completion: completion) } static func registerUser(withUserInfo info: RegistrationViewModel, completion: @escaping (Error?)->Void) { guard let image = info.profileImage else { return } ImageUploader.uploadImage(image: image) { imageUrl in - Auth.auth().createUser(withEmail: info.email.value, password: info.password.value) { result, error in + AUTH.createUser(withEmail: info.email.value, password: info.password.value) { result, error in guard error == nil else { print("Fail uploadImage"); return } guard let uid = result?.user.uid else { return } let userModel = info.getUserInfoModel(uid: uid, url: imageUrl) let encodedUserModel = encodeToNSDictionary(codableType: userModel) - Firestore.firestore().collection(firestoreUsers).document(uid).setData(encodedUserModel, completion: completion) + COLLECTION_USERS.document(uid).setData(encodedUserModel, completion: completion) } } } diff --git a/Clone App/Instagram/Instagram/API/UserService.swift b/Clone App/Instagram/Instagram/API/UserService.swift index d09e4188..0f2b095d 100644 --- a/Clone App/Instagram/Instagram/API/UserService.swift +++ b/Clone App/Instagram/Instagram/API/UserService.swift @@ -13,15 +13,13 @@ struct UserService { static func updateCurrentUserInfo(CodableType info: UserInfoModel) { let encodedUserModel = encodeToNSDictionary(codableType: info) - Firestore.firestore().collection(firestoreUsers).document(info.uid).updateData(encodedUserModel) + COLLECTION_USERS.document(info.uid).updateData(encodedUserModel) } static func fetchCurrentUserInfo(completion: @escaping (UserInfoModel?)->Void) { - guard let userUID = Auth.auth().currentUser?.uid else { completion((nil)); return } - let db = Firestore.firestore() - let userCollection = db.collection(firestoreUsers) - userCollection.document(userUID).getDocument() { documentSnapshot, error in + guard let userUID = CURRENT_USER?.uid else { completion((nil)); return } + COLLECTION_USERS.document(userUID).getDocument() { documentSnapshot, error in guard error == nil else { return } guard let document = documentSnapshot else { return } do { @@ -34,9 +32,9 @@ struct UserService { } static func fetchUserProfile(userProfile url: String, completion: @escaping (UIImage?) -> Void) { - let storageReference = Storage.storage().reference(forURL: url) + let storageReference = STORAGE.reference(forURL: url) - storageReference.getData(maxSize: userProfileMegaByte) { data, error in + storageReference.getData(maxSize: USERPROFILEIMAGEMEGABYTE) { data, error in guard error == nil else { return } guard let data = data else { completion(nil); return } completion(UIImage(data: data)) diff --git a/Clone App/Instagram/Instagram/Utils/Constants.swift b/Clone App/Instagram/Instagram/Utils/Constants.swift new file mode 100644 index 00000000..9e9986dc --- /dev/null +++ b/Clone App/Instagram/Instagram/Utils/Constants.swift @@ -0,0 +1,17 @@ +// +// Constants.swift +// Instagram +// +// Created by μ–‘μŠΉν˜„ on 2022/10/31. +// + +import Firebase + +//MARK: - Constant properties +let USERPROFILEIMAGEMEGABYTE = Int64(1*400*400) +let FIRESTORE_USERS = "users" +let COLLECTION_USERS = Firestore.firestore().collection(FIRESTORE_USERS) +let FIRESTORE_DB = Firestore.firestore() +let CURRENT_USER = Auth.auth().currentUser +let STORAGE = Storage.storage() +let AUTH = Auth.auth() diff --git a/Clone App/Instagram/Instagram/Utils/Extensions.swift b/Clone App/Instagram/Instagram/Utils/Extensions.swift index 3bbe9f0b..5f07560c 100644 --- a/Clone App/Instagram/Instagram/Utils/Extensions.swift +++ b/Clone App/Instagram/Instagram/Utils/Extensions.swift @@ -6,9 +6,6 @@ // import UIKit -//MARK: - Constant properties -let userProfileMegaByte = Int64(1*400*400) - extension UIViewController { func setupViewGradientBackground() { let gradient = CAGradientLayer()