-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
240 additions
and
0 deletions.
There are no files selected for viewing
161 changes: 161 additions & 0 deletions
161
.../Projects/Modules/Networks/Demo/Sources/ServiceTest/AuthServiceTest/AuthServiceView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
// | ||
// AuthServiceView.swift | ||
// NetworksDemo | ||
// | ||
// Created by 류희재 on 11/23/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import SwiftUI | ||
import Networks | ||
import Core | ||
import Combine | ||
|
||
struct AuthServiceView: View { | ||
|
||
@StateObject var viewModel: AuthServiceViewModel | ||
|
||
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("AuthServiceTest") | ||
.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: AuthServiceViewModel | ||
|
||
init(viewModel: AuthServiceViewModel) { | ||
self.viewModel = viewModel | ||
} | ||
|
||
let apiList: [String] = [ | ||
"SignUp", | ||
"SocialLogin" | ||
] | ||
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: AuthServiceViewModel | ||
|
||
init(apiTitle: String, index: Int, viewModel: AuthServiceViewModel) { | ||
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 AuthServiceView( | ||
viewModel: AuthServiceViewModel( | ||
service: AuthService() | ||
) | ||
) | ||
} | ||
|
79 changes: 79 additions & 0 deletions
79
...ects/Modules/Networks/Demo/Sources/ServiceTest/AuthServiceTest/AuthServiceViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// AuthServiceViewModel.swift | ||
// NetworksDemo | ||
// | ||
// Created by 류희재 on 11/23/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
import Core | ||
import Networks | ||
|
||
class AuthServiceViewModel: ObservableObject { | ||
|
||
//MARK: Action, State | ||
|
||
enum Action { | ||
case serviceButtonDidTap(Int) | ||
} | ||
|
||
struct State { | ||
var resultText: [String] = Array(repeating: "", count: 2) | ||
var networkLoggingText: String = "네트워크 결과창입니다!" | ||
} | ||
|
||
//MARK: Dependency | ||
|
||
let service: AuthServiceType | ||
|
||
// MARK: - Init | ||
init(service: AuthServiceType) { | ||
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.signUp(request: .stub) | ||
.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.socialLogin(request: .init(socialPlatform: "KAKAO")) | ||
.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 | ||
} | ||
} | ||
} | ||
} | ||
|
||
|