Permalink
Browse files

New LastFmCache protocol. API calls are going to return NSOperations …

…which can be canclled. Completely new example app.
  • Loading branch information...
kevinrenskers committed Oct 31, 2012
1 parent 67a3f6c commit 82066b483473a01ea521a6febb362dbcd053268c
Showing with 740 additions and 5,313 deletions.
  1. +6 −0 .gitignore
  2. +0 −395 Example.xcodeproj/project.pbxproj
  3. +17 −1 Example/AppDelegate.m
  4. +13 −0 Example/ArtistCell.h
  5. +35 −0 Example/ArtistCell.m
  6. +376 −0 Example/Example.xcodeproj/project.pbxproj
  7. +14 −0 Example/ExampleCache.h
  8. +33 −0 Example/ExampleCache.m
  9. +0 −24 Example/KissXML/Additions/DDXMLElementAdditions.h
  10. +0 −131 Example/KissXML/Additions/DDXMLElementAdditions.m
  11. +0 −14 Example/KissXML/Categories/NSString+DDXML.h
  12. +0 −31 Example/KissXML/Categories/NSString+DDXML.m
  13. +0 −196 Example/KissXML/DDXML.h
  14. +0 −84 Example/KissXML/DDXMLDocument.h
  15. +0 −140 Example/KissXML/DDXMLDocument.m
  16. +0 −66 Example/KissXML/DDXMLElement.h
  17. +0 −801 Example/KissXML/DDXMLElement.m
  18. +0 −156 Example/KissXML/DDXMLNode.h
  19. +0 −2,905 Example/KissXML/DDXMLNode.m
  20. +0 −229 Example/KissXML/Private/DDXMLPrivate.h
  21. +5 −0 Example/Podfile
  22. BIN Example/Supporting Files/Default-568h@2x.png
  23. 0 Example/{ → Supporting Files}/Example-Info.plist
  24. 0 Example/{ → Supporting Files}/Example-Prefix.pch
  25. BIN Example/{ → Supporting Files}/Icon.png
  26. BIN Example/{ → Supporting Files}/Icon@2x.png
  27. BIN Example/Supporting Files/b_close.png
  28. BIN Example/Supporting Files/b_close@2x.png
  29. 0 Example/{ → Supporting Files}/en.lproj/InfoPlist.strings
  30. +82 −72 Example/{ → Supporting Files}/en.lproj/MainStoryboard.storyboard
  31. 0 Example/{ → Supporting Files}/main.m
  32. +59 −34 Example/ViewController.m
  33. +8 −2 LastFm/LastFm.h
  34. +77 −28 LastFm/LastFm.m
  35. +15 −4 README.md
View
@@ -13,3 +13,9 @@ build/*
xcuserdata
profile
*.moved-aside
+
+# CocoaPods
+Pods
+Build
+Podfile.lock
+

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -9,15 +9,31 @@
#import "AppDelegate.h"
#import "LastFm.h"
#import "ViewController.h"
+#import "ExampleCache.h"
+#import "SDURLCache.h"
+
+@interface AppDelegate ()
+@property (strong, nonatomic) ExampleCache *exampleCache;
+@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Set the Last.fm session info
+ // Setup NSURLCache
+ SDURLCache *URLCache = [[SDURLCache alloc] initWithMemoryCapacity:10 * 1024 * 1024
+ diskCapacity:50 * 1024 * 1024
+ diskPath:[SDURLCache defaultCachePath]];
+ [NSURLCache setSharedURLCache:URLCache];
+
+ self.exampleCache = [[ExampleCache alloc] init];
+
+ // Setup the Last.fm SDK
[LastFm sharedInstance].apiKey = @"349b1b1344545e7c7832d0c2a91f44fe";
[LastFm sharedInstance].apiSecret = @"d2a6f3aa73d473d989118e9430a36608";
[LastFm sharedInstance].session = [[NSUserDefaults standardUserDefaults] stringForKey:SESSION_KEY];
[LastFm sharedInstance].username = [[NSUserDefaults standardUserDefaults] stringForKey:USERNAME_KEY];
+ [LastFm sharedInstance].cacheDelegate = self.exampleCache;
+
return YES;
}
View
@@ -0,0 +1,13 @@
+//
+// ArtistCell.h
+// Example
+//
+// Created by Kevin Renskers on 31-10-12.
+// Copyright (c) 2012 Gangverk. All rights reserved.
+//
+
+#import <UIKit/UIKit.h>
+
+@interface ArtistCell : UITableViewCell
+- (void)loadLastFmDataForArtist:(NSString *)artist;
+@end
View
@@ -0,0 +1,35 @@
+//
+// ArtistCell.m
+// Example
+//
+// Created by Kevin Renskers on 31-10-12.
+// Copyright (c) 2012 Gangverk. All rights reserved.
+//
+
+#import "ArtistCell.h"
+#import "UIImageView+WebCache.h"
+#import "LastFm.h"
+
+@interface ArtistCell ()
+@property (strong, nonatomic) NSOperation *operation;
+@end
+
+@implementation ArtistCell
+
+- (void)loadLastFmDataForArtist:(NSString *)artist {
+ self.operation = [[LastFm sharedInstance] getInfoForArtist:artist successHandler:^(NSDictionary *result) {
+ NSURL *image = [result objectForKey:@"image"];
+ if (image) {
+ [self.imageView setImageWithURL:image placeholderImage:[UIImage imageNamed:@"Icon"]];
+ }
+ self.detailTextLabel.text = [NSString stringWithFormat:@"%@ scrobbles", [result objectForKey:@"playcount"]];
+ } failureHandler:nil];
+}
+
+- (void)prepareForReuse {
+ [self.operation cancel];
+ [self.imageView cancelCurrentImageLoad];
+ [super prepareForReuse];
+}
+
+@end

Large diffs are not rendered by default.

Oops, something went wrong.
View
@@ -0,0 +1,14 @@
+//
+// ExampleCache.h
+// Example
+//
+// Created by Kevin Renskers on 31-10-12.
+// Copyright (c) 2012 Gangverk. All rights reserved.
+//
+
+#import <Foundation/Foundation.h>
+#import "LastFm.h"
+
+@interface ExampleCache : NSObject <LastFmCache>
+
+@end
View
@@ -0,0 +1,33 @@
+//
+// ExampleCache.m
+// Example
+//
+// Created by Kevin Renskers on 31-10-12.
+// Copyright (c) 2012 Gangverk. All rights reserved.
+//
+
+#import "ExampleCache.h"
+
+@interface ExampleCache ()
+@property (strong, nonatomic) NSCache *cache;
+@end
+
+@implementation ExampleCache
+
+- (id)init {
+ self = [super init];
+ if (self) {
+ self.cache = [[NSCache alloc] init];
+ }
+ return self;
+}
+
+- (NSArray *)cachedArrayForKey:(NSString *)key {
+ return [self.cache objectForKey:key];
+}
+
+- (void)cacheArray:(NSArray *)array forKey:(NSString *)key {
+ [self.cache setObject:array forKey:key];
+}
+
+@end
@@ -1,24 +0,0 @@
-#import <Foundation/Foundation.h>
-#import "DDXML.h"
-
-// These methods are not part of the standard NSXML API.
-// But any developer working extensively with XML will likely appreciate them.
-
-@interface DDXMLElement (DDAdditions)
-
-+ (DDXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns;
-
-- (DDXMLElement *)elementForName:(NSString *)name;
-- (DDXMLElement *)elementForName:(NSString *)name xmlns:(NSString *)xmlns;
-
-- (NSString *)xmlns;
-- (void)setXmlns:(NSString *)ns;
-
-- (NSString *)prettyXMLString;
-- (NSString *)compactXMLString;
-
-- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string;
-
-- (NSDictionary *)attributesAsDictionary;
-
-@end
@@ -1,131 +0,0 @@
-#import "DDXMLElementAdditions.h"
-
-@implementation DDXMLElement (DDAdditions)
-
-/**
- * Quick method to create an element
-**/
-+ (DDXMLElement *)elementWithName:(NSString *)name xmlns:(NSString *)ns
-{
- DDXMLElement *element = [DDXMLElement elementWithName:name];
- [element setXmlns:ns];
- return element;
-}
-
-/**
- * This method returns the first child element for the given name.
- * If no child element exists for the given name, returns nil.
-**/
-- (DDXMLElement *)elementForName:(NSString *)name
-{
- NSArray *elements = [self elementsForName:name];
- if([elements count] > 0)
- {
- return [elements objectAtIndex:0];
- }
- else
- {
- // Note: If you port this code to work with Apple's NSXML, beware of the following:
- //
- // There is a bug in the NSXMLElement elementsForName: method.
- // Consider the following XML fragment:
- //
- // <query xmlns="jabber:iq:private">
- // <x xmlns="some:other:namespace"></x>
- // </query>
- //
- // Calling [query elementsForName:@"x"] results in an empty array!
- //
- // However, it will work properly if you use the following:
- // [query elementsForLocalName:@"x" URI:@"some:other:namespace"]
- //
- // The trouble with this is that we may not always know the xmlns in advance,
- // so in this particular case there is no way to access the element without looping through the children.
- //
- // This bug was submitted to apple on June 1st, 2007 and was classified as "serious".
- //
- // --!!-- This bug does NOT exist in DDXML --!!--
-
- return nil;
- }
-}
-
-/**
- * This method returns the first child element for the given name and given xmlns.
- * If no child elements exist for the given name and given xmlns, returns nil.
-**/
-- (DDXMLElement *)elementForName:(NSString *)name xmlns:(NSString *)xmlns
-{
- NSArray *elements = [self elementsForLocalName:name URI:xmlns];
- if([elements count] > 0)
- {
- return [elements objectAtIndex:0];
- }
- else
- {
- return nil;
- }
-}
-
-/**
- * Returns the common xmlns "attribute", which is only accessible via the namespace methods.
- * The xmlns value is often used in jabber elements.
-**/
-- (NSString *)xmlns
-{
- return [[self namespaceForPrefix:@""] stringValue];
-}
-
-- (void)setXmlns:(NSString *)ns
-{
- // If you use setURI: then the xmlns won't be displayed in the XMLString.
- // Adding the namespace this way works properly.
- //
- // This applies to both Apple's NSXML and DDXML.
-
- [self addNamespace:[DDXMLNode namespaceWithName:@"" stringValue:ns]];
-}
-
-/**
- * Shortcut to get a pretty (formatted) string representation of the element.
-**/
-- (NSString *)prettyXMLString
-{
- return [self XMLStringWithOptions:(DDXMLNodePrettyPrint | DDXMLNodeCompactEmptyElement)];
-}
-
-/**
- * Shortcut to get a compact string representation of the element.
-**/
-- (NSString *)compactXMLString
-{
- return [self XMLStringWithOptions:DDXMLNodeCompactEmptyElement];
-}
-
-/**
- * Shortcut to avoid having to manually create a DDXMLNode everytime.
-**/
-- (void)addAttributeWithName:(NSString *)name stringValue:(NSString *)string
-{
- [self addAttribute:[DDXMLNode attributeWithName:name stringValue:string]];
-}
-
-/**
- * Returns all the attributes as a dictionary.
-**/
-- (NSDictionary *)attributesAsDictionary
-{
- NSArray *attributes = [self attributes];
- NSMutableDictionary *result = [NSMutableDictionary dictionaryWithCapacity:[attributes count]];
-
- uint i;
- for(i = 0; i < [attributes count]; i++)
- {
- DDXMLNode *node = [attributes objectAtIndex:i];
-
- [result setObject:[node stringValue] forKey:[node name]];
- }
- return result;
-}
-
-@end
@@ -1,14 +0,0 @@
-#import <Foundation/Foundation.h>
-#import <libxml/tree.h>
-
-
-@interface NSString (DDXML)
-
-/**
- * xmlChar - A basic replacement for char, a byte in a UTF-8 encoded string.
-**/
-- (const xmlChar *)xmlChar;
-
-- (NSString *)stringByTrimming;
-
-@end
@@ -1,31 +0,0 @@
-#import "NSString+DDXML.h"
-
-#if ! __has_feature(objc_arc)
-#warning This file must be compiled with ARC. Use -fobjc-arc flag (or convert project to ARC).
-#endif
-
-@implementation NSString (DDXML)
-
-- (const xmlChar *)xmlChar
-{
- return (const xmlChar *)[self UTF8String];
-}
-
-#ifdef GNUSTEP
-- (NSString *)stringByTrimming
-{
- return [self stringByTrimmingSpaces];
-}
-#else
-- (NSString *)stringByTrimming
-{
- NSMutableString *mStr = [self mutableCopy];
- CFStringTrimWhitespace((__bridge CFMutableStringRef)mStr);
-
- NSString *result = [mStr copy];
-
- return result;
-}
-#endif
-
-@end
Oops, something went wrong.

0 comments on commit 82066b4

Please sign in to comment.