Skip to content

Commit

Permalink
Merge pull request #37 from wizeline/feature/add-custom-view
Browse files Browse the repository at this point in the history
Adding custom views as Empty State
  • Loading branch information
Jorge R Ovalle Z authored Jul 24, 2019
2 parents 4dcf9da + 647f281 commit 66c3203
Show file tree
Hide file tree
Showing 22 changed files with 700 additions and 590 deletions.
6 changes: 3 additions & 3 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ PODS:
- iOSSnapshotTestCase/Core (4.0.1)
- iOSSnapshotTestCase/SwiftSupport (4.0.1):
- iOSSnapshotTestCase/Core
- WLEmptyState (1.0.2)
- WLEmptyState (2.0.0)

DEPENDENCIES:
- iOSSnapshotTestCase
Expand All @@ -20,8 +20,8 @@ EXTERNAL SOURCES:

SPEC CHECKSUMS:
iOSSnapshotTestCase: f3b2b7e606fe03fdbe49af84316bd235df32dc44
WLEmptyState: 083ceb8181b65b0008e1cca260b14a768f6c3ab6
WLEmptyState: 0573edb909655cd6e5e7a367b371a10525dcee5a

PODFILE CHECKSUM: 5fd6a2332b7cd50dfecaf0807164322168c50c78

COCOAPODS: 1.6.1
COCOAPODS: 1.7.3
15 changes: 8 additions & 7 deletions Example/Pods/Local Podspecs/WLEmptyState.podspec.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions Example/Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1,066 changes: 530 additions & 536 deletions Example/Pods/Pods.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 23 additions & 2 deletions Example/Tests/DataSourceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class DataSourceTests: XCTestCase {
XCTAssertNotNil(imageDataSource)
}

// MARK: - Custom protocol implementation
// MARK: - Custom protocol implementation for default component

func testTableCustomDataSetProperties() {
let customTableViewController = CustomTableViewController()
Expand Down Expand Up @@ -77,5 +77,26 @@ class DataSourceTests: XCTestCase {
let imageDataSource = customCollectionViewController.collectionView.emptyStateDataSource?.imageForEmptyDataSet()
XCTAssertNil(imageDataSource)
}


// MARK: Custom view on data source

func testTableCustomEmptyStateView() {
let customTableViewController = CustomEmptyStateTableViewController()

let view = UIView()
view.backgroundColor = .green

XCTAssertEqual(customTableViewController.tableView.emptyStateDataSource?.customViewForEmptyState()?.backgroundColor, view.backgroundColor)
}

func testCollectionCustomEmptyStateView() {
let customCollectionViewController = CustomEmptyStateCollectionViewController(collectionViewLayout: UICollectionViewFlowLayout())

let view = UIView()
view.backgroundColor = .green

XCTAssertEqual(customCollectionViewController.collectionView.emptyStateDataSource?.customViewForEmptyState()?.backgroundColor, view.backgroundColor)
}


}
24 changes: 24 additions & 0 deletions Example/Tests/UICollectionViewMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import Foundation
import WLEmptyState

// MARK: Default Empty State on UICollectionView

final class DefaultCollectionViewController: UICollectionViewController, WLEmptyStateDataSource {

override func viewDidLoad() {
Expand All @@ -18,6 +20,8 @@ final class DefaultCollectionViewController: UICollectionViewController, WLEmpty

}

// MARK: Customized default Empty State on UICollectionView

final class CustomCollectionViewController: UICollectionViewController, WLEmptyStateDataSource {

override func viewDidLoad() {
Expand All @@ -38,3 +42,23 @@ final class CustomCollectionViewController: UICollectionViewController, WLEmptyS
return nil
}
}

// MARK: Custom Empty State on UICollectionView

final class CustomEmptyStateCollectionViewController: UICollectionViewController, WLEmptyStateDataSource {

override func viewDidLoad() {
super.viewDidLoad()

collectionView.emptyStateDataSource = self
}

func customViewForEmptyState() -> UIView? {
let view = UIView()
view.backgroundColor = .green
return view
}

}


23 changes: 23 additions & 0 deletions Example/Tests/UITableViewMocks.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import Foundation
import WLEmptyState

// MARK: Default Empty State on UITableView

final class DefaultTableViewController: UITableViewController, WLEmptyStateDataSource {

override func viewDidLoad() {
Expand All @@ -18,6 +20,8 @@ final class DefaultTableViewController: UITableViewController, WLEmptyStateDataS

}

// MARK: Customized default Empty State on UITableView

final class CustomTableViewController: UITableViewController, WLEmptyStateDataSource {

override func viewDidLoad() {
Expand All @@ -39,6 +43,25 @@ final class CustomTableViewController: UITableViewController, WLEmptyStateDataSo
}
}

// MARK: Custom Empty State on UITableView

final class CustomEmptyStateTableViewController: UITableViewController, WLEmptyStateDataSource {

override func viewDidLoad() {
super.viewDidLoad()

tableView.emptyStateDataSource = self
}

func customViewForEmptyState() -> UIView? {
let view = UIView()
view.backgroundColor = .green
return view
}

}


enum DefaultDataSource {
static let title = NSAttributedString(string: "Empty DataSet",
attributes: [NSAttributedString.Key.font: UIFont.preferredFont(forTextStyle: .headline)])
Expand Down
8 changes: 0 additions & 8 deletions Example/WLEmptyState.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -335,15 +335,11 @@
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-WLEmptyState_Tests/Pods-WLEmptyState_Tests-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/iOSSnapshotTestCase/FBSnapshotTestCase.framework",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
);
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/FBSnapshotTestCase.framework",
);
Expand All @@ -357,15 +353,11 @@
buildActionMask = 2147483647;
files = (
);
inputFileListPaths = (
);
inputPaths = (
"${PODS_ROOT}/Target Support Files/Pods-WLEmptyState_Example/Pods-WLEmptyState_Example-frameworks.sh",
"${BUILT_PRODUCTS_DIR}/WLEmptyState/WLEmptyState.framework",
);
name = "[CP] Embed Pods Frameworks";
outputFileListPaths = (
);
outputPaths = (
"${TARGET_BUILD_DIR}/${FRAMEWORKS_FOLDER_PATH}/WLEmptyState.framework",
);
Expand Down
1 change: 0 additions & 1 deletion Example/WLEmptyState/SampleTableViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,3 @@ final class SampleTableViewController: UITableViewController, WLEmptyStateDataSo
}

}

18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ After you run your project with an empty dataset for your entity, you'll be able

![Default WLEmptyState](https://user-images.githubusercontent.com/6756995/52525837-21b7cc00-2c75-11e9-8ef4-6c2ca226ddb3.png)

## Customizing WLEmptyState

If you want to customize the text, description, or image, you need to implement the `WLEmptyStateDataSource` function. You can find the code for customization in the following list:
## Customizing Default WLEmptyState
If you want to customize the text, description, or image, of the defatult component you need to implement the `WLEmptyStateDataSource` function. You can find the code for customization in the following list:

- Customize Image

Expand Down Expand Up @@ -125,6 +124,19 @@ func descriptionForEmptyDataSet() -> NSAttributedString {

<img src="https://user-images.githubusercontent.com/3466367/52595877-8fe6c500-2e14-11e9-85f3-2937746d4b24.png" width="300">

## Creating your own EmptyState
Using `customViewForEmptyState()` allows you to provide your own implementation for Empty State.

```Swift
func customViewForEmptyState() -> UIView? {
let activityIndicatorView = UIActivityIndicatorView()
activityIndicatorView.startAnimating()
activityIndicatorView.color = .purple
return activityIndicatorView
}
```
![Simulator Screen Shot - iPhone X - 2019-07-24 at 16 07 43](https://user-images.githubusercontent.com/6756995/61828904-62735e00-ae2d-11e9-8020-2014ac3bfb17.png)

## Contributing to WLEmptyState

We actively welcome your pull requests. We are trying to make contributions to this project as transparent and accessible as possible, please read our [Contributing guidelines](contributing.md) and follow the [Code of conduct](CODE_OF_CONDUCT.md).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,21 @@ extension UICollectionView: WLEmptyStateProtocol {
}

@objc private dynamic func swizzledReload() {
self.swizzledReload()
swizzledReload()

if numberOfItems == 0 && self.subviews.count > 1 {
emptyStateView.titleLabel.attributedText = self.emptyStateDataSource?.titleForEmptyDataSet()
emptyStateView.descriptionLabel.attributedText = self.emptyStateDataSource?.descriptionForEmptyDataSet()
emptyStateView.image.image = self.emptyStateDataSource?.imageForEmptyDataSet()

backgroundView = emptyStateView

if let emptyStateView = emptyStateView as? EmptyStateView {
emptyStateView.titleLabel.attributedText = self.emptyStateDataSource?.titleForEmptyDataSet()
emptyStateView.descriptionLabel.attributedText = self.emptyStateDataSource?.descriptionForEmptyDataSet()
emptyStateView.image.image = self.emptyStateDataSource?.imageForEmptyDataSet()
} else {
emptyStateView.translatesAutoresizingMaskIntoConstraints = false
emptyStateView.heightAnchor.constraint(equalTo: heightAnchor).isActive = true
emptyStateView.widthAnchor.constraint(equalTo: widthAnchor).isActive = true
emptyStateView.centerYAnchor.constraint(equalTo: centerYAnchor).isActive = true
emptyStateView.centerXAnchor.constraint(equalTo: centerXAnchor).isActive = true
}
} else {
removeEmptyView()
}
Expand Down
Loading

0 comments on commit 66c3203

Please sign in to comment.