Skip to content

Commit

Permalink
Merge pull request #126 from DeveloperAcademy-POSTECH/fix/Login/OrgName
Browse files Browse the repository at this point in the history
[Fix] ์•ฑ ๋กœ๊ทธ์ธ ์ธ์ฆ๋Œ€ํ•™ ์—†์Œ ๋ฒ„๊ทธ ์ˆ˜์ •
  • Loading branch information
Gobans authored Dec 4, 2022
2 parents 76c8787 + d9a60f3 commit 7c16a34
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Projects/App/Sources/Application/SceneDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate {
let window = UIWindow(windowScene: windowScene)
let userAuthToken = KeyChainManager.read(key: .authToken)
let userNickName = UserInfoManager.userInfo?.userNickname
let organizationID = UserInfoManager.userInfo?.userOrganization
let isEmptyOrganization = UserInfoManager.userInfo?.userOrganization.isEmpty
let navigationController = UINavigationController(rootViewController: ThirdPartyLoginViewController())
// ์ •์ƒ์ ์œผ๋กœ ๊ฐ€์ž…์„ ํ•œ๊ฒฝ์šฐ
if userAuthToken != nil && userNickName != nil && organizationID != nil {
if userAuthToken != nil && userNickName != nil && isEmptyOrganization == false {
navigationController.pushViewController(PlaceListViewController(), animated: false)
}
window.rootViewController = navigationController
Expand Down
32 changes: 32 additions & 0 deletions Projects/App/Sources/Domain/User/UserInfoManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@
//

import Foundation
import Combine

final class UserInfoManager {

public static let shared = UserInfoManager()

private var useCase = UserInfoManagerUseCase()
private var cancelBag = Set<AnyCancellable>()
public var isNameFetched = PassthroughSubject<Bool, Never>()

private init() {}

private enum UserInfoKeys: String, CaseIterable {
case userNickname
case userID
Expand Down Expand Up @@ -100,4 +109,27 @@ final class UserInfoManager {
UserInfoManager.userInfo?.userOrganization = userOrganization
UserInfoManager.userInfo?.userOrgName = userOrgName
}

}

extension UserInfoManager: ErrorMapper {

// TODO: fetchOrganizationList ์™€ userOrgName ์—…๋ฐ์ดํŠธ ๋ถ„๋ฆฌ
func fetchOrganizationList(orgID: Int) {
useCase.fetchOrganizationList()
.sink { [weak self] error in
guard let self = self else { return }
switch error {
case .failure(let error):
let errorMessage = self.errorMessage(for: error)
print(errorMessage)
case .finished: break
}
} receiveValue: { [weak self] orgList in
UserInfoManager.userInfo?.userOrgName = orgList.first(where: {$0.id == orgID})?.name ?? "(์ธ์ฆ๋Œ€ํ•™์—†์Œ)"
self?.isNameFetched.send(true)
}
.store(in: &cancelBag)
}

}
23 changes: 23 additions & 0 deletions Projects/App/Sources/Domain/User/UserInfoManagerUseCase.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// UserInfoManagerUseCase.swift
// App
//
// Created by Lee Myeonghwan on 2022/12/04.
// Copyright ยฉ 2022 com.zesty. All rights reserved.
//

import Combine
import Foundation
import Network

final class UserInfoManagerUseCase {

func fetchOrganizationList() -> AnyPublisher<[Organization], NetworkError> {
return OrganizationAPI.fetchOrgList()
.map { orgList in
orgList.map { Organization(dto: $0) }
}
.eraseToAnyPublisher()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
//
// Created by ๊น€ํƒœํ˜ธ on 2022/10/25.
// Updated by ๋ฆฌ์•„ on 2022/11/01.
// Updated userOrg by ๊ณ ๋ฐ˜ on 2022/12/03.
// Copyright (c) 2022 zesty. All rights reserved.
//

Expand Down Expand Up @@ -33,6 +34,8 @@ final class PlaceListViewController: UIViewController {
private var emptyImageView = UIImageView()
private var emptyLabel = UILabel()

private let placeTitle = UILabel()

// MARK: - LifeCycle

init(viewModel: PlaceListViewModel = PlaceListViewModel()) {
Expand All @@ -50,17 +53,48 @@ final class PlaceListViewController: UIViewController {
configureDataSource()
createLayout()
configureUI()
bind()
analytics()
}

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
bind()
bindResult()
}

override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
// TODO: ๊ณ„์† sink ๊ฐ€ ์Œ“์ด๋ฉด์„œ ์ค‘๋ณต ์‹คํ–‰๋˜๋Š” ๋ฒ„๊ทธ ํ•ด๊ฒฐ์„ ์œ„ํ•ด ์ž„์‹œ๋กœ cancelBag ์ดˆ๊ธฐํ™”
cancelBag.removeAll()
}

// MARK: - Function

private func analytics() {
FirebaseAnalytics.Analytics.logEvent("place_list_viewed", parameters: [
AnalyticsParameterScreenName: "place_list"
])
}

}

// MARK: - Bind Function

extension PlaceListViewController {

private func bind() {
UserInfoManager.shared.isNameFetched
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self = self else { return }
// TODO: ๊ณ„์† sink ๊ฐ€ ์Œ“์ด๋ฉด์„œ ์ค‘๋ณต ์‹คํ–‰๋˜๋Š” ๋ฒ„๊ทธ๊ฐ€ ์žˆ์Œ. cancelBag์—์„œ binding์ด ์ œ๋Œ€๋กœ ํ•ด์ œ๊ฐ€ ์•ˆ๋˜๋Š” ๊ฒƒ ๊ฐ™์Œ.
self.placeTitle.text = UserInfoManager.userInfo?.userOrgName
self.navigationItem.leftBarButtonItem = UIBarButtonItem.init(customView: self.placeTitle)
}
.store(in: &cancelBag)
}

private func bindResult() {
viewModel.$result
.receive(on: DispatchQueue.main)
.sink { [weak self] placeList in
Expand All @@ -71,12 +105,6 @@ final class PlaceListViewController: UIViewController {
.store(in: &cancelBag)
}

private func analytics() {
FirebaseAnalytics.Analytics.logEvent("place_list_viewed", parameters: [
AnalyticsParameterScreenName: "place_list"
])
}

}

extension PlaceListViewController: UICollectionViewDataSourcePrefetching, UICollectionViewDelegate {
Expand Down Expand Up @@ -251,9 +279,14 @@ extension PlaceListViewController {
let searchItem = UIBarButtonItem(barButtonSystemItem: .search, target: self, action: #selector(searchButtonTapped))
let personCropCircle = UIImage(systemName: "person.crop.circle")
let userInfoItem = UIBarButtonItem(image: personCropCircle, style: .plain, target: self, action: #selector(userInfoButtonTapped))
let placeTitle = UILabel()
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(orgDetailButtonTapped))


// TODO: ์—ฌ๋Ÿฌ ์กฐ์ง์„ ๊ฐ€์ž…์„ ๋Œ€๋น„ํ•˜์—ฌ ํ™•์žฅ์„ฑ ์žˆ๊ฒŒ ๊ตฌ์กฐ ๊ฐœ์„ 
if UserInfoManager.userInfo?.userOrgName != nil {
placeTitle.text = UserInfoManager.userInfo?.userOrgName
} else if let orgID = UserInfoManager.userInfo?.userOrganization.first {
UserInfoManager.shared.fetchOrganizationList(orgID: orgID)
}
placeTitle.text = UserInfoManager.userInfo?.userOrgName ?? "(์ธ์ฆ๋Œ€ํ•™์—†์Œ)"
placeTitle.font = .systemFont(ofSize: 17, weight: .bold)
placeTitle.isUserInteractionEnabled = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ final class ThirdPartyLoginViewModel {
self.shouldSetNicknameSubject.send(true)
return
}
if UserInfoManager.userInfo?.userOrganization == nil {
let isEmptyOrganization = UserInfoManager.userInfo?.userOrganization.isEmpty ?? true
if isEmptyOrganization {
self.shouldSetOrganizationSubject.send(true)
return
}
Expand Down

0 comments on commit 7c16a34

Please sign in to comment.