Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: CameraAPIWorker 기존 Network 모듈 리펙토링 #705

Merged
merged 2 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -85,27 +85,23 @@ public final class CameraDisplayViewReactor: Reactor {
switch action {
case .viewDidLoad:
let fileName = "\(currentState.displayData.hashValue).jpg"
let parameters: CameraDisplayImageParameters = CameraDisplayImageParameters(imageName: "\(fileName)")

let body = CreatePresignedURLRequest(imageName: fileName)
return .concat(
.just(.setLoading(false)),
.just(.setError(false)),
.just(.setRenderImage(currentState.displayData)),
createPresignedCameraUseCase.execute(parameter: parameters)
.asObservable()
createPresignedCameraUseCase.execute(body: body)
.withUnretained(self)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.asObservable()
.flatMap { owner, entity -> Observable<CameraDisplayViewReactor.Mutation> in
guard let originalURL = entity?.imageURL else {
guard let remoteURL = entity?.imageURL else {
return .concat(
.just(.setLoading(true)),
.just(.setError(true))
)
}
return owner.uploadImageUseCase.execute(to: originalURL, from: owner.currentState.displayData)
return owner.uploadImageUseCase.execute(remoteURL, image: owner.currentState.displayData)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.asObservable()
.flatMap { isSuccess -> Observable<CameraDisplayViewReactor.Mutation> in
if isSuccess {
return .concat(
Expand Down Expand Up @@ -160,17 +156,12 @@ public final class CameraDisplayViewReactor: Reactor {
MPEvent.Camera.uploadPhoto.track(with: nil)

guard let presingedURL = currentState.displayEntity?.imageURL else { return .just(.setError(true)) }
let originURL = configureOriginalS3URL(url: presingedURL)
let cameraQuery = CameraMissionFeedQuery(type: currentState.cameraType.rawValue, isUploded: true)

let parameters: CameraDisplayPostParameters = CameraDisplayPostParameters(
imageUrl: originURL,
content: currentState.displayDescrption,
uploadTime: DateFormatter.yyyyMMddTHHmmssXXX.string(from: Date())
)
let remoteURL = configureOriginalS3URL(url: presingedURL)

return fetchCameraImageUseCase.execute(parameter: parameters, query: cameraQuery)
.asObservable()
let query = CreateFeedQuery(type: currentState.cameraType.rawValue)
let body = CreateFeedRequest(imageUrl: remoteURL, content: currentState.displayDescrption, uploadTime: DateFormatter.yyyyMMddTHHmmssXXX.string(from: .now))

return fetchCameraImageUseCase.execute(query: query, body: body)
.catchAndReturn(nil)
.withUnretained(self)
.flatMap { owner, entity -> Observable<CameraDisplayViewReactor.Mutation> in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class CameraViewReactor: Reactor {
@Injected private var editProfileImageUseCase: EditCameraProfileImageUseCaseProtocol
@Injected private var fetchRealEmojiListUseCase: FetchCameraRealEmojiListUseCaseProtocol
@Injected private var fetchRealEmojiPreSignedUseCase: FetchCameraRealEmojiUseCaseProtocol
@Injected private var fetchMyMemberIdUseCase: FetchMyMemberIdUseCaseProtocol
@Injected private var provider: ServiceProviderProtocol


Expand Down Expand Up @@ -220,9 +221,12 @@ extension CameraViewReactor {
switch cameraType {
case .realEmoji:

guard let memberId = fetchMyMemberIdUseCase.execute() else {
return .just(.setErrorAlert(true))
}

return .concat(
fetchRealEmojiListUseCase.execute()
.asObservable()
fetchRealEmojiListUseCase.execute(memberId: memberId)
.withUnretained(self)
.flatMap { owner, entity -> Observable<CameraViewReactor.Mutation> in
var sectionItem: [EmojiSectionItem] = []
Expand Down Expand Up @@ -283,28 +287,24 @@ extension CameraViewReactor {
)

case .profile:
//Profile 관련 이미지 업로드 Mutation
let profileImage = "\(imageData.hashValue).jpg"
let profileParameter = CameraDisplayImageParameters(imageName: profileImage)
let body = CreatePresignedURLRequest(imageName: profileImage)

return .concat(
.just(.setLoading(false)),
createProfileImageUseCase.execute(parameter: profileParameter)
.asObservable()
createProfileImageUseCase.execute(body: body)
.withUnretained(self)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.flatMap { owner, entity -> Observable<CameraViewReactor.Mutation> in
//TODO: 추후 오류 Alert 추가
guard let presingedURL = entity?.imageURL else {
guard let remoteURL = entity?.imageURL else {
return .concat(
.just(.setLoading(true)),
.just(.setErrorAlert(true))
)
}

return owner.uploadImageUseCase.execute(to: presingedURL, from: imageData)
return owner.uploadImageUseCase.execute(remoteURL, image: imageData)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.asObservable()
.flatMap { isSuccess -> Observable<CameraViewReactor.Mutation> in

if owner.memberId.isEmpty {
Expand All @@ -316,12 +316,11 @@ extension CameraViewReactor {
)
}

let originalURL = owner.configureProfileOriginalS3URL(url: presingedURL, with: .profile)
let profileImageEditParameter = ProfileImageEditParameter(profileImageUrl: originalURL)
let originalURL = owner.configureProfileOriginalS3URL(url: remoteURL, with: .profile)
let body = UpdateProfileImageRequest(profileImageUrl: originalURL)

if isSuccess {
return owner.editProfileImageUseCase.execute(memberId: owner.memberId, parameter: profileImageEditParameter)
.asObservable()
return owner.editProfileImageUseCase.execute(memberId: owner.memberId, body: body)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.flatMap { editEntity -> Observable<CameraViewReactor.Mutation> in

Expand All @@ -346,33 +345,33 @@ extension CameraViewReactor {

)
case .realEmoji:
let realEmojiImage = "\(imageData.hashValue).jpg"
let realEmojiParameter = CameraRealEmojiParameters(imageName: realEmojiImage)
guard let memberId = fetchMyMemberIdUseCase.execute() else {
return .just(.setErrorAlert(true))
}

let realEmojiImage = "\(imageData.hashValue).jpg"
let body = CreatePresignedURLRequest(imageName: realEmojiImage)
if currentState.realEmojiEntity[currentState.emojiType.rawValue - 1] == nil {
return .concat(
.just(.setLoading(false)),
fetchRealEmojiPreSignedUseCase.execute(memberId: memberId, parameter: realEmojiParameter)
.asObservable()
fetchRealEmojiPreSignedUseCase.execute(memberId: memberId, body: body)
.withUnretained(self)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.flatMap { owner, entity -> Observable<CameraViewReactor.Mutation> in
guard let presingedURL = entity?.imageURL else { return .just(.setErrorAlert(true))}
guard let remoteURL = entity?.imageURL else { return .just(.setErrorAlert(true))}

return owner.uploadImageUseCase.execute(to: presingedURL, from: imageData)
return owner.uploadImageUseCase.execute(remoteURL, image: imageData)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.asObservable()
.flatMap { isSuccess -> Observable<CameraViewReactor.Mutation> in
let originalURL = owner.configureProfileOriginalS3URL(url: presingedURL, with: .realEmoji)
let realEmojiCreateParameter = CameraCreateRealEmojiParameters(type: owner.currentState.emojiType.emojiString, imageUrl: originalURL)
let originalURL = owner.configureProfileOriginalS3URL(url: remoteURL, with: .realEmoji)
let body = CreateEmojiImageRequest(type: owner.currentState.emojiType.emojiString, imageUrl: originalURL)
if isSuccess {
return owner.fetchRealEmojiCreateUseCase.execute(memberId: owner.memberId, parameter: realEmojiCreateParameter)
.asObservable()
return owner.fetchRealEmojiCreateUseCase.execute(memberId: memberId, body: body)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.flatMap { realEmojiEntity -> Observable<CameraViewReactor.Mutation> in
guard let createRealEmojiEntity = realEmojiEntity else { return .just(.setErrorAlert(true))}
owner.provider.realEmojiGlobalState.createRealEmojiImage(indexPath: owner.currentState.emojiType.rawValue - 1, image: createRealEmojiEntity.realEmojiImageURL, emojiType: createRealEmojiEntity.realEmojiType)
return owner.fetchRealEmojiListUseCase.execute()
return owner.fetchRealEmojiListUseCase.execute(memberId: memberId)
.asObservable()
.flatMap { reloadEntity -> Observable<CameraViewReactor.Mutation> in
return .concat(
Expand All @@ -397,24 +396,23 @@ extension CameraViewReactor {
)
} else {
let realEmojiImage = "\(imageData.hashValue).jpg"
let realEmojiParameter = CameraRealEmojiParameters(imageName: realEmojiImage)
let body = CreatePresignedURLRequest(imageName: realEmojiImage)
return .concat(
.just(.setLoading(false)),
fetchRealEmojiPreSignedUseCase.execute(memberId: memberId, parameter: realEmojiParameter)
.asObservable()
fetchRealEmojiPreSignedUseCase.execute(memberId: memberId, body: body)
.withUnretained(self)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.flatMap { owner, entity -> Observable<CameraViewReactor.Mutation> in
guard let presingedURL = entity?.imageURL else { return .just(.setErrorAlert(true))}
let originalURL = owner.configureProfileOriginalS3URL(url: presingedURL, with: .realEmoji)
let updateRealEmojiParameter = CameraUpdateRealEmojiParameters(imageUrl: originalURL)
return owner.uploadImageUseCase.execute(to: presingedURL, from: imageData)
guard let remoteURL = entity?.imageURL else { return .just(.setErrorAlert(true))}
let originalURL = owner.configureProfileOriginalS3URL(url: remoteURL, with: .realEmoji)

return owner.uploadImageUseCase.execute(remoteURL, image: imageData)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.asObservable()
.flatMap { isSuccess -> Observable<CameraViewReactor.Mutation> in
if isSuccess {
return owner.fetchRealEmojiUpdateUseCase.execute(memberId: owner.memberId, emojiId: owner.currentState.realEmojiEntity[owner.currentState.emojiType.rawValue - 1]?.realEmojiId ?? "", parameter: updateRealEmojiParameter)
.asObservable()
let body = UpdateRealEmojiImageRequest(imageUrl: originalURL)
return owner.fetchRealEmojiUpdateUseCase.execute(memberId: memberId, realEmojiId: owner.currentState.realEmojiEntity[owner.currentState.emojiType.rawValue - 1]?.realEmojiId ?? "", body: body)
.flatMap { updateRealEmojiEntity -> Observable<CameraViewReactor.Mutation> in
guard let updateEntity = updateRealEmojiEntity else { return .just(.setErrorAlert(true))}
owner.provider.realEmojiGlobalState.updateRealEmojiImage(indexPath: owner.currentState.emojiType.rawValue - 1, image: updateEntity.realEmojiImageURL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,30 +84,27 @@ public final class ProfileViewReactor: Reactor {


public func mutate(action: Action) -> Observable<Mutation> {
//TODO: Keychain, UserDefaults 추가
switch action {
case .viewDidLoad:
return fetchMembersProfileUseCase.execute(memberId: currentState.memberId)
.asObservable()
.flatMap { entity -> Observable<ProfileViewReactor.Mutation> in
.just(.setProfileMemberItems(entity))
}
case let .updateNickNameProfile(nickNameFileData):
let nickNameProfileImage: String = "\(nickNameFileData.hashValue).jpg"
let nickNameImageEditParameter: CameraDisplayImageParameters = CameraDisplayImageParameters(imageName: nickNameProfileImage)
case let .updateNickNameProfile(imageData):
let profileImage = "\(imageData.hashValue).jpg"
let createPresignedURL = CreatePresignedURLRequest(imageName: profileImage)
return .concat(
.just(.setLoading(false)),
createProfilePresignedUseCase.execute(parameter: nickNameImageEditParameter)
.asObservable()
createProfilePresignedUseCase.execute(body: createPresignedURL)
.withUnretained(self)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.flatMap { owner, entity -> Observable<ProfileViewReactor.Mutation> in
guard let profilePresingedURL = entity?.imageURL else { return .empty() }
return owner.uploadProfileImageUseCase.execute(to: profilePresingedURL, from: nickNameFileData)
guard let remoteURL = entity?.imageURL else { return .empty() }
return owner.uploadProfileImageUseCase.execute(remoteURL, image: imageData)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.asObservable()
.flatMap { isSuccess -> Observable<ProfileViewReactor.Mutation> in
let originalPath = owner.configureProfileOriginalS3URL(url: profilePresingedURL)
let originalPath = owner.configureProfileOriginalS3URL(url: remoteURL)
let profileEditParameter: ProfileImageEditParameter = ProfileImageEditParameter(profileImageUrl: originalPath)
if isSuccess {
return owner.updateProfileUseCase.execute(memberId: owner.currentState.memberId, parameter: profileEditParameter)
Expand Down Expand Up @@ -140,22 +137,20 @@ public final class ProfileViewReactor: Reactor {
)
}

case let .didSelectPHAssetsImage(fileData):
let profileImage: String = "\(fileData.hashValue).jpg"
let profileImageEditParameter: CameraDisplayImageParameters = CameraDisplayImageParameters(imageName: profileImage)
case let .didSelectPHAssetsImage(assetImage):
let imageName: String = "\(assetImage.hashValue).jpg"
let createPresignedURL = CreatePresignedURLRequest(imageName: imageName)
return .concat(
.just(.setLoading(false)),
createProfilePresignedUseCase.execute(parameter: profileImageEditParameter)
.asObservable()
createProfilePresignedUseCase.execute(body: createPresignedURL)
.withUnretained(self)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.flatMap { owner, entity -> Observable<ProfileViewReactor.Mutation> in
guard let profilePresingedURL = entity?.imageURL else { return .empty() }
return owner.uploadProfileImageUseCase.execute(to: profilePresingedURL, from: fileData)
guard let remoteURL = entity?.imageURL else { return .empty() }
return owner.uploadProfileImageUseCase.execute(remoteURL, image: assetImage)
.subscribe(on: ConcurrentDispatchQueueScheduler.init(qos: .background))
.asObservable()
.flatMap { isSuccess -> Observable<ProfileViewReactor.Mutation> in
let originalPath = owner.configureProfileOriginalS3URL(url: profilePresingedURL)
let originalPath = owner.configureProfileOriginalS3URL(url: remoteURL)
let profileEditParameter: ProfileImageEditParameter = ProfileImageEditParameter(profileImageUrl: originalPath)
if isSuccess {
return owner.updateProfileUseCase.execute(memberId: owner.currentState.memberId, parameter: profileEditParameter)
Expand Down
Loading
Loading