Skip to content

Commit

Permalink
Refactor app to use new await async network framework
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorgvoi committed Nov 13, 2024
1 parent e729602 commit 2818f95
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,17 @@ import Foundation

struct APIListData {
let id = UUID()
let url: URL
let url: URL?
let endpoints: [AnyHashable]
}

// MARK: -
extension APIListData {
var name: String? { url.host }
var name: String? { url?.host }
}

// MARK: -
extension APIListData: Hashable {

func hash(into hasher: inout Hasher) {
hasher.combine(id)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ final class ImageViewController: UIViewController {
return spinner
}()

private let responsePublisher: AnyPublisher<Data, Error>
private let responseData: Data
private var cancel: AnyCancellable?

// MARK: - Init
init(publisher: AnyPublisher<Data, Error>) {
self.responsePublisher = publisher
init(data: Data) {
self.responseData = data
super.init(nibName: nil, bundle: nil)
}

Expand All @@ -46,10 +46,6 @@ final class ImageViewController: UIViewController {

spinner.startAnimating()

cancel = responsePublisher
.compactMap { UIImage(data: $0) }
.assertNoFailure()
.receive(on: RunLoop.main)
.assign(to: \.image, on: imageView)
imageView.image = UIImage(data: responseData)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class ResourceViewController: UIViewController, UITableViewDelegate {
// MARK: - Init
init(apiData: APIListData) {
self.apiData = apiData
self.service = Network.Service(server: ServerConfig(baseURL: apiData.url.absoluteString))
self.service = Network.Service(server: ServerConfig(baseURL: apiData.url?.absoluteString ?? "")!)
super.init(nibName: nil, bundle: nil)
}

Expand All @@ -39,7 +39,7 @@ final class ResourceViewController: UIViewController, UITableViewDelegate {
override func viewDidLoad() {
super.viewDidLoad()

title = apiData.url.host
title = apiData.url?.host
view.addSubview(tableView)
setupData()
}
Expand Down Expand Up @@ -68,20 +68,23 @@ final class ResourceViewController: UIViewController, UITableViewDelegate {

guard let request = dataSource.itemIdentifier(for: indexPath) as? Requestable else { return }

do {
let viewController = viewController(fromRequest: request, withPublisher: try service.dataPublisher(request))
navigationController?.pushViewController(viewController, animated: true)
} catch {
fatalError("Something bad happened :(")
Task {
do {
let data = try await service.data(request)
let viewController = viewController(fromRequest: request, withData: data)
navigationController?.pushViewController(viewController, animated: true)
} catch {
fatalError("Something bad happened :(")
}
}
}

// MARK: - Private functions
private func viewController(fromRequest request: Requestable, withPublisher publisher: AnyPublisher<Data, Error>) -> UIViewController {
guard let request = request as? HTTPBin.Request else { return ResponseViewController(publisher: publisher) }
private func viewController(fromRequest request: Requestable, withData data: Data) -> UIViewController {
guard let request = request as? HTTPBin.Request else { return ResponseViewController(data: data) }
switch request {
case .jpeg, .png: return ImageViewController(publisher: publisher)
default: return ResponseViewController(publisher: publisher)
case .jpeg, .png: return ImageViewController(data: data)
default: return ResponseViewController(data: data)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ final class ResponseViewController: UIViewController {
return textView
}()

private let responsePublisher: AnyPublisher<Data, Error>
private let responseData: Data
private var cancel: AnyCancellable?

// MARK: - Init
init(publisher: AnyPublisher<Data, Error>) {
self.responsePublisher = publisher
init(data: Data) {
self.responseData = data
super.init(nibName: nil, bundle: nil)
}

Expand All @@ -38,14 +38,6 @@ final class ResponseViewController: UIViewController {
title = "Response"
view.addSubview(textView)

cancel = responsePublisher
.tryMap { try JSONSerialization.jsonObject(with: $0) }
.tryMap { try JSONSerialization.data(withJSONObject: $0, options: [.prettyPrinted, .sortedKeys]) }
.compactMap { String(data: $0, encoding: .utf8) }
.compactMap { $0.replacingOccurrences(of: " :", with: ":") }
.compactMap { $0.replacingOccurrences(of: "\\", with: "") }
.assertNoFailure()
.receive(on: RunLoop.main)
.assign(to: \.text, on: textView)
textView.text = String(data: responseData, encoding: .utf8)
}
}
6 changes: 3 additions & 3 deletions Networking-Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- Networking-Swift (0.8.9)
- Networking-Swift (0.9.0)

DEPENDENCIES:
- Networking-Swift
Expand All @@ -9,8 +9,8 @@ SPEC REPOS:
- Networking-Swift

SPEC CHECKSUMS:
Networking-Swift: b3713108e9baa1e6cf07333d6cf35e483295974e
Networking-Swift: 86529d4a3a3123bc0d190c577f00bfb6c44bd02e

PODFILE CHECKSUM: 2c5e9876a119abb0735997db7330924e72930e0a

COCOAPODS: 1.11.3
COCOAPODS: 1.15.2

0 comments on commit 2818f95

Please sign in to comment.