Skip to content

Commit

Permalink
refactor OTRBuddy
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisballinger committed Sep 12, 2011
1 parent 43b0566 commit 4598618
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 43 deletions.
6 changes: 6 additions & 0 deletions Off the Record.xcodeproj/project.pbxproj
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
D902D31E141E930C00AA3479 /* OTRBuddy.m in Sources */ = {isa = PBXBuildFile; fileRef = D902D31D141E930C00AA3479 /* OTRBuddy.m */; };
D90B80A913FC7026006EFF3D /* MBProgressHUD.m in Sources */ = {isa = PBXBuildFile; fileRef = D90B80A813FC7026006EFF3D /* MBProgressHUD.m */; };
D90B80AC13FC719B006EFF3D /* CoreGraphics.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = D90B80AB13FC719B006EFF3D /* CoreGraphics.framework */; };
D90B80AF13FC7E5F006EFF3D /* OTRCodec.m in Sources */ = {isa = PBXBuildFile; fileRef = D90B80AE13FC7E5F006EFF3D /* OTRCodec.m */; };
Expand Down Expand Up @@ -242,6 +243,8 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
D902D31C141E930C00AA3479 /* OTRBuddy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = OTRBuddy.h; sourceTree = "<group>"; };
D902D31D141E930C00AA3479 /* OTRBuddy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = OTRBuddy.m; sourceTree = "<group>"; };
D90B80A713FC7026006EFF3D /* MBProgressHUD.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MBProgressHUD.h; sourceTree = "<group>"; };
D90B80A813FC7026006EFF3D /* MBProgressHUD.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = MBProgressHUD.m; sourceTree = "<group>"; };
D90B80AB13FC719B006EFF3D /* CoreGraphics.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreGraphics.framework; path = System/Library/Frameworks/CoreGraphics.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -1036,6 +1039,8 @@
D9D69B07141806BB0056FB9E /* Model */ = {
isa = PBXGroup;
children = (
D902D31C141E930C00AA3479 /* OTRBuddy.h */,
D902D31D141E930C00AA3479 /* OTRBuddy.m */,
D999075B14142CC400D573EE /* OTRProtocolManager.h */,
D999075C14142CC400D573EE /* OTRProtocolManager.m */,
D99907511414299200D573EE /* OTROscarManager.h */,
Expand Down Expand Up @@ -1794,6 +1799,7 @@
D9D69B7A141809110056FB9E /* XMPPElement+Delay.m in Sources */,
D99550B4141AD8CD00A82FAC /* OTRAccountsViewController.m in Sources */,
D99BA290141D3F3A00D051B6 /* OTRMessage.m in Sources */,
D902D31E141E930C00AA3479 /* OTRBuddy.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
Binary file not shown.
29 changes: 29 additions & 0 deletions Off the Record/OTRBuddy.h
@@ -0,0 +1,29 @@
//
// OTRBuddy.h
// Off the Record
//
// Created by Chris Ballinger on 9/12/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

typedef unsigned int OTRBuddyStatus;

enum OTRBuddyStatus {
kOTRBuddyStatusOffline = 0,
kOTRBuddyStatusAvailable = 1,
kOTRBuddyStatusAway = 2
};

@interface OTRBuddy : NSObject

@property (readonly, retain) NSString* name;
@property (readonly, retain) NSString* protocol;
@property (nonatomic, retain) NSString* groupName;
@property OTRBuddyStatus status;

-(id)initWithName:(NSString*)buddyName protocol:(NSString*)buddyProtocol status:(OTRBuddyStatus)buddyStatus groupName:(NSString*)buddyGroupName;
+(OTRBuddy*)buddyWithName:(NSString*)buddyName protocol:(NSString*)buddyProtocol status:(OTRBuddyStatus)buddyStatus groupName:(NSString*)buddyGroupName;

@end
38 changes: 38 additions & 0 deletions Off the Record/OTRBuddy.m
@@ -0,0 +1,38 @@
//
// OTRBuddy.m
// Off the Record
//
// Created by Chris Ballinger on 9/12/11.
// Copyright (c) 2011 __MyCompanyName__. All rights reserved.
//

#import "OTRBuddy.h"

@implementation OTRBuddy

@synthesize name;
@synthesize protocol;
@synthesize groupName;
@synthesize status;

-(id)initWithName:(NSString*)buddyName protocol:(NSString*)buddyProtocol status:(OTRBuddyStatus)buddyStatus groupName:(NSString*)buddyGroupName
{
self = [self init];

if(self)
{
name = buddyName;
protocol = buddyProtocol;
status = buddyStatus;
groupName = buddyGroupName;
}
return self;
}

+(OTRBuddy*)buddyWithName:(NSString*)buddyName protocol:(NSString*)buddyProtocol status:(OTRBuddyStatus)buddyStatus groupName:(NSString *)buddyGroupName
{
OTRBuddy *newBuddy = [[[OTRBuddy alloc] initWithName:buddyName protocol:buddyProtocol status:buddyStatus groupName:buddyGroupName] autorelease];
return newBuddy;
}

@end
7 changes: 2 additions & 5 deletions Off the Record/OTRBuddyListViewController.h
Expand Up @@ -16,9 +16,7 @@


NSMutableDictionary *chatViewControllers;

// UIViewController *loginController;


OTRProtocolManager *protocolManager;

NSMutableDictionary *buddyList;
Expand All @@ -29,11 +27,10 @@
@property (nonatomic, retain) UIViewController *chatListController;
@property (nonatomic, retain) UITabBarController *tabController;

@property (nonatomic, retain) OTRProtocolManager *protocolManager;
@property (nonatomic, retain) OTRProtocolManager *protocolManager;
@property (nonatomic, retain) NSMutableDictionary *recentMessages;

-(void)enterConversation:(NSString*)buddyName withProtocol:(NSString*)protocol;
//-(void)loggedInSuccessfully;
-(void)buddyListUpdate;
-(void)messageReceived:(NSNotification*)notification;

Expand Down
41 changes: 20 additions & 21 deletions Off the Record/OTRBuddyListViewController.m
Expand Up @@ -9,7 +9,7 @@
#import "OTRBuddyListViewController.h"
#import "OTRChatViewController.h"
#import "OTRLoginViewController.h"

#import "OTRBuddy.h"

//#define kSignoffTime 500

Expand Down Expand Up @@ -189,31 +189,30 @@ - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(N
NSArray *buddyKeys = [buddyDictionary allKeys];
NSString *currentBuddyKey = [buddyKeys objectAtIndex:indexPath.row];

NSDictionary *buddyData = [buddyDictionary objectForKey:currentBuddyKey];
OTRBuddy *buddyData = [buddyDictionary objectForKey:currentBuddyKey];

NSString *buddyUsername = [buddyData objectForKey:@"buddy_name"];
NSString *buddyStatus = [buddyData objectForKey:@"status"];
NSString *buddyUsername = buddyData.name;
OTRBuddyStatus buddyStatus = buddyData.status;

cell.textLabel.text = buddyUsername;

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
cell.detailTextLabel.textColor = [UIColor lightGrayColor];

if([buddyStatus isEqualToString:@"Offline"])
{
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.detailTextLabel.text = @"Offline";
}
else if([buddyStatus isEqualToString:@"Away"])
switch(buddyStatus)
{
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.detailTextLabel.text = @"Away";
}
else
{
cell.textLabel.textColor = [UIColor darkTextColor];
cell.detailTextLabel.text = @"Available";

case kOTRBuddyStatusOffline:
cell.textLabel.textColor = [UIColor lightGrayColor];
cell.detailTextLabel.text = @"Offline";
break;
case kOTRBuddyStatusAway:
cell.textLabel.textColor = [UIColor darkGrayColor];
cell.detailTextLabel.text = @"Away";
break;
default:
cell.textLabel.textColor = [UIColor darkTextColor];
cell.detailTextLabel.text = @"Available";
break;
}
}

Expand All @@ -231,10 +230,10 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
NSArray *buddyKeys = [buddyDictionary allKeys];
NSString *currentBuddyKey = [buddyKeys objectAtIndex:indexPath.row];

NSDictionary *buddyData = [buddyDictionary objectForKey:currentBuddyKey];
OTRBuddy *buddyData = [buddyDictionary objectForKey:currentBuddyKey];

NSString *buddyUsername = [buddyData objectForKey:@"buddy_name"];
NSString *buddyProtocol = [buddyData objectForKey:@"protocol"];
NSString *buddyUsername = buddyData.name;
NSString *buddyProtocol = buddyData.protocol;

[self enterConversation:buddyUsername withProtocol:buddyProtocol];
[tableView deselectRowAtIndexPath:indexPath animated:YES];
Expand Down
32 changes: 15 additions & 17 deletions Off the Record/OTRProtocolManager.m
Expand Up @@ -7,6 +7,7 @@
//

#import "OTRProtocolManager.h"
#import "OTRBuddy.h"

static OTRProtocolManager *sharedManager = nil;

Expand Down Expand Up @@ -161,24 +162,24 @@ -(void)buddyListUpdate

for(AIMBlistBuddy *buddy in group.buddies)
{
NSMutableDictionary *buddyData = [[NSMutableDictionary alloc] init];
[buddyData setObject:buddy.username forKey:@"buddy_name"];
[buddyData setObject:@"prpl-oscar" forKey:@"protocol"];
OTRBuddyStatus buddyStatus;

switch (buddy.status.statusType)
{
case AIMBuddyStatusAvailable:
[buddyData setObject:@"Available" forKey:@"status"];
buddyStatus = kOTRBuddyStatusAvailable;
break;
case AIMBuddyStatusAway:
[buddyData setObject:@"Away" forKey:@"status"];
buddyStatus = kOTRBuddyStatusAway;
break;
default:
[buddyData setObject:@"Offline" forKey:@"status"];
buddyStatus = kOTRBuddyStatusOffline;
break;
}

[buddyDictionary setObject:buddyData forKey:buddy.username];
OTRBuddy *newBuddy = [OTRBuddy buddyWithName:buddy.username protocol:@"prpl-oscar" status:buddyStatus groupName:group.name];

[buddyDictionary setObject:newBuddy forKey:buddy.username];
}
[groupDictionary setObject:buddyDictionary forKey:@"group_data"];

Expand Down Expand Up @@ -207,18 +208,19 @@ -(void)buddyListUpdate
NSMutableDictionary *groupDictionary = [[NSMutableDictionary alloc] initWithCapacity:sectionsCount];

NSString *sectionName;
NSString *status;
OTRBuddyStatus otrBuddyStatus;

switch (i) {
case 0:
sectionName = @"XMPP - Available";
status = @"Available";
otrBuddyStatus = kOTRBuddyStatusAvailable;
break;
case 1:
sectionName = @"XMPP - Away";
status = @"Away";
otrBuddyStatus = kOTRBuddyStatusAway;
default:
sectionName = @"XMPP - Offline";
status = @"Offline";
otrBuddyStatus = kOTRBuddyStatusOffline;
break;
}
[groupDictionary setObject:sectionName forKey:@"group_name"];
Expand All @@ -231,13 +233,9 @@ -(void)buddyListUpdate

XMPPUserCoreDataStorageObject *user = [frc objectAtIndexPath:indexPath];

NSMutableDictionary *buddyData = [[NSMutableDictionary alloc] init];
[buddyData setObject:[user.displayName copy] forKey:@"buddy_name"];
[buddyData setObject:@"xmpp" forKey:@"protocol"];
[buddyData setObject:status forKey:@"status"];

OTRBuddy *newBuddy = [OTRBuddy buddyWithName:user.displayName protocol:@"xmpp" status:otrBuddyStatus groupName:sectionName];

[buddyDictionary setObject:buddyData forKey:user.displayName];
[buddyDictionary setObject:newBuddy forKey:user.displayName];
}
[groupDictionary setObject:buddyDictionary forKey:@"group_data"];

Expand Down

0 comments on commit 4598618

Please sign in to comment.