Skip to content

Commit 752a25d

Browse files
author
Joey Lorich
committed
Add placeholder to collection view when there are no items
1 parent 2f9ee18 commit 752a25d

6 files changed

Lines changed: 196 additions & 11 deletions

File tree

EasyReader.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@
8989
9D010F1718F83940001999EE /* EZRMenuFeedCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D010F1518F83940001999EE /* EZRMenuFeedCell.m */; };
9090
9D010F1818F83940001999EE /* EZRMenuFeedCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D010F1518F83940001999EE /* EZRMenuFeedCell.m */; };
9191
9D010F1918F83940001999EE /* EZRMenuFeedCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D010F1518F83940001999EE /* EZRMenuFeedCell.m */; };
92+
9D0602131911970D009A94C0 /* UIView+PlaceholderAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D0602121911970D009A94C0 /* UIView+PlaceholderAdditions.m */; };
93+
9D0602141911970D009A94C0 /* UIView+PlaceholderAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D0602121911970D009A94C0 /* UIView+PlaceholderAdditions.m */; };
94+
9D0602151911970D009A94C0 /* UIView+PlaceholderAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D0602121911970D009A94C0 /* UIView+PlaceholderAdditions.m */; };
95+
9D0602161911970D009A94C0 /* UIView+PlaceholderAdditions.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D0602121911970D009A94C0 /* UIView+PlaceholderAdditions.m */; };
9296
9D06C6481901AE190029A233 /* EZRHomeSocialToolbarDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D06C6471901AE190029A233 /* EZRHomeSocialToolbarDataSource.m */; };
9397
9D06C6491901AE190029A233 /* EZRHomeSocialToolbarDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D06C6471901AE190029A233 /* EZRHomeSocialToolbarDataSource.m */; };
9498
9D06C64A1901AE190029A233 /* EZRHomeSocialToolbarDataSource.m in Sources */ = {isa = PBXBuildFile; fileRef = 9D06C6471901AE190029A233 /* EZRHomeSocialToolbarDataSource.m */; };
@@ -387,6 +391,8 @@
387391
9D010F0F18F832C5001999EE /* EZRTestFlightService.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EZRTestFlightService.m; sourceTree = "<group>"; };
388392
9D010F1418F8392F001999EE /* EZRMenuFeedCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZRMenuFeedCell.h; sourceTree = "<group>"; };
389393
9D010F1518F83940001999EE /* EZRMenuFeedCell.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EZRMenuFeedCell.m; sourceTree = "<group>"; };
394+
9D0602111911970D009A94C0 /* UIView+PlaceholderAdditions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "UIView+PlaceholderAdditions.h"; sourceTree = "<group>"; };
395+
9D0602121911970D009A94C0 /* UIView+PlaceholderAdditions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "UIView+PlaceholderAdditions.m"; sourceTree = "<group>"; };
390396
9D06C6461901AE190029A233 /* EZRHomeSocialToolbarDataSource.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = EZRHomeSocialToolbarDataSource.h; path = Home/EZRHomeSocialToolbarDataSource.h; sourceTree = "<group>"; };
391397
9D06C6471901AE190029A233 /* EZRHomeSocialToolbarDataSource.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = EZRHomeSocialToolbarDataSource.m; path = Home/EZRHomeSocialToolbarDataSource.m; sourceTree = "<group>"; };
392398
9D16D32618DB57D0001E9C38 /* EZRRegisterRoutesService.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EZRRegisterRoutesService.h; sourceTree = "<group>"; };
@@ -619,6 +625,8 @@
619625
2751F4D118DA02C100339C96 /* Categories */ = {
620626
isa = PBXGroup;
621627
children = (
628+
9D0602111911970D009A94C0 /* UIView+PlaceholderAdditions.h */,
629+
9D0602121911970D009A94C0 /* UIView+PlaceholderAdditions.m */,
622630
9D010F0818F72F0B001999EE /* NSSet+CSSortingAdditions.h */,
623631
9D010F0918F72F0B001999EE /* NSSet+CSSortingAdditions.m */,
624632
9D5ACFBD18E5C787003CF3E7 /* UIImageView+EZRFeedImageAdditions.h */,
@@ -1384,6 +1392,7 @@
13841392
9D00DE86190816FF00A0C65F /* CLDSocialShareToolbar.m in Sources */,
13851393
9DFD89F818FC7D9300630CBF /* CSArrayTableViewDataSource.m in Sources */,
13861394
9D16D33E18DB7767001E9C38 /* EZRFeedUpdateService.m in Sources */,
1395+
9D0602151911970D009A94C0 /* UIView+PlaceholderAdditions.m in Sources */,
13871396
9D9CBAC318DCC52900532F8C /* EZRApplicationStyleService.m in Sources */,
13881397
9D639E7818E0A52300350B5F /* APIMockRequest.m in Sources */,
13891398
9D010F1818F83940001999EE /* EZRMenuFeedCell.m in Sources */,
@@ -1446,6 +1455,7 @@
14461455
9D00DE87190816FF00A0C65F /* CLDSocialShareToolbar.m in Sources */,
14471456
9DFD89F918FC7D9300630CBF /* CSArrayTableViewDataSource.m in Sources */,
14481457
9D16D33F18DB7767001E9C38 /* EZRFeedUpdateService.m in Sources */,
1458+
9D0602161911970D009A94C0 /* UIView+PlaceholderAdditions.m in Sources */,
14491459
9D9CBAC418DCC52A00532F8C /* EZRApplicationStyleService.m in Sources */,
14501460
9D639E7918E0A52300350B5F /* APIMockRequest.m in Sources */,
14511461
9D010F1918F83940001999EE /* EZRMenuFeedCell.m in Sources */,
@@ -1534,6 +1544,7 @@
15341544
9D00DE85190816FF00A0C65F /* CLDSocialShareToolbar.m in Sources */,
15351545
9DFD89F718FC7D9300630CBF /* CSArrayTableViewDataSource.m in Sources */,
15361546
9D9CBAC718DCC68A00532F8C /* EZRFeedUpdateService.m in Sources */,
1547+
9D0602141911970D009A94C0 /* UIView+PlaceholderAdditions.m in Sources */,
15371548
9D9CBAC818DCC68A00532F8C /* EZRApplicationStyleService.m in Sources */,
15381549
9D010F1718F83940001999EE /* EZRMenuFeedCell.m in Sources */,
15391550
9D9CBAC918DCC68A00532F8C /* Feed.m in Sources */,
@@ -1596,6 +1607,7 @@
15961607
9D00DE84190816F900A0C65F /* CLDSocialShareToolbar.m in Sources */,
15971608
9DFD89F618FC7D9300630CBF /* CSArrayTableViewDataSource.m in Sources */,
15981609
9D16D34E18DBA9E9001E9C38 /* APIRouter.m in Sources */,
1610+
9D0602131911970D009A94C0 /* UIView+PlaceholderAdditions.m in Sources */,
15991611
9D16D33D18DB7767001E9C38 /* EZRFeedUpdateService.m in Sources */,
16001612
9D9CBABF18DC826700532F8C /* EZRApplicationStyleService.m in Sources */,
16011613
9D010F1618F83940001999EE /* EZRMenuFeedCell.m in Sources */,
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
2+
//
3+
// UIView+PlaceholderAdditions.h
4+
// uwithus
5+
//
6+
// Created by Joseph Lorich on 3/10/14.
7+
// Copyright (c) 2014 CSORGNAME. All rights reserved.
8+
//
9+
10+
#import <UIKit/UIKit.h>
11+
12+
/**
13+
* Helpful additions for adding placeholders instead of an empty UIView
14+
*/
15+
@interface UIView (PlaceholderAdditions)
16+
17+
18+
#pragma mark - Methods
19+
20+
/**
21+
* Hides the current UIView and adds the given placeholder centered over where this view was
22+
* Replaces any pre-existing placeholder views on this view
23+
*/
24+
- (void)hideAndInstantiatePlaceHolderWithView:(UIView *)placeholderView;
25+
26+
/**
27+
* Inserts the given placeholder centered inside this view.
28+
* Replaces any pre-existing placeholder views on this view
29+
*/
30+
- (void)insertCenteredPlaceholderView:(UIView *)placeholderView;
31+
32+
/**
33+
* Removes all placeholder views and shows the original view
34+
*/
35+
- (void)removePlaceholderViewAndShow;
36+
37+
/**
38+
* Generates a plain placeholder with a title and replaces the current view with it
39+
*/
40+
- (void)hideAndInstantiatePlaceHolderWithTitle:(NSString *)title;
41+
42+
43+
#pragma mark - Properties
44+
45+
/// An array of currently existing placholder views
46+
@property (nonatomic, retain) UIView *placeholderView;
47+
48+
49+
@end
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//
2+
// UIView+PlaceholderAdditions.m
3+
// uwithus
4+
//
5+
// Created by Joseph Lorich on 3/10/14.
6+
// Copyright (c) 2014 CSORGNAME. All rights reserved.
7+
//
8+
9+
#import "UIView+PlaceholderAdditions.h"
10+
#import <objc/runtime.h>
11+
#import <Block-KVO/MTKObserving.h>
12+
13+
14+
static void *PlaceholderPropertyKey = &PlaceholderPropertyKey;
15+
16+
@implementation UIView (PlaceholderAdditions)
17+
18+
- (UIView *)placeholderView {
19+
return objc_getAssociatedObject(self, PlaceholderPropertyKey);
20+
}
21+
22+
- (void)setPlaceholderView:(NSArray *)placeholderView {
23+
objc_setAssociatedObject(self, PlaceholderPropertyKey, placeholderView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
24+
}
25+
26+
27+
/**
28+
* Generates a frame centered over another frame
29+
*/
30+
- (CGRect)centeredFrame:(CGRect)frame overFrame:(CGRect)overFrame
31+
{
32+
return CGRectMake(
33+
overFrame.origin.x + overFrame.size.width/2.0 - frame.size.width/2.0,
34+
overFrame.origin.y + overFrame.size.height/2.0 - frame.size.height/2.0,
35+
frame.size.width,
36+
frame.size.height
37+
);
38+
}
39+
40+
/**
41+
* Updates the placeholderView frame on parent frame change
42+
*/
43+
- (void)frameDidChange:(id)sender
44+
{
45+
if (self.placeholderView)
46+
{
47+
CGRect frame = [self centeredFrame:self.placeholderView.frame overFrame:self.frame];
48+
[self.placeholderView setFrame:frame];
49+
}
50+
}
51+
52+
- (void)insertCenteredPlaceholderView:(UIView *)placeholderView
53+
{
54+
[self removeAllObservations];
55+
[self observeProperty:@"bounds" withSelector:@selector(frameDidChange:)];
56+
57+
CGRect frame = [self centeredFrame:placeholderView.frame overFrame:self.frame];
58+
[placeholderView setFrame:frame];
59+
60+
[self addSubview:placeholderView];
61+
62+
if (self.placeholderView)
63+
{
64+
[self.placeholderView removeFromSuperview];
65+
}
66+
67+
self.placeholderView = placeholderView;
68+
69+
}
70+
71+
- (void)hideAndInstantiatePlaceHolderWithView:(UIView *)placeholderView
72+
{
73+
[self removeAllObservations];
74+
[self observeProperty:@"bounds" withSelector:@selector(frameDidChange:)];
75+
76+
77+
CGRect frame = [self centeredFrame:placeholderView.frame overFrame:self.frame];
78+
[placeholderView setFrame:frame];
79+
80+
[self setHidden:YES];
81+
[[self superview] addSubview:placeholderView];
82+
83+
if (self.placeholderView)
84+
{
85+
[self.placeholderView removeFromSuperview];
86+
}
87+
88+
self.placeholderView = placeholderView;
89+
}
90+
91+
- (void)removePlaceholderViewAndShow
92+
{
93+
[self removeAllObservations];
94+
[self.placeholderView removeFromSuperview];
95+
96+
self.placeholderView = nil;
97+
98+
[self setHidden:NO];
99+
}
100+
101+
102+
- (void)hideAndInstantiatePlaceHolderWithTitle:(NSString *)title
103+
{
104+
UIView *placeholder = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 280, 40)];
105+
106+
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 280, 40)];
107+
[label setTextColor:[UIColor lightGrayColor]];
108+
[label setTextAlignment:NSTextAlignmentCenter];
109+
[label setLineBreakMode:NSLineBreakByWordWrapping];
110+
[label setNumberOfLines:3];
111+
[label setFont:[UIFont fontWithName:@"HelveticaNeue" size:14.0f]];
112+
113+
[label setText:title];
114+
115+
[placeholder addSubview:label];
116+
117+
118+
[self hideAndInstantiatePlaceHolderWithView:placeholder];
119+
}
120+
121+
122+
123+
@end

EasyReader/Application/Controllers/EZRHomePageControlDataSource.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ @implementation EZRHomePageControlDataSource
1414

1515
- (NSInteger)numberOfPagesForPageControl
1616
{
17-
return [[EZRCurrentFeedsProvider shared].feedItems count];
17+
return [[EZRCurrentFeedsProvider shared].visibleFeedItems count];
1818
}
1919

2020
@end

EasyReader/Application/Controllers/Home/EZRHomeViewController.m

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#import "EZRGoogleAnalyticsService.h"
4242

4343
#import "CCARadialGradientLayer.h"
44+
#import "UIView+PlaceholderAdditions.h"
4445

4546
@interface EZRHomeViewController()
4647

@@ -290,6 +291,14 @@ - (void)selectedFeedDidChange:(NSNotification *)notification
290291
* Updates the data source when the feed items change
291292
*/
292293
- (void) visibleFeedItemsDidChange:(EZRCurrentFeedsProvider *)currentFeedService visibleFeeditems:(NSArray *)visibleFeedItems {
294+
if (visibleFeedItems.count == 0) {
295+
self.scrollView_vertical.scrollEnabled = NO;
296+
[self.collectionView_feedItems hideAndInstantiatePlaceHolderWithTitle:@"No feed items are available at this time."];
297+
} else {
298+
[self.collectionView_feedItems removePlaceholderViewAndShow];
299+
self.scrollView_vertical.scrollEnabled = YES;
300+
}
301+
293302
if (self.currentFeedItem) {
294303
lastSelectedFeedItem = self.currentFeedItem;
295304
}
@@ -304,15 +313,7 @@ - (void) visibleFeedItemsDidChange:(EZRCurrentFeedsProvider *)currentFeedService
304313
} else if ([visibleFeedItems count] > 0){
305314
[self resetWebView];
306315

307-
NSIndexPath *destinationIndexPath;
308-
309-
// if (self.currentPageIndex < [visibleFeedItems count]) {
310-
// destinationIndexPath = [NSIndexPath indexPathForRow:[visibleFeedItems count] -1 inSection:0];
311-
// } else {
312-
// destinationIndexPath = [NSIndexPath indexPathForRow:self.currentPageIndex - 1 inSection:0];
313-
// }
314-
315-
destinationIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
316+
NSIndexPath *destinationIndexPath = [NSIndexPath indexPathForRow:0 inSection:0];
316317

317318
[self.collectionView_feedItems scrollToItemAtIndexPath:destinationIndexPath atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
318319
[self loadURLForFeedItem:visibleFeedItems[0]];

EasyReader/Application/Controllers/Menu/MenuDataSources/EZRMenuUserFeedDataSource.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
5959

6060
if (indexPath.row == 0) {
6161
cell = [tableView dequeueReusableCellWithIdentifier:self.reusableCellIdentifier];
62+
((EZRMenuFeedCell*)cell).feed = nil;
6263
((EZRMenuFeedCell*)cell).label_name.text = @"All Feeds";
63-
6464
if (self.currentFeedsProvider.currentFeed == nil) {
6565
cell.selected = YES;
6666
}

0 commit comments

Comments
 (0)