Skip to content

Instantly share code, notes, and snippets.

@Jxrgxn
Created August 6, 2015 23:01
Show Gist options
  • Save Jxrgxn/2af1f26f89ff0dc84bcd to your computer and use it in GitHub Desktop.
Save Jxrgxn/2af1f26f89ff0dc84bcd to your computer and use it in GitHub Desktop.
Token Cache system
//
// TKCache.h
// TokenApp
//
// Created by BASEL FARAG on 12/18/14.
// Copyright (c) 2014 ABaselNotBasilProduction. All rights reserved.
//
//The purpose of this Support file is to provide custom caching behavior that can be used to quickly display up to date information about media. When the current user likes or comment on a given media we manually update the local cache as soon as the request is sent. This allows us to propogate the model change throughout the app without need to wait for the initial save request to finish and then refreshing the content.
//So for example we can manually manage the like and comment information of images so that the UI stays responsive and accurate when the user likes and comments on photos.
#import <Foundation/Foundation.h>
#import "Macros.h"
@interface TKCache : NSObject
+ (id)sharedCache;
- (void)clear;
- (void)setAttributesForPhoto:(PFObject *)photo likers:(NSArray *)likers commenters:(NSArray *)commenters likedByCurrentUser:(BOOL)likedByCurrentUser;
- (NSDictionary *)attributesForPhoto:(PFObject *)photo;
- (NSNumber *)likeCountForPhoto:(PFObject *)photo;
- (NSNumber *)commentCountForPhoto:(PFObject *)photo;
- (NSArray *)likersForPhoto:(PFObject *)photo;
- (NSArray *)commentersForPhoto:(PFObject *)photo;
- (void)setPhotoIsLikedByCurrentUser:(PFObject *)photo liked:(BOOL)liked;
- (BOOL)isPhotoLikedByCurrentUser:(PFObject *)photo;
- (void)incrementLikerCountForPhoto:(PFObject *)photo;
- (void)decrementLikerCountForPhoto:(PFObject *)photo;
- (void)incrementCommentCountForPhoto:(PFObject *)photo;
- (void)decrementCommentCountForPhoto:(PFObject *)photo;
//-(void)setAttributesForVideo:(PFObject *)video;
- (NSDictionary *)attributesForUser:(PFUser *)user;
- (NSNumber *)photoCountForUser:(PFUser *)user;
- (BOOL)followStatusForUser:(PFUser *)user;
- (void)setPhotoCount:(NSNumber *)count user:(PFUser *)user;
- (void)setFollowStatus:(BOOL)following user:(PFUser *)user;
- (void)setFacebookFriends:(NSArray *)friends;
- (NSArray *)facebookFriends;
@end
//
// TKCache.m
// TokenApp
//
// Created by BASEL FARAG on 12/18/14.
// Copyright (c) 2014 ABaselNotBasilProduction. All rights reserved.
//
#import "TKCache.h"
#import "Macros.h"
#import "Constants.h"
@interface TKCache()
@property (nonatomic, strong) NSCache *cache;
- (void)setAttributes:(NSDictionary *)attributes forPhoto:(PFObject *)photo;
@end
@implementation TKCache
@synthesize cache;
#pragma mark - Initialization
+ (id)sharedCache {
static dispatch_once_t pred = 0;
__strong static id _sharedObject = nil;
dispatch_once(&pred, ^{
_sharedObject = [[self alloc] init];
});
return _sharedObject;
}
- (id)init {
self = [super init];
if (self) {
self.cache = [[NSCache alloc] init];
}
return self;
}
#pragma mark - PTKCache
- (void)clear {
[self.cache removeAllObjects];
}
- (void)setAttributesForPhoto:(PFObject *)photo likers:(NSArray *)likers commenters:(NSArray *)commenters likedByCurrentUser:(BOOL)likedByCurrentUser {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:likedByCurrentUser],kPTKPhotoAttributesIsLikedByCurrentUserKey,
@([likers count]),kPTKPhotoAttributesLikeCountKey,
likers,kPTKPhotoAttributesLikeCountKey,
@([commenters count]),kPTKPhotoAttributesLikeCountKey,
commenters,kPTKPhotoAttributesCommentersKey,
nil];
[self setAttributes:attributes forPhoto:photo];
}
- (NSDictionary *)attributesForPhoto:(PFObject *)photo {
NSString *key = [self keyForPhoto:photo];
return [self.cache objectForKey:key];
}
- (NSNumber *)likeCountForPhoto:(PFObject *)photo {
NSDictionary *attributes = [self attributesForPhoto:photo];
if (attributes) {
return [attributes objectForKey:kPTKPhotoAttributesLikeCountKey];
}
return [NSNumber numberWithInt:0];
}
- (NSNumber *)commentCountForPhoto:(PFObject *)photo {
NSDictionary *attributes = [self attributesForPhoto:photo];
if (attributes) {
return [attributes objectForKey:kPTKPhotoAttributesLikeCountKey];
}
return [NSNumber numberWithInt:0];
}
- (NSArray *)likersForPhoto:(PFObject *)photo {
NSDictionary *attributes = [self attributesForPhoto:photo];
if (attributes) {
return [attributes objectForKey:kPTKPhotoAttributesLikeCountKey];
}
return [NSArray array];
}
- (NSArray *)commentersForPhoto:(PFObject *)photo {
NSDictionary *attributes = [self attributesForPhoto:photo];
if (attributes) {
return [attributes objectForKey:kPTKPhotoAttributesCommentersKey];
}
return [NSArray array];
}
- (void)setPhotoIsLikedByCurrentUser:(PFObject *)photo liked:(BOOL)liked {
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForPhoto:photo]];
[attributes setObject:[NSNumber numberWithBool:liked] forKey:kPTKPhotoAttributesIsLikedByCurrentUserKey];
[self setAttributes:attributes forPhoto:photo];
}
- (BOOL)isPhotoLikedByCurrentUser:(PFObject *)photo {
NSDictionary *attributes = [self attributesForPhoto:photo];
if (attributes) {
return [[attributes objectForKey:kPTKPhotoAttributesIsLikedByCurrentUserKey] boolValue];
}
return NO;
}
- (void)incrementLikerCountForPhoto:(PFObject *)photo {
NSNumber *likerCount = [NSNumber numberWithInt:[[self likeCountForPhoto:photo] intValue] + 1];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForPhoto:photo]];
[attributes setObject:likerCount forKey:kPTKPhotoAttributesLikeCountKey];
[self setAttributes:attributes forPhoto:photo];
}
- (void)decrementLikerCountForPhoto:(PFObject *)photo {
NSNumber *likerCount = [NSNumber numberWithInt:[[self likeCountForPhoto:photo] intValue] - 1];
if ([likerCount intValue] < 0) {
return;
}
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForPhoto:photo]];
[attributes setObject:likerCount forKey:kPTKPhotoAttributesLikeCountKey];
[self setAttributes:attributes forPhoto:photo];
}
- (void)incrementCommentCountForPhoto:(PFObject *)photo {
NSNumber *commentCount = [NSNumber numberWithInt:[[self commentCountForPhoto:photo] intValue] + 1];
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForPhoto:photo]];
[attributes setObject:commentCount forKey:kPTKPhotoAttributesLikeCountKey];
[self setAttributes:attributes forPhoto:photo];
}
- (void)decrementCommentCountForPhoto:(PFObject *)photo {
NSNumber *commentCount = [NSNumber numberWithInt:[[self commentCountForPhoto:photo] intValue] - 1];
if ([commentCount intValue] < 0) {
return;
}
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForPhoto:photo]];
[attributes setObject:commentCount forKey:kPTKPhotoAttributesLikeCountKey];
[self setAttributes:attributes forPhoto:photo];
}
- (void)setAttributesForUser:(PFUser *)user photoCount:(NSNumber *)count followedByCurrentUser:(BOOL)following {
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
count,kPTKUserAttributesPhotoCountKey,
[NSNumber numberWithBool:following],kPTKPhotoAttributesIsLikedByCurrentUserKey,
nil];
[self setAttributes:attributes forUser:user];
}
- (NSDictionary *)attributesForUser:(PFUser *)user {
NSString *key = [self keyForUser:user];
return [self.cache objectForKey:key];
}
- (NSNumber *)photoCountForUser:(PFUser *)user {
NSDictionary *attributes = [self attributesForUser:user];
if (attributes) {
NSNumber *photoCount = [attributes objectForKey:kPTKUserAttributesPhotoCountKey];
if (photoCount) {
return photoCount;
}
}
return [NSNumber numberWithInt:0];
}
- (BOOL)followStatusForUser:(PFUser *)user {
NSDictionary *attributes = [self attributesForUser:user];
if (attributes) {
NSNumber *followStatus = [attributes objectForKey:kPTKPhotoAttributesIsLikedByCurrentUserKey];
if (followStatus) {
return [followStatus boolValue];
}
}
return NO;
}
- (void)setPhotoCount:(NSNumber *)count user:(PFUser *)user {
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForUser:user]];
[attributes setObject:count forKey:kPTKUserAttributesPhotoCountKey];
[self setAttributes:attributes forUser:user];
}
- (void)setFollowStatus:(BOOL)following user:(PFUser *)user {
NSMutableDictionary *attributes = [NSMutableDictionary dictionaryWithDictionary:[self attributesForUser:user]];
[attributes setObject:[NSNumber numberWithBool:following] forKey:kPTKPhotoAttributesIsLikedByCurrentUserKey];
[self setAttributes:attributes forUser:user];
}
- (void)setFacebookFriends:(NSArray *)friends {
NSString *key = kPTKUserFacebookFriendsKey;
[self.cache setObject:friends forKey:key];
[[NSUserDefaults standardUserDefaults] setObject:friends forKey:key];
[[NSUserDefaults standardUserDefaults] synchronize];
}
- (NSArray *)facebookFriends {
NSString *key = kPTKUserFacebookFriendsKey;
if ([self.cache objectForKey:key]) {
return [self.cache objectForKey:key];
}
NSArray *friends = [[NSUserDefaults standardUserDefaults] objectForKey:key];
if (friends) {
[self.cache setObject:friends forKey:key];
}
return friends;
}
#pragma mark - ()
- (void)setAttributes:(NSDictionary *)attributes forPhoto:(PFObject *)photo {
NSString *key = [self keyForPhoto:photo];
[self.cache setObject:attributes forKey:key];
}
- (void)setAttributes:(NSDictionary *)attributes forUser:(PFUser *)user {
NSString *key = [self keyForUser:user];
[self.cache setObject:attributes forKey:key];
}
- (NSString *)keyForPhoto:(PFObject *)photo {
return [NSString stringWithFormat:@"photo_%@", [photo objectId]];
}
- (NSString *)keyForUser:(PFUser *)user {
return [NSString stringWithFormat:@"user_%@", [user objectId]];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment