Skip to content

Commit c9c6e07

Browse files
committed
Perfect Trending Repos
Former-commit-id: bb3b568
1 parent 00e4444 commit c9c6e07

15 files changed

+334
-30
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
f1ba80a80162553d07ef5520c62a894b020a680b
1+
05d5f2f4536dd17e00ab3cee6cf047777b39c9a4

MVVMReactiveCocoa/Model/MRCRepositoryServiceImpl.m

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,18 @@ - (RACSignal *)requestTrendingRepositoriesSince:(NSString *)since language:(NSSt
5050
since = since.lowercaseString;
5151
language = language.lowercaseString;
5252

53+
if ([since isEqualToString:@"today"]) {
54+
since = @"daily";
55+
} else if ([since isEqualToString:@"this week"]) {
56+
since = @"weekly";
57+
} else if ([since isEqualToString:@"this month"]) {
58+
since = @"monthly";
59+
}
60+
61+
if ([language isEqualToString:@"all languages"]) {
62+
language = nil;
63+
}
64+
5365
return [[[RACSignal
5466
createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
5567
MKNetworkEngine *networkEngine = [[MKNetworkEngine alloc] initWithHostName:@"trending.codehub-app.com" apiPath:@"v2" customHeaderFields:nil];

MVVMReactiveCocoa/Util/MRCRouter.m

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ - (NSDictionary *)viewModelViewMappings {
5757
@"MRCPublicReposViewModel": @"MRCPublicReposViewController",
5858
@"MRCNewsViewModel": @"MRCNewsViewController",
5959
@"MRCSearchViewModel": @"MRCSearchViewController",
60+
@"MRCTrendingViewModel": @"MRCTrendingViewController",
61+
@"MRCTrendingSettingsViewModel": @"MRCTrendingSettingsViewController",
6062
};
6163
}
6264

MVVMReactiveCocoa/View/BaseClass/MRCViewController.m

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,10 +58,12 @@ - (void)bindViewModel {
5858
// Double title view
5959
MRCDoubleTitleView *doubleTitleView = [[MRCDoubleTitleView alloc] init];
6060

61+
RAC(doubleTitleView.titleLabel, text) = RACObserve(self.viewModel, title);
62+
RAC(doubleTitleView.subtitleLabel, text) = RACObserve(self.viewModel, subtitle);
63+
6164
@weakify(self)
62-
[[[self
65+
[[self
6366
rac_signalForSelector:@selector(viewWillTransitionToSize:withTransitionCoordinator:)]
64-
startWith:nil]
6567
subscribeNext:^(id x) {
6668
@strongify(self)
6769
doubleTitleView.titleLabel.text = self.viewModel.title;

MVVMReactiveCocoa/View/MRCHomepageViewController.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ - (void)viewDidLoad {
5656
MRCSearchViewController *searchViewController = [[MRCSearchViewController alloc] initWithViewModel:self.viewModel.searchViewModel];
5757

5858
UIImage *searchImage = [UIImage octicon_imageWithIdentifier:@"Search" size:CGSizeMake(25, 25)];
59-
searchViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Explore" image:searchImage tag:3];
59+
searchViewController.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Discover" image:searchImage tag:3];
6060

6161
[[MRCNavigationController alloc] initWithRootViewController:searchViewController];
6262
});

MVVMReactiveCocoa/View/MRCSearchViewController.m

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#import "MRCSearchViewController.h"
1010
#import "MRCSearchViewModel.h"
1111
#import "MRCReposSearchResultsViewController.h"
12+
#import "MRCTrendingViewModel.h"
1213

1314
@interface MRCSearchViewController () <UISearchControllerDelegate, UISearchBarDelegate>
1415

@@ -47,7 +48,17 @@ - (UIEdgeInsets)contentInset {
4748
}
4849

4950
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(MRCSearch *)search {
50-
cell.textLabel.text = search.keyword;
51+
if (indexPath.section == 0) {
52+
cell.imageView.image = [UIImage octicon_imageWithIcon:@"Flame"
53+
backgroundColor:[UIColor clearColor]
54+
iconColor:HexRGB(0x24AFFC)
55+
iconScale:1
56+
andSize:MRC_LEFT_IMAGE_SIZE];
57+
cell.textLabel.text = @"Trending";
58+
} else {
59+
cell.textLabel.text = search.keyword;
60+
}
61+
cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
5162
}
5263

5364
#pragma mark - UITableViewDataSource
@@ -68,14 +79,14 @@ - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger
6879
view.backgroundColor = HexRGB(0xF7F7F7);
6980

7081
UILabel *label = [[UILabel alloc] init];
71-
label.text = @"Recent Searches";
82+
label.text = section == 0 ? @"Explore" : @"Recent Searches";
7283
label.font = [UIFont systemFontOfSize:15];
7384
[label sizeToFit];
7485

7586
CGFloat height = [self tableView:tableView heightForHeaderInSection:section];
7687

7788
CGRect frame = label.frame;
78-
frame.origin.x = 20;
89+
frame.origin.x = 15;
7990
frame.origin.y = (height - frame.size.height) / 2;
8091
label.frame = frame;
8192

@@ -89,23 +100,28 @@ - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSIntege
89100
}
90101

91102
- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath {
92-
return UITableViewCellEditingStyleDelete;
103+
return indexPath.section == 0 ? UITableViewCellEditingStyleNone : UITableViewCellEditingStyleDelete;
93104
}
94105

95106
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
96107
[tableView deselectRowAtIndexPath:indexPath animated:YES];
97108

98-
[self presentViewController:self.searchController animated:YES completion:NULL];
99-
100-
MRCSearch *search = self.viewModel.dataSource[indexPath.section][indexPath.row];
101-
self.searchController.searchBar.text = search.keyword;
102-
103-
if ([self.searchController.searchBar.delegate respondsToSelector:@selector(searchBar:textDidChange:)]) {
104-
[self.searchController.searchBar.delegate searchBar:self.searchController.searchBar textDidChange:search.keyword];
105-
}
106-
107-
if ([self.searchController.searchBar.delegate respondsToSelector:@selector(searchBarSearchButtonClicked:)]) {
108-
[self.searchController.searchBar.delegate searchBarSearchButtonClicked:self.searchController.searchBar];
109+
if (indexPath.section == 0) {
110+
MRCTrendingViewModel *trendingViewModel = [[MRCTrendingViewModel alloc] initWithServices:self.viewModel.services params:nil];
111+
[self.viewModel.services pushViewModel:trendingViewModel animated:YES];
112+
} else {
113+
[self presentViewController:self.searchController animated:YES completion:NULL];
114+
115+
MRCSearch *search = self.viewModel.dataSource[indexPath.section][indexPath.row];
116+
self.searchController.searchBar.text = search.keyword;
117+
118+
if ([self.searchController.searchBar.delegate respondsToSelector:@selector(searchBar:textDidChange:)]) {
119+
[self.searchController.searchBar.delegate searchBar:self.searchController.searchBar textDidChange:search.keyword];
120+
}
121+
122+
if ([self.searchController.searchBar.delegate respondsToSelector:@selector(searchBarSearchButtonClicked:)]) {
123+
[self.searchController.searchBar.delegate searchBarSearchButtonClicked:self.searchController.searchBar];
124+
}
109125
}
110126
}
111127

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// MRCTrendingSettingsViewController.h
3+
// MVVMReactiveCocoa
4+
//
5+
// Created by leichunfeng on 15/10/21.
6+
// Copyright © 2015年 leichunfeng. All rights reserved.
7+
//
8+
9+
#import "MRCTableViewController.h"
10+
11+
@interface MRCTrendingSettingsViewController : MRCTableViewController
12+
13+
@end
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// MRCTrendingSettingsViewController.m
3+
// MVVMReactiveCocoa
4+
//
5+
// Created by leichunfeng on 15/10/21.
6+
// Copyright © 2015年 leichunfeng. All rights reserved.
7+
//
8+
9+
#import "MRCTrendingSettingsViewController.h"
10+
#import "MRCTrendingSettingsViewModel.h"
11+
12+
@interface MRCTrendingSettingsViewController ()
13+
14+
@property (nonatomic, strong, readonly) MRCTrendingSettingsViewModel *viewModel;
15+
16+
@end
17+
18+
@implementation MRCTrendingSettingsViewController
19+
20+
@dynamic viewModel;
21+
22+
- (void)viewDidLoad {
23+
[super viewDidLoad];
24+
25+
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel
26+
target:self
27+
action:@selector(didClickCancelButton:)];
28+
29+
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
30+
target:self
31+
action:@selector(didClickDoneButton:)];
32+
33+
@weakify(self)
34+
[[[RACSignal
35+
combineLatest:@[
36+
RACObserve(self.viewModel, since).distinctUntilChanged,
37+
RACObserve(self.viewModel, language).distinctUntilChanged
38+
]]
39+
skip:1]
40+
subscribeNext:^(id x) {
41+
@strongify(self)
42+
[self.tableView reloadData];
43+
}];
44+
}
45+
46+
- (void)configureCell:(UITableViewCell *)cell atIndexPath:(NSIndexPath *)indexPath withObject:(NSString *)string {
47+
cell.textLabel.text = string;
48+
cell.accessoryType = UITableViewCellAccessoryNone;
49+
50+
if (indexPath.section == 0) {
51+
if ([string isEqualToString:self.viewModel.since]) {
52+
cell.accessoryType = UITableViewCellAccessoryCheckmark;
53+
}
54+
} else if (indexPath.section == 1) {
55+
if ([string isEqualToString:self.viewModel.language]) {
56+
cell.accessoryType = UITableViewCellAccessoryCheckmark;
57+
}
58+
}
59+
}
60+
61+
- (void)didClickCancelButton:(id)sender {
62+
[self.viewModel.services dismissViewModelAnimated:YES completion:NULL];
63+
}
64+
65+
- (void)didClickDoneButton:(id)sender {
66+
if (self.viewModel.callback) {
67+
self.viewModel.callback(RACTuplePack(self.viewModel.since, self.viewModel.language));
68+
}
69+
[self.viewModel.services dismissViewModelAnimated:YES completion:NULL];
70+
}
71+
72+
#pragma mark - UITableViewDataSource
73+
74+
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
75+
return nil;
76+
}
77+
78+
#pragma mark - UITableViewDelegate
79+
80+
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
81+
[tableView deselectRowAtIndexPath:indexPath animated:YES];
82+
83+
NSString *string = self.viewModel.dataSource[indexPath.section][indexPath.row];
84+
if (indexPath.section == 0) {
85+
self.viewModel.since = string;
86+
} else if (indexPath.section == 1) {
87+
self.viewModel.language = string;
88+
}
89+
}
90+
91+
//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
92+
// return section == 0 ? 30 : 15;
93+
//}
94+
//
95+
//- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
96+
// return (section == tableView.numberOfSections - 1) ? 30 : 15;
97+
//}
98+
99+
@end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.XIB" version="3.0" toolsVersion="8191" systemVersion="15A284" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
3+
<dependencies>
4+
<deployment identifier="iOS"/>
5+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="8154"/>
6+
</dependencies>
7+
<objects>
8+
<placeholder placeholderIdentifier="IBFilesOwner" id="-1" userLabel="File's Owner" customClass="MRCTrendingSettingsViewController">
9+
<connections>
10+
<outlet property="tableView" destination="4QX-1N-Yyf" id="9y1-5P-Rvg"/>
11+
<outlet property="view" destination="i5M-Pr-FkT" id="sfx-zR-JGt"/>
12+
</connections>
13+
</placeholder>
14+
<placeholder placeholderIdentifier="IBFirstResponder" id="-2" customClass="UIResponder"/>
15+
<view clearsContextBeforeDrawing="NO" contentMode="scaleToFill" id="i5M-Pr-FkT">
16+
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
17+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
18+
<subviews>
19+
<tableView clipsSubviews="YES" contentMode="scaleToFill" alwaysBounceVertical="YES" style="grouped" separatorStyle="default" rowHeight="44" sectionHeaderHeight="18" sectionFooterHeight="18" translatesAutoresizingMaskIntoConstraints="NO" id="4QX-1N-Yyf">
20+
<rect key="frame" x="0.0" y="0.0" width="600" height="600"/>
21+
<animations/>
22+
<color key="backgroundColor" cocoaTouchSystemColor="groupTableViewBackgroundColor"/>
23+
<connections>
24+
<outlet property="dataSource" destination="-1" id="sm6-DQ-ASF"/>
25+
<outlet property="delegate" destination="-1" id="xez-dE-fbb"/>
26+
<outlet property="emptyDataSetDelegate" destination="-1" id="6ql-2F-8d6"/>
27+
<outlet property="emptyDataSetSource" destination="-1" id="0FC-mx-By4"/>
28+
</connections>
29+
</tableView>
30+
</subviews>
31+
<animations/>
32+
<color key="backgroundColor" white="1" alpha="1" colorSpace="custom" customColorSpace="calibratedWhite"/>
33+
<constraints>
34+
<constraint firstItem="4QX-1N-Yyf" firstAttribute="leading" secondItem="i5M-Pr-FkT" secondAttribute="leading" id="7Hh-FK-PzZ"/>
35+
<constraint firstAttribute="trailing" secondItem="4QX-1N-Yyf" secondAttribute="trailing" id="AdB-oY-jrU"/>
36+
<constraint firstItem="4QX-1N-Yyf" firstAttribute="top" secondItem="i5M-Pr-FkT" secondAttribute="top" id="E0p-Jl-ec6"/>
37+
<constraint firstAttribute="bottom" secondItem="4QX-1N-Yyf" secondAttribute="bottom" id="p0T-Hn-xsq"/>
38+
</constraints>
39+
<point key="canvasLocation" x="360" y="379"/>
40+
</view>
41+
</objects>
42+
</document>

MVVMReactiveCocoa/View/MRCTrendingViewController.m

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

99
#import "MRCTrendingViewController.h"
10+
#import "MRCTrendingViewModel.h"
11+
#import "MRCTrendingSettingsViewModel.h"
1012

1113
@interface MRCTrendingViewController ()
1214

15+
@property (nonatomic, strong, readonly) MRCTrendingViewModel *viewModel;
16+
1317
@end
1418

1519
@implementation MRCTrendingViewController
1620

21+
@dynamic viewModel;
22+
1723
- (void)viewDidLoad {
1824
[super viewDidLoad];
1925

@@ -26,8 +32,26 @@ - (void)viewDidLoad {
2632
action:@selector(didClickSettingsButton:)];
2733
}
2834

35+
- (UIEdgeInsets)contentInset {
36+
return UIEdgeInsetsMake(64, 0, 0, 0);
37+
}
38+
2939
- (void)didClickSettingsButton:(id)sender {
40+
NSDictionary *params = @{ @"since": self.viewModel.since ?: @"", @"language": self.viewModel.language ?: @"", };
41+
42+
MRCTrendingSettingsViewModel *settingsViewModel = [[MRCTrendingSettingsViewModel alloc] initWithServices:self.viewModel.services params:params];
43+
44+
@weakify(self)
45+
settingsViewModel.callback = ^(RACTuple *tuple) {
46+
@strongify(self)
47+
48+
RACTupleUnpack(NSString *since, NSString *language) = tuple;
49+
50+
self.viewModel.since = since;
51+
self.viewModel.language = language;
52+
};
3053

54+
[self.viewModel.services presentViewModel:settingsViewModel animated:YES completion:NULL];
3155
}
3256

3357
@end

MVVMReactiveCocoa/ViewModel/MRCSearchViewModel.m

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,12 @@ - (void)initialize {
2525
rac_addObserverForName:MRCRecentSearchesDidChangeNotification object:nil]
2626
startWith:nil]
2727
map:^(id value) {
28-
NSArray *recentSearches = [MRCSearch recentSearches];
29-
return recentSearches ? @[ recentSearches ] : nil;
28+
MRCSearch *trendingSearch = [[MRCSearch alloc] init];
29+
30+
NSArray *trendingSearchs = @[ trendingSearch ];
31+
NSArray *recentSearches = [MRCSearch recentSearches] ?: @[];
32+
33+
return @[ trendingSearchs, recentSearches ];
3034
}];
3135
}
3236

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//
2+
// MRCTrendingSettingsViewModel.h
3+
// MVVMReactiveCocoa
4+
//
5+
// Created by leichunfeng on 15/10/21.
6+
// Copyright © 2015年 leichunfeng. All rights reserved.
7+
//
8+
9+
#import "MRCTableViewModel.h"
10+
11+
@interface MRCTrendingSettingsViewModel : MRCTableViewModel
12+
13+
@property (nonatomic, copy) NSString *since;
14+
@property (nonatomic, copy) NSString *language;
15+
16+
@end

0 commit comments

Comments
 (0)