From 038a6a1357c3fd35c89d027f9f2a87c59eb3447b Mon Sep 17 00:00:00 2001 From: hhhello Date: Fri, 4 Oct 2024 13:18:21 +0900 Subject: [PATCH] Fix user & fetch user info --- .../graduating/graduatingserver/api/user/dto/UserRes.kt | 2 +- .../graduating/graduatingserver/core/user/User.kt | 2 +- Graduating-iOS/Data/Network/UserService.swift | 4 ++-- Graduating-iOS/Graduating/Feature/Main/MainView.swift | 1 + .../Graduating/Feature/Observable/AppState.swift | 8 ++++++++ Graduating-iOS/Model/User/User.swift | 4 ++-- 6 files changed, 15 insertions(+), 6 deletions(-) diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/user/dto/UserRes.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/user/dto/UserRes.kt index 16225a3..0f65a71 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/user/dto/UserRes.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/api/user/dto/UserRes.kt @@ -4,7 +4,7 @@ import com.bestswlkh0310.graduating.graduatingserver.core.user.User data class UserRes( val username: String, - val nickname: String, + val nickname: String?, ) { companion object { fun of(user: User) = UserRes( diff --git a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/user/User.kt b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/user/User.kt index 0b99a22..7dc20ea 100644 --- a/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/user/User.kt +++ b/Graduating-Server/src/main/kotlin/com/bestswlkh0310/graduating/graduatingserver/core/user/User.kt @@ -6,7 +6,7 @@ import jakarta.persistence.* class User( id: Long = 0, username: String, - nickname: String, + nickname: String?, role: UserRole = UserRole.USER, platformType: PlatformType ) { diff --git a/Graduating-iOS/Data/Network/UserService.swift b/Graduating-iOS/Data/Network/UserService.swift index f57811d..525a5c7 100644 --- a/Graduating-iOS/Data/Network/UserService.swift +++ b/Graduating-iOS/Data/Network/UserService.swift @@ -34,11 +34,11 @@ public class UserService { public static let shared = UserService() let netRunnner = DefaultNetRunner() - func getMe() -> AnyPublisher { + public func getMe() -> AnyPublisher { netRunnner.deepDive(.getMe, res: User.self) } - func editUser(_ req: EditUserReq) -> AnyPublisher { + public func editUser(_ req: EditUserReq) -> AnyPublisher { netRunnner.deepDive(.editUser(req), res: VoidDTO.self) } } diff --git a/Graduating-iOS/Graduating/Feature/Main/MainView.swift b/Graduating-iOS/Graduating/Feature/Main/MainView.swift index 282d2f2..19d0e05 100644 --- a/Graduating-iOS/Graduating/Feature/Main/MainView.swift +++ b/Graduating-iOS/Graduating/Feature/Main/MainView.swift @@ -61,6 +61,7 @@ struct MainView: View { fetchMeals() fetchGraduating() selectedTab = data[0] + appState.fetchCurrentUser() } .onChange(of: appState.graduating) { selectedTab = .home diff --git a/Graduating-iOS/Graduating/Feature/Observable/AppState.swift b/Graduating-iOS/Graduating/Feature/Observable/AppState.swift index b99aba1..c48e375 100644 --- a/Graduating-iOS/Graduating/Feature/Observable/AppState.swift +++ b/Graduating-iOS/Graduating/Feature/Observable/AppState.swift @@ -46,6 +46,7 @@ final class AppState: BaseViewModel { } } @Published var graduatingFetchFailure = false + @Published var currentUser: User? private var observer: NSKeyValueObservation? override init() { @@ -67,4 +68,11 @@ final class AppState: BaseViewModel { } .store(in: &subscriptions) } + + func fetchCurrentUser() { + UserService.shared.getMe() + .ignoreError() + .assign(to: \.currentUser, on: self) + .store(in: &subscriptions) + } } diff --git a/Graduating-iOS/Model/User/User.swift b/Graduating-iOS/Model/User/User.swift index 32ba271..49f4dd6 100644 --- a/Graduating-iOS/Model/User/User.swift +++ b/Graduating-iOS/Model/User/User.swift @@ -9,9 +9,9 @@ import Foundation public struct User: ModelProtocol { public let username: String - public let nickname: String + public let nickname: String? - public init(username: String, nickname: String) { + public init(username: String, nickname: String?) { self.username = username self.nickname = nickname }