Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Branch: master
Fetching contributors…

Cannot retrieve contributors at this time

191 lines (154 sloc) 5.806 kB
//
// LQMessageViewController.m
// TateTracker
//
// Created by Aaron Parecki on 2011-09-07.
// Copyright 2011 Geoloqi.com. All rights reserved.
//
#import "LQMessageViewController.h"
#import "CJSONSerializer.h"
@implementation LQMessageViewController
@synthesize messageText, sendButton, webView, messages, textEntryView, lastMessageReceiveTimestamp, doneButton;
// The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization.
}
return self;
}
- (void)registerForKeyboardNotifications
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeShown:)
name:UIKeyboardWillShowNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(keyboardWillBeHidden:)
name:UIKeyboardWillHideNotification object:nil];
}
- (void)viewDidLoad {
// [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"LQMessageHistory" ofType:@"html"]isDirectory:NO]]];
NSString *path = [[NSBundle mainBundle] pathForResource:@"LQMessageHistory" ofType:@"html"];
webViewHasFinishedLoadingOnce = NO;
[self.webView loadHTMLString:[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil] baseURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] bundlePath]]];
[self registerForKeyboardNotifications];
}
- (void)scheduleMessageRefresh {
messageRefreshTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0
target:self
selector:@selector(messageRefreshTimerDidFire:)
userInfo:nil
repeats:NO] retain];
}
- (void)messageRefreshTimerDidFire:(NSTimer *)timer {
}
- (void)viewWillAppear:(BOOL)animated {
// if(messageRefreshTimer == nil)
// [self scheduleMessageRefresh];
}
- (void)viewWillDisappear:(BOOL)animated {
[messageRefreshTimer invalidate];
[messageRefreshTimer release];
messageRefreshTimer = nil;
}
#pragma mark -
#pragma mark Done Button
- (IBAction)doneButtonWasTapped:(id)sender {
[self.messageText resignFirstResponder];
}
- (void)hideDoneButton {
self.navigationItem.rightBarButtonItem = nil;
}
- (void)showDoneButton {
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Done"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(doneButtonWasTapped:)];
}
#pragma mark -
- (IBAction)sendWasTapped:(id)sender {
if([self.messageText.text isEqualToString:@" "] || [self.messageText.text isEqualToString:@""]){
return;
}
NSString *tag = [NSString stringWithFormat:@"%d", time(0)];
NSDictionary *message = [[NSDictionary alloc] initWithObjectsAndKeys:@"[you]", @"from",
self.messageText.text, @"text",
tag, @"tag",
nil];
NSLog(@"Send was tapped: %@", message);
NSLog(@"%@", [self.webView stringByEvaluatingJavaScriptFromString:[NSString stringWithFormat:@"sentMessage(%@)", [[CJSONSerializer serializer] serializeDictionary:message]]]);
self.messageText.text = @" ";
[self.messageText resignFirstResponder];
}
// Called when the UIKeyboardDidShowNotification is sent.
- (void)keyboardWillBeShown:(NSNotification*)aNotification
{
[self showDoneButton];
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
// If active text field is hidden by keyboard, scroll it so it's visible
// Your application might not need or want this behavior.
CGRect aRect = self.view.frame;
aRect.size.height -= (kbSize.height - 44); // 44 accounts for tabBar height
[UIView animateWithDuration:0.3 animations:^{
self.view.frame = aRect;
}];
}
// Called when the UIKeyboardWillHideNotification is sent
- (void)keyboardWillBeHidden:(NSNotification*)aNotification
{
NSDictionary* info = [aNotification userInfo];
CGSize kbSize = [[info objectForKey:UIKeyboardFrameBeginUserInfoKey] CGRectValue].size;
CGRect aRect = self.view.frame;
aRect.size.height += (kbSize.height - 44); // 44 accounts for tabBar height
[UIView animateWithDuration:0.3 animations:^{
self.view.frame = aRect;
}];
[self hideDoneButton];
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField {
if(textField.text.length == 0)
return YES;
else {
self.messageText.text = @"";
return NO;
}
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
if(!webViewHasFinishedLoadingOnce) {
}
webViewHasFinishedLoadingOnce = YES;
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc. that aren't in use.
}
- (void)viewDidUnload {
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
#pragma mark -
#pragma mark UITextInputDelegate
- (void)textDidChange:(id <UITextInput>)textInput {
}
- (void)textWillChange:(id <UITextInput>)textInput {
}
- (void)selectionDidChange:(id <UITextInput>)textInput {
}
- (void)selectionWillChange:(id <UITextInput>)textInput {
}
#pragma mark -
- (void)dealloc {
[messageRefreshTimer release];
[lastMessageReceiveTimestamp release];
[messageText release];
[sendButton release];
[textEntryView release];
[webView release];
[lastMessageReceiveTimestamp release];
[doneButton release];
[super dealloc];
}
@end
Jump to Line
Something went wrong with that request. Please try again.