Skip to content
This repository has been archived by the owner on Apr 4, 2024. It is now read-only.

Commit

Permalink
feat :: 강제 업데이트
Browse files Browse the repository at this point in the history
  • Loading branch information
HongSJae committed Oct 6, 2023
1 parent 7cd546a commit 3d0149f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 61 deletions.
68 changes: 39 additions & 29 deletions Projects/Feature/SplashFeature/Sources/SplashView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
)
)
}
}
}
75 changes: 43 additions & 32 deletions Projects/Feature/SplashFeature/Sources/SplashViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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() {
Expand Down

0 comments on commit 3d0149f

Please sign in to comment.