Skip to content

Commit

Permalink
Refactoring. Add global constant properties
Browse files Browse the repository at this point in the history
  • Loading branch information
SHcommit committed Oct 31, 2022
1 parent 65c9017 commit ca9b3c5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 15 deletions.
4 changes: 4 additions & 0 deletions Clone App/Instagram/Instagram.xcodeproj/project.pbxproj
Expand Up @@ -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 */

Expand Down Expand Up @@ -69,6 +70,7 @@
15ACA73B290D80030011E665 /* AuthenticationViewModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = AuthenticationViewModel.swift; sourceTree = "<group>"; };
15ACA73C290D80040011E665 /* UserInfoModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = UserInfoModel.swift; sourceTree = "<group>"; };
15C18489290FF465004EF35F /* UserService.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserService.swift; sourceTree = "<group>"; };
15C1848B290FF76E004EF35F /* Constants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Constants.swift; sourceTree = "<group>"; };
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 = "<group>"; };
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 = "<group>"; };
Expand Down Expand Up @@ -200,6 +202,7 @@
isa = PBXGroup;
children = (
15ACA734290D79FA0011E665 /* Extensions.swift */,
15C1848B290FF76E004EF35F /* Constants.swift */,
);
path = Utils;
sourceTree = "<group>";
Expand Down Expand Up @@ -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 */,
Expand Down
8 changes: 3 additions & 5 deletions Clone App/Instagram/Instagram/API/AuthService.swift
Expand Up @@ -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)
}
}
}
Expand Down
12 changes: 5 additions & 7 deletions Clone App/Instagram/Instagram/API/UserService.swift
Expand Up @@ -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 {
Expand All @@ -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))
Expand Down
17 changes: 17 additions & 0 deletions 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()
3 changes: 0 additions & 3 deletions Clone App/Instagram/Instagram/Utils/Extensions.swift
Expand Up @@ -6,9 +6,6 @@
//
import UIKit

//MARK: - Constant properties
let userProfileMegaByte = Int64(1*400*400)

extension UIViewController {
func setupViewGradientBackground() {
let gradient = CAGradientLayer()
Expand Down

0 comments on commit ca9b3c5

Please sign in to comment.