Skip to content

Commit

Permalink
Merge branch 'develop' into feature/#78-recommend-tab-fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jongnan committed Oct 3, 2024
2 parents e0b7825 + 6c3c73b commit ad85fe9
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Projects/Core/PPACData/Sources/DTO/MemeResponseDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct MemeResponseDTO: Decodable {
let updatedAt: String
let isSaved: Bool
let isReaction: Bool
let watch: Int
let watch: Int?

public init(
_id: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,8 @@ public class MemeRepositoryImpl: MemeRepository {
let endpoint = MemeEndpoint.reaction(memeId: memeId, count: count)
let result = await networkservice.request(endpoint, dataType: BaseDTO<MemeReactionResponseDTO>.self)
switch result {
case .success(let data):
guard let data = data.data else {
throw NetworkError.dataDecodingError
}
return data.count
case .success(let count):
return count.data?.count ?? 0
case .failure(let failure):
throw failure
}
Expand Down
20 changes: 11 additions & 9 deletions Projects/Features/MemeDetail/Sources/MemeDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,17 @@ public struct MemeDetailView: View {
tabBarTap(tab)
}
.background(
KFImage(URL(string: viewModel.state.meme.imageUrlString))
.resizable()
.loadDiskFileSynchronously()
.cacheMemoryOnly()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.clipped()
.opacity(0.4)
.edgesIgnoringSafeArea(.top)
KFImage(URL(string: viewModel.state.meme.imageUrlString))
.resizable()
.loadDiskFileSynchronously()
.cacheMemoryOnly()
.aspectRatio(contentMode: .fill)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .center)
.clipped()
.opacity(0.4) // Image Opacity: 40%
.blur(radius: 50) // Layer Blur: 50
.overlay(Color.white.opacity(0.3)) // White Dim: #fff, Opacity: 30%
.edgesIgnoringSafeArea(.top)
)
.onAppear {
viewModel.logMemeDetail(interaction: .view, event: .meme)
Expand Down
61 changes: 46 additions & 15 deletions Projects/Features/MemeDetail/Sources/MemeDetailViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public final class MemeDetailViewModel: ViewModelType, ObservableObject {
private let watchMemeUseCase: WatchMemeUseCase
private let reactToMemeUseCase: ReactToMemeUseCase

private var reactionCount = 0
private var reactionTask: Task<Void, Never>?

// MARK: - Initializers

public init(
Expand All @@ -67,7 +70,8 @@ public final class MemeDetailViewModel: ViewModelType, ObservableObject {
}

deinit {
print("memeviewmodel deinit")
print("memeviewmodel deinit")
reactionTask?.cancel()
}

// MARK: - Methods
Expand Down Expand Up @@ -110,23 +114,50 @@ public final class MemeDetailViewModel: ViewModelType, ObservableObject {
}

private extension MemeDetailViewModel {

@MainActor
func postReaction() async {
do {
let memeReactionCount = try await reactToMemeUseCase.execute(
memeId: state.meme.id,
count: 1
)

self.state.meme.reaction = memeReactionCount
func postReaction() {
reactionCount += 1
self.state.meme.reaction += 1
self.state.meme.isReaction = true
self.logMemeDetail(event: .reaction)
print("reaction success")
} catch {
// TODO: - 에러처리
print("Failed to post reaction: \(error)")
}

reactionTask?.cancel()

reactionTask = Task { [weak self] in
guard let self = self else { return }
do {
try await Task.sleep(nanoseconds: 3 * 1_000_000_000)
await self.sendReactions()
} catch {
if Task.isCancelled {
// 태스크가 취소되었으므로 아무 작업도 하지 않음
return
} else {
print("Task error: \(error)")
}
}
}
}


@MainActor
func sendReactions() async {
let count = reactionCount
guard count > 0 else {
// 전송할 리액션이 없음
return
}
reactionCount = 0
do {
let count = try await reactToMemeUseCase.execute(memeId: state.meme.id, count: count)
print("currentMeme count: \(self.state.meme.reaction)")
print("new count: \(count)")
self.state.meme.reaction = count
print("Reactions sent successfully with count: \(count)")
} catch {
print("Failed to send reactions: \(error)")
}
}

@MainActor
Expand Down

0 comments on commit ad85fe9

Please sign in to comment.