Skip to content

Commit

Permalink
Refactor loading parameter
Browse files Browse the repository at this point in the history
  • Loading branch information
nalexn committed Apr 5, 2020
1 parent 9a129cc commit 5a3efb6
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 13 deletions.
12 changes: 5 additions & 7 deletions CountriesSwiftUI/Interactors/CountriesInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftUI

protocol CountriesInteractor {
func loadCountries()
func load(countryDetails: Binding<Loadable<Country.Details>>, country: Country)
func load(countryDetails: LoadableSubject<Country.Details>, country: Country)
}

struct RealCountriesInteractor: CountriesInteractor {
Expand All @@ -26,19 +26,17 @@ struct RealCountriesInteractor: CountriesInteractor {
}

func loadCountries() {
let countries = appState.value.userData.countries.value
let cancelBag = CancelBag()
appState[\.userData.countries] = .isLoading(last: countries, cancelBag: cancelBag)
appState[\.userData.countries].setIsLoading(cancelBag: cancelBag)
weak var weakAppState = appState
webRepository.loadCountries()
.sinkToLoadable { weakAppState?[\.userData.countries] = $0 }
.store(in: cancelBag)
}

func load(countryDetails: Binding<Loadable<Country.Details>>, country: Country) {
func load(countryDetails: LoadableSubject<Country.Details>, country: Country) {
let cancelBag = CancelBag()
countryDetails.wrappedValue = .isLoading(last: countryDetails.wrappedValue.value,
cancelBag: cancelBag)
countryDetails.wrappedValue.setIsLoading(cancelBag: cancelBag)
let countriesArray = appState
.map { $0.userData.countries }
.tryMap { countries -> [Country] in
Expand All @@ -64,6 +62,6 @@ struct StubCountriesInteractor: CountriesInteractor {
func loadCountries() {
}

func load(countryDetails: Binding<Loadable<Country.Details>>, country: Country) {
func load(countryDetails: LoadableSubject<Country.Details>, country: Country) {
}
}
8 changes: 4 additions & 4 deletions CountriesSwiftUI/Interactors/ImagesInteractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import Foundation
import SwiftUI

protocol ImagesInteractor {
func load(image: Binding<Loadable<UIImage>>, url: URL?)
func load(image: LoadableSubject<UIImage>, url: URL?)
}

struct RealImagesInteractor: ImagesInteractor {
Expand All @@ -22,12 +22,12 @@ struct RealImagesInteractor: ImagesInteractor {
self.webRepository = webRepository
}

func load(image: Binding<Loadable<UIImage>>, url: URL?) {
func load(image: LoadableSubject<UIImage>, url: URL?) {
guard let url = url else {
image.wrappedValue = .notRequested; return
}
let cancelBag = CancelBag()
image.wrappedValue = .isLoading(last: image.wrappedValue.value, cancelBag: cancelBag)
image.wrappedValue.setIsLoading(cancelBag: cancelBag)
webRepository.load(imageURL: url, width: 300)
.sinkToLoadable {
image.wrappedValue = $0
Expand All @@ -37,6 +37,6 @@ struct RealImagesInteractor: ImagesInteractor {
}

struct StubImagesInteractor: ImagesInteractor {
func load(image: Binding<Loadable<UIImage>>, url: URL?) {
func load(image: LoadableSubject<UIImage>, url: URL?) {
}
}
7 changes: 7 additions & 0 deletions CountriesSwiftUI/Utilities/Loadable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
//

import Foundation
import SwiftUI

typealias LoadableSubject<Value> = Binding<Loadable<Value>>

enum Loadable<T> {

Expand All @@ -32,6 +35,10 @@ enum Loadable<T> {

extension Loadable {

mutating func setIsLoading(cancelBag: CancelBag) {
self = .isLoading(last: value, cancelBag: cancelBag)
}

mutating func cancelLoading() {
switch self {
case let .isLoading(last, cancelBag):
Expand Down
1 change: 1 addition & 0 deletions CountriesSwiftUI/Utilities/NetworkingHelpers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
// Copyright © 2020 Alexey Naumov. All rights reserved.
//

import SwiftUI
import Combine
import Foundation

Expand Down
4 changes: 2 additions & 2 deletions UnitTests/Mocks/MockedInteractors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ struct MockedCountriesInteractor: Mock, CountriesInteractor {
register(.loadCountries)
}

func load(countryDetails: Binding<Loadable<Country.Details>>, country: Country) {
func load(countryDetails: LoadableSubject<Country.Details>, country: Country) {
register(.loadCountryDetails(country))
}
}
Expand All @@ -67,7 +67,7 @@ struct MockedImagesInteractor: Mock, ImagesInteractor {
self.actions = .init(expected: expected)
}

func load(image: Binding<Loadable<UIImage>>, url: URL?) {
func load(image: LoadableSubject<UIImage>, url: URL?) {
register(.loadImage(url))
}
}

0 comments on commit 5a3efb6

Please sign in to comment.