Skip to content

Commit

Permalink
Add option to sort events by startDate instead of endDate
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Aug 19, 2014
1 parent 845240b commit d376af8
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 20 deletions.
9 changes: 7 additions & 2 deletions iBurn/BRCDatabaseManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ + (NSComparisonResult) compareDistanceOfFirstObject:(BRCDataObject*)object1 seco
+ (YapDatabaseViewSortingBlock)sortingBlockForClass:(Class)viewClass extensionType:(BRCDatabaseViewExtensionType)extensionType fromLocation:(CLLocation*)fromLocation {
YapDatabaseViewSortingBlock sortingBlock;
if (extensionType == BRCDatabaseViewExtensionTypeTimeThenDistance) {
BOOL shouldSortEventsByStartTime = [[NSUserDefaults standardUserDefaults] shouldSortEventsByStartTime];
sortingBlock = ^(NSString *group, NSString *collection1, NSString *key1, id obj1,
NSString *collection2, NSString *key2, id obj2){
if ([obj1 isKindOfClass:[BRCEventObject class]] && [obj2 isKindOfClass:[BRCEventObject class]]) {
Expand All @@ -191,8 +192,12 @@ + (YapDatabaseViewSortingBlock)sortingBlockForClass:(Class)viewClass extensionTy
else if (!event1.isAllDay && event2.isAllDay) {
return NSOrderedAscending;
}

NSComparisonResult dateComparison = [event1.endDate compare:event2.endDate];
NSComparisonResult dateComparison = NSOrderedSame;
if (shouldSortEventsByStartTime) {
dateComparison = [event1.startDate compare:event2.startDate];
} else {
dateComparison = [event1.endDate compare:event2.endDate];
}
if (dateComparison == NSOrderedSame) {
NSComparisonResult distanceComparison = [self compareDistanceOfFirstObject:event1 secondObject:event2 fromLocation:fromLocation];
return distanceComparison;
Expand Down
4 changes: 2 additions & 2 deletions iBurn/BRCEventsFilterTableViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
@class BRCEventsFilterTableViewController;

@protocol BRCEventsFilterTableViewControllerDelegate <NSObject>

@required
- (void)didSetNewFilterSettingsInFilterTableViewController:(BRCEventsFilterTableViewController *)viewController;

- (void)didSetNewSortSettingsInFilterTableViewController:(BRCEventsFilterTableViewController *)viewController;
@end

@interface BRCEventsFilterTableViewController : UIViewController
Expand Down
30 changes: 23 additions & 7 deletions iBurn/BRCEventsFilterTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ @interface BRCEventsFilterTableViewController () <UITableViewDelegate, UITableVi
@property (nonatomic, strong) NSArray *eventTypeArray;

@property (nonatomic) BOOL showExpiredEvents;
@property (nonatomic) BOOL shouldSortEventsByStartTime;

@property (nonatomic, strong) YapDatabaseConnection* databaseConnection;

Expand Down Expand Up @@ -81,9 +82,10 @@ - (void)viewDidLoad

[self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:BRCFilterTableViewCellIdentifier];

self.timeStrings = @[@"Show Expired Events"];
self.timeStrings = @[@"Show Expired Events", @"Sort Events by Start Time"];

self.showExpiredEvents = [[NSUserDefaults standardUserDefaults] showExpiredEvents];
self.shouldSortEventsByStartTime = [[NSUserDefaults standardUserDefaults] shouldSortEventsByStartTime];

//All the event types to select from
NSArray *eventTypes = @[@(BRCEventTypeWorkshop),
Expand Down Expand Up @@ -130,9 +132,17 @@ - (void)viewWillDisappear:(BOOL)animated
[[NSUserDefaults standardUserDefaults] setSelectedEventTypes:filteredArray];
[[NSUserDefaults standardUserDefaults] setShowExpiredEvents:self.showExpiredEvents];

if ([self.delegate respondsToSelector:@selector(didSetNewFilterSettingsInFilterTableViewController:)]) {
[self.delegate didSetNewFilterSettingsInFilterTableViewController:self];
BOOL didChangeSort = NO;
BOOL oldShouldSortEventsByStartTime = [[NSUserDefaults standardUserDefaults] shouldSortEventsByStartTime];
if (self.shouldSortEventsByStartTime != oldShouldSortEventsByStartTime) {
didChangeSort = YES;
}
[[NSUserDefaults standardUserDefaults] setShouldSortEventsByStartTime:self.shouldSortEventsByStartTime];
if (didChangeSort) {
[self.delegate didSetNewSortSettingsInFilterTableViewController:self];
}

[self.delegate didSetNewFilterSettingsInFilterTableViewController:self];
}

- (NSArray *)filteredTypes
Expand Down Expand Up @@ -168,9 +178,11 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N

BOOL showCheckMark = NO;
if (indexPath.section == 0) {

showCheckMark = self.showExpiredEvents;

if (indexPath.row == 0) {
showCheckMark = self.showExpiredEvents;
} else if (indexPath.row == 1) {
showCheckMark = self.shouldSortEventsByStartTime;
}
NSString *text = self.timeStrings[indexPath.row];
cell.textLabel.text = text;

Expand Down Expand Up @@ -213,7 +225,11 @@ - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInte
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.section == 0) {
self.showExpiredEvents = !self.showExpiredEvents;
if (indexPath.row == 0) {
self.showExpiredEvents = !self.showExpiredEvents;
} else if (indexPath.row == 1) {
self.shouldSortEventsByStartTime = !self.shouldSortEventsByStartTime;
}
}
else if (indexPath.section == 1) {
BRCEventTypeContainer *container = self.eventTypeArray[indexPath.row];
Expand Down
52 changes: 50 additions & 2 deletions iBurn/BRCEventsTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ @interface BRCEventsTableViewController () <BRCEventsFilterTableViewControllerDe
@property (nonatomic, strong, readwrite) NSString *filteredByDayDistanceViewName;
@property (nonatomic, strong, readwrite) NSString *filteredDistanceViewName;
@property (nonatomic, strong, readwrite) NSString *favoritesFilterForTimeAndDistanceViewName;

@property (nonatomic) BOOL isRefreshingEventTimeSort;
@property (nonatomic, strong) BRCStringPickerView *dayPicker;
@end

Expand Down Expand Up @@ -153,7 +153,7 @@ - (void) setupDayPicker {
self.dayPicker = [[BRCStringPickerView alloc] initWithTitle:@"Choose a Day" pickerStrings:self.dayPickerRowTitles initialSelection:currentSelection doneBlock:^(BRCStringPickerView *picker, NSUInteger selectedIndex, NSString *selectedValue) {
NSDate *selectedDate = [self dateForIndex:selectedIndex];
self.selectedDay = selectedDate;
[self refreshDistanceInformationFromLocation:self.locationManager.location];
[self refreshDistanceInformationFromLocation:self.locationManager.location forceRefresh:NO];
} cancelBlock:nil];
}

Expand Down Expand Up @@ -260,6 +260,49 @@ - (void) replaceTimeBasedEventMappings {
}];
}

- (BOOL) shouldAnimateLoadingIndicator {
BOOL shouldAnimate = [super shouldAnimateLoadingIndicator];
if (shouldAnimate || self.isRefreshingEventTimeSort) {
return YES;
} else {
return NO;
}
}

- (void) setIsRefreshingEventTimeSort:(BOOL)isRefreshingEventTimeSort {
_isRefreshingEventTimeSort = isRefreshingEventTimeSort;
[self refreshLoadingIndicatorViewAnimation];
}

- (void) refreshEventTimeSort {
if (self.isRefreshingEventTimeSort) {
return;
}
self.isRefreshingEventTimeSort = YES;

// Refresh the distance view sorting block here

[[BRCDatabaseManager sharedInstance].readWriteDatabaseConnection asyncReadWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
YapDatabaseViewTransaction *viewTransaction = [transaction ext:self.timeAndDistanceViewName];
if (!viewTransaction) {
return;
}
BRCDatabaseViewExtensionType extensionType = BRCDatabaseViewExtensionTypeTimeThenDistance;
Class viewClass = self.viewClass;
YapDatabaseViewGroupingBlock groupingBlock = [BRCDatabaseManager groupingBlockForClass:viewClass extensionType:extensionType];
YapDatabaseViewBlockType groupingBlockType = [BRCDatabaseManager groupingBlockTypeForClass:viewClass extensionType:extensionType];
YapDatabaseViewSortingBlock sortingBlock = [BRCDatabaseManager sortingBlockForClass:viewClass extensionType:extensionType fromLocation:self.locationManager.location];
YapDatabaseViewBlockType sortingBlockType = [BRCDatabaseManager sortingBlockTypeForClass:viewClass extensionType:extensionType];
[viewTransaction setGroupingBlock:groupingBlock groupingBlockType:groupingBlockType sortingBlock:sortingBlock sortingBlockType:sortingBlockType versionTag:[[NSUUID UUID] UUIDString]];
} completionBlock:^{
[self updateAllMappingsWithCompletionBlock:^{
[self.tableView reloadData];
self.isRefreshingEventTimeSort = NO;
}];
}];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
BRCEventObjectTableViewCell *cell = (BRCEventObjectTableViewCell*)[super tableView:tableView cellForRowAtIndexPath:indexPath];
Expand All @@ -274,4 +317,9 @@ - (void)didSetNewFilterSettingsInFilterTableViewController:(BRCEventsFilterTable
[self updateFilteredViews];
}

- (void)didSetNewSortSettingsInFilterTableViewController:(BRCEventsFilterTableViewController *)viewController
{
[self refreshEventTimeSort];
}

@end
20 changes: 14 additions & 6 deletions iBurn/BRCFilteredTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ - (instancetype)initWithViewClass:(Class)viewClass
[self registerDatabaseExtensions];
[self setupMappingsDictionary];
[self updateAllMappingsWithCompletionBlock:nil];
[self refreshDistanceInformationFromLocation:self.locationManager.location];
[self refreshDistanceInformationFromLocation:self.locationManager.location forceRefresh:NO];
}
return self;
}
Expand All @@ -125,8 +125,16 @@ - (void) setupLoadingIndicatorView {
self.navigationItem.rightBarButtonItem = loadingButtonItem;
}

- (void) refreshLoadingIndicatorViewAnimation {
- (BOOL) shouldAnimateLoadingIndicator {
if (self.isUpdatingDistanceInformation || self.isUpdatingFilters) {
return YES;
} else {
return NO;
}
}

- (void) refreshLoadingIndicatorViewAnimation {
if ([self shouldAnimateLoadingIndicator]) {
[self.loadingIndicatorView startAnimating];
} else {
[self.loadingIndicatorView stopAnimating];
Expand Down Expand Up @@ -238,7 +246,7 @@ - (void) updateAllMappingsWithCompletionBlock:(dispatch_block_t)completionBlock

- (void) viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[self refreshDistanceInformationFromLocation:self.locationManager.location];
[self refreshDistanceInformationFromLocation:self.locationManager.location forceRefresh:NO];
self.navBarHairlineImageView.hidden = YES;
}

Expand All @@ -259,7 +267,7 @@ - (void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArra
if (!recentLocation) {
return;
}
[self refreshDistanceInformationFromLocation:recentLocation];
[self refreshDistanceInformationFromLocation:recentLocation forceRefresh:NO];
}

- (void) setIsUpdatingDistanceInformation:(BOOL)isUpdatingDistanceInformation {
Expand Down Expand Up @@ -289,11 +297,11 @@ - (BOOL) shouldRefreshDistanceInformationForNewLocation:(CLLocation*)newLocation
return NO;
}

- (void) refreshDistanceInformationFromLocation:(CLLocation*)fromLocation {
- (void) refreshDistanceInformationFromLocation:(CLLocation*)fromLocation forceRefresh:(BOOL)forceRefresh {
if (self.isUpdatingDistanceInformation) {
return;
}
if (![self shouldRefreshDistanceInformationForNewLocation:fromLocation]) {
if (![self shouldRefreshDistanceInformationForNewLocation:fromLocation] && !forceRefresh) {
return;
}
self.isUpdatingDistanceInformation = YES;
Expand Down
3 changes: 2 additions & 1 deletion iBurn/BRCFilteredTableViewController_Private.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@
- (void) setupMappingsDictionary;
- (void) updateAllMappingsWithCompletionBlock:(dispatch_block_t)completionBlock;

- (BOOL) shouldAnimateLoadingIndicator;
- (void) refreshLoadingIndicatorViewAnimation;
- (void) refreshDistanceInformationFromLocation:(CLLocation*)fromLocation;
- (void) refreshDistanceInformationFromLocation:(CLLocation*)fromLocation forceRefresh:(BOOL)forceRefresh;
- (BOOL) shouldRefreshDistanceInformationForNewLocation:(CLLocation*)newLocation;

- (NSArray *) segmentedControlInfo;
Expand Down
3 changes: 3 additions & 0 deletions iBurn/NSUserDefaults+iBurn.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ extern NSString *const kBRCGateUnlockNotificationKey;
- (NSArray *)selectedEventTypes;
- (void)setSelectedEventTypes:(NSArray *)selectedEventTypes;

- (BOOL)shouldSortEventsByStartTime;
- (void)setShouldSortEventsByStartTime:(BOOL)shouldSortByStart;

- (BOOL)showExpiredEvents;
- (void)setShowExpiredEvents:(BOOL)showEpiredEvents;

Expand Down
12 changes: 12 additions & 0 deletions iBurn/NSUserDefaults+iBurn.m
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
static NSString *const kBRCRecentLocationKey = @"kBRCRecentLocationKey";
static NSString *const kBRCEnteredEmbargoPasscodeKey = @"kBRCEnteredEmbargoPasscodeKey";
NSString *const kBRCGateUnlockNotificationKey = @"kBRCGateUnlockNotificationKey";
NSString *const kBRCSortEventsByStartTimeKey = @"kBRCSortEventsByStartTimeKey";


@implementation NSUserDefaults (iBurn)
Expand Down Expand Up @@ -69,6 +70,17 @@ - (void)setShowExpiredEvents:(BOOL)showEpiredEvents
[self synchronize];
}

- (BOOL)shouldSortEventsByStartTime
{
return [self boolForKey:kBRCSortEventsByStartTimeKey];
}

- (void)setShouldSortEventsByStartTime:(BOOL)shouldSortByStart
{
[self setBool:shouldSortByStart forKey:kBRCSortEventsByStartTimeKey];
[self synchronize];
}

- (void) setRecentLocation:(CLLocation *)recentLocation {
NSData *locationData = [NSKeyedArchiver archivedDataWithRootObject:recentLocation];
[self setObject:locationData forKey:kBRCRecentLocationKey];
Expand Down

0 comments on commit d376af8

Please sign in to comment.