-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEAT] 상상의 닉네임 뷰 구현 및 리팩토링(?) #48
base: main
Are you sure you want to change the base?
Changes from all commits
1cb4a62
d1d7e98
cd78a70
824f103
d273600
910eb6c
3209c9e
bf25c54
5f9c01a
d0783bf
90a256e
66e6208
e58a0d9
da7668d
c668e67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
{ | ||
"images" : [ | ||
{ | ||
"filename" : "Frame 136.png", | ||
"idiom" : "universal", | ||
"platform" : "ios", | ||
"size" : "1024x1024" | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// | ||
// HomeViewModel.swift | ||
// SOPKATHON-iOS-TEAM2 | ||
// | ||
// Created by 이지희 on 6/4/24. | ||
// | ||
|
||
import SwiftUI | ||
|
||
import Moya | ||
|
||
|
||
final class HomeViewModel: ObservableObject { | ||
@State var homeQuestions: [DTO.GetQuestionsResponse.Question] = [] | ||
private let networkService: QuestionsNetworkService | ||
|
||
init(networkService: QuestionsNetworkService = QuestionsNetworkService()) { | ||
self.networkService = networkService | ||
self.fetchQuestions(completion: { }) | ||
} | ||
|
||
|
||
/// 질문 목록 가져오기 | ||
func fetchQuestions(completion: @escaping () -> Void) { | ||
networkService.getQuestions { [weak self] result in | ||
switch result { | ||
case .success(let response): | ||
response.data.forEach { question in | ||
|
||
} | ||
case .failure(let error): | ||
print(error) | ||
} | ||
completion() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// SolvedViewModel.swift | ||
// SOPKATHON-iOS-TEAM2 | ||
// | ||
// Created by 이지희 on 6/5/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Question { | ||
let type: Int | ||
let question: String | ||
let answer: [Option] | ||
let nextQuestionID: Int | ||
} | ||
|
||
struct Option { | ||
let option: String | ||
let isCorrect: Bool | ||
} | ||
|
||
|
||
final class QuestionViewModel: ObservableObject { | ||
private let networkService: QuestionsNetworkService | ||
|
||
var questions: [Question] = [] | ||
var questionDescription: String = "" | ||
var errorMessage: String? | ||
|
||
init(networkService: QuestionsNetworkService = QuestionsNetworkService()) { | ||
self.networkService = networkService | ||
} | ||
|
||
/// 질문 상세 정보 가져오기 | ||
func fetchDetailQuestion(questionId: Int, completion: @escaping () -> Void) { | ||
let request = DTO.GetDetailQuestionRequest(questionId: questionId) | ||
networkService.getDetailQuestions(requestDTO: request) { [weak self] result in | ||
switch result { | ||
case .success(let response): | ||
let data = response.data | ||
let options = data.options.map { option in | ||
Option(option: option.option, isCorrect: option.isAnswer) | ||
} | ||
self?.questions.append(Question(type: data.type, | ||
question: data.question, | ||
answer: options, | ||
nextQuestionID: data.nextQuestionID)) | ||
case .failure(let error): | ||
self?.errorMessage = error.localizedDescription | ||
} | ||
completion() | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// | ||
// SolvedViewModel.swift | ||
// SOPKATHON-iOS-TEAM2 | ||
// | ||
// Created by 이지희 on 6/5/24. | ||
// | ||
|
||
import Foundation | ||
|
||
final class SolvedViewModel { | ||
var questionTitle: String = "" { | ||
didSet { | ||
self.onUpdate?() | ||
} | ||
} | ||
var answerText: String = "" { | ||
didSet { | ||
self.onUpdate?() | ||
} | ||
} | ||
var questionDescription: String = "" { | ||
didSet { | ||
self.onUpdate?() | ||
} | ||
} | ||
|
||
var onUpdate: (() -> Void)? | ||
|
||
private let networkService: QuestionsNetworkService | ||
|
||
init(networkService: QuestionsNetworkService, questionId: Int) { | ||
self.networkService = networkService | ||
let memberId = UserDefaults.standard.integer(forKey: "memberID") | ||
print(memberId) | ||
fetchQuestionDescription(questionId: questionId, memberId: memberId) | ||
} | ||
|
||
func fetchQuestionDescription(questionId: Int, memberId: Int) { | ||
let request = DTO.GetQuestionDescriptionRequest(memberId: memberId, questionId: questionId) | ||
networkService.getQuestionDescription(requestDTO: request) { [weak self] result in | ||
DispatchQueue.main.async { | ||
switch result { | ||
case .success(let response): | ||
let data = response.data | ||
self?.questionTitle = data.question | ||
self?.answerText = data.answer | ||
self?.questionDescription = data.solution | ||
case .failure(let error): | ||
print(error) | ||
} | ||
} | ||
} | ||
Comment on lines
+40
to
+52
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 사실 좀 편하게 해보려고 rx 도입 생각한건데, 뭔가 다시 생각해보니까 라이브러리 도입 없이도 충분히 고칠 수 있을 것 같다! |
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
|
||
import Foundation | ||
|
||
import Moya | ||
|
||
protocol NetworkServiceProtocol { | ||
associatedtype Target: TargetType | ||
var provider: MoyaProvider<Target> { get } | ||
func request<T: Decodable>(_ target: Target, completion: @escaping (Result<T, Error>) -> Void) | ||
} | ||
|
||
class NetworkService<T: TargetType>: NetworkServiceProtocol { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
typealias Target = T | ||
let provider: MoyaProvider<T> | ||
|
||
init(provider: MoyaProvider<T> = MoyaProvider<T>(plugins: [NetworkLoggerPlugin()])) { | ||
self.provider = provider | ||
} | ||
|
||
func request<U: Decodable>(_ target: T, completion: @escaping (Result<U, Error>) -> Void) { | ||
provider.request(target) { result in | ||
switch result { | ||
case .success(let response): | ||
do { | ||
let decoder = JSONDecoder() | ||
let decoded = try decoder.decode(U.self, from: response.data) | ||
completion(.success(decoded)) | ||
} catch let error { | ||
completion(.failure(error)) | ||
} | ||
case .failure(let error): | ||
completion(.failure(error)) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.