Skip to content

Commit

Permalink
Refactoring code logic
Browse files Browse the repository at this point in the history
  • Loading branch information
SHcommit committed Oct 31, 2022
1 parent 0e47905 commit 65c9017
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 37 deletions.
4 changes: 4 additions & 0 deletions Clone App/Instagram/Instagram.xcodeproj/project.pbxproj
Expand Up @@ -34,6 +34,7 @@
15ACA739290D7EDF0011E665 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 15ACA738290D7EDF0011E665 /* GoogleService-Info.plist */; };
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 */; };
81BBDDEDACB63985F43988AC /* Pods_Instagram.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 5B288DF44C633C67CA7F5E28 /* Pods_Instagram.framework */; };
/* End PBXBuildFile section */

Expand Down Expand Up @@ -67,6 +68,7 @@
15ACA738290D7EDF0011E665 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
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>"; };
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 @@ -151,6 +153,7 @@
children = (
15ACA71E290D793D0011E665 /* AuthService.swift */,
15ACA71D290D793D0011E665 /* ImageUploader.swift */,
15C18489290FF465004EF35F /* UserService.swift */,
);
path = API;
sourceTree = "<group>";
Expand Down Expand Up @@ -362,6 +365,7 @@
15ACA728290D795C0011E665 /* UserHelpLabelAndButton.swift in Sources */,
15ACA736290D79FA0011E665 /* Extensions.swift in Sources */,
15ACA72F290D796F0011E665 /* ProfileCell.swift in Sources */,
15C1848A290FF465004EF35F /* UserService.swift in Sources */,
15ACA716290D779B0011E665 /* SearchController.swift in Sources */,
15ACA71A290D79250011E665 /* RegistrationController.swift in Sources */,
15ACA714290D77960011E665 /* FeedController.swift in Sources */,
Expand Down
40 changes: 6 additions & 34 deletions Clone App/Instagram/Instagram/API/AuthService.swift
Expand Up @@ -23,50 +23,22 @@ struct AuthService {
Auth.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)
}
}
}

}

//MAKR: Extension
extension AuthService {

static func encodeToNSDictionary(codableType info: Codable) -> [String : Any] {
guard let dataDictionary = info.encodeToDictionary else { fatalError() }
return dataDictionary
}

static func updateCurrentUserInfo(CodableType info: UserInfoModel) {
let encodedUserModel = encodeToNSDictionary(codableType: info)
Firestore.firestore().collection(firestoreUsers).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 error == nil else { return }
guard let document = documentSnapshot else { return }
do {
completion(try document.data(as: UserInfoModel.self))
}catch let e {
print("Fail decode user document field : \(e.localizedDescription)")
}
completion(nil)
}
}

static func fetchUserProfile(userProfile url: String, completion: @escaping (UIImage?) -> Void) {
let storageReference = Storage.storage().reference(forURL: url)

storageReference.getData(maxSize: userProfileMegaByte) { data, error in
guard error == nil else { return }
guard let data = data else { completion(nil); return }
completion(UIImage(data: data))
}
}


}
56 changes: 56 additions & 0 deletions Clone App/Instagram/Instagram/API/UserService.swift
@@ -0,0 +1,56 @@
//
// UserService.swift
// Instagram
//
// Created by 양승현 on 2022/10/31.
//

import Firebase
import FirebaseFirestore


struct UserService {

static func updateCurrentUserInfo(CodableType info: UserInfoModel) {
let encodedUserModel = encodeToNSDictionary(codableType: info)
Firestore.firestore().collection(firestoreUsers).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 error == nil else { return }
guard let document = documentSnapshot else { return }
do {
completion(try document.data(as: UserInfoModel.self))
}catch let e {
print("Fail decode user document field : \(e.localizedDescription)")
}
completion(nil)
}
}

static func fetchUserProfile(userProfile url: String, completion: @escaping (UIImage?) -> Void) {
let storageReference = Storage.storage().reference(forURL: url)

storageReference.getData(maxSize: userProfileMegaByte) { data, error in
guard error == nil else { return }
guard let data = data else { completion(nil); return }
completion(UIImage(data: data))
}
}

}

//MAKR: Extension
extension UserService {

static func encodeToNSDictionary(codableType info: Codable) -> [String : Any] {
guard let dataDictionary = info.encodeToDictionary else { fatalError() }
return dataDictionary
}

}
Expand Up @@ -47,7 +47,7 @@ extension ProfileHeader {
static func initialNameLabel() -> UILabel {
let lb = UILabel()
lb.translatesAutoresizingMaskIntoConstraints = false
AuthService.fetchCurrentUserInfo() { userInfo in
UserService.fetchCurrentUserInfo() { userInfo in
guard let userInfo = userInfo else { return }
lb.text = userInfo.username
}
Expand All @@ -59,10 +59,10 @@ extension ProfileHeader {

func initialProfileIV() -> UIImageView {
let iv = UIImageView()
AuthService.fetchCurrentUserInfo() { userInfo in
UserService.fetchCurrentUserInfo() { userInfo in
guard let userInfo = userInfo else { return }
DispatchQueue.main.async {
AuthService.fetchUserProfile(userProfile: userInfo.profileURL) { image in
UserService.fetchUserProfile(userProfile: userInfo.profileURL) { image in
guard let image = image else { return }
iv.image = image
}
Expand Down

0 comments on commit 65c9017

Please sign in to comment.