Created
March 12, 2013 09:09
-
-
Save leejunkit/5141357 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// FeedViewController.m | |
// BurppleShare | |
// | |
// Created by Lee Jun Kit on 6/3/13. | |
// Copyright (c) 2013 Burpple. All rights reserved. | |
// | |
#import "FeedViewController.h" | |
#import "AppDelegate.h" | |
#import "JournalCell.h" | |
#import "Journal.h" | |
#import "Photo.h" | |
#import "DraftsViewController.h" | |
#import <AssetsLibrary/AssetsLibrary.h> | |
#import <SDWebImage/SDImageCache.h> | |
@interface FeedViewController () | |
@end | |
@implementation FeedViewController { | |
NSURL *photosDirectory; | |
NSManagedObjectContext *moc; | |
NSArray *journalArray; | |
ALAssetsLibrary* assetsLibrary; | |
} | |
- (id)initWithStyle:(UITableViewStyle)style | |
{ | |
self = [super initWithStyle:style]; | |
if (self) { | |
//get a reference to the managed object context | |
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; | |
moc = appDelegate.managedObjectContext; | |
assetsLibrary = [[ALAssetsLibrary alloc] init]; | |
} | |
return self; | |
} | |
- (void)viewDidLoad | |
{ | |
[super viewDidLoad]; | |
//register for notifications | |
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sharingFlowCompleted:) name:@"SharingFlowComplete" object:nil]; | |
//configure the nav bar | |
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(addButtonPressed:)]; | |
UIButton *draftsButton = [UIButton buttonWithType:UIButtonTypeCustom]; | |
draftsButton.frame = CGRectMake(0, 0, 120, 44); | |
[draftsButton setTitle:@"Tap for Drafts" forState:UIControlStateNormal]; | |
[draftsButton addTarget:self action:@selector(draftsButtonTapped:) forControlEvents:UIControlEventTouchUpInside]; | |
self.navigationItem.titleView = draftsButton; | |
//configure the table | |
if ([self.tableView respondsToSelector:@selector(registerClass:forCellReuseIdentifier:)]) { | |
[self.tableView registerClass:[JournalCell class] forCellReuseIdentifier:@"JournalCell"]; | |
} | |
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone; | |
//get a reference to the photos directory | |
AppDelegate *appDelegate = [[UIApplication sharedApplication] delegate]; | |
photosDirectory = [appDelegate.applicationDocumentsDirectory URLByAppendingPathComponent:@"photos" isDirectory:YES]; | |
[self reloadFeedData]; | |
// Uncomment the following line to preserve selection between presentations. | |
// self.clearsSelectionOnViewWillAppear = NO; | |
// Uncomment the following line to display an Edit button in the navigation bar for this view controller. | |
// self.navigationItem.rightBarButtonItem = self.editButtonItem; | |
} | |
- (void)reloadFeedData { | |
//fetch objects from Core Data | |
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Journal" inManagedObjectContext:moc]; | |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; | |
fetchRequest.entity = entityDescription; | |
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(state == 1)"]; | |
fetchRequest.predicate = predicate; | |
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"created" ascending:NO]; | |
fetchRequest.sortDescriptors = @[sortDescriptor]; | |
NSError *fetchError; | |
NSArray *journalResults = [moc executeFetchRequest:fetchRequest error:&fetchError]; | |
if (journalResults) { | |
journalArray = journalResults; | |
SDImageCache *imageCache = [SDImageCache sharedImageCache]; | |
for (Journal *journal in journalArray) { | |
NSOrderedSet *photosInJournal = journal.photos; | |
NSLog(@"the count of photosInJournal is %i", photosInJournal.count); | |
NSLog(@"is journal a fault? %i", journal.isFault); | |
//NSLog(@"ok gonna try accessing them %@", [[photosInJournal objectAtIndex:0] valueForKey:@"resizedImageID"]); | |
for (Photo *photo in photosInJournal) { | |
//pre-load photos into cache | |
if (![imageCache imageFromMemoryCacheForKey:photo.resizedImageID]) { | |
NSData *imageData = [NSData dataWithContentsOfURL:[photosDirectory URLByAppendingPathComponent:[NSString stringWithFormat:@"%@.jpg", photo.resizedImageID]]]; | |
UIImage *image = [UIImage imageWithData:imageData]; | |
[imageCache storeImage:image forKey:photo.resizedImageID toDisk:NO]; | |
} | |
} | |
NSLog(@"now is journal a fault? %i", journal.isFault); | |
} | |
[self.tableView reloadData]; | |
} | |
else { | |
NSLog(@"Error fetching journal entries from Core Data: %@", [fetchError localizedDescription]); | |
} | |
} | |
- (void)didReceiveMemoryWarning | |
{ | |
[super didReceiveMemoryWarning]; | |
} | |
#pragma mark - View actions | |
- (void)addButtonPressed:(id) sender { | |
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Storyboard" bundle:nil]; | |
[self presentModalViewController:[storyboard instantiateViewControllerWithIdentifier:@"SharingFlowStartNavView"] animated:YES]; | |
} | |
- (void)sharingFlowCompleted:(NSNotification *)notification { | |
[self dismissViewControllerAnimated:YES completion:^{ | |
[self reloadFeedData]; | |
}]; | |
} | |
- (void)draftsButtonTapped:(id)sender { | |
DraftsViewController *draftsController = [[DraftsViewController alloc] init]; | |
[self.navigationController pushViewController:draftsController animated:YES]; | |
} | |
#pragma mark - Table view data source | |
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView | |
{ | |
return 1; | |
} | |
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section | |
{ | |
return journalArray.count; | |
} | |
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { | |
return [JournalCell heightForRenderedCellWithJournal:journalArray[indexPath.row]]; | |
} | |
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
static NSString *CellIdentifier = @"JournalCell"; | |
JournalCell *cell; | |
if ([tableView respondsToSelector:@selector(dequeueReusableCellWithIdentifier:forIndexPath:)]) { | |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier forIndexPath:indexPath]; | |
} | |
else { | |
//ios 5 | |
cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier]; | |
if (!cell) { | |
cell = [[JournalCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier]; | |
} | |
} | |
cell.assetsLibary = assetsLibrary; | |
Journal *journal = journalArray[indexPath.row]; | |
[cell renderWithJournal:journal]; | |
return cell; | |
} | |
/* | |
// Override to support conditional editing of the table view. | |
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
// Return NO if you do not want the specified item to be editable. | |
return YES; | |
} | |
*/ | |
/* | |
// Override to support editing the table view. | |
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
if (editingStyle == UITableViewCellEditingStyleDelete) { | |
// Delete the row from the data source | |
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade]; | |
} | |
else if (editingStyle == UITableViewCellEditingStyleInsert) { | |
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view | |
} | |
} | |
*/ | |
/* | |
// Override to support rearranging the table view. | |
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath | |
{ | |
} | |
*/ | |
/* | |
// Override to support conditional rearranging of the table view. | |
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
// Return NO if you do not want the item to be re-orderable. | |
return YES; | |
} | |
*/ | |
#pragma mark - Table view delegate | |
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath | |
{ | |
// Navigation logic may go here. Create and push another view controller. | |
/* | |
<#DetailViewController#> *detailViewController = [[<#DetailViewController#> alloc] initWithNibName:@"<#Nib name#>" bundle:nil]; | |
// ... | |
// Pass the selected object to the new view controller. | |
[self.navigationController pushViewController:detailViewController animated:YES]; | |
*/ | |
} | |
- (void)dealloc { | |
[[NSNotificationCenter defaultCenter] removeObserver:self]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment