Skip to content

Instantly share code, notes, and snippets.

@TheLarkInn
Last active December 29, 2015 08:59
Show Gist options
  • Save TheLarkInn/7647579 to your computer and use it in GitHub Desktop.
Save TheLarkInn/7647579 to your computer and use it in GitHub Desktop.
My Bug Viewer Working!
//
// AppDelegate.h
// Bug Viewer
//
// Created by Sean Larkin on 11/25/13.
// Copyright (c) 2013 Sean Larkin. All rights reserved.
//
#import <Cocoa/Cocoa.h>
@interface AppDelegate : NSObject <NSApplicationDelegate>
@property (assign) IBOutlet NSWindow *window;
@property (assign) IBOutlet NSTableView *bugTicketTable;
@property (assign) IBOutlet NSArrayController *myArrayController;
@property NSUInteger fixedTicketsCount;
@property NSUInteger openTicketsCount;
@property NSUInteger closedTicketsCount;
-(BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag;
-(NSString *)getDataFrom:(NSString *)url;
-(void) populateTable;
-(void)openTicketInTT:(id)sender;
-(void)openTTLink:(NSString*)ttLink;
@end
//
// AppDelegate.m
// Bug Viewer
//
// Created by Sean Larkin on 11/25/13.
// Copyright (c) 2013 Sean Larkin. All rights reserved.
//
#import "AppDelegate.h"
@implementation AppDelegate
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
//populate table with the populateTable method
[_window setReleasedWhenClosed:NO];
[[self bugTicketTable] setDoubleAction:@selector(openTicketInTT:)];
[self populateTable];
[NSTimer scheduledTimerWithTimeInterval:900.0
target:self
selector:@selector(populateTable)
userInfo:nil repeats:YES];
}
//----------------------------------------------------------------------------
- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag{
[_window setIsVisible:YES];
return YES;
}
//----------------------------------------------------------------------------
-(NSData *)getDataFrom:(NSString *)url
{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:url]];
NSError *error = [[NSError alloc] init];
NSHTTPURLResponse *responseCode = nil;
NSData *oResponseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
if([responseCode statusCode] != 200){
NSLog(@"Error getting %@, HTTP status code %li", url, (long)[responseCode statusCode]);
NSLog(@"Status Code %li", [responseCode statusCode]);
return nil;
}
return oResponseData;
}
//----------------------------------------------------------------------------
- (void)populateTable
{
NSString *selectedTicket;
if ([[[self myArrayController] selectedObjects] count] > 0)
{
selectedTicket = [[[self myArrayController] selectedObjects] lastObject][@"ticket_id"];
}
//first check to see if a row is highlighted already...
NSData *data = [self getDataFrom:@"http://172.27.13.175/tt_pt_api/api/v1/reports/tied_emr_tickets"];
NSError *error = nil;
if(error) [[NSAlert alertWithError:error] beginSheetModalForWindow:[self window] modalDelegate:self didEndSelector:nil contextInfo:nil];
NSMutableDictionary *res = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:&error];
NSArray *array = [res valueForKey:@"reports"];
[self.myArrayController setContent:array];
NSUInteger selectedRowIndex = [[[self myArrayController] arrangedObjects] indexOfObjectPassingTest:
^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
return [[obj valueForKey:@"ticket_id"] isEqual:selectedTicket];
}];
[self.myArrayController setSelectionIndex:selectedRowIndex];
self.fixedTicketsCount = [[[[self myArrayController] arrangedObjects] indexesOfObjectsPassingTest:
^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
return [obj[@"status"] isEqualToString:@"Fixed"];
}] count];
self.closedTicketsCount = [[[[self myArrayController] arrangedObjects] indexesOfObjectsPassingTest:
^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
return [obj[@"status"] isEqualToString:@"Closed"];
}] count];
self.openTicketsCount = [[[[self myArrayController] arrangedObjects] indexesOfObjectsPassingTest:
^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
return [obj[@"status"] isEqualToString:@"Open"];
}] count];
}
//----------------------------------------------------------------------------
-(void)openTicketInTT:(id)sender
{
long clickedRowId = [[self bugTicketTable] clickedRow];
NSDictionary *clickedRow = [[[self myArrayController] arrangedObjects] objectAtIndex:clickedRowId];
NSString *ticketIdURL = [clickedRow valueForKey:@"ticket_id"];
[self openTTLink:ticketIdURL];
}
//----------------------------------------------------------------------------
-(void)openTTLink:(NSString*)ttLink
{
[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:ttLink]];
}
//----------------------------------------------------------------------------
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment