Skip to content

Commit

Permalink
feat #144: GetServerURLUseCase 반영
Browse files Browse the repository at this point in the history
  • Loading branch information
minsangKang committed Jun 30, 2024
1 parent ec27002 commit a1531f5
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 30 deletions.
55 changes: 37 additions & 18 deletions Project_Timer/Data/NetworkURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,52 @@

import Foundation
import SwiftUI
import Moya
import Combine

class NetworkURL {
final class NetworkURL {
static let shared = NetworkURL()
private(set) var serverURL: String?
// Combine binding
private var cancellables = Set<AnyCancellable>()

private init() {
self.updateServerURL() {}
self.getServerURL()
.sink { version in
print(version ?? "nil")
}
.store(in: &self.cancellables)
}

func updateServerURL(completion: @escaping () -> Void) {
let getServerURLUseCase = GetServerURLUseCase_lagacy(repository: ServerURLRepository())
getServerURLUseCase.getServerURL { [weak self] result in
switch result {
case .success(let url):
guard url != "nil" else {
self?.serverURL = nil
completion()
return
func getServerURL() -> AnyPublisher<String?, Never> {
// TODO: DI 수정
let api = TTProvider<FirebaseAPI>(session: Session(interceptor: NetworkInterceptor.shared))
let repository = FirebaseRepository(api: api)
let getServerURLUseCase = GetServerURLUseCase(repository: repository)

return Future<String?, Never> { [weak self] promise in
guard let self else { promise(.success(nil)); return }

getServerURLUseCase.execute()
.sink { [weak self] completion in
if case .failure(let networkError) = completion {
print("ERROR", #function, networkError)
self?.serverURL = nil
promise(.success(nil))
}
} receiveValue: { [weak self] url in
guard url != "nil" else {
self?.serverURL = nil
promise(.success(nil))
return
}

self?.serverURL = url
promise(.success(url))
}

self?.serverURL = url
completion()
case .failure(_):
self?.serverURL = nil
completion()
}
.store(in: &self.cancellables)
}
.eraseToAnyPublisher()
}

static let appstore: String = "itms-apps://itunes.apple.com/app/id1519159240"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import Combine

final class GetServerURLUseCasee {
final class GetServerURLUseCase {
private let repository: FirebaseRepository // TODO: 프로토콜로 수정

init(repository: FirebaseRepository) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ final class SignupSigninVM {
@Published var alert: (title: String, text: String)?
@Published var postable: Bool = false
@Published var signinSuccess: Bool = false
// Combine binding
private var cancellables = Set<AnyCancellable>()

init(authUseCase: AuthUseCaseInterface,
isSignin: Bool) {
Expand All @@ -26,11 +28,14 @@ final class SignupSigninVM {
}

private func checkServerURL() {
NetworkURL.shared.updateServerURL { [weak self] in
if NetworkURL.shared.serverURL == nil {
self?.alert = (title: Localized.string(.Server_Popup_ServerCantUseTitle), text: Localized.string(.Server_Popup_ServerCantUseDesc))
NetworkURL.shared.getServerURL()
.sink { [weak self] url in
if url == nil {
self?.alert = (title: Localized.string(.Server_Popup_ServerCantUseTitle), text: Localized.string(.Server_Popup_ServerCantUseDesc))
}
}
}
.store(in: &self.cancellables)

}

func signup(info: TestUserSignupRequest) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ final class SyncDailysVM {
@Published private(set) var loading: Bool = false
@Published private(set) var saveDailysSuccess: Bool = false
private(set) var loadingText: SyncDailysVC.LoadingStatus?
// Combine binding
private var cancellables = Set<AnyCancellable>()

init(dailysUseCase: DailysUseCaseInterface,
recordTimesUseCase: RecordTimesUseCaseInterface,
Expand All @@ -33,14 +35,16 @@ final class SyncDailysVM {
}

private func checkServerURL() {
NetworkURL.shared.updateServerURL { [weak self] in
if NetworkURL.shared.serverURL == nil {
self?.alert = (title: Localized.string(.Server_Popup_ServerCantUseTitle), text: Localized.string(.Server_Popup_ServerCantUseDesc))
} else {
// fetch 서버 syncLog
self?.getSyncLog(afterUploaded: false)
NetworkURL.shared.getServerURL()
.sink { [weak self] url in
if url == nil {
self?.alert = (title: Localized.string(.Server_Popup_ServerCantUseTitle), text: Localized.string(.Server_Popup_ServerCantUseDesc))
} else {
// fetch 서버 syncLog
self?.getSyncLog(afterUploaded: false)
}
}
}
.store(in: &self.cancellables)
}
}

Expand Down

0 comments on commit a1531f5

Please sign in to comment.