Skip to content

Commit

Permalink
Merge pull request #157 from LifePoop/153-cheering_fix
Browse files Browse the repository at this point in the history
�힘주기 요청 후 버튼 비활성화 처리 및 홈화면 헤더 상태 변경
  • Loading branch information
sanghyeok-kim authored Nov 6, 2023
2 parents 0a04a61 + bd5b2c6 commit 0e7733a
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 23 deletions.
4 changes: 3 additions & 1 deletion Projects/Core/CoreEntity/Sources/UserProfileEntity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ import Foundation

public struct UserProfileEntity {

public init(nickname: String, profileCharacter: ProfileCharacter) {
public init(userId: Int, nickname: String, profileCharacter: ProfileCharacter) {
self.userId = userId
self.nickname = nickname
self.profileCharacter = profileCharacter
}

public let userId: Int
public let nickname: String
public let profileCharacter: ProfileCharacter
}
11 changes: 11 additions & 0 deletions Projects/Core/CoreNetworkService/Sources/DTO/UserProfileDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,27 @@
import Foundation

public struct UserProfileDTO: Codable {
let userId: Int
let nickname: String
let characterColor, characterShape: Int
let isCheered: Bool?

enum CodingKeys: String, CodingKey {
case userId = "id"
case nickname
case characterColor
case characterShape
case isCheered
}

public init(
userId: Int,
nickname: String,
characterColor: Int,
characterShape: Int,
isCheered: Bool? = nil
) {
self.userId = userId
self.nickname = nickname
self.characterColor = characterColor
self.characterShape = characterShape
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public struct CheeringInfoEntityMapper: DataMapper {
}

return UserProfileEntity(
userId: $0.userId,
nickname: $0.nickname,
profileCharacter: ProfileCharacter(
color: color,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ public struct StoryFeedEntityMapper: DataMapper {
}
let nickname = dto.user.nickname
return StoryFeedEntity(
user: UserProfileEntity(nickname: nickname, profileCharacter: ProfileCharacter(
color: color,
shape: shape
user: UserProfileEntity(
userId: dto.user.userId,
nickname: nickname,
profileCharacter: ProfileCharacter(
color: color,
shape: shape
)),
stories: try dto.stories.map {
guard let color = StoolColor(rawValue: $0.color),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public struct UserProfileDTOMapper: DataMapper {

public func transform(_ entity: UserProfileEntity) throws -> UserProfileDTO {
return UserProfileDTO(
userId: entity.userId,
nickname: entity.nickname,
characterColor: entity.profileCharacter.color.rawValue,
characterShape: entity.profileCharacter.shape.rawValue
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,13 @@ public final class DefaultHomeCoordinator: HomeCoordinator {
self?.startSettingCoordinatorFlow()
case .reportButtonDidTap:
self?.pushReportViewController()
case .storyFeedButtonDidTap(let stories, let isCheered):
self?.presentFriendStoolStoryViewController(stories: stories, isCheered: isCheered, animated: true)
case .storyFeedButtonDidTap(let friendUserId, let stories, let isCheered):
self?.presentFriendStoolStoryViewController(
friendUserId: friendUserId,
stories: stories,
isCheered: isCheered,
animated: true
)
case .storyCloseButtonDidTap:
self?.dismissFriendStoolStoryViewController(animated: true)
}
Expand All @@ -83,11 +88,17 @@ private extension DefaultHomeCoordinator {
}

func presentFriendStoolStoryViewController(
friendUserId: Int,
stories: [StoryEntity],
isCheered: Bool,
animated: Bool
) {
let viewModel = FriendStoolStoryViewModel(coordinator: self, stories: stories, isCheered: isCheered)
let viewModel = FriendStoolStoryViewModel(
coordinator: self,
friendUserId: friendUserId,
stories: stories,
isCheered: isCheered
)
let viewController = FriendStoolStoryViewController()
viewController.bind(viewModel: viewModel)
viewController.modalPresentationStyle = .overFullScreen
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public enum HomeCoordinateAction {
case stoolLogButtonDidTap(stoolLogsRelay: BehaviorRelay<[StoolLogEntity]>)
case settingButtonDidTap
case reportButtonDidTap
case storyFeedButtonDidTap(stories: [StoryEntity], isCheered: Bool)
case storyFeedButtonDidTap(friendUserId: Int, stories: [StoryEntity], isCheered: Bool)
case storyCloseButtonDidTap
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,16 @@ public final class FriendStoolStoryViewModel: ViewModelType {

private var disposeBag = DisposeBag()

public init(coordinator: HomeCoordinator?, stories: [StoryEntity], isCheered: Bool) {
bind(coordinator: coordinator, stories: stories, isCheered: isCheered)
public init(
coordinator: HomeCoordinator?,
friendUserId: Int,
stories: [StoryEntity],
isCheered: Bool
) {
bind(coordinator: coordinator, friendUserId: friendUserId, stories: stories, isCheered: isCheered)
}

private func bind(coordinator: HomeCoordinator?, stories: [StoryEntity], isCheered: Bool) {
private func bind(coordinator: HomeCoordinator?, friendUserId: Int, stories: [StoryEntity], isCheered: Bool) {

Observable.just(isCheered)
.withUnretained(self)
Expand Down Expand Up @@ -147,14 +152,12 @@ public final class FriendStoolStoryViewModel: ViewModelType {
.disposed(by: disposeBag)

input.cheeringButtonDidTap
.withLatestFrom(output.updateShownStory)
.map { $0.id }
.withUnretained(self)
.do(onNext: { `self`, _ in
self.output.showLoadingIndicator.accept(true)
})
.flatMapLatest { `self`, id in
self.cheeringInfoUseCase.requestCheering(withIdOf: id)
.flatMapLatest { `self`, _ in
self.cheeringInfoUseCase.requestCheering(withIdOf: friendUserId)
}
.withUnretained(self)
.do(onNext: { `self`, _ in
Expand All @@ -170,7 +173,10 @@ public final class FriendStoolStoryViewModel: ViewModelType {
let cheeringButtonText = isSuccess ? LocalizableString.doneBoost
: LocalizableString.boost
self.output.updateCheeringButtonText.accept(cheeringButtonText)


if isSuccess {
NotificationCenter.default.post(name: .updateCheering, object: friendUserId)
}
})
.disposed(by: disposeBag)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,13 @@ public final class StoolLogHeaderViewModel: ViewModelType {
.map { $0.item }
.withLatestFrom(state.storyFeeds) { $1[$0] }
.bind(onNext: { storyFeed in

coordinator?.coordinate(
by: .storyFeedButtonDidTap(stories: storyFeed.stories, isCheered: storyFeed.isCheered)
by: .storyFeedButtonDidTap(
friendUserId: storyFeed.user.userId,
stories: storyFeed.stories,
isCheered: storyFeed.isCheered
)
)
})
.disposed(by: disposeBag)
Expand All @@ -135,6 +140,33 @@ public final class StoolLogHeaderViewModel: ViewModelType {
)
.bind { coordinator?.coordinate(by: .cheeringButtonDidTap) }
.disposed(by: disposeBag)

NotificationCenter.default.rx.notification(.updateCheering)
.compactMap { $0.object as? Int }
.withLatestFrom(state.storyFeeds) {
(storyFeeds: $1, updatedFriendUserId: $0)
}
.map { storyFeeds, updatedFriendUserId in
guard let targetStoryFeed = storyFeeds.first(
where: { $0.user.userId == updatedFriendUserId }
) else {
return storyFeeds
}

var updatedStoryFeeds = storyFeeds
let updatedStoryFeed = StoryFeedEntity(
user: targetStoryFeed.user,
stories: targetStoryFeed.stories,
isCheered: true
)
if let targetIndex = storyFeeds.firstIndex(of: targetStoryFeed) {
updatedStoryFeeds[targetIndex] = updatedStoryFeed
}

return updatedStoryFeeds
}
.bind(to: state.storyFeeds)
.disposed(by: disposeBag)
}

deinit {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public final class DefaultProfileEditUseCase: ProfileEditUseCase {

@Inject(SharedDIContainer.shared) private var keyChainRepository: KeyChainRepository
@Inject(SharedDIContainer.shared) private var userDefaultsRepository: UserDefaultsRepository
@Inject(SharedDIContainer.shared) private var userInfoUseCase: UserInfoUseCase
@Inject(SharedDIContainer.shared) private var nicknameUseCase: NicknameUseCase
@Inject(SharedDIContainer.shared) private var profileCharacterUseCase: ProfileCharacterUseCase
@Inject(SharedDIContainer.shared) private var userProfileEditUseCase: UserProfileEditUseCase
Expand All @@ -38,13 +39,21 @@ public final class DefaultProfileEditUseCase: ProfileEditUseCase {
}

public func updateProfileInfo(newProfileCharacter: ProfileCharacter, newNickname: String) -> Completable {
let newUserProfile = UserProfileEntity(
nickname: newNickname,
profileCharacter: newProfileCharacter
)

return userProfileEditUseCase
.editUserProfile(userProfileEntity: newUserProfile)
let newUserProfile = userInfoUseCase.userInfo
.compactMap { $0?.userId }
.map { userId in
UserProfileEntity(
userId: userId,
nickname: newNickname,
profileCharacter: newProfileCharacter
)
}

return newUserProfile
.withUnretained(self)
.flatMap { $0.userProfileEditUseCase.editUserProfile(userProfileEntity: $1) }
.asCompletable()
.andThen(
Completable.zip(
nicknameUseCase.updateNickname(to: newNickname),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ import Foundation

public extension NSNotification.Name {
static let resetLogin = NSNotification.Name("ResetLogin")
static let updateCheering = NSNotification.Name("CheeringFriend")
}

0 comments on commit 0e7733a

Please sign in to comment.