Skip to content

Commit

Permalink
refact :: [#102] 프로필 로직 리팩토링
Browse files Browse the repository at this point in the history
  • Loading branch information
cyj513 committed Oct 18, 2024
1 parent ead90a5 commit 8ece310
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ public enum UserDefaultKeys: String {
case pickerTimeMode
case userInfoData
case userNameData
case userProfileImageData
}

public struct UserDefaultStorage: UserDefault {
Expand Down
4 changes: 3 additions & 1 deletion Projects/Presentation/Sources/DI/PresentationAssembly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,9 @@ public final class PresentationAssembly: Assembly {
}
container.register(AllTabViewModel.self) { resolver in
AllTabViewModel(
logoutUseCase: resolver.resolve(LogoutUseCase.self)!)
fetchProfileUsecase: resolver.resolve(FetchSimpleProfileUseCase.self)!,
logoutUseCase: resolver.resolve(LogoutUseCase.self)!
)
}
// Notice
container.register(NoticeListViewController.self) { resolver in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ public class AllTabViewController: BaseViewController<AllTabViewModel> {
}
public override func bind() {
let input = AllTabViewModel.Input(
viewWillAppear: viewWillAppearRelay.asObservable(),
clickSelfStudyTab: helpSectionView.getSelectedItem(type: .selfStudy).asObservable(),
clickNoticeTab: helpSectionView.getSelectedItem(type: .notice).asObservable(),
clickBugReportTab: helpSectionView.getSelectedItem(type: .bugReport).asObservable(),
Expand All @@ -53,7 +54,18 @@ public class AllTabViewController: BaseViewController<AllTabViewModel> {
clickLogOutTab: logoutRelay.asObservable()
)

_ = viewModel.transform(input: input)
let output = viewModel.transform(input: input)

output.profileData.asObservable()
.withUnretained(self)
.bind { owner, profileData in
let userInfoData = UserDefaultStorage.shared.get(forKey: .userInfoData) as? String

owner.profileView.setup(
image: profileData.profile ?? "",
info: userInfoData ?? "정보가 없는 사용자"
)
}.disposed(by: disposeBag)

accountSectionView
.getSelectedItem(type: .logOut)
Expand Down Expand Up @@ -103,16 +115,4 @@ public class AllTabViewController: BaseViewController<AllTabViewModel> {
}
}

public override func setLayoutData() {
let userDefaultStorage = UserDefaultStorage.shared

let userInfoData = userDefaultStorage.get(forKey: .userInfoData) as? String
let userInfoImage = userDefaultStorage.get(forKey: .userProfileImageData) as? String

self.profileView.setup(
image: userInfoImage ?? "",
info: userInfoData ?? "정보가 없는 사용자"
)
}

}
28 changes: 25 additions & 3 deletions Projects/Presentation/Sources/Scene/AllTab/AllTabViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ import Domain
public class AllTabViewModel: BaseViewModel, Stepper {
private let disposeBag = DisposeBag()
public var steps = PublishRelay<Step>()

private let fetchProfileUsecase: FetchSimpleProfileUseCase
private let logoutUseCase: LogoutUseCase

public init(logoutUseCase: LogoutUseCase) {
public init(
fetchProfileUsecase: FetchSimpleProfileUseCase,
logoutUseCase: LogoutUseCase
) {
self.fetchProfileUsecase = fetchProfileUsecase
self.logoutUseCase = logoutUseCase
}

public struct Input {
let viewWillAppear: Observable<Void>
let clickSelfStudyTab: Observable<IndexPath>
let clickNoticeTab: Observable<IndexPath>
let clickBugReportTab: Observable<IndexPath>
Expand All @@ -25,9 +32,24 @@ public class AllTabViewModel: BaseViewModel, Stepper {
let clickMyPageTab: Observable<IndexPath>
let clickLogOutTab: Observable<Void>
}
public struct Output {}
public struct Output {
let profileData: Signal<SimpleProfileEntity>
}

private let profileData = PublishRelay<SimpleProfileEntity>()

public func transform(input: Input) -> Output {
input.viewWillAppear
.flatMap {
self.fetchProfileUsecase.execute()
.catch {
print($0.localizedDescription)
return .never()
}
}
.bind(to: profileData)
.disposed(by: disposeBag)

input.clickSelfStudyTab
.map { _ in PiCKStep.selfStudyIsRequired }
.bind(to: steps)
Expand Down Expand Up @@ -66,7 +88,7 @@ public class AllTabViewModel: BaseViewModel, Stepper {
.bind(to: steps)
.disposed(by: disposeBag)

return Output()
return Output(profileData: profileData.asSignal())
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,6 @@ extension MyPageViewController: UIImagePickerControllerDelegate, UINavigationCon
let image = info[UIImagePickerController.InfoKey.editedImage] as? UIImage
self?.profileImageView.image = image
self?.profileImageData.accept(image?.jpegData(compressionQuality: 0.1) ?? Data())
UserDefaultStorage.shared.remove(forKey: .userProfileImageData)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ public class MyPageViewModel: BaseViewModel, Stepper {
private let disposeBag = DisposeBag()
public var steps = PublishRelay<Step>()

private let profileUsecase: FetchDetailProfileUseCase
private let fetchProfileUsecase: FetchDetailProfileUseCase
private let uploadProfileImageUseCase: UploadProfileImageUseCase

public init(
profileUsecase: FetchDetailProfileUseCase,
uploadProfileImageUseCase: UploadProfileImageUseCase
) {
self.profileUsecase = profileUsecase
self.fetchProfileUsecase = profileUsecase
self.uploadProfileImageUseCase = uploadProfileImageUseCase
}

Expand All @@ -35,7 +35,7 @@ public class MyPageViewModel: BaseViewModel, Stepper {
public func transform(input: Input) -> Output {
input.viewWillAppear
.flatMap {
self.profileUsecase.execute()
self.fetchProfileUsecase.execute()
.catch {
print($0.localizedDescription)
return .never()
Expand Down
23 changes: 11 additions & 12 deletions Projects/Presentation/Sources/Scene/Home/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,17 @@ public class HomeViewController: BaseViewController<HomeViewModel> {
owner.homeViewType = data
}.disposed(by: disposeBag)

output.profileData.asObservable()
.withUnretained(self)
.bind { owner, data in
let userInfoData = UserDefaultStorage.shared.get(forKey: .userInfoData) as? String

owner.profileView.setup(
image: data.profile ?? "",
info: userInfoData ?? "정보가 없는 사용자"
)
}.disposed(by: disposeBag)

output.applyStatusData.asObservable()
.withUnretained(self)
.bind { owner, data in
Expand Down Expand Up @@ -296,18 +307,6 @@ public class HomeViewController: BaseViewController<HomeViewModel> {
}
}

public override func setLayoutData() {
let userDefaultStorage = UserDefaultStorage.shared

let userInfoData = userDefaultStorage.get(forKey: .userInfoData) as? String
let userInfoImage = userDefaultStorage.get(forKey: .userProfileImageData) as? String

self.profileView.setup(
image: userInfoImage ?? "",
info: userInfoData ?? "정보가 없는 사용자"
)
}

private func setupViewType(type: HomeViewType) {
switch type {
case .timeTable:
Expand Down
15 changes: 14 additions & 1 deletion Projects/Presentation/Sources/Scene/Home/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ public class HomeViewModel: BaseViewModel, Stepper {
}
public struct Output {
let viewMode: Signal<HomeViewType>
let profileData: Signal<SimpleProfileEntity>
let applyStatusData: Signal<HomeApplyStatusEntity>
let weekendMealPeriodData: Signal<WeekendMealPeriodEntity>
let timetableData: Driver<[TimeTableEntityElement]>
Expand All @@ -71,6 +72,7 @@ public class HomeViewModel: BaseViewModel, Stepper {
}

private let viewModeData = PublishRelay<HomeViewType>()
private let profileData = PublishRelay<SimpleProfileEntity>()
private let applyStatusData = PublishRelay<HomeApplyStatusEntity>()
private let weekendMealPeriodData = PublishRelay<WeekendMealPeriodEntity>()
private let timetableData = BehaviorRelay<[TimeTableEntityElement]>(value: [])
Expand All @@ -93,6 +95,17 @@ public class HomeViewModel: BaseViewModel, Stepper {
}
}).disposed(by: disposeBag)

input.viewWillAppear.flatMap {
self.fetchProfileUseCase.execute()
.catch {
print($0.localizedDescription)
return .never()
}
}
.bind(to: profileData)
.disposed(by: disposeBag)


input.viewWillAppear
.flatMap {
self.fetchWeekendMealPeriodUseCase.execute()
Expand All @@ -117,7 +130,6 @@ public class HomeViewModel: BaseViewModel, Stepper {

self.userDefaultStorage.set(to: infoValue, forKey: .userInfoData)
self.userDefaultStorage.set(to: data.name, forKey: .userNameData)
// self.userDefaultStorage.set(to: data.profile, forKey: .userProfileImageData)
}).disposed(by: disposeBag)

input.viewWillAppear
Expand Down Expand Up @@ -239,6 +251,7 @@ public class HomeViewModel: BaseViewModel, Stepper {

return Output(
viewMode: viewModeData.asSignal(),
profileData: profileData.asSignal(),
applyStatusData: applyStatusData.asSignal(),
weekendMealPeriodData: weekendMealPeriodData.asSignal(),
timetableData: timetableData.asDriver(),
Expand Down

0 comments on commit 8ece310

Please sign in to comment.