Skip to content

Commit

Permalink
Merge branch 'juri' into refactor/#146-reportNavigationMVI
Browse files Browse the repository at this point in the history
  • Loading branch information
juri123123 committed Jan 30, 2025
2 parents 5cd1ba6 + f5861b4 commit 4b9073e
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ enum DetailTargetType {
case scoopReview(userId: Int, postId: Int)
case scrapReview(userId: Int, postId: Int)
case unScrapReview(userId: Int, postId: Int)
case getUserInfo(userId: Int)
}

extension DetailTargetType: TargetType {
Expand All @@ -34,12 +35,14 @@ extension DetailTargetType: TargetType {
return "/post/zzim"
case .unScrapReview(let userId, let postId):
return "/post/zzim/\(userId)/\(postId)"
case .getUserInfo(let userId):
return "/user/\(userId)"
}
}

var method: Moya.Method {
switch self {
case .getDetailReview:
case .getDetailReview, .getUserInfo:
return .get
case .scoopReview,
.scrapReview:
Expand All @@ -51,7 +54,7 @@ extension DetailTargetType: TargetType {

var task: Moya.Task {
switch self {
case .getDetailReview:
case .getDetailReview, .getUserInfo:
return .requestPlain
case .scoopReview(let userId, let postId), .scrapReview(let userId, let postId):
return .requestParameters(
Expand Down
4 changes: 0 additions & 4 deletions Spoony-iOS/Spoony-iOS/Resource/Helper/ToastModifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,3 @@ struct ToastModifier: ViewModifier {
workItem = nil
}
}

#Preview {
ContentView()
}
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,26 @@ public class DetailService {
}
}
}

func getUserInfo(userId: Int) async throws -> UserInfoModel {
return try await withCheckedThrowingContinuation { continuation in
detailProvider.request(.getUserInfo(userId: userId)) { result in
switch result {
case .success(let response):
do {
let result = try response.map(BaseResponse<UserInfoModel>.self)
if let result = result.data {
return continuation.resume(returning: result)
} else {
print("옵셔널 바인딩 X")
}
} catch {
continuation.resume(throwing: error)
}
case .failure(let error):
continuation.resume(throwing: error)
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ struct DetailState {
var toast: Toast?

var successService: Bool = true
var userInfo: UserInfoModel = .init(userId: 30, userEmail: "", userName: "", userImageUrl: "", regionName: "", createdAt: "", updatedAt: "")
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ struct DetailView: View {
@StateObject private var store: DetailViewStore = DetailViewStore()
let postId: Int

init( postId: Int) {
init(postId: Int) {
self.postId = postId
}

Expand Down Expand Up @@ -104,18 +104,17 @@ extension DetailView {
private var userProfileSection: some View {
HStack(alignment: .center, spacing: 14.adjustedH) {

Image(.imageThingjin)
.resizable()
RemoteImageView(urlString: store.state.userInfo.userImageUrl)
.scaledToFit()
.clipShape(Circle())
.frame(width: 48.adjusted, height: 48.adjustedH)

VStack(alignment: .leading, spacing: 4.adjustedH) {
Text(store.state.userName)
Text(store.state.userInfo.userName)
.customFont(.body2b)
.foregroundStyle(.black)

Text("서울시 성동구 수저")
Text(store.state.userInfo.regionName)
.customFont(.caption1m)
.foregroundStyle(.gray400)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,22 @@ import UIKit
final class DetailViewStore: ObservableObject {
@Published private(set) var state: DetailState = DetailState()

private let service = DetailService()
private let detailService = DetailService()
private let homeService = DefaultHomeService()
private var userSpoonCount: Int = 0
private var userInfo: UserInfoModel = .init(userId: 30, userEmail: "", userName: "", userImageUrl: "", regionName: "", createdAt: "", updatedAt: "")
// MARK: - Reducer

@MainActor
func send(intent: DetailIntent) {
switch intent {
case .getInitialValue(let userId, let postId):
case .getInitialValue(_, let postId):
Task {
do {
let data = try await DefaultHomeService().fetchSpoonCount(userId: Config.userId)
let data = try await homeService.fetchSpoonCount(userId: Config.userId)
let userData = try await detailService.getUserInfo(userId: Config.userId)
self.userSpoonCount = data
self.userInfo = userData
state.successService = true

await fetchInitialData(userId: Config.userId, postId: postId)
Expand Down Expand Up @@ -50,7 +54,7 @@ final class DetailViewStore: ObservableObject {
private func fetchInitialData(userId: Int, postId: Int) async {
state.isLoading = true
do {
let data = try await service.getReviewDetail(userId: userId, postId: postId)
let data = try await detailService.getReviewDetail(userId: userId, postId: postId)
updateState(with: data)
} catch {
state.toast = Toast(style: .gray, message: "데이터를 불러오는데 실패했습니다.", yOffset: 539.adjustedH)
Expand Down Expand Up @@ -81,14 +85,15 @@ final class DetailViewStore: ObservableObject {
categoryColorResponse: data.categoryColorResponse,
isMine: data.isMine,
spoonCount: self.userSpoonCount,
successService: true
successService: true,
userInfo: self.userInfo
)
}

// 스크랩 버튼 처리
private func handleScrapButton(isScrap: Bool) {
if isScrap {
service.unScrapReview(userId: state.userId, postId: state.postId)
detailService.unScrapReview(userId: state.userId, postId: state.postId)
state.zzimCount -= 1
state.isZzim = false
state.toast = Toast(
Expand All @@ -97,7 +102,7 @@ final class DetailViewStore: ObservableObject {
yOffset: 539.adjustedH
)
} else {
service.scrapReview(userId: state.userId, postId: state.postId)
detailService.scrapReview(userId: state.userId, postId: state.postId)
state.zzimCount += 1
state.isZzim = true
state.toast = Toast(
Expand All @@ -113,7 +118,7 @@ final class DetailViewStore: ObservableObject {
private func handleScoopButton() async throws {

do {
let data = try await service.scoopReview(userId: Config.userId, postId: state.postId)
let data = try await detailService.scoopReview(userId: Config.userId, postId: state.postId)

if data {
state.isScoop.toggle()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// UserInfoModel.swift
// Spoony-iOS
//
// Created by 이명진 on 1/28/25.
//

import Foundation

// MARK: - UserData

struct UserInfoModel: Codable {
let userId: Int
let userEmail: String
let userName: String
let userImageUrl: String
let regionName: String
let createdAt: String
let updatedAt: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

import Foundation


// MARK: - ReviewDetailModel

struct ReviewDetailModel: Codable {
Expand Down

0 comments on commit 4b9073e

Please sign in to comment.