Skip to content

Commit a0b595f

Browse files
author
Igors Nemenonoks
committed
Property wrapper for view model
1 parent 61b2764 commit a0b595f

File tree

5 files changed

+68
-39
lines changed

5 files changed

+68
-39
lines changed

Classes/UI/BaseMagazineLayout/BaseMagazineLayoutVC.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import SnapKit
1515
protocol PBaseMagazineLayoutVC: UICollectionViewDelegateMagazineLayout, UICollectionViewDataSource {
1616
var sections: Binder<[MagazineLayoutSection]> { get }
1717
var collectionView: UICollectionView { get }
18-
func didSelectItem<DataType: Diffable, CellType: ConfigurableCell>() -> Observable<MagazineCellConfigurator<DataType, CellType>>
18+
func didSelectItem<DataType: MagazineCellDataType, CellType: ConfigurableCell>() -> Observable<MagazineCellConfigurator<DataType, CellType>>
1919
}
2020

2121
class BaseMagazineLayoutVC: UIViewController, PBaseMagazineLayoutVC {
@@ -59,7 +59,7 @@ class BaseMagazineLayoutVC: UIViewController, PBaseMagazineLayoutVC {
5959
}).disposed(by: bag)
6060
}
6161

62-
func didSelectItem<DataType: Diffable, CellType: ConfigurableCell>() -> Observable<MagazineCellConfigurator<DataType, CellType>> {
62+
func didSelectItem<DataType: MagazineCellDataType, CellType: ConfigurableCell>() -> Observable<MagazineCellConfigurator<DataType, CellType>> {
6363
return self.selectPublisher
6464
.filter({ (configurator) -> Bool in
6565
return configurator is MagazineCellConfigurator<DataType, CellType>

Classes/UI/Explore/ExploreVM.swift

+31-32
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ import RxFlow
1313

1414
final class ExploreVM: SectionedViewModel {
1515

16-
private let _sections: BehaviorRelay<[MagazineLayoutSection]>
17-
var sections: Observable<[MagazineLayoutSection]> {
18-
return _sections.asObservable()
19-
}
16+
@VMProperty([]) var sections: Observable<[MagazineLayoutSection]>
2017

2118
let steps = PublishRelay<Step>()
2219
private let bag = DisposeBag()
@@ -40,36 +37,38 @@ final class ExploreVM: SectionedViewModel {
4037

4138
let header = HeaderSectionTitleVM(title: "Last visited", subtitle: "Your last visited destinations")
4239

43-
self._sections = BehaviorRelay(value: [
44-
MagazineLayoutSection(items: [
45-
RowHeadline1VM(title: "Explore new places and meet new people").configurator(),
46-
RowCaptionVM(title: "Most popular destinations").configurator()
47-
], sectionInset: UIEdgeInsets(top: 16, left: 32, bottom: 16, right: 16)),
48-
MagazineLayoutSection(items: [
49-
RowHorizontalCardsCollectionVM(items: destinations,
50-
itemWidth: 136,
51-
itemHeight: 224,
52-
itemsSpacing: 8).configurator()
53-
], sectionInset: UIEdgeInsets(top: 24, left: 0, bottom: 24, right: 0)),
54-
MagazineLayoutSection(items: [
55-
RowFlightInfoVM(info: FlightInfo(departureTime: "05:00",
56-
departureAirport: "Riga (RIX)",
57-
arrivalTime: "20:00",
58-
arrivalAirport: "Tokyo (HND)")).configurator(),
59-
RowFlightInfoVM(info: FlightInfo(departureTime: "14:00",
60-
departureAirport: "Riga (RIX)",
61-
arrivalTime: "23:30",
62-
arrivalAirport: "Bali (DPS)")).configurator(),
40+
$sections.accept(
41+
[
42+
MagazineLayoutSection(items: [
43+
RowHeadline1VM(title: "Explore new places and meet new people").configurator(),
44+
RowCaptionVM(title: "Most popular destinations").configurator()
45+
], sectionInset: UIEdgeInsets(top: 16, left: 32, bottom: 16, right: 16)),
46+
MagazineLayoutSection(items: [
47+
RowHorizontalCardsCollectionVM(items: destinations,
48+
itemWidth: 136,
49+
itemHeight: 224,
50+
itemsSpacing: 8).configurator()
51+
], sectionInset: UIEdgeInsets(top: 24, left: 0, bottom: 24, right: 0)),
52+
MagazineLayoutSection(items: [
53+
RowFlightInfoVM(info: FlightInfo(departureTime: "05:00",
54+
departureAirport: "Riga (RIX)",
55+
arrivalTime: "20:00",
56+
arrivalAirport: "Tokyo (HND)")).configurator(),
57+
RowFlightInfoVM(info: FlightInfo(departureTime: "14:00",
58+
departureAirport: "Riga (RIX)",
59+
arrivalTime: "23:30",
60+
arrivalAirport: "Bali (DPS)")).configurator(),
6361

64-
RowFlightInfoVM(info: FlightInfo(departureTime: "07:30",
65-
departureAirport: "Riga (RIX)",
66-
arrivalTime: "12:15",
67-
arrivalAirport: "Cyprus (LCA)")).configurator()
68-
],
69-
header: .init(item: header.configurator(), visibilityMode: .visible(heightMode: header.heightMode)),
70-
sectionInset: UIEdgeInsets(top: 24, left: 32, bottom: 32, right: 16))
62+
RowFlightInfoVM(info: FlightInfo(departureTime: "07:30",
63+
departureAirport: "Riga (RIX)",
64+
arrivalTime: "12:15",
65+
arrivalAirport: "Cyprus (LCA)")).configurator()
66+
],
67+
header: .init(item: header.configurator(), visibilityMode: .visible(heightMode: header.heightMode)),
68+
sectionInset: UIEdgeInsets(top: 24, left: 32, bottom: 32, right: 16))
7169

72-
])
70+
]
71+
)
7372

7473
destinations.forEach {
7574
$0.steps.bind(to: self.steps).disposed(by: bag)

Classes/UI/Page/DestinationVM.swift

+2-5
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@ import RxFlow
1313

1414
final class DestinationVM: SectionedViewModel {
1515

16-
private let _sections: BehaviorRelay<[MagazineLayoutSection]>
17-
var sections: Observable<[MagazineLayoutSection]> {
18-
return _sections.asObservable()
19-
}
16+
@VMProperty([]) var sections: Observable<[MagazineLayoutSection]>
2017

2118
let steps = PublishRelay<Step>()
2219
private let bag = DisposeBag()
@@ -25,7 +22,7 @@ final class DestinationVM: SectionedViewModel {
2522
let header1 = HeaderHeadline2VM(title: "Introduction")
2623
let header2 = HeaderHeadline2VM(title: "Places to see")
2724

28-
self._sections = BehaviorRelay(value: [
25+
$sections.accept([
2926
MagazineLayoutSection(items: [
3027
RowDestinationTitleVM(title: "Indonesia, Bali").configurator()
3128
], sectionInset: UIEdgeInsets(top: 0, left: 32, bottom: 16, right: 16)),

Classes/UI/Utils/RxUtils.swift

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
//
2+
// RxUtils.swift
3+
// DesignSystemExample
4+
//
5+
// Created by Igors Nemenonoks on 19/05/2020.
6+
// Copyright © 2020 Chili Labs. All rights reserved.
7+
//
8+
9+
import Foundation
10+
import RxSwift
11+
import RxCocoa
12+
13+
@propertyWrapper
14+
struct VMProperty<Type> {
15+
16+
private let relay: BehaviorRelay<Type>
17+
18+
init(_ defaultValue: Type) {
19+
self.relay = .init(value: defaultValue)
20+
}
21+
22+
var wrappedValue: Observable<Type> {
23+
return relay.asObservable()
24+
}
25+
26+
var projectedValue: BehaviorRelay<Type> {
27+
return relay
28+
}
29+
}

DesignSystemExample.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
/* Begin PBXBuildFile section */
1010
04D3491EBF034C557B0624BD /* CellConfigurators.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F1DDB77D41F8E5654210256 /* CellConfigurators.swift */; };
11+
4929660F2473B88A0061C94E /* RxUtils.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4929660E2473B88A0061C94E /* RxUtils.swift */; };
1112
49760BA624699AFF00FEFD61 /* Reusable.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49760BA524699AFF00FEFD61 /* Reusable.swift */; };
1213
49760BB8246B0B3900FEFD61 /* RowHeadline1VM.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49760BB6246B0B3900FEFD61 /* RowHeadline1VM.swift */; };
1314
49760BB9246B0B3900FEFD61 /* RowHeadline1.swift in Sources */ = {isa = PBXBuildFile; fileRef = 49760BB7246B0B3900FEFD61 /* RowHeadline1.swift */; };
@@ -97,6 +98,7 @@
9798
37375CAB15538E0540D15D45 /* Icons.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Icons.xcassets; sourceTree = "<group>"; };
9899
383D08CC216E24568F227648 /* Screen.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Screen.swift; sourceTree = "<group>"; };
99100
46377B511079178264596B00 /* CollectionViewCellsRegistrator.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionViewCellsRegistrator.swift; sourceTree = "<group>"; };
101+
4929660E2473B88A0061C94E /* RxUtils.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RxUtils.swift; sourceTree = "<group>"; };
100102
49760BA524699AFF00FEFD61 /* Reusable.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Reusable.swift; sourceTree = "<group>"; };
101103
49760BB6246B0B3900FEFD61 /* RowHeadline1VM.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowHeadline1VM.swift; sourceTree = "<group>"; };
102104
49760BB7246B0B3900FEFD61 /* RowHeadline1.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RowHeadline1.swift; sourceTree = "<group>"; };
@@ -210,6 +212,7 @@
210212
isa = PBXGroup;
211213
children = (
212214
49760BA524699AFF00FEFD61 /* Reusable.swift */,
215+
4929660E2473B88A0061C94E /* RxUtils.swift */,
213216
);
214217
path = Utils;
215218
sourceTree = "<group>";
@@ -714,6 +717,7 @@
714717
49760BDB246C261F00FEFD61 /* HeaderSectionTitle.swift in Sources */,
715718
49760BCF246BEB6400FEFD61 /* CardsHorizontalRowVM.swift in Sources */,
716719
49760BDA246C261F00FEFD61 /* HeaderSectionTitleVM.swift in Sources */,
720+
4929660F2473B88A0061C94E /* RxUtils.swift in Sources */,
717721
49760BF2246D1F2700FEFD61 /* RowDestinationTitle.swift in Sources */,
718722
7836C978DD3EF1B13922EA51 /* FlowStep.swift in Sources */,
719723
49760BD5246BEF3B00FEFD61 /* CardDestinationVM.swift in Sources */,

0 commit comments

Comments
 (0)