Skip to content

Commit 5a3efb6

Browse files
author
Alexey Naumov
committed
Refactor loading parameter
1 parent 9a129cc commit 5a3efb6

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

CountriesSwiftUI/Interactors/CountriesInteractor.swift

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import SwiftUI
1212

1313
protocol CountriesInteractor {
1414
func loadCountries()
15-
func load(countryDetails: Binding<Loadable<Country.Details>>, country: Country)
15+
func load(countryDetails: LoadableSubject<Country.Details>, country: Country)
1616
}
1717

1818
struct RealCountriesInteractor: CountriesInteractor {
@@ -26,19 +26,17 @@ struct RealCountriesInteractor: CountriesInteractor {
2626
}
2727

2828
func loadCountries() {
29-
let countries = appState.value.userData.countries.value
3029
let cancelBag = CancelBag()
31-
appState[\.userData.countries] = .isLoading(last: countries, cancelBag: cancelBag)
30+
appState[\.userData.countries].setIsLoading(cancelBag: cancelBag)
3231
weak var weakAppState = appState
3332
webRepository.loadCountries()
3433
.sinkToLoadable { weakAppState?[\.userData.countries] = $0 }
3534
.store(in: cancelBag)
3635
}
3736

38-
func load(countryDetails: Binding<Loadable<Country.Details>>, country: Country) {
37+
func load(countryDetails: LoadableSubject<Country.Details>, country: Country) {
3938
let cancelBag = CancelBag()
40-
countryDetails.wrappedValue = .isLoading(last: countryDetails.wrappedValue.value,
41-
cancelBag: cancelBag)
39+
countryDetails.wrappedValue.setIsLoading(cancelBag: cancelBag)
4240
let countriesArray = appState
4341
.map { $0.userData.countries }
4442
.tryMap { countries -> [Country] in
@@ -64,6 +62,6 @@ struct StubCountriesInteractor: CountriesInteractor {
6462
func loadCountries() {
6563
}
6664

67-
func load(countryDetails: Binding<Loadable<Country.Details>>, country: Country) {
65+
func load(countryDetails: LoadableSubject<Country.Details>, country: Country) {
6866
}
6967
}

CountriesSwiftUI/Interactors/ImagesInteractor.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
import SwiftUI
1212

1313
protocol ImagesInteractor {
14-
func load(image: Binding<Loadable<UIImage>>, url: URL?)
14+
func load(image: LoadableSubject<UIImage>, url: URL?)
1515
}
1616

1717
struct RealImagesInteractor: ImagesInteractor {
@@ -22,12 +22,12 @@ struct RealImagesInteractor: ImagesInteractor {
2222
self.webRepository = webRepository
2323
}
2424

25-
func load(image: Binding<Loadable<UIImage>>, url: URL?) {
25+
func load(image: LoadableSubject<UIImage>, url: URL?) {
2626
guard let url = url else {
2727
image.wrappedValue = .notRequested; return
2828
}
2929
let cancelBag = CancelBag()
30-
image.wrappedValue = .isLoading(last: image.wrappedValue.value, cancelBag: cancelBag)
30+
image.wrappedValue.setIsLoading(cancelBag: cancelBag)
3131
webRepository.load(imageURL: url, width: 300)
3232
.sinkToLoadable {
3333
image.wrappedValue = $0
@@ -37,6 +37,6 @@ struct RealImagesInteractor: ImagesInteractor {
3737
}
3838

3939
struct StubImagesInteractor: ImagesInteractor {
40-
func load(image: Binding<Loadable<UIImage>>, url: URL?) {
40+
func load(image: LoadableSubject<UIImage>, url: URL?) {
4141
}
4242
}

CountriesSwiftUI/Utilities/Loadable.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
//
88

99
import Foundation
10+
import SwiftUI
11+
12+
typealias LoadableSubject<Value> = Binding<Loadable<Value>>
1013

1114
enum Loadable<T> {
1215

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

3336
extension Loadable {
3437

38+
mutating func setIsLoading(cancelBag: CancelBag) {
39+
self = .isLoading(last: value, cancelBag: cancelBag)
40+
}
41+
3542
mutating func cancelLoading() {
3643
switch self {
3744
case let .isLoading(last, cancelBag):

CountriesSwiftUI/Utilities/NetworkingHelpers.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
// Copyright © 2020 Alexey Naumov. All rights reserved.
77
//
88

9+
import SwiftUI
910
import Combine
1011
import Foundation
1112

UnitTests/Mocks/MockedInteractors.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct MockedCountriesInteractor: Mock, CountriesInteractor {
4848
register(.loadCountries)
4949
}
5050

51-
func load(countryDetails: Binding<Loadable<Country.Details>>, country: Country) {
51+
func load(countryDetails: LoadableSubject<Country.Details>, country: Country) {
5252
register(.loadCountryDetails(country))
5353
}
5454
}
@@ -67,7 +67,7 @@ struct MockedImagesInteractor: Mock, ImagesInteractor {
6767
self.actions = .init(expected: expected)
6868
}
6969

70-
func load(image: Binding<Loadable<UIImage>>, url: URL?) {
70+
func load(image: LoadableSubject<UIImage>, url: URL?) {
7171
register(.loadImage(url))
7272
}
7373
}

0 commit comments

Comments
 (0)