Skip to content

Commit

Permalink
[Feat/#118] UserService demo 앱 기능 개발
Browse files Browse the repository at this point in the history
  • Loading branch information
HELLOHIDI committed Nov 23, 2024
1 parent 4fe8b8c commit 3bc5898
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ fileprivate struct ServiceCellView: View {

Button {
viewModel.send(action: .serviceButtonDidTap(index))
} label:
} label: {
Text("Test")
.foregroundColor(.white)
.font(.title)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,3 @@ class ChallengeServiceViewModel: ObservableObject {
}
}
}



Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
//
// UserServiceView.swift
// NetworksDemo
//
// Created by 류희재 on 11/23/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import SwiftUI
import Networks
import Core
import Combine

struct UserServiceView: View {

@StateObject var viewModel: UserServiceViewModel

var body: some View {
VStack {
HStack {
Button {
print("뒤로 가기")
} label: {
Image(systemName: "chevron.backward")
.resizable()
.foregroundColor(.white)
.frame(width: 12, height: 24)
}
.padding(.leading, 14)

Spacer()

Text("UserService")
.foregroundStyle(Color(
red: 165 / 255.0,
green: 165 / 255.0,
blue: 187 / 255.0
))
.font(.title)
.bold()
.multilineTextAlignment(.center)

Spacer()
}
.padding(.vertical, 18)

ServiceListView(viewModel: viewModel)
.padding(.horizontal, 20)
.padding(.bottom, 25)
.frame(height: 360)

Rectangle()
.foregroundColor(.white)
.frame(width: .infinity, height: 3)
.padding(.horizontal, 20)
.padding(.bottom, 25)

Text(viewModel.state.networkLoggingText)
.padding(.top, 25)
.padding(.horizontal, 20)
.frame(width: 335, height: 240)
.background(.gray)

Spacer()
}
.background(Color(asset: NetworksDemoAsset.blackground))
}
}

fileprivate struct ServiceListView: View {
private let viewModel: UserServiceViewModel

init(viewModel: UserServiceViewModel) {
self.viewModel = viewModel
}

let apiList: [String] = [
"Logout",
"DeleteAccount",
"GetUserData",
"GetCurrentPoint"
]
var body: some View {
VStack {
Spacer()
.frame(height: 24)

ScrollView(.vertical, showsIndicators: true) {
ForEach(Array(apiList.enumerated()), id: \.element) { index, api in
ServiceCellView(apiTitle: api, index: index, viewModel: viewModel)
Rectangle()
.foregroundColor(.gray)
.frame(width: .infinity, height: 1)
}
}
}
}
}

fileprivate struct ServiceCellView: View {
private let apiTitle: String
private let index: Int
@ObservedObject private var viewModel: UserServiceViewModel

init(apiTitle: String, index: Int, viewModel: UserServiceViewModel) {
self.apiTitle = apiTitle
self.index = index
self.viewModel = viewModel
}


var body: some View {
HStack {
VStack(alignment: .leading) {
Text(apiTitle)
.foregroundStyle(Color(
red: 219 / 255.0,
green: 218 / 255.0,
blue: 231 / 255.0
))
.font(.title2)
.bold()
.frame(height: 2)
.padding(.bottom, 10)

Text("테스트 결과: \(viewModel.state.resultText[index])")
.foregroundColor(.white)
.font(.body)
.bold()
}
.padding(.vertical, 18)

Spacer()

Button {
viewModel.send(action: .serviceButtonDidTap(index))
} label: {
Text("Test")
.foregroundColor(.white)
.font(.title)
.bold()
.padding(.horizontal, 16)
.padding(.vertical, 8)
.background(Color(
red: 70 / 255.0,
green: 30 / 255.0,
blue: 229 / 255.0
))
.clipShape(RoundedRectangle(cornerRadius: 4))
}
}
.frame(height: 80)
}
}

#Preview {
return UserServiceView(
viewModel: UserServiceViewModel(
service: UserService()
)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
//
// UserServiceViewModel.swift
// NetworksDemo
//
// Created by 류희재 on 11/23/24.
// Copyright © 2024 HMH-iOS. All rights reserved.
//

import Foundation
import Combine
import Core
import Networks

class UserServiceViewModel: ObservableObject {

//MARK: Action, State

enum Action {
case serviceButtonDidTap(Int)
}

struct State {
var resultText: [String] = Array(repeating: "", count: 4)
var networkLoggingText: String = "네트워크 결과창입니다!"
}

//MARK: Dependency

let service: UserServiceType

// MARK: - Init
init(service: UserServiceType) {
self.service = service
self.state = State()
}

//MARK: Properties

@Published private(set) var state: State
private let cancelBag = CancelBag()

//MARK: Methods

func send(action: Action) {
switch action {
case .serviceButtonDidTap(let index):
switch index {
case 0:
service.logout()
.sink(receiveCompletion: { [weak self] completion in
if case let .failure(error) = completion {
self?.state.networkLoggingText = error.description
self?.state.resultText[index] = "❌ 실패"
}
}) { [weak self] result in
self?.state.networkLoggingText = "\(result)"
self?.state.resultText[index] = "✅ 성공"
}
.store(in: cancelBag)
case 1:
service.deleteAccount()
.sink(receiveCompletion: { [weak self] completion in
if case let .failure(error) = completion {
self?.state.networkLoggingText = error.description
self?.state.resultText[index] = "❌ 실패"
}
}) { [weak self] result in
self?.state.networkLoggingText = "\(result)"
self?.state.resultText[index] = "✅ 성공"
}
.store(in: cancelBag)
case 2:
service.getUserData()
.sink(receiveCompletion: { [weak self] completion in
if case let .failure(error) = completion {
self?.state.networkLoggingText = error.description
self?.state.resultText[index] = "❌ 실패"
}
}) { [weak self] result in
self?.state.networkLoggingText = "\(result)"
self?.state.resultText[index] = "✅ 성공"
}
.store(in: cancelBag)
case 3:
service.getCurrentPoint()
.sink(receiveCompletion: { [weak self] completion in
if case let .failure(error) = completion {
self?.state.networkLoggingText = error.description
self?.state.resultText[index] = "❌ 실패"
}
}) { [weak self] result in
self?.state.networkLoggingText = "\(result)"
self?.state.resultText[index] = "✅ 성공"
}
.store(in: cancelBag)
default:
break
}
}
}
}

0 comments on commit 3bc5898

Please sign in to comment.