Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38880bf

Browse files
committed
Separate CoreData code. support NSDictionary also!
1 parent a8c867b commit 38880bf

14 files changed

Lines changed: 372 additions & 245 deletions

AcaniChat.xcodeproj/project.pbxproj

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
0215231114C541DB0006E055 /* ChatViewController__coreData.m in Sources */ = {isa = PBXBuildFile; fileRef = 0215231014C541DB0006E055 /* ChatViewController__coreData.m */; };
1011
025167CF14C3BA1F00D2A951 /* ChatBar.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167CE14C3BA1F00D2A951 /* ChatBar.m */; };
1112
025167D514C3CCE000D2A951 /* Defines.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167D414C3CCDF00D2A951 /* Defines.m */; };
1213
025167D914C3F53800D2A951 /* TVCell_Date.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167D814C3F53800D2A951 /* TVCell_Date.m */; };
1314
025167DC14C4025C00D2A951 /* TVCell_Message.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167DB14C4025C00D2A951 /* TVCell_Message.m */; };
14-
025167DF14C40F4800D2A951 /* ChatViewController_mutable.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167DE14C40F4700D2A951 /* ChatViewController_mutable.m */; };
1515
025167E214C4126700D2A951 /* ChatViewController+Mutability.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167E114C4126700D2A951 /* ChatViewController+Mutability.m */; };
1616
025167E514C412EE00D2A951 /* ChatViewController+Keyboard.m in Sources */ = {isa = PBXBuildFile; fileRef = 025167E414C412ED00D2A951 /* ChatViewController+Keyboard.m */; };
1717
363AE19613EC32A000F3C4B1 /* [email protected] in Resources */ = {isa = PBXBuildFile; fileRef = 363AE19413EC32A000F3C4B1 /* [email protected] */; };
@@ -37,6 +37,8 @@
3737
/* End PBXBuildFile section */
3838

3939
/* Begin PBXFileReference section */
40+
0215230F14C541DB0006E055 /* ChatViewController__coreData.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ChatViewController__coreData.h; sourceTree = "<group>"; };
41+
0215231014C541DB0006E055 /* ChatViewController__coreData.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ChatViewController__coreData.m; sourceTree = "<group>"; };
4042
025167CD14C3BA1F00D2A951 /* ChatBar.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = ChatBar.h; path = Application/Views/ChatBar.h; sourceTree = SOURCE_ROOT; };
4143
025167CE14C3BA1F00D2A951 /* ChatBar.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = ChatBar.m; path = Application/Views/ChatBar.m; sourceTree = SOURCE_ROOT; };
4244
025167D314C3CCDF00D2A951 /* Defines.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Defines.h; sourceTree = "<group>"; };
@@ -45,8 +47,6 @@
4547
025167D814C3F53800D2A951 /* TVCell_Date.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TVCell_Date.m; path = TableViewCells/TVCell_Date.m; sourceTree = "<group>"; };
4648
025167DA14C4025C00D2A951 /* TVCell_Message.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = TVCell_Message.h; path = TableViewCells/TVCell_Message.h; sourceTree = "<group>"; };
4749
025167DB14C4025C00D2A951 /* TVCell_Message.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = TVCell_Message.m; path = TableViewCells/TVCell_Message.m; sourceTree = "<group>"; };
48-
025167DD14C40F4700D2A951 /* ChatViewController_mutable.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ChatViewController_mutable.h; sourceTree = "<group>"; };
49-
025167DE14C40F4700D2A951 /* ChatViewController_mutable.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = ChatViewController_mutable.m; sourceTree = "<group>"; };
5050
025167E014C4126700D2A951 /* ChatViewController+Mutability.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ChatViewController+Mutability.h"; sourceTree = "<group>"; };
5151
025167E114C4126700D2A951 /* ChatViewController+Mutability.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = "ChatViewController+Mutability.m"; sourceTree = "<group>"; };
5252
025167E314C412ED00D2A951 /* ChatViewController+Keyboard.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "ChatViewController+Keyboard.h"; sourceTree = "<group>"; };
@@ -170,12 +170,12 @@
170170
025167CE14C3BA1F00D2A951 /* ChatBar.m */,
171171
025167D314C3CCDF00D2A951 /* Defines.h */,
172172
025167D414C3CCDF00D2A951 /* Defines.m */,
173-
025167DD14C40F4700D2A951 /* ChatViewController_mutable.h */,
174-
025167DE14C40F4700D2A951 /* ChatViewController_mutable.m */,
175173
025167E014C4126700D2A951 /* ChatViewController+Mutability.h */,
176174
025167E114C4126700D2A951 /* ChatViewController+Mutability.m */,
177175
025167E314C412ED00D2A951 /* ChatViewController+Keyboard.h */,
178176
025167E414C412ED00D2A951 /* ChatViewController+Keyboard.m */,
177+
0215230F14C541DB0006E055 /* ChatViewController__coreData.h */,
178+
0215231014C541DB0006E055 /* ChatViewController__coreData.m */,
179179
);
180180
path = Controllers;
181181
sourceTree = "<group>";
@@ -250,6 +250,9 @@
250250
/* Begin PBXProject section */
251251
C92BF84213631710003B20F6 /* Project object */ = {
252252
isa = PBXProject;
253+
attributes = {
254+
LastUpgradeCheck = 0420;
255+
};
253256
buildConfigurationList = C92BF84513631710003B20F6 /* Build configuration list for PBXProject "AcaniChat" */;
254257
compatibilityVersion = "Xcode 3.2";
255258
developmentRegion = English;
@@ -301,9 +304,9 @@
301304
025167D514C3CCE000D2A951 /* Defines.m in Sources */,
302305
025167D914C3F53800D2A951 /* TVCell_Date.m in Sources */,
303306
025167DC14C4025C00D2A951 /* TVCell_Message.m in Sources */,
304-
025167DF14C40F4800D2A951 /* ChatViewController_mutable.m in Sources */,
305307
025167E214C4126700D2A951 /* ChatViewController+Mutability.m in Sources */,
306308
025167E514C412EE00D2A951 /* ChatViewController+Keyboard.m in Sources */,
309+
0215231114C541DB0006E055 /* ChatViewController__coreData.m in Sources */,
307310
);
308311
runOnlyForDeploymentPostprocessing = 0;
309312
};

Application/Controllers/ChatViewController+Keyboard.m

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ - (void)scrollToBottomAnimated:(BOOL)animated {
7878
NSInteger bottomRow = [cellMap count] - 1;
7979
if (bottomRow >= 0) {
8080
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:bottomRow inSection:0];
81-
[self.chatContent scrollToRowAtIndexPath:indexPath
81+
[chatContent scrollToRowAtIndexPath:indexPath
8282
atScrollPosition:UITableViewScrollPositionBottom animated:animated];
8383
}
8484
}

Application/Controllers/ChatViewController+Mutability.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,16 @@
1010

1111
@interface ChatViewController (Mutability)
1212

13-
- (NSUInteger)addMessage:(Message *)message;
13+
- (NSUInteger)addMessage:(id)message;
1414
- (NSUInteger)removeMessageAtIndex:(NSUInteger)index;
1515
- (void)clearAll;
16+
17+
- (void) clearAllMessage;
18+
19+
20+
- (void) removeMessage: (id) message;
21+
22+
+ (NSDate *) sendDateInMessage: (id) message;
23+
24+
+ (void) setIsMine: (BOOL) isMine inMessage: (id) message;
1625
@end

Application/Controllers/ChatViewController+Mutability.m

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,32 +8,39 @@
88

99
#import "ChatViewController+Mutability.h"
1010

11-
#import "Message.h"
11+
//#import "Message.h"
1212

1313

1414
#define ClearConversationButtonIndex 0
1515

1616
@implementation ChatViewController (Mutability)
1717

18+
+ (NSDate*) sendDateInMessage:(id)message {
19+
return [message valueForKey: @"sentDate"];
20+
}
1821

19-
22+
+ (void) setIsMine:(BOOL)isMine inMessage:(id)message {
23+
[message setValue: [NSNumber numberWithBool: isMine] forKey:@"isMine"];
24+
}
2025
// Returns number of objects added to cellMap (1 or 2).
21-
- (NSUInteger)addMessage:(Message *)message
26+
- (NSUInteger)addMessage:(id)message
2227
{
2328
// Show sentDates at most every 15 minutes.
24-
NSDate *currentSentDate = message.sentDate;
29+
NSDate *currentSentDate = [[self class] sendDateInMessage: message]; // message.sentDate;
2530
NSUInteger numberOfObjectsAdded = 1;
2631
NSUInteger prevIndex = [cellMap count] - 1;
2732

2833
// Show sentDates at most every 15 minutes.
2934

3035
if([cellMap count])
3136
{
32-
BOOL prevIsMessage = [[cellMap objectAtIndex:prevIndex] isKindOfClass:[Message class]];
37+
// BOOL prevIsMessage = [[cellMap objectAtIndex:prevIndex] isKindOfClass:[Message class]];
38+
BOOL prevIsMessage = ! [[cellMap objectAtIndex:prevIndex] isKindOfClass:[NSDate class]];
39+
3340
if(prevIsMessage)
3441
{
35-
Message * temp = [cellMap objectAtIndex:prevIndex];
36-
NSDate * previousSentDate = temp.sentDate;
42+
id temp = [cellMap objectAtIndex:prevIndex];
43+
NSDate * previousSentDate =[[self class] sendDateInMessage: temp]; // temp.sentDate;
3744
// if there has been more than a 15 min gap between this and the previous message!
3845
if([currentSentDate timeIntervalSinceDate:previousSentDate] > SECONDS_BETWEEN_MESSAGES)
3946
{
@@ -51,7 +58,9 @@ - (NSUInteger)addMessage:(Message *)message
5158

5259
[cellMap addObject:message];
5360

54-
message.isMine = [NSNumber numberWithBool: (([cellMap count] %3) == 0)];
61+
BOOL isMine_ = (([cellMap count] %3) == 0);
62+
[[self class] setIsMine: isMine_ inMessage: message];
63+
//message.isMine = [NSNumber numberWithBool: (([cellMap count] %3) == 0)];
5564

5665
// [message.managedObjectContext save: nil];
5766

@@ -71,7 +80,7 @@ - (NSUInteger)removeMessageAtIndex:(NSUInteger)index {
7180
BOOL isLastObject = index == cellMapCount;
7281
BOOL prevIsDate = [[cellMap objectAtIndex:prevIndex] isKindOfClass:[NSDate class]];
7382

74-
if (isLastObject && prevIsDate ||
83+
if ((isLastObject && prevIsDate) ||
7584
prevIsDate && [[cellMap objectAtIndex:index] isKindOfClass:[NSDate class]]) {
7685
[cellMap removeObjectAtIndex:prevIndex];
7786
numberOfObjectsRemoved = 2;
@@ -93,26 +102,20 @@ - (void)clearAll {
93102

94103
}
95104

105+
- (void) removeMessage:(id)message {
106+
107+
}
108+
109+
- (void) clearAllMessage {
110+
111+
}
96112
#pragma mark UIActionSheetDelegate
97113

98114
- (void)actionSheet:(UIActionSheet *)modalView clickedButtonAtIndex:(NSInteger)buttonIndex {
99115
switch (buttonIndex) {
100116
case ClearConversationButtonIndex: {
101-
NSError *error;
102-
fetchedResultsController.delegate = nil; // turn off delegate callbacks
103-
for (Message *message in [fetchedResultsController fetchedObjects]) {
104-
[managedObjectContext deleteObject:message];
105-
}
106-
if (![managedObjectContext save:&error]) {
107-
// TODO: Handle the error appropriately.
108-
NSLog(@"Delete message error %@, %@", error, [error userInfo]);
109-
}
110-
fetchedResultsController.delegate = self; // reconnect after mass delete
111-
if (![fetchedResultsController performFetch:&error]) { // resync controller
112-
// TODO: Handle the error appropriately.
113-
NSLog(@"fetchResults error %@, %@", error, [error userInfo]);
114-
}
115-
117+
[self clearAllMessage];
118+
116119
[cellMap removeAllObjects];
117120
[chatContent reloadData];
118121

@@ -134,7 +137,9 @@ - (UITableViewCellEditingStyle)tableView:(UITableView *)tableView
134137

135138

136139
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
137-
return [[cellMap objectAtIndex:[indexPath row]] isKindOfClass:[Message class]];
140+
return ! [[cellMap objectAtIndex:[indexPath row]] isKindOfClass:[NSDate class]];
141+
142+
//return [[cellMap objectAtIndex:[indexPath row]] isKindOfClass:[Message class]];
138143
// return [[tableView cellForRowAtIndexPath:indexPath] reuseIdentifier] == kMessageCell;
139144
}
140145

@@ -150,13 +155,7 @@ - (void)tableView:(UITableView *)tableView
150155

151156
// NSLog(@"Delete %@", object);
152157

153-
// Remove message from managed object context by index path.
154-
[managedObjectContext deleteObject:(Message *)object];
155-
NSError *error;
156-
if (![managedObjectContext save:&error]) {
157-
// TODO: Handle the error appropriately.
158-
NSLog(@"Delete message error %@, %@", error, [error userInfo]);
159-
}
158+
[self removeMessage: object];
160159
}
161160
}
162161

Application/Controllers/ChatViewController.h

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,31 @@
33

44
#import "ChatBar.h"
55

6-
@class Message;
6+
//@class Message;
77

8-
@interface ChatViewController : UIViewController <NSFetchedResultsControllerDelegate,
8+
@interface ChatViewController : UIViewController <
99
UITableViewDelegate, UITableViewDataSource, UIActionSheetDelegate,
1010

1111
ChatBarDelegate
1212
> {
1313

14+
SystemSoundID receiveMessageSound;
1415
NSMutableArray *cellMap;
1516
UITableView *chatContent;
1617
ChatBar *chatBar;
1718

18-
NSManagedObjectContext *managedObjectContext;
19-
20-
NSFetchedResultsController *fetchedResultsController;
2119
}
22-
20+
/*
2321
@property (nonatomic, assign) SystemSoundID receiveMessageSound;
2422
2523
@property (nonatomic, strong) UITableView *chatContent;
2624
2725
@property (nonatomic, strong) ChatBar *chatBar;
2826
29-
@property (nonatomic, copy) NSMutableArray *cellMap;
30-
31-
@property (nonatomic, strong) NSManagedObjectContext *managedObjectContext;
32-
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
33-
27+
//@property (nonatomic, copy) NSMutableArray *cellMap;
28+
*/
29+
- (id) createNewMessageWithText: (NSString*) text;
3430

35-
- (void)fetchResults;
31+
@property (nonatomic, strong) NSArray * array;
3632

3733
@end

0 commit comments

Comments
 (0)