Skip to content

Commit

Permalink
[Add] #311 AppStoreRepository 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
isakatty committed Aug 1, 2024
1 parent 3637db5 commit b407074
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 0 deletions.
77 changes: 77 additions & 0 deletions Projects/Data/Sources/Repository/DefaultAppStoreRepository.swift
Original file line number Diff line number Diff line change
@@ -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<AppInfoResponse> {
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)
}
}
}
Original file line number Diff line number Diff line change
@@ -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<AppInfoResponse>
}

0 comments on commit b407074

Please sign in to comment.