Selaa lähdekoodia

Remove usage of deprecated APIs and cleanup TestChat app. (#404)

Nikita Lutsenko 9 vuotta sitten
vanhempi
commit
8065053898

+ 1 - 1
TestChat/TCViewController.h

@@ -11,7 +11,7 @@
 
 @interface TCViewController : UITableViewController
 
-@property (nonatomic, retain) IBOutlet UITextView *inputView;
+@property (nonatomic, strong) IBOutlet UITextView *inputView;
 
 - (IBAction)reconnect:(id)sender;
 - (IBAction)sendPing:(id)sender;

+ 94 - 87
TestChat/TCViewController.m

@@ -8,109 +8,137 @@
 //
 
 #import "TCViewController.h"
-#import <SocketRocket/SRWebSocket.h>
+
+#import <SocketRocket/SocketRocket.h>
+
 #import "TCChatCell.h"
 
 @interface TCMessage : NSObject
 
-- (instancetype)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
+- (instancetype)initWithMessage:(NSString *)message incoming:(BOOL)incoming;
 
-@property (nonatomic, retain, readonly) NSString *message;
-@property (nonatomic, readonly)  BOOL fromMe;
+@property (nonatomic, copy, readonly) NSString *message;
+@property (nonatomic, assign, readonly, getter=isIncoming) BOOL incoming;
 
 @end
 
+@implementation TCMessage
+
+- (instancetype)initWithMessage:(NSString *)message incoming:(BOOL)incoming
+{
+    self = [super init];
+    if (!self) return self;
+
+    _incoming = incoming;
+    _message = message;
 
-@interface TCViewController () <SRWebSocketDelegate, UITextViewDelegate> 
+    return self;
+}
 
 @end
 
-@implementation TCViewController {
+
+@interface TCViewController () <SRWebSocketDelegate, UITextViewDelegate>
+{
     SRWebSocket *_webSocket;
-    NSMutableArray *_messages;
+    NSMutableArray<TCMessage *> *_messages;
 }
 
-@synthesize inputView = _inputView;
+@end
+
+@implementation TCViewController
 
-#pragma mark - View lifecycle
+///--------------------------------------
+#pragma mark - View
+///--------------------------------------
 
 - (void)viewDidLoad;
 {
     [super viewDidLoad];
+
     _messages = [[NSMutableArray alloc] init];
-    
-    [self.tableView reloadData];
 }
 
-- (void)_reconnect;
+- (void)viewWillAppear:(BOOL)animated
 {
-    _webSocket.delegate = nil;
-    [_webSocket close];
-    
-    _webSocket = [[SRWebSocket alloc] initWithURLRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"ws://localhost:9000/chat"]]];
-    _webSocket.delegate = self;
-    
-    self.title = @"Opening Connection...";
-    [_webSocket open];
+    [super viewWillAppear:animated];
 
+    [self reconnect:nil];
 }
 
-- (void)viewWillAppear:(BOOL)animated
+- (void)viewDidAppear:(BOOL)animated
 {
-    [super viewWillAppear:animated];
-    [self _reconnect];
+    [super viewDidAppear:animated];
+
+    [_inputView becomeFirstResponder];
 }
 
-- (void)reconnect:(id)sender;
+- (void)viewDidDisappear:(BOOL)animated
 {
-    [self _reconnect];
+    [super viewDidDisappear:animated];
+
+    [_webSocket close];
+    _webSocket = nil;
 }
 
-- (void)sendPing:(id)sender;
+///--------------------------------------
+#pragma mark - Actions
+///--------------------------------------
+
+- (IBAction)reconnect:(id)sender
 {
-    [_webSocket sendPing:nil];
+    _webSocket.delegate = nil;
+    [_webSocket close];
+
+    _webSocket = [[SRWebSocket alloc] initWithURL:[NSURL URLWithString:@"wss://echo.websocket.org"]];
+    _webSocket.delegate = self;
+
+    self.title = @"Opening Connection...";
+    [_webSocket open];
 }
 
-- (void)viewDidAppear:(BOOL)animated;
+- (void)sendPing:(id)sender;
 {
-    [super viewDidAppear:animated];
-    
-    [_inputView becomeFirstResponder];
+    [_webSocket sendPing:nil];
 }
 
-- (void)viewDidDisappear:(BOOL)animated
+///--------------------------------------
+#pragma mark - Messages
+///--------------------------------------
+
+- (void)_addMessage:(TCMessage *)message
 {
-	[super viewDidDisappear:animated];
-    
-    _webSocket.delegate = nil;
-    [_webSocket close];
-    _webSocket = nil;
+    [_messages addObject:message];
+    [self.tableView insertRowsAtIndexPaths:@[ [NSIndexPath indexPathForRow:_messages.count - 1 inSection:0] ]
+                          withRowAnimation:UITableViewRowAnimationNone];
+    [self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
 }
 
+///--------------------------------------
 #pragma mark - UITableViewController
+///--------------------------------------
 
-
-- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section;
+- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
 {
     return _messages.count;
 }
 
-- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath;
+- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {
-    TCChatCell *chatCell = (id)cell;
-    TCMessage *message = [_messages objectAtIndex:indexPath.row];
-    chatCell.textView.text = message.message;
-    chatCell.nameLabel.text = message.fromMe ? @"Me" : @"Other";
-}
+    TCMessage *message = _messages[indexPath.row];
 
-- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath;
-{
-    TCMessage *message = [_messages objectAtIndex:indexPath.row];
+    TCChatCell *cell = [self.tableView dequeueReusableCellWithIdentifier:message.incoming ? @"ReceivedCell" : @"SentCell"
+                                                            forIndexPath:indexPath];
 
-    return [self.tableView dequeueReusableCellWithIdentifier:message.fromMe ? @"SentCell" : @"ReceivedCell"];
+    cell.textView.text = message.message;
+    cell.nameLabel.text = message.incoming ? @"Other" : @"Me";
+
+    return cell;
 }
 
+///--------------------------------------
 #pragma mark - SRWebSocketDelegate
+///--------------------------------------
 
 - (void)webSocketDidOpen:(SRWebSocket *)webSocket;
 {
@@ -121,17 +149,15 @@
 - (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error;
 {
     NSLog(@":( Websocket Failed With Error %@", error);
-    
+
     self.title = @"Connection Failed! (see logs)";
     _webSocket = nil;
 }
 
-- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(id)message;
+- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessageWithString:(nonnull NSString *)string
 {
-    NSLog(@"Received \"%@\"", message);
-    [_messages addObject:[[TCMessage alloc] initWithMessage:message fromMe:NO]];
-    [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_messages.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
-    [self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
+    NSLog(@"Received \"%@\"", string);
+    [self _addMessage:[[TCMessage alloc] initWithMessage:string incoming:YES]];
 }
 
 - (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean;
@@ -143,46 +169,27 @@
 
 - (void)webSocket:(SRWebSocket *)webSocket didReceivePong:(NSData *)pongPayload;
 {
-    NSLog(@"Websocket received pong");
+    NSLog(@"WebSocket received pong");
 }
 
-- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text;
-{
-    if ([text rangeOfString:@"\n"].location != NSNotFound) {
-        NSString *message = [[textView.text stringByReplacingCharactersInRange:range withString:text] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
-        [_webSocket send:message];
-        [_messages addObject:[[TCMessage alloc] initWithMessage:message fromMe:YES]];
-        
-        [self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:[NSIndexPath indexPathForRow:_messages.count - 1 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
-        [self.tableView scrollRectToVisible:self.tableView.tableFooterView.frame animated:YES];
-
-        textView.text = @"";
-        return NO;
-    }
-    return YES;
-}
+///--------------------------------------
+#pragma mark - UITextViewDelegate
+///--------------------------------------
 
-- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation;
+- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
 {
-    return YES;
-}
+    if ([text rangeOfString:@"\n"].location != NSNotFound) {
+        NSString *message = [textView.text stringByReplacingCharactersInRange:range withString:text];
+        message = [message stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
 
-@end
+        [_webSocket sendString:message];
 
-@implementation TCMessage
+        [self _addMessage:[[TCMessage alloc] initWithMessage:message incoming:NO]];
 
-@synthesize message = _message;
-@synthesize fromMe = _fromMe;
-
-- (instancetype)initWithMessage:(NSString *)message fromMe:(BOOL)fromMe;
-{
-    self = [super init];
-    if (self) {
-        _fromMe = fromMe;
-        _message = message;
+        textView.text = nil;
+        return NO;
     }
-    
-    return self;
+    return YES;
 }
 
 @end

+ 4 - 4
TestChat/en.lproj/MainStoryboard.storyboard

@@ -1,8 +1,8 @@
 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
-<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5056" systemVersion="13E28" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="J5d-9g-n8O">
+<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="10117" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" initialViewController="J5d-9g-n8O">
     <dependencies>
-        <deployment defaultVersion="1280" identifier="iOS"/>
-        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
+        <deployment identifier="iOS"/>
+        <plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="10085"/>
     </dependencies>
     <scenes>
         <!--View Controller-->
@@ -89,7 +89,7 @@
                         </barButtonItem>
                         <barButtonItem key="rightBarButtonItem" systemItem="refresh" id="cNK-1A-b66">
                             <connections>
-                                <action selector="reconnect:" destination="X5f-jW-I9m" id="V5L-sy-yng"/>
+                                <action selector="reconnect:" destination="X5f-jW-I9m" id="yjV-1U-Tjn"/>
                             </connections>
                         </barButtonItem>
                     </navigationItem>