Skip to content

Commit

Permalink
Merge pull request #38 from Anatoli4/master
Browse files Browse the repository at this point in the history
[iOS]Re-organize decorator work when data empty or request return error
  • Loading branch information
sergeimikhan authored Mar 22, 2019
2 parents 82f3e4e + 08e9844 commit 29ca850
Show file tree
Hide file tree
Showing 12 changed files with 160 additions and 222 deletions.
8 changes: 8 additions & 0 deletions Demo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
/* Begin PBXBuildFile section */
636ACB5FDF6BF177BD8A2753 /* Pods_Astrolabe_Tests_iOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 737BBC239C1705EC7F7C404F /* Pods_Astrolabe_Tests_iOSTests.framework */; };
75490423C50700B4F16DC05E /* Pods_Astrolabe_Tests_tvOSTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 49C3E7785873F6BC64AD1EE9 /* Pods_Astrolabe_Tests_tvOSTests.framework */; };
8B030260222D77F200B3BEDB /* EmptyDataCollectionCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B03025F222D77F200B3BEDB /* EmptyDataCollectionCell.swift */; };
8B030264222EA22100B3BEDB /* EmptyDataTableCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 8B030263222EA22100B3BEDB /* EmptyDataTableCell.swift */; };
A1540D84E8C43C1ADE9B2592 /* Pods_Astrolabe_DemoTV.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 03A48EC1C90A82F3FC569A6D /* Pods_Astrolabe_DemoTV.framework */; };
A5532A9C1F615C4600186A9F /* SelectionViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5532A9B1F615C4600186A9F /* SelectionViewController.swift */; };
AA0376A320DA5F1100E6343A /* BasicTestViewModelViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = AA52599520BC56010089ECD2 /* BasicTestViewModelViewController.swift */; };
Expand Down Expand Up @@ -101,6 +103,8 @@
49C3E7785873F6BC64AD1EE9 /* Pods_Astrolabe_Tests_tvOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Astrolabe_Tests_tvOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
66AE31EC04CA8A047821FDD8 /* Pods-Astrolabe-Demo.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-Astrolabe-Demo.debug.xcconfig"; path = "Pods/Target Support Files/Pods-Astrolabe-Demo/Pods-Astrolabe-Demo.debug.xcconfig"; sourceTree = "<group>"; };
737BBC239C1705EC7F7C404F /* Pods_Astrolabe_Tests_iOSTests.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Astrolabe_Tests_iOSTests.framework; sourceTree = BUILT_PRODUCTS_DIR; };
8B03025F222D77F200B3BEDB /* EmptyDataCollectionCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyDataCollectionCell.swift; sourceTree = "<group>"; };
8B030263222EA22100B3BEDB /* EmptyDataTableCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EmptyDataTableCell.swift; sourceTree = "<group>"; };
8DCCE6F89A6E4A048A3EFEAD /* Pods_Astrolabe_Demo.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_Astrolabe_Demo.framework; sourceTree = BUILT_PRODUCTS_DIR; };
A5532A9B1F615C4600186A9F /* SelectionViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; indentWidth = 2; lastKnownFileType = sourcecode.swift; path = SelectionViewController.swift; sourceTree = "<group>"; tabWidth = 2; };
AA0376A520DA5FBA00E6343A /* BasicTestSectionableViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BasicTestSectionableViewModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -260,6 +264,8 @@
AA52599520BC56010089ECD2 /* BasicTestViewModelViewController.swift */,
AA0376A520DA5FBA00E6343A /* BasicTestSectionableViewModel.swift */,
AA0376A720DA5FF700E6343A /* BasicTestSectionableViewModelViewController.swift */,
8B03025F222D77F200B3BEDB /* EmptyDataCollectionCell.swift */,
8B030263222EA22100B3BEDB /* EmptyDataTableCell.swift */,
);
path = Demo;
sourceTree = "<group>";
Expand Down Expand Up @@ -713,10 +719,12 @@
buildActionMask = 2147483647;
files = (
B5716F041F0CC54D005146DB /* BaseViewController.swift in Sources */,
8B030264222EA22100B3BEDB /* EmptyDataTableCell.swift in Sources */,
AA52599420BC4CF10089ECD2 /* BasicTestViewModel.swift in Sources */,
B5716F091F0CC54D005146DB /* TestTableCell.swift in Sources */,
AA0376A620DA5FBA00E6343A /* BasicTestSectionableViewModel.swift in Sources */,
B5716F031F0CC54D005146DB /* Generator.swift in Sources */,
8B030260222D77F200B3BEDB /* EmptyDataCollectionCell.swift in Sources */,
B5716F071F0CC54D005146DB /* ExpandableViewController.swift in Sources */,
B5716F0C1F0CC54D005146DB /* SourceViewController.swift in Sources */,
A5532A9C1F615C4600186A9F /* SelectionViewController.swift in Sources */,
Expand Down
61 changes: 17 additions & 44 deletions Demo/BaseViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class BaseCollectionViewController<T: CollectionViewSource>: BaseViewController<
}

class BaseLoaderViewController<T: UIView>: BaseViewController<T>, Loadable, Containerable where T: AccessorView, T.Source: LoaderReusableSource {

convenience init(type: LoaderResult) {
self.init()
self.type = type
Expand All @@ -102,26 +102,6 @@ class BaseLoaderViewController<T: UIView>: BaseViewController<T>, Loadable, Cont
return activityIndicator
} ()

let errorLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 22)
label.textColor = UIColor.black
label.text = "Error"
label.textAlignment = .center
label.isHidden = true
return label
}()

let noDataLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 22)
label.textColor = UIColor.black
label.text = "No data"
label.textAlignment = .center
label.isHidden = true
return label
}()

override func loadView() {
super.loadView()

Expand All @@ -132,35 +112,13 @@ class BaseLoaderViewController<T: UIView>: BaseViewController<T>, Loadable, Cont
containerView.source.stopProgress = { [weak self] _ in
self?.activityIndicator.stopAnimating()
}

containerView.source.updateEmptyView = { [weak self] state in
guard let strongSelf = self else { return }

switch state {
case .empty:
strongSelf.noDataLabel.isHidden = false
strongSelf.errorLabel.isHidden = true
case .error:
strongSelf.noDataLabel.isHidden = true
strongSelf.errorLabel.isHidden = false
default:
strongSelf.noDataLabel.isHidden = true
strongSelf.errorLabel.isHidden = true
}
}
}

override func viewDidLoad() {
super.viewDidLoad()

view.addSubview(activityIndicator)
view.addSubview(noDataLabel)
view.addSubview(errorLabel)
noDataLabel.sizeToFit()
errorLabel.sizeToFit()
activityIndicator.center = view.center
noDataLabel.center = view.center
errorLabel.center = view.center
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -224,12 +182,27 @@ class BaseLoaderViewController<T: UIView>: BaseViewController<T>, Loadable, Cont
}
}

class BaseLoaderTableViewController<T: LoaderReusableSource>: BaseLoaderViewController<TableView<T>> where T.Container == UITableView { }
class BaseLoaderTableViewController<T: LoaderReusableSource>: BaseLoaderViewController<TableView<T>> where T.Container == UITableView {

typealias Empty = TableCell<EmptyDataTableCell>

override func loadView() {
super.loadView()
containerView.source.noDataState = { state in
return [Section(cells: [Empty(data: state)])]
}
}
}

class BaseLoaderCollectionViewController<T: LoaderReusableSource>: BaseLoaderViewController<CollectionView<T>> where T.Container == UICollectionView {

typealias Empty = CollectionCell<EmptyDataCollectionCell>

override func loadView() {
super.loadView()
containerView.source.noDataState = { state in
return [Section(cells: [Empty(data: state)])]
}
containerView.collectionViewLayout = collectionViewLayout()
}

Expand Down
43 changes: 3 additions & 40 deletions Demo/BasicDataExampleCollectionViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,33 +15,14 @@ class BasicDataExampleCollectionViewController: UIViewController, Loadable, Acce

typealias Cell = CollectionCell<TestCollectionCell>
typealias Header = CollectionCell<TestCollectionHeaderCell>
typealias Empty = CollectionCell<EmptyDataCollectionCell>

let activityIndicator: UIActivityIndicatorView = {
let activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
activityIndicator.color = .black
return activityIndicator
} ()

let errorLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 22)
label.textColor = UIColor.black
label.text = "Error"
label.textAlignment = .center
label.isHidden = true
return label
}()

let noDataLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 22)
label.textColor = UIColor.black
label.text = "No data"
label.textAlignment = .center
label.isHidden = true
return label
}()

let containerView = CollectionView<LoaderDecoratorSource<CollectionViewSource>>()

var sections: [Sectionable] {
Expand All @@ -66,20 +47,8 @@ class BasicDataExampleCollectionViewController: UIViewController, Loadable, Acce
containerView.source.stopProgress = { [weak self] _ in
self?.activityIndicator.stopAnimating()
}
containerView.source.updateEmptyView = { [weak self] state in
guard let strongSelf = self else { return }

switch state {
case .empty:
strongSelf.noDataLabel.isHidden = false
strongSelf.errorLabel.isHidden = true
case .error:
strongSelf.noDataLabel.isHidden = true
strongSelf.errorLabel.isHidden = false
default:
strongSelf.noDataLabel.isHidden = true
strongSelf.errorLabel.isHidden = true
}
containerView.source.noDataState = { state in
return [Section(cells: [Empty(data: state)])]
}
containerView.collectionViewLayout = collectionViewLayout()

Expand All @@ -89,13 +58,7 @@ class BasicDataExampleCollectionViewController: UIViewController, Loadable, Acce
}

view.addSubview(activityIndicator)
view.addSubview(noDataLabel)
view.addSubview(errorLabel)
noDataLabel.sizeToFit()
errorLabel.sizeToFit()
activityIndicator.center = view.center
noDataLabel.center = view.center
errorLabel.center = view.center
}

func collectionViewLayout() -> UICollectionViewFlowLayout {
Expand Down
46 changes: 5 additions & 41 deletions Demo/BasicTestSectionableViewModelViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,14 @@ import Astrolabe
import RxSwift

class BasicTestSectionableViewModelViewController: UIViewController, Accessor {

typealias Empty = CollectionCell<EmptyDataCollectionCell>

let activityIndicator: UIActivityIndicatorView = {
let activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
activityIndicator.color = .black
return activityIndicator
} ()

let errorLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 22)
label.textColor = UIColor.black
label.text = "Error"
label.textAlignment = .center
label.isHidden = true
return label
} ()

let noDataLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 22)
label.textColor = UIColor.black
label.text = "No data"
label.textAlignment = .center
label.isHidden = true
return label
} ()

let containerView = CollectionView<LoaderDecoratorSource<CollectionViewSource>>()
let viewModel = BasicTestSectionableViewModel()
let disposeBag = DisposeBag()
Expand Down Expand Up @@ -65,21 +46,10 @@ class BasicTestSectionableViewModelViewController: UIViewController, Accessor {
containerView.source.stopProgress = { [weak self] _ in
self?.activityIndicator.stopAnimating()
}
containerView.source.updateEmptyView = { [weak self] state in
guard let strongSelf = self else { return }

switch state {
case .empty:
strongSelf.noDataLabel.isHidden = false
strongSelf.errorLabel.isHidden = true
case .error:
strongSelf.noDataLabel.isHidden = true
strongSelf.errorLabel.isHidden = false
default:
strongSelf.noDataLabel.isHidden = true
strongSelf.errorLabel.isHidden = true
}
containerView.source.noDataState = { state in
return [Section(cells: [Empty(data: state)])]
}

containerView.collectionViewLayout = collectionViewLayout()

view.addSubview(containerView)
Expand All @@ -88,14 +58,8 @@ class BasicTestSectionableViewModelViewController: UIViewController, Accessor {
}

view.addSubview(activityIndicator)
view.addSubview(noDataLabel)
view.addSubview(errorLabel)

noDataLabel.sizeToFit()
errorLabel.sizeToFit()
activityIndicator.center = view.center
noDataLabel.center = view.center
errorLabel.center = view.center

viewModel.sectionPubliser.subscribe(onNext: { [weak self] sections in
self?.sections = sections
Expand Down
46 changes: 5 additions & 41 deletions Demo/BasicTestViewModelViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,33 +11,14 @@ import Astrolabe
import RxSwift

class BasicTestViewModelViewController: UIViewController, Accessor {

typealias Empty = CollectionCell<EmptyDataCollectionCell>

let activityIndicator: UIActivityIndicatorView = {
let activityIndicator = UIActivityIndicatorView(style: .whiteLarge)
activityIndicator.color = .black
return activityIndicator
} ()

let errorLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 22)
label.textColor = UIColor.black
label.text = "Error"
label.textAlignment = .center
label.isHidden = true
return label
} ()

let noDataLabel: UILabel = {
let label = UILabel()
label.font = UIFont.systemFont(ofSize: 22)
label.textColor = UIColor.black
label.text = "No data"
label.textAlignment = .center
label.isHidden = true
return label
} ()

let containerView = CollectionView<LoaderDecoratorSource<CollectionViewSource>>()
let viewModel = BasicTestViewModel()
let disposeBag = DisposeBag()
Expand Down Expand Up @@ -65,21 +46,10 @@ class BasicTestViewModelViewController: UIViewController, Accessor {
containerView.source.stopProgress = { [weak self] _ in
self?.activityIndicator.stopAnimating()
}
containerView.source.updateEmptyView = { [weak self] state in
guard let strongSelf = self else { return }

switch state {
case .empty:
strongSelf.noDataLabel.isHidden = false
strongSelf.errorLabel.isHidden = true
case .error:
strongSelf.noDataLabel.isHidden = true
strongSelf.errorLabel.isHidden = false
default:
strongSelf.noDataLabel.isHidden = true
strongSelf.errorLabel.isHidden = true
}
containerView.source.noDataState = { state in
return [Section(cells: [Empty(data: state)])]
}

containerView.collectionViewLayout = collectionViewLayout()

view.addSubview(containerView)
Expand All @@ -88,14 +58,8 @@ class BasicTestViewModelViewController: UIViewController, Accessor {
}

view.addSubview(activityIndicator)
view.addSubview(noDataLabel)
view.addSubview(errorLabel)

noDataLabel.sizeToFit()
errorLabel.sizeToFit()
activityIndicator.center = view.center
noDataLabel.center = view.center
errorLabel.center = view.center

viewModel.sectionPubliser.subscribe(onNext: { [weak self] sections in
self?.sections = sections
Expand Down
Loading

0 comments on commit 29ca850

Please sign in to comment.