-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
92 additions
and
0 deletions.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
Projects/Data/Sources/Repository/DefaultAppStoreRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
Projects/Domain/Sources/RepositoryInterface/AppstoreRepository.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} |