Support for Archiving. (Closes #213)

This commit is contained in:
Frederic Jacobs 2014-12-05 23:38:13 +01:00
parent 2d722d499b
commit b5ba841c67
6 changed files with 83 additions and 37 deletions

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6250" systemVersion="14C68k" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="6254" systemVersion="14B25" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6244"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="6247"/>
<capability name="Alignment constraints with different attributes" minToolsVersion="5.1"/>
<capability name="Constraints to layout margins" minToolsVersion="6.0"/>
</dependencies>
@ -87,7 +87,7 @@
<segment title="Archive"/>
</segments>
<connections>
<action selector="segmentDidChange:" destination="MY2-bB-USa" eventType="valueChanged" id="hEg-ZL-P9U"/>
<action selector="segmentDidChange:" destination="MY2-bB-USa" eventType="valueChanged" id="eE7-lp-jRr"/>
</connections>
</segmentedControl>
<barButtonItem key="rightBarButtonItem" systemItem="compose" id="Oft-fU-tf5">
@ -97,7 +97,7 @@
</barButtonItem>
</navigationItem>
<connections>
<outlet property="segmentedControl" destination="3zh-Df-ewk" id="WwI-JF-Y2X"/>
<outlet property="inboxArchiveSwitch" destination="3zh-Df-ewk" id="ANJ-FH-ESj"/>
<outlet property="tableView" destination="PaA-ol-uQT" id="nQU-tR-wbL"/>
</connections>
</viewController>

View file

@ -61,6 +61,7 @@ typedef NS_ENUM(NSInteger, TSLastActionType) {
@property (getter=isBlocked) BOOL blocked;
@property (nonatomic) uint64_t lastMessageId;
@property NSDate *archivalDate;
- (NSDate*)lastMessageDate;

View file

@ -10,7 +10,8 @@
@interface TSDatabaseView : NSObject
extern NSString *TSThreadGroup;
extern NSString *TSInboxGroup;
extern NSString *TSArchiveGroup;
extern NSString *TSThreadDatabaseViewExtensionName;
extern NSString *TSMessageDatabaseViewExtensionName;

View file

@ -15,10 +15,11 @@
#import "TSStorageManager.h"
#import "TSRecipient.h"
NSString *TSThreadGroup = @"TSThreadGroup";
NSString *TSInboxGroup = @"TSInboxGroup";
NSString *TSArchiveGroup = @"TSArchiveGroup";
NSString *TSThreadDatabaseViewExtensionName = @"TSThreadDatabaseViewExtensionName";
NSString *TSMessageDatabaseViewExtensionName = @"TSMessageDatabaseViewExtensionName";
NSString *TSThreadDatabaseViewExtensionName = @"TSThreadDatabaseViewExtensionName";
NSString *TSMessageDatabaseViewExtensionName = @"TSMessageDatabaseViewExtensionName";
@implementation TSDatabaseView
@ -31,24 +32,18 @@ NSString *TSMessageDatabaseViewExtensionName = @"TSMessageDatabaseViewExtensi
YapDatabaseViewGrouping *viewGrouping = [YapDatabaseViewGrouping withObjectBlock:^NSString *(NSString *collection, NSString *key, id object) {
if ([object isKindOfClass:[TSThread class]]){
TSThread *thread = (TSThread*)object;
if (thread.lastMessageDate) {
return TSThreadGroup;
if (thread.archivalDate) {
return ([self threadShouldBeInInbox:thread])?TSInboxGroup:TSArchiveGroup;
}
return TSInboxGroup;
}
}
return nil;
}];
YapDatabaseViewSorting *viewSorting = [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(NSString *group, NSString *collection1, NSString *key1, id object1, NSString *collection2, NSString *key2, id object2) {
if ([group isEqualToString:TSThreadGroup]) {
if ([object1 isKindOfClass:[TSThread class]] && [object2 isKindOfClass:[TSThread class]]){
TSThread *thread1 = (TSThread*)object1;
TSThread *thread2 = (TSThread*)object2;
return [thread2.lastMessageDate compare:thread1.lastMessageDate];
}
}
return NSOrderedSame;
}];
YapDatabaseViewSorting *viewSorting = [self threadSorting];
YapDatabaseViewOptions *options = [[YapDatabaseViewOptions alloc] init];
options.isPersistent = YES;
@ -98,4 +93,39 @@ NSString *TSMessageDatabaseViewExtensionName = @"TSMessageDatabaseViewExtensi
return [[TSStorageManager sharedManager].database registerExtension:view withName:TSMessageDatabaseViewExtensionName];
}
/**
* Determines whether a thread belongs to the archive or inbox
*
* @param thread TSThread
*
* @return Inbox if true, Archive if false
*/
+ (BOOL)threadShouldBeInInbox:(TSThread*)thread {
NSDate *lastMessage = thread.lastMessageDate;
NSDate *archivalDate = thread.archivalDate;
if (lastMessage&&archivalDate) {
return ([lastMessage timeIntervalSinceDate:archivalDate]>0)?YES:NO;
}
return YES;
}
+ (YapDatabaseViewSorting*)threadSorting {
return [YapDatabaseViewSorting withObjectBlock:^NSComparisonResult(NSString *group, NSString *collection1, NSString *key1, id object1, NSString *collection2, NSString *key2, id object2) {
if ([group isEqualToString:TSArchiveGroup] || [group isEqualToString:TSInboxGroup]) {
if ([object1 isKindOfClass:[TSThread class]] && [object2 isKindOfClass:[TSThread class]]){
TSThread *thread1 = (TSThread*)object1;
TSThread *thread2 = (TSThread*)object2;
return [thread2.lastMessageDate compare:thread1.lastMessageDate];
}
}
return NSOrderedSame;
}];
}
@end

View file

@ -18,7 +18,7 @@
@property (nonatomic) GroupModel *groupFromCompose;
@property (nonatomic, retain) IBOutlet UITableView *tableView;
@property (nonatomic, retain) IBOutlet UISegmentedControl *segmentedControl;
@property (nonatomic, retain) IBOutlet UISegmentedControl *inboxArchiveSwitch;
@end

View file

@ -31,13 +31,10 @@
static NSString *const inboxTableViewCell = @"inBoxTableViewCell";
static NSString *const kSegueIndentifier = @"showSegue";
@interface SignalsViewController () {
NSArray * _dataArray;
NSUInteger numberOfCells;
}
@interface SignalsViewController ()
@property (strong, nonatomic) UILabel * emptyViewLabel;
@property (nonatomic, strong) YapDatabaseConnection *editingDbConnection;
@property (nonatomic, strong) YapDatabaseConnection *uiDatabaseConnection;
@property (nonatomic, strong) YapDatabaseViewMappings *threadMappings;
@ -52,9 +49,13 @@ static NSString *const kSegueIndentifier = @"showSegue";
- (void)viewDidLoad {
[super viewDidLoad];
[self tableViewSetUp];
self.editingDbConnection = TSStorageManager.sharedManager.newDatabaseConnection;
[self.uiDatabaseConnection beginLongLivedReadTransaction];
self.threadMappings = [[YapDatabaseViewMappings alloc] initWithGroups:@[TSThreadGroup]
self.threadMappings = [[YapDatabaseViewMappings alloc] initWithGroups:@[TSInboxGroup]
view:TSThreadDatabaseViewExtensionName];
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction){
@ -106,7 +107,7 @@ static NSString *const kSegueIndentifier = @"showSegue";
}
[cell configureWithThread:thread];
[cell configureForState:_segmentedControl.selectedSegmentIndex == 0 ? kInboxState : kArchiveState];
[cell configureForState:_inboxArchiveSwitch.selectedSegmentIndex == 0 ? kInboxState : kArchiveState];
return cell;
}
@ -131,11 +132,19 @@ static NSString *const kSegueIndentifier = @"showSegue";
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
TSThread *thread = [self threadForIndexPath:indexPath];
[thread remove];
[self.editingDbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[thread removeWithTransaction:transaction];
}];
}
- (void)tableViewCellTappedArchive:(InboxTableViewCell*)cell {
NSLog(@"Archive");
NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
TSThread *thread = [self threadForIndexPath:indexPath];
thread.archivalDate = [NSDate date];
[self.editingDbConnection readWriteWithBlock:^(YapDatabaseReadWriteTransaction *transaction) {
[thread saveWithTransaction:transaction];
}];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath*)indexPath{
@ -165,18 +174,23 @@ static NSString *const kSegueIndentifier = @"showSegue";
-(IBAction)segmentDidChange:(id)sender
{
switch (_segmentedControl.selectedSegmentIndex) {
switch (_inboxArchiveSwitch.selectedSegmentIndex) {
case 0:
numberOfCells=5;
[self.tableView reloadData];
self.threadMappings = [[YapDatabaseViewMappings alloc] initWithGroups:@[TSInboxGroup]
view:TSThreadDatabaseViewExtensionName];
break;
case 1:
numberOfCells=3;
[self.tableView reloadData];
self.threadMappings = [[YapDatabaseViewMappings alloc] initWithGroups:@[TSArchiveGroup]
view:TSThreadDatabaseViewExtensionName];
break;
}
[self.uiDatabaseConnection readWithBlock:^(YapDatabaseReadTransaction *transaction){
[self.threadMappings updateWithTransaction:transaction];
}];
[self.tableView reloadData];
[self updateTableViewHeader];
}
#pragma mark Database delegates
@ -275,10 +289,10 @@ static NSString *const kSegueIndentifier = @"showSegue";
{
CGRect r = CGRectMake(0, 60, 300, 70);
_emptyViewLabel = [[UILabel alloc]initWithFrame:r];
_emptyViewLabel.text = @"You have no messages yet.";
_emptyViewLabel.textColor = [UIColor grayColor];
_emptyViewLabel.font = [UIFont ows_thinFontWithSize:14.0f];
_emptyViewLabel.textAlignment = NSTextAlignmentCenter;
_emptyViewLabel.text = @"You have no messages yet.";
self.tableView.tableHeaderView = _emptyViewLabel;
} else {
_emptyViewLabel = nil;