Skip to content

Commit

Permalink
feat: Splash 화면에서 앱 버전 조회 및 업데이트 필요할 시 앱스토어로 보내는 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
FirstDo committed Oct 14, 2024
1 parent 7b1cccc commit 737fe23
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

enum VersionAPI {
case version(VersionRequestDTO) // 버전조회
}

extension VersionAPI: TargetType {
var baseURL: URL? {
return try? Config.base.asURL()
}

var path: String {
switch self {
case .version:
return "v1/version"
}
}

var method: HTTPMethod {
switch self {
case .version:
return .post
}
}

var task: HTTPTask {
switch self {
case let .version(param):
return .requestJSONEncodable(params: param)
}
}

var headers: [String : String]? {
return [
"Content-Type": "application/json;charset=UTF-8"
]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import Foundation

public protocol VersionRepositoryInterface {
func get() async throws
}

public final class VersionRepository: VersionRepositoryInterface {
private let networkManager: NetworkManagerInterfacae

public init(networkManager: NetworkManagerInterfacae) {
self.networkManager = networkManager
}

public func get() async throws {
if let version = Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String {
let targetType = VersionAPI.version(VersionRequestDTO(version: version))
try await networkManager.request(target: targetType)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import Foundation

struct VersionRequestDTO: Encodable {
let version: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public extension SignCoordinator {
navigationController.popViewController(animated: animated)
}

func alert(title: String) {
AlertsManager.show(title: title)
func alert(title: String, okAction: (() -> Void)? = nil) {
if let okAction {
AlertsManager.show(title: title, type: .onlyOkButton(okAction))
} else {
AlertsManager.show(title: title)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,11 @@ public final class SignDIContainer {
kakaoAuthManager: KakaoAuthManager(),
appleAuthManager: AppleAuthManager()
)
let versionRepository = VersionRepository(networkManager: networkManager)
vc.reactor = SplashReactor(
signRepository: signRepository,
userRepo: UserRepository(networkManager: networkManager, localStorage: localStorage)
userRepo: UserRepository(networkManager: networkManager, localStorage: localStorage),
versionRepo: versionRepository
)
vc.coordinator = coordinator
return vc
Expand Down
33 changes: 25 additions & 8 deletions Projects/Feature/Sign/Sources/Scene/Splash/SplashReactor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ final class SplashReactor: Reactor {

enum Mutation {
case setDestination(Destination)
case setAlert
}

enum Destination {
Expand All @@ -19,27 +20,41 @@ final class SplashReactor: Reactor {

struct State {
@Pulse var destination: Destination?
@Pulse var isUpdateAlert: Bool = false
}

let initialState: State = State()
private let signRepository: SignRepositoryInterface
private let userRepo: UserRepositoryInterface
private let versionRepo: VersionRepositoryInterface

init(signRepository: SignRepositoryInterface, userRepo: UserRepositoryInterface) {
init(
signRepository: SignRepositoryInterface,
userRepo: UserRepositoryInterface,
versionRepo: VersionRepositoryInterface
) {
self.signRepository = signRepository
self.userRepo = userRepo
self.versionRepo = versionRepo
}

func mutate(action: Action) -> Observable<Mutation> {
switch action {
case .onAppear:
return .task {
let result = try await signRepository.autoSign()
_ = try await userRepo.user()
return result
}
.map { .setDestination($0.schoolInfoProvided ? .main : .login) }
.catch { _ in .just(.setDestination(.login)) }
.task {
try await versionRepo.get()
let result = try await signRepository.autoSign()
_ = try await userRepo.user()
return result
}
.map { .setDestination($0.schoolInfoProvided ? .main : .login) }
.catch { error in
if error.localizedDescription == "앱 업데이트가 필요합니다." {
return .just(.setAlert)
} else {
return .just(.setDestination(.login))
}
}
}
}

Expand All @@ -48,6 +63,8 @@ final class SplashReactor: Reactor {
switch mutation {
case .setDestination(let destination):
newState.destination = destination
case .setAlert:
newState.isUpdateAlert = true
}
return newState
}
Expand Down
21 changes: 21 additions & 0 deletions Projects/Feature/Sign/Sources/Scene/Splash/SplashVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,27 @@ final class SplashVC: BaseVC, View {
}
}
.disposed(by: disposeBag)

reactor.pulse(\.$isUpdateAlert)
.observe(on: MainScheduler.instance)
.filter { $0 }
.bind(with: self) { owner, value in
owner.coordinator?.alert(title: "안정적인 머니몽 사용을 위해\n최신 버전으로 업데이트가 필요해요!") {
if let url = URL(string: "itms-apps://itunes.apple.com/app/id6503661220"),
UIApplication.shared.canOpenURL(url)
{
UIApplication.shared.open(url) { result in
if result {
UIApplication.shared.perform(#selector(NSXPCConnection.suspend))
DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
exit(0)
}
}
}
}
}
}
.disposed(by: disposeBag)

rx.viewDidAppear
.delay(.milliseconds(500), scheduler: MainScheduler.instance)
Expand Down

0 comments on commit 737fe23

Please sign in to comment.