-
Notifications
You must be signed in to change notification settings - Fork 0
/
BillsViewController.m
134 lines (113 loc) · 4.24 KB
/
BillsViewController.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
//
// BillsViewController.m
// Represent
//
// Created by Ria Vora on 7/14/20.
// Copyright © 2020 Ria Vora. All rights reserved.
//
#import "BillsViewController.h"
@interface BillsViewController () <UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSMutableArray *bills;
@property (strong, nonatomic) NSDate *lastRefreshed;
@property (strong, nonatomic) UIRefreshControl *refreshControl;
@property (assign, nonatomic) BOOL isMoreDataLoading;
@end
static int OFFSET = 20;
@implementation BillsViewController
#pragma mark - UIViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.tableView.delegate = self;
self.tableView.dataSource = self;
self.bills = [[NSMutableArray alloc] init];
[self initRefreshControl];
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.isMoreDataLoading = false;
self.lastRefreshed = [NSDate now];
[self updateBills];
}
- (void)fetchBills: (BOOL)getNewBills {
APIManager *manager = [APIManager new];
int offset = 0;
if (getNewBills) {
offset = OFFSET;
}
[manager fetchRecentBills:offset :^(NSArray * _Nonnull bills, NSError * _Nonnull error) {
if (error) {
NSLog(@"Error with fetching recent bills: %@", error.localizedDescription);
} else {
NSLog(@"Successfully fetched new bills");
for (NSDictionary *dictionary in bills) {
[Bill updateBills:dictionary];
}
[self getBillsParse:getNewBills];
[MBProgressHUD hideHUDForView:self.view animated:true];
}
}];
}
- (void)updateBills {
self.lastRefreshed = [NSDate now];
[self fetchBills:NO];
}
- (void)getBillsParse: (BOOL)getNewBills {
PFQuery *billQuery = [Bill query];
[billQuery orderByDescending:@"date"];
[billQuery whereKey:@"headBill" equalTo:@(YES)];
billQuery.limit = 20;
if (getNewBills) {
billQuery.skip = self.bills.count;
}
[billQuery findObjectsInBackgroundWithBlock:^(NSArray * _Nullable bills, NSError * _Nullable error) {
if (error) {
NSLog(@"Error with fetching bills from Parse: %@", error.localizedDescription);
} else {
NSLog(@"Success with fetching bills from Parse!");
if (getNewBills) {
for (Bill *bill in bills) {
[self.bills addObject:bill];
}
self.isMoreDataLoading = false;
OFFSET += 20;
} else {
self.bills = [NSMutableArray arrayWithArray:bills];
}
[self.tableView reloadData];
[self.refreshControl endRefreshing];
}
}];
}
- (void)initRefreshControl {
self.refreshControl = [[UIRefreshControl alloc] init];
[self.refreshControl addTarget:self action:@selector(updateBills) forControlEvents:UIControlEventValueChanged];
[self.tableView insertSubview:self.refreshControl atIndex:0];
}
#pragma mark - UITableViewDataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
BillCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BillCell"];
cell.bill = self.bills[indexPath.row];
[cell updateValues];
return cell;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
return self.bills.count;
}
#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
if (!self.isMoreDataLoading) {
int scrollViewContentHeight = self.tableView.contentSize.height;
int scrollOffsetThreshold = scrollViewContentHeight - self.tableView.bounds.size.height;
if (scrollView.contentOffset.y > scrollOffsetThreshold && self.tableView.isDragging) {
[MBProgressHUD showHUDAddedTo:self.view animated:YES];
self.isMoreDataLoading = true;
[self fetchBills:YES];
}
}
}
#pragma mark - Navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
BillCell *cell = sender;
BillDetailsViewController *billDetailsVC = [segue destinationViewController];
billDetailsVC.bill = cell.bill;
}
@end