From 3d0149fa711d3547abb03089defa582d58b1a345 Mon Sep 17 00:00:00 2001 From: "hsj._.06" Date: Fri, 6 Oct 2023 17:17:23 +0900 Subject: [PATCH] =?UTF-8?q?feat=20::=20=20=EA=B0=95=EC=A0=9C=20=EC=97=85?= =?UTF-8?q?=EB=8D=B0=EC=9D=B4=ED=8A=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../SplashFeature/Sources/SplashView.swift | 68 ++++++++++------- .../Sources/SplashViewModel.swift | 75 +++++++++++-------- 2 files changed, 82 insertions(+), 61 deletions(-) diff --git a/Projects/Feature/SplashFeature/Sources/SplashView.swift b/Projects/Feature/SplashFeature/Sources/SplashView.swift index 66199280..f0c12f99 100644 --- a/Projects/Feature/SplashFeature/Sources/SplashView.swift +++ b/Projects/Feature/SplashFeature/Sources/SplashView.swift @@ -18,6 +18,45 @@ struct SplashView: View { .resizable() .frame(width: 200, height: 200) } + .alert(isPresented: $viewModel.showUpdateAlert) { + if viewModel.forceAlert { + return Alert( + title: Text(viewModel.alertTitle), + message: Text(viewModel.alertMessage), + dismissButton: Alert.Button.destructive( + Text("확인"), + action: { + viewModel.openAppStore() + } + ) + ) + } else { + let reissue: () -> Void = { + viewModel.compareRefreshToke { authority in + appState.authority = authority + appState.sceneFlow = .main + } onError: { _ in + appState.sceneFlow = .auth + } + } + + return Alert( + title: Text(viewModel.alertTitle), + message: Text(viewModel.alertMessage), + primaryButton: Alert.Button.destructive( + Text("확인"), + action: { + viewModel.openAppStore() + reissue() + } + ), + secondaryButton: Alert.Button.cancel( + Text("취소"), + action: reissue + ) + ) + } + } .onAppear { viewModel.onAppear { authority in appState.authority = authority @@ -26,34 +65,5 @@ struct SplashView: View { appState.sceneFlow = .auth } } - .alert(isPresented: $viewModel.showUpdateAlert) { - Alert( - title: Text(viewModel.alertTitle), - message: Text(viewModel.alertMessage), - primaryButton: Alert.Button.destructive( - Text("확인"), - action: { - viewModel.openAppStore() - appState.sceneFlow = .auth - } - ), - secondaryButton: Alert.Button.cancel( - Text("취소") - ) - ) - } - .alert(isPresented: $viewModel.showUpdateForceAlert) { - Alert( - title: Text(viewModel.alertTitle), - message: Text(viewModel.alertMessage), - dismissButton: Alert.Button.destructive( - Text("확인"), - action: { - viewModel.openAppStore() - appState.sceneFlow = .auth - } - ) - ) - } } } diff --git a/Projects/Feature/SplashFeature/Sources/SplashViewModel.swift b/Projects/Feature/SplashFeature/Sources/SplashViewModel.swift index c427a30d..97609fbc 100644 --- a/Projects/Feature/SplashFeature/Sources/SplashViewModel.swift +++ b/Projects/Feature/SplashFeature/Sources/SplashViewModel.swift @@ -7,7 +7,7 @@ import UsersDomainInterface final class SplashViewModel: BaseViewModel { private let reissueTokenUseCase: any ReissueTokenUseCase @Published var showUpdateAlert: Bool = false - @Published var showUpdateForceAlert: Bool = false + @Published var forceAlert: Bool = false public var alertTitle = "" public var alertMessage = "" private let appleID = "6450888392" @@ -23,46 +23,57 @@ final class SplashViewModel: BaseViewModel { onError: @escaping (Error) -> Void ) { update { - addCancellable(reissueTokenUseCase.execute()) { authority in - if authority { - onSuccess(.developer) - } else { - onSuccess(.student) - } - } onReceiveError: { error in - onError(error) + self.compareRefreshToke(onSuccess: onSuccess, onError: onError) + } + } + + func compareRefreshToke( + onSuccess: @escaping (AuthorityType) -> Void, + onError: @escaping (Error) -> Void + ) { + addCancellable(reissueTokenUseCase.execute()) { authority in + if authority { + onSuccess(.developer) + } else { + onSuccess(.student) } + } onReceiveError: { error in + onError(error) } } - private func update(action: () -> Void) { + private func update(action: @escaping () -> Void) { let appVersion = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String + let urlString = "http://itunes.apple.com/lookup?id=\(self.appleID)&country=kr" - let latestVersion: String? = { - guard let url = URL(string: "http://itunes.apple.com/lookup?id=\(self.appleID)&country=kr"), - let data = try? Data(contentsOf: url), - let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [String: Any], + guard appVersion != "0.0.0" else { return action() } + guard let url = URL(string: urlString) else { return } + let urlRequest = URLRequest(url: url) + URLSession.shared.dataTask(with: urlRequest) { data, _, _ in + guard let json = try? JSONSerialization.jsonObject(with: data!, options: .allowFragments) as? [String: Any], let results = json["results"] as? [[String: Any]], - let appStoreVersion = results[0]["version"] as? String else { - return nil - } - return appStoreVersion - }() - - let marketingVersion = latestVersion! - let currentProjectVersion = appVersion! - let splitMarketingVersion = marketingVersion.split(separator: ".").map {$0} - let splitCurrentProjectVersion = currentProjectVersion.split(separator: ".").map {$0} + let marketingVersion = results[0]["version"] as? String else { return } + let currentProjectVersion = appVersion! + let splitMarketingVersion = marketingVersion.split(separator: ".").map {$0} + let splitCurrentProjectVersion = currentProjectVersion.split(separator: ".").map {$0} - alertTitle = "업데이트 알림" - alertMessage = "JOBIS의 새로운 버전이 있습니다.\n\(marketingVersion) 버전으로 업데이트 해주세요." - if splitCurrentProjectVersion[0] < splitMarketingVersion[0] { - showUpdateForceAlert.toggle() - } else if splitCurrentProjectVersion[1] < splitMarketingVersion[1] { - showUpdateForceAlert.toggle() - } else { - showUpdateAlert.toggle() + DispatchQueue.main.async { + self.alertTitle = "업데이트 알림" + self.alertMessage = "JOBIS의 새로운 버전이 있습니다.\n\(marketingVersion) 버전으로 업데이트 해주세요." + if splitCurrentProjectVersion[0] < splitMarketingVersion[0] { + self.showUpdateAlert.toggle() + self.forceAlert.toggle() + } else if splitCurrentProjectVersion[1] < splitMarketingVersion[1] { + self.showUpdateAlert.toggle() + self.forceAlert.toggle() + } else if splitCurrentProjectVersion[2] < splitMarketingVersion[2] { + self.showUpdateAlert.toggle() + } else { + action() + } + } } + .resume() } func openAppStore() {