From b4070743c693b6f5ce459f725920440d42545d8a Mon Sep 17 00:00:00 2001 From: Jisoo Ham <133845468+isakatty@users.noreply.github.com> Date: Thu, 1 Aug 2024 22:39:30 +0900 Subject: [PATCH] =?UTF-8?q?[Add]=20#311=20AppStoreRepository=20=EC=83=9D?= =?UTF-8?q?=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../DefaultAppStoreRepository.swift | 77 +++++++++++++++++++ .../AppstoreRepository.swift | 15 ++++ 2 files changed, 92 insertions(+) create mode 100644 Projects/Data/Sources/Repository/DefaultAppStoreRepository.swift create mode 100644 Projects/Domain/Sources/RepositoryInterface/AppstoreRepository.swift diff --git a/Projects/Data/Sources/Repository/DefaultAppStoreRepository.swift b/Projects/Data/Sources/Repository/DefaultAppStoreRepository.swift new file mode 100644 index 00000000..2c624a1c --- /dev/null +++ b/Projects/Data/Sources/Repository/DefaultAppStoreRepository.swift @@ -0,0 +1,77 @@ +// +// DefaultAppStoreRepository.swift +// Data +// +// Created by Jisoo HAM on 8/1/24. +// Copyright © 2024 Pepsi-Club. All rights reserved. +// + +import UIKit + +import Domain +import NetworkService + +import RxSwift + +enum AppStoreError: Error, LocalizedError { + case invalidAppStoreId + case invalidURL + case noData + case parsingError + case networkError(Error) + + var errorDescription: String? { + switch self { + case .invalidAppStoreId: + "AppStore Id 잘못됨" + case .invalidURL: + "AppStore URL 잘못됨" + case .noData: + "Data 잘못됨" + case .parsingError: + "데이터 parsing 잘못됨" + case .networkError(let error): + "\(error) 네트워크 에러" + } + } +} + +public final class DefaultAppStoreRepository: AppstoreRepository { + private let networkService: NetworkService + private let appStoreId = Bundle.main + .object(forInfoDictionaryKey: "APPSTORE_ID") as? String + + public let appStoreURLString + = "itms-apps://itunes.apple.com/app/apple-store/" + + public init(networkService: NetworkService) { + self.networkService = networkService + } + + public func latestVersion() -> Observable { + guard let appStoreId else { + return Observable.error(AppStoreError.invalidAppStoreId) + } + let data = networkService.request( + endPoint: AppStoreEndPoint(appStoreID: appStoreId) + ) + .decode( + type: AppInfoDTO.self, + decoder: JSONDecoder() + ) + .compactMap { $0.toDomain } + return data + } + + public func openAppStore() { + guard let appStoreId, + let url = URL( + string: appStoreURLString + appStoreId + ) + else { return } + + if UIApplication.shared.canOpenURL(url) { + UIApplication.shared.open(url) + } + } +} diff --git a/Projects/Domain/Sources/RepositoryInterface/AppstoreRepository.swift b/Projects/Domain/Sources/RepositoryInterface/AppstoreRepository.swift new file mode 100644 index 00000000..f4348fc1 --- /dev/null +++ b/Projects/Domain/Sources/RepositoryInterface/AppstoreRepository.swift @@ -0,0 +1,15 @@ +// +// AppstoreRepository.swift +// Domain +// +// Created by Jisoo HAM on 8/1/24. +// Copyright © 2024 Pepsi-Club. All rights reserved. +// + +import Foundation + +import RxSwift + +public protocol AppstoreRepository { + func latestVersion() -> Observable +}