Skip to content

Commit

Permalink
feat #144: GetUpdateHistorysUseCase 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
minsangKang committed Jul 6, 2024
1 parent 9c517cb commit 89c5ef3
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 44 deletions.
4 changes: 2 additions & 2 deletions Project_Timer/Data/API/FirebaseAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ extension FirebaseAPI: TargetType {
}
case .getUpdateHistorys:
switch Language.current {
case .ko: return "/updates?pageSize=100"
case .en, .zh: return "/updates_eng?pageSize=100"
case .ko: return "/updates"
case .en, .zh: return "/updates_eng"
}
case .getYoutubeLink:
return "/youtube"
Expand Down
7 changes: 0 additions & 7 deletions Project_Timer/Data/NetworkURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ final class NetworkURL {
default: return domain + "/surveys_eng"
}
}

static var updates: String {
switch Language.current {
case .ko: return domain + "/updates?pageSize=100"
default: return domain + "/updates_eng?pageSize=100"
}
}
}

enum WidgetInfo {
Expand Down
22 changes: 0 additions & 22 deletions Project_Timer/Data/lagacy/NetworkController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,6 @@
import Foundation
import Alamofire

protocol UpdateHistoryFetchable {
func getUpdateHistorys_lagacy(completion: @escaping (Result<[UpdateHistoryInfo], NetworkError>) -> Void)
}

protocol SurveysFetchable {
func getSurveys_lagacy(completion: @escaping (Result<[SurveyInfo], NetworkError>) -> Void)
}
Expand All @@ -24,24 +20,6 @@ final class NetworkController {
}
}

extension NetworkController: UpdateHistoryFetchable {
func getUpdateHistorys_lagacy(completion: @escaping (Result<[UpdateHistoryInfo], NetworkError>) -> Void) {
self.network.request(url: NetworkURL.Firestore.updates, method: .get) { result in
switch result.status {
case .SUCCESS:
guard let data = result.data,
let updateInfos: UpdateHistoryResponse = try? JSONDecoder().decode(UpdateHistoryResponse.self, from: data) else {
completion(.failure(.DECODEERROR))
return
}
completion(.success(updateInfos.updateInfos))
default:
completion(.failure(NetworkError.error(result)))
}
}
}
}

extension NetworkController: SurveysFetchable {
func getSurveys_lagacy(completion: @escaping (Result<[SurveyInfo], NetworkError>) -> Void) {
self.network.request(url: NetworkURL.Firestore.surveys, method: .get) { result in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import UIKit
import Combine
import Moya

final class SettingUpdateHistoryVC: UIViewController {
static let identifier = "SettingUpdateHistoryVC"
Expand Down Expand Up @@ -58,9 +59,12 @@ extension SettingUpdateHistoryVC {
}

private func configureViewModel() {
// MARK: NetworkController 주입 고민이 필요
let networkController = NetworkController(network: Network())
self.viewModel = SettingUpdateHistoryVM(networkController: networkController)
// TODO: DI 수정
let api = TTProvider<FirebaseAPI>(session: Session(interceptor: NetworkInterceptor.shared))
let repository = FirebaseRepository(api: api)
let getUpdateHistorysUseCase = GetUpdateHistorysUseCase(repository: repository)

self.viewModel = SettingUpdateHistoryVM(getUpdateHistorysUseCase: getUpdateHistorysUseCase)
}

private func stopLoader() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,29 @@ import Foundation
import Combine

final class SettingUpdateHistoryVM {
let networkController: UpdateHistoryFetchable
let getUpdateHistorysUseCase: GetUpdateHistorysUseCase
@Published private(set) var infos: [UpdateHistoryInfo] = []
@Published private(set) var warning: (title: String, text: String)?
// Combine binding
private var cancellables = Set<AnyCancellable>()

init(networkController: UpdateHistoryFetchable) {
self.networkController = networkController
init(getUpdateHistorysUseCase: GetUpdateHistorysUseCase) {
self.getUpdateHistorysUseCase = getUpdateHistorysUseCase
self.configureInfos()
}

private func configureInfos() {
self.networkController.getUpdateHistorys_lagacy { [weak self] result in
switch result {
case .success(let updateInfo):
self?.infos = updateInfo.sorted(by: {
self.getUpdateHistorysUseCase.execute()
.sink { [weak self] completion in
if case .failure(let networkError) = completion {
print("ERROR", #function, networkError)
self?.warning = networkError.alertMessage
}
} receiveValue: { [weak self] updateHistoryInfos in
self?.infos = updateHistoryInfos.sorted(by: {
$0.version.value.compare($1.version.value, options: .numeric) == .orderedDescending
})
case .failure(let error):
self?.warning = error.alertMessage
}
}
.store(in: &self.cancellables)
}
}

0 comments on commit 89c5ef3

Please sign in to comment.