-
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
7 changed files
with
210 additions
and
65 deletions.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -29,4 +29,3 @@ public extension Publisher { | |
.eraseToAnyPublisher() | ||
} | ||
} | ||
|
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
120 changes: 120 additions & 0 deletions
120
...t_iOS/Projects/Modules/Networks/Demo/Sources/PointServiceTest/PointServiceViewModel.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,120 @@ | ||
// | ||
// PointServiceViewModel.swift | ||
// NetworksDemo | ||
// | ||
// Created by 류희재 on 11/23/24. | ||
// Copyright © 2024 HMH-iOS. All rights reserved. | ||
|
||
|
||
import Foundation | ||
import Combine | ||
import Core | ||
import Networks | ||
|
||
class PointServiceViewModel: ObservableObject { | ||
|
||
//MARK: Action, State | ||
|
||
enum Action { | ||
case serviceButtonDidTap(Int) | ||
} | ||
|
||
struct State { | ||
var resultText: [String] = Array(repeating: "", count: 5) | ||
var networkLoggingText: String = "네트워크 결과창입니다!" | ||
} | ||
|
||
//MARK: Dependency | ||
|
||
let service: PointServiceType | ||
|
||
//MARK: Init | ||
|
||
|
||
|
||
// MARK: - Init | ||
init(service: PointServiceType) { | ||
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): | ||
let requestPublisher: AnyPublisher<Any, Error> | ||
switch index { | ||
case 0: | ||
service.patchPointUse() | ||
.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.getEarnPoint() | ||
.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.getUsagePoint() | ||
.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.patchEarnPoint(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 4: | ||
service.getPointList() | ||
.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 | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
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
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
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
Oops, something went wrong.