Skip to content

Commit

Permalink
Fix the address list fetcher by mapping the returned objects
Browse files Browse the repository at this point in the history
  • Loading branch information
agologan committed Mar 13, 2024
1 parent 52fbc1c commit 9cedefe
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 32 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ In the addresses section fill in as many websites as you want the screensaver to

Passing in a negative time value e.g. `-1` will notify the screensaver to remain on that website indefinitely.

Need some website ideas? Check out suggestions in the [examples](examples.md) section.
Need some website ideas? Check out suggestions in the [examples](examples.json). (feel free to suggest others)

The example file can also be used with the **fetch URLs** feature: `https://raw.githubusercontent.com/liquidx/webviewscreensaver/master/examples.json`

Local **absolute** paths can also be used as an address with or without the `file://` schema.

Expand Down
4 changes: 2 additions & 2 deletions WebViewScreenSaver/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,11 @@
<key>CFBundlePackageType</key>
<string>BNDL</string>
<key>CFBundleShortVersionString</key>
<string>2.3</string>
<string>2.4</string>
<key>CFBundleSignature</key>
<string>????</string>
<key>CFBundleVersion</key>
<string>2.3</string>
<string>2.4</string>
<key>NSPrincipalClass</key>
<string>WebViewScreenSaverView</string>
</dict>
Expand Down
2 changes: 2 additions & 0 deletions WebViewScreenSaver/WVSSAddressListFetcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

#import <Foundation/Foundation.h>

extern NSExceptionName const WVSSInvalidArgumentException;

@protocol WVSSAddressListFetcherDelegate;

@interface WVSSAddressListFetcher : NSObject
Expand Down
53 changes: 44 additions & 9 deletions WebViewScreenSaver/WVSSAddressListFetcher.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
//

#import "WVSSAddressListFetcher.h"
#import "WVSSAddress.h"

NSExceptionName const WVSSInvalidArgumentException = @"WVSSInvalidArgumentException";

@interface WVSSAddressListFetcher () {
NSURLSessionTask *_task;
Expand All @@ -46,7 +49,6 @@ - (id)initWithURL:(NSString *)url {

- (void)didFinishLoading:(NSData *)data error:(NSError *)error {
if (error != nil) {
NSLog(@"Unable to fetch URLs: %@", error);
[self.delegate addressListFetcher:self didFailWithError:error];
return;
}
Expand All @@ -56,19 +58,52 @@ - (void)didFinishLoading:(NSData *)data error:(NSError *)error {
options:NSJSONReadingMutableContainers
error:&jsonError];
if (jsonError) {
NSLog(@"Unable to read connection data: %@", jsonError);
[self.delegate addressListFetcher:self didFailWithError:jsonError];
return;
}

if (![response isKindOfClass:[NSArray class]]) {
NSLog(@"Expected array but got %@", [response class]);
[self.delegate addressListFetcher:self didFailWithError:nil];
return;

NSMutableArray *parsed = [[NSMutableArray alloc] init];

@try {
expectClass(response, NSArray.class);
for (NSDictionary *item in (NSArray *)response) {
expectClass(item, NSDictionary.class);

id url = item[@"url"];
id duration = item[@"duration"];

expectClass(url, NSString.class);
expectClass(duration, NSNumber.class);

[parsed addObject:[WVSSAddress addressWithURL:url
duration:[(NSNumber *)duration intValue]]];
}

[self.delegate addressListFetcher:self didFinishWithArray:parsed];
} @catch (NSException *exception) {
if ([exception.name isEqualToString:WVSSInvalidArgumentException]) {
NSError *error = [NSError errorWithDomain:WVSSInvalidArgumentException
code:-1
userInfo:@{NSLocalizedDescriptionKey: exception.reason}];
[self.delegate addressListFetcher:self didFailWithError:error];
} else {
[exception raise];
}
}

[self.delegate addressListFetcher:self didFinishWithArray:response];

NSLog(@"fetching URLS finished");
}

void expectClass(id target, Class aclass) {
if (![target isKindOfClass:aclass]) {
NSString *reason = [NSString stringWithFormat:@"Expected %@ but got %@",
NSStringFromClass(aclass),
NSStringFromClass([target class])];
NSException *exc = [NSException exceptionWithName:WVSSInvalidArgumentException
reason:reason
userInfo:nil];
[exc raise];
}
}

@end
1 change: 1 addition & 0 deletions WebViewScreenSaver/WVSSConfigController.m
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ - (void)clearWebViewHistory {
#pragma mark -

- (void)addressListFetcher:(WVSSAddressListFetcher *)fetcher didFailWithError:(NSError *)error {
NSLog(@"URLs fetcher encountered issue: %@", error.localizedDescription);
}

- (void)addressListFetcher:(WVSSAddressListFetcher *)fetcher
Expand Down
15 changes: 15 additions & 0 deletions examples.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
[
{ "url": "https://ykob.github.io/sketch-threejs/sketch/dna.html", "duration": 600},
{ "url": "https://ykob.github.io/sketch-threejs/sketch/sun.html", "duration": 600},
{ "url": "http://www.google.com/trends/hottrends/visualize?pn=p1", "duration": 600 },
{ "url": "http://randomstreetview.com/#slideshow", "duration": 600 },
{ "url": "http://mrdoob.com/lab/javascript/webgl/clouds/", "duration": 600 },
{ "url": "http://mrdoob.com/lab/javascript/webgl/particles/magicdust.html", "duration": 600 },
{ "url": "http://www.iamnop.com/particles/", "duration": 600 },
{ "url": "http://www.airtightinteractive.com/demos/js/nebula/", "duration": 600 },
{ "url": "http://alteredqualia.com/three/examples/webgl_cubes.html", "duration": 600 },
{ "url": "http://akirodic.com/p/jellyfish/", "duration": 600 },
{ "url": "http://matthew.wagerfield.com/flat-surface-shader/", "duration": 600 },
{ "url": "http://codepen.io/ykob/full/zGpjeK/", "duration": 600 },
{ "url": "http://www.aresluna.org/polish-tv-clock/", "duration": 600 }
]
20 changes: 0 additions & 20 deletions examples.md

This file was deleted.

0 comments on commit 9cedefe

Please sign in to comment.