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

Feature/reaction 한번에 모아서 호출 #80

Merged
merged 5 commits into from
Oct 1, 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
24 changes: 24 additions & 0 deletions Projects/Core/PPACData/Sources/DTO/MemeReactionRequestDTO.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// MemeReactionRequestDTO.swift
// PPACData
//
// Created by kimchansoo on 10/1/24.
//

import Foundation

public struct MemeReactionRequestDTO: Codable {
public let count: Int

public init(count: Int) {
self.count = count
}
}

public struct MemeReactionResponseDTO: Codable {
public let count: Int

public init(count: Int) {
self.count = count
}
}
10 changes: 5 additions & 5 deletions Projects/Core/PPACData/Sources/Endpoint/MemeEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public enum MemeEndpoint: Requestable {
case deleteBookmark(memeId: String)
case share(memeId: String)
case watch(memeId: String, type: String)
case reaction(memeId: String)
case reaction(memeId: String, count: Int)

public var httpMethod: PPACNetwork.HTTPMethod {
switch self {
Expand Down Expand Up @@ -61,8 +61,8 @@ public enum MemeEndpoint: Requestable {
return "/meme/\(memeId)/share"
case .watch(let memeId, let type):
return "/meme/\(memeId)/watch/\(type)"
case .reaction(let memeId):
return "/meme/\(memeId)/reaction"
case .reaction(let memeId, _):
return "meme/\(memeId)/reaction"
}
}

Expand All @@ -85,8 +85,8 @@ public enum MemeEndpoint: Requestable {
return nil
case .watch:
return nil
case .reaction:
return nil
case let .reaction(memeId, count):
return .body(MemeReactionRequestDTO(count: count))
case .meme(memeId: _):
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,12 @@ public class MemeRepositoryImpl: MemeRepository {
}
}

public func reactToMeme(memeId: String) async throws {
let endpoint = MemeEndpoint.reaction(memeId: memeId)
let result = await networkservice.request(endpoint, dataType: BaseDTO<VoidResponse>.self)
public func reactToMeme(memeId: String, count: Int) async throws -> Int {
let endpoint = MemeEndpoint.reaction(memeId: memeId, count: count)
let result = await networkservice.request(endpoint, dataType: BaseDTO<MemeReactionResponseDTO>.self)
switch result {
case .success:
return
case .success(let count):
return count.data?.count ?? 0
case .failure(let failure):
throw failure
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public protocol MemeRepository {
func deleteBookmarkMeme(memeId: String) async throws
func shareMeme(memeId: String) async throws
func watchMeme(memeId: String, type: String) async throws
func reactToMeme(memeId: String) async throws
func reactToMeme(memeId: String, count: Int) async throws -> Int

func registerMeme(formData: FormData, title: String, source: String, keywordIds: [String]) async throws
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import Foundation
import PPACModels

public protocol ReactToMemeUseCase {
func execute(memeId: String) async throws
func execute(memeId: String, count: Int) async throws -> Int
}

public class ReactToMemeUseCaseImpl: ReactToMemeUseCase {
Expand All @@ -20,7 +20,7 @@ public class ReactToMemeUseCaseImpl: ReactToMemeUseCase {
self.repository = repository
}

public func execute(memeId: String) async throws {
try await repository.reactToMeme(memeId: memeId)
public func execute(memeId: String, count: Int) async throws -> Int {
try await repository.reactToMeme(memeId: memeId, count: count)
}
}
55 changes: 45 additions & 10 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,19 +114,50 @@ public final class MemeDetailViewModel: ViewModelType, ObservableObject {
}

private extension MemeDetailViewModel {

@MainActor
func postReaction() async {
do {
try await reactToMemeUseCase.execute(memeId: state.meme.id)
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
Loading