-
Notifications
You must be signed in to change notification settings - Fork 87
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from chenxi92/peak
Save image to file; use combine framework to download image
- Loading branch information
Showing
5 changed files
with
176 additions
and
19 deletions.
There are no files selected for viewing
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
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
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,69 @@ | ||
// | ||
// LocalFileManager.swift | ||
// iAppStore | ||
// | ||
// Created by peak on 2022/1/25. | ||
// Copyright © 2022 37 Mobile Games. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
class LocalFileManager { | ||
static let instance = LocalFileManager() | ||
private init() {} | ||
|
||
func saveImage(image: UIImage, imageName: String, folderName: String) { | ||
createFolderIfNeeded(folderName: folderName) | ||
|
||
guard let data = image.pngData(), | ||
let url = getURLForImage(imageName: imageName, folderName: folderName) | ||
else { | ||
return | ||
} | ||
|
||
do { | ||
try data.write(to: url) | ||
} catch let error { | ||
print("Error saving image. ImageName: \(imageName) \(error)") | ||
} | ||
} | ||
|
||
func getImage(imageName: String, folderName: String) -> UIImage? { | ||
guard let url = getURLForImage(imageName: imageName, folderName: folderName), | ||
FileManager.default.fileExists(atPath: url.path) else { | ||
return nil | ||
} | ||
return UIImage(contentsOfFile: url.path) | ||
} | ||
|
||
// MARK: Private | ||
|
||
private func createFolderIfNeeded(folderName: String) { | ||
guard let url = getURLForFolder(folderName: folderName) else { | ||
return | ||
} | ||
|
||
if !FileManager.default.fileExists(atPath: url.path) { | ||
do { | ||
try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: nil) | ||
} catch let error { | ||
print("Error creating directory. FolderName: \(folderName). \(error)") | ||
} | ||
} | ||
} | ||
|
||
private func getURLForFolder(folderName: String) -> URL? { | ||
guard let url = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first, !folderName.isEmpty else { | ||
return nil | ||
} | ||
return url.appendingPathComponent(folderName) | ||
} | ||
|
||
private func getURLForImage(imageName: String, folderName: String) -> URL? { | ||
guard let folderURL = getURLForFolder(folderName: folderName), !imageName.isEmpty else { | ||
return nil | ||
} | ||
return folderURL.appendingPathComponent(imageName + ".png") | ||
} | ||
} |
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,49 @@ | ||
// | ||
// NetworkManager.swift | ||
// iAppStore | ||
// | ||
// Created by peak on 2022/1/25. | ||
// Copyright © 2022 37 Mobile Games. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
import Combine | ||
|
||
class NetworkingManager { | ||
|
||
enum NetworkingError: LocalizedError { | ||
case badURLResponse(url: URL) | ||
case unknown | ||
|
||
var errorDescription: String? { | ||
switch self { | ||
case .badURLResponse(url: let url): return "[🔥] Bad response from URL: \(url)" | ||
case .unknown: return "[⚠️] Unknown error occured" | ||
} | ||
} | ||
} | ||
|
||
static func download(url: URL) -> AnyPublisher<Data, Error> { | ||
return URLSession.shared.dataTaskPublisher(for: url) | ||
.tryMap({ try handleURLResponse(output: $0, url: url) }) | ||
.retry(2) | ||
.eraseToAnyPublisher() | ||
} | ||
|
||
static func handleURLResponse(output: URLSession.DataTaskPublisher.Output, url: URL) throws -> Data { | ||
guard let response = output.response as? HTTPURLResponse, | ||
response.statusCode >= 200 && response.statusCode < 300 else { | ||
throw NetworkingError.badURLResponse(url: url) | ||
} | ||
return output.data | ||
} | ||
|
||
static func handleCompletion(completion: Subscribers.Completion<Error>) { | ||
switch completion { | ||
case .finished: | ||
break | ||
case .failure(let error): | ||
print(error.localizedDescription) | ||
} | ||
} | ||
} |
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