Skip to content

Commit

Permalink
Made path to stored image creating dynamically since app id is changi…
Browse files Browse the repository at this point in the history
…ng every time app is launched
  • Loading branch information
OmelDM committed Apr 7, 2015
1 parent 254a494 commit 350e161
Show file tree
Hide file tree
Showing 18 changed files with 118 additions and 54 deletions.
52 changes: 44 additions & 8 deletions CSNCensorNetUaProject.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
objects = {

/* Begin PBXBuildFile section */
1A0BB12A1AD3F828004D3E1C /* CSNCommonFunctions.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A0BB1291AD3F828004D3E1C /* CSNCommonFunctions.m */; };
1A3EC1601ACC9AFD001D43D3 /* NewsData.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 1A3EC15E1ACC9AFD001D43D3 /* NewsData.xcdatamodeld */; };
1A4494F01AB87A7100DB3E4E /* CSNNewsTableViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A4494EF1AB87A7100DB3E4E /* CSNNewsTableViewController.m */; };
1A689C581ABC7D92004A6627 /* CSNDownloader.m in Sources */ = {isa = PBXBuildFile; fileRef = 1A689C571ABC7D92004A6627 /* CSNDownloader.m */; };
Expand Down Expand Up @@ -34,6 +35,9 @@
/* End PBXContainerItemProxy section */

/* Begin PBXFileReference section */
1A0BB1271AD3F53A004D3E1C /* CSNPrefixHeader.pch */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = CSNPrefixHeader.pch; path = Sources/CSNPrefixHeader.pch; sourceTree = "<group>"; };
1A0BB1281AD3F7FC004D3E1C /* CSNCommonFunctions.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; name = CSNCommonFunctions.h; path = Sources/CSNCommonFunctions.h; sourceTree = "<group>"; };
1A0BB1291AD3F828004D3E1C /* CSNCommonFunctions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = CSNCommonFunctions.m; path = Sources/CSNCommonFunctions.m; sourceTree = "<group>"; };
1A3EC15F1ACC9AFD001D43D3 /* NewsData.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = NewsData.xcdatamodel; sourceTree = "<group>"; };
1A4494EE1AB87A7100DB3E4E /* CSNNewsTableViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CSNNewsTableViewController.h; sourceTree = "<group>"; };
1A4494EF1AB87A7100DB3E4E /* CSNNewsTableViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CSNNewsTableViewController.m; sourceTree = "<group>"; };
Expand Down Expand Up @@ -80,6 +84,38 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
1A0BB12B1AD3FB09004D3E1C /* Resources */ = {
isa = PBXGroup;
children = (
1AF3C5981AB877F4006FB959 /* Main.storyboard */,
1AF3C59B1AB877F4006FB959 /* Images.xcassets */,
1AF3C59D1AB877F4006FB959 /* LaunchScreen.xib */,
);
name = Resources;
sourceTree = "<group>";
};
1A0BB12C1AD3FB25004D3E1C /* Common */ = {
isa = PBXGroup;
children = (
1AF3C58F1AB877F4006FB959 /* AppDelegate.h */,
1AF3C5901AB877F4006FB959 /* AppDelegate.m */,
1A0BB1271AD3F53A004D3E1C /* CSNPrefixHeader.pch */,
1A0BB1281AD3F7FC004D3E1C /* CSNCommonFunctions.h */,
1A0BB1291AD3F828004D3E1C /* CSNCommonFunctions.m */,
);
name = Common;
sourceTree = "<group>";
};
1A0BB12D1AD3FB48004D3E1C /* Model */ = {
isa = PBXGroup;
children = (
1AA5EBB21ABAF9B90066A62B /* CSNNews.h */,
1AA5EBB31ABAF9B90066A62B /* CSNNews.m */,
1A3EC15E1ACC9AFD001D43D3 /* NewsData.xcdatamodeld */,
);
name = Model;
sourceTree = "<group>";
};
1AF3C57F1AB877F4006FB959 = {
isa = PBXGroup;
children = (
Expand All @@ -101,8 +137,7 @@
1AF3C58A1AB877F4006FB959 /* CSNCensorNetUaProject */ = {
isa = PBXGroup;
children = (
1AF3C58F1AB877F4006FB959 /* AppDelegate.h */,
1AF3C5901AB877F4006FB959 /* AppDelegate.m */,
1A0BB12C1AD3FB25004D3E1C /* Common */,
1A4494EE1AB87A7100DB3E4E /* CSNNewsTableViewController.h */,
1A4494EF1AB87A7100DB3E4E /* CSNNewsTableViewController.m */,
1ACFF8041AC02B2C00B0DE11 /* CSNNewsDetailsViewController.h */,
Expand All @@ -111,16 +146,12 @@
1A742AE71ABDA0BE00849948 /* CSNNewsTableViewCell.m */,
1A6DF5051AC43E6C00C92AFF /* CSNArticlesTableViewController.h */,
1A6DF5061AC43E6C00C92AFF /* CSNArticlesTableViewController.m */,
1AA5EBB21ABAF9B90066A62B /* CSNNews.h */,
1AA5EBB31ABAF9B90066A62B /* CSNNews.m */,
1AA5EBB51ABAFB1F0066A62B /* CSNXMLParserOperation.h */,
1AA5EBB61ABAFB1F0066A62B /* CSNXMLParserOperation.m */,
1A689C561ABC7D92004A6627 /* CSNDownloader.h */,
1A689C571ABC7D92004A6627 /* CSNDownloader.m */,
1AF3C5981AB877F4006FB959 /* Main.storyboard */,
1AF3C59B1AB877F4006FB959 /* Images.xcassets */,
1AF3C59D1AB877F4006FB959 /* LaunchScreen.xib */,
1A3EC15E1ACC9AFD001D43D3 /* NewsData.xcdatamodeld */,
1A0BB12D1AD3FB48004D3E1C /* Model */,
1A0BB12B1AD3FB09004D3E1C /* Resources */,
1AF3C58B1AB877F4006FB959 /* Supporting Files */,
);
path = CSNCensorNetUaProject;
Expand Down Expand Up @@ -257,6 +288,7 @@
1AA5EBB71ABAFB1F0066A62B /* CSNXMLParserOperation.m in Sources */,
1AF3C5911AB877F4006FB959 /* AppDelegate.m in Sources */,
1AA5EBB41ABAF9B90066A62B /* CSNNews.m in Sources */,
1A0BB12A1AD3F828004D3E1C /* CSNCommonFunctions.m in Sources */,
1A6DF5071AC43E6C00C92AFF /* CSNArticlesTableViewController.m in Sources */,
1A4494F01AB87A7100DB3E4E /* CSNNewsTableViewController.m in Sources */,
1AF3C58E1AB877F4006FB959 /* main.m in Sources */,
Expand Down Expand Up @@ -385,6 +417,8 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = CSNCensorNetUaProject/Sources/CSNPrefixHeader.pch;
INFOPLIST_FILE = CSNCensorNetUaProject/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand All @@ -395,6 +429,8 @@
isa = XCBuildConfiguration;
buildSettings = {
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
GCC_PRECOMPILE_PREFIX_HEADER = YES;
GCC_PREFIX_HEADER = CSNCensorNetUaProject/Sources/CSNPrefixHeader.pch;
INFOPLIST_FILE = CSNCensorNetUaProject/Info.plist;
LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks";
PRODUCT_NAME = "$(TARGET_NAME)";
Expand Down
4 changes: 2 additions & 2 deletions CSNCensorNetUaProject/AppDelegate.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
// Created by Dmytro Omelchuk on 3/17/15.
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////

#import <UIKit/UIKit.h>

////////////////////////////////////////////////////////////////////////////////
@interface AppDelegate : UIResponder <UIApplicationDelegate>

@property (strong, nonatomic) UIWindow *window;
Expand Down
1 change: 0 additions & 1 deletion CSNCensorNetUaProject/CSNNewsTableViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
#import <UIKit/UIKit.h>

////////////////////////////////////////////////////////////////////////////////
@interface CSNNewsTableViewController : UITableViewController
Expand Down
1 change: 0 additions & 1 deletion CSNCensorNetUaProject/CSNNewsTableViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ - (CSNNews *)newsWithDictionary:(NSDictionary *)aDictionary
theResultNews.pubDate = aDictionary[kPubDateName];
theResultNews.comments = aDictionary[kCommentsName];
theResultNews.enclosure = aDictionary[kEnclosureName];
theResultNews.imagePath = aDictionary[kImagePathName];
theResultNews.content = aDictionary[kContentName];

return theResultNews;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@
<attribute name="details" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="enclosure" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="guid" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="imagePath" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="link" optional="YES" attributeType="String" syncable="YES"/>
<attribute name="pubDate" optional="YES" attributeType="Date" syncable="YES"/>
<attribute name="title" optional="YES" attributeType="String" syncable="YES"/>
</entity>
<elements>
<element name="News" positionX="-63" positionY="-18" width="128" height="180"/>
<element name="News" positionX="-63" positionY="-18" width="128" height="165"/>
</elements>
</model>
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
#import <UIKit/UIKit.h>

////////////////////////////////////////////////////////////////////////////////
@interface CSNArticlesTableViewController : UITableViewController
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ - (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(
CSNNews *theNews = self.articles[anIndexPath.row];
theCell.dateLabel.text = [NSDateFormatter localizedStringFromDate:theNews.pubDate
dateStyle:NSDateFormatterNoStyle timeStyle:NSDateFormatterShortStyle];
theCell.titleView.text = theNews.title;
theCell.titleLabel.text = theNews.title;

return theCell;
}
Expand Down
15 changes: 15 additions & 0 deletions CSNCensorNetUaProject/Sources/CSNCommonFunctions.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//
// CSNCommonFunctions.h
// CSNCensorNetUaProject
//
// Created by Dmytro Omelchuk on 4/7/15.
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////

#ifndef CSNCensorNetUaProject_CSNCommonFunctions_h
#define CSNCensorNetUaProject_CSNCommonFunctions_h

NSString *CSNPathToStoredFileFromURLString(NSString *anURLString);

#endif
19 changes: 19 additions & 0 deletions CSNCensorNetUaProject/Sources/CSNCommonFunctions.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//
// CSNCommonFunctions.m
// CSNCensorNetUaProject
//
// Created by Dmytro Omelchuk on 4/7/15.
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
#import "CSNCommonFunctions.h"

////////////////////////////////////////////////////////////////////////////////
NSString *CSNPathToStoredFileFromURLString(NSString *anURLString)
{
NSString *theDirectory = [NSSearchPathForDirectoriesInDomains(NSApplicationSupportDirectory,
NSUserDomainMask, YES) lastObject];
NSString *thePath = [theDirectory stringByAppendingFormat:@"/%@/%ld.jpg",
[[NSBundle mainBundle] bundleIdentifier], [anURLString hash]];
return thePath;
}
6 changes: 1 addition & 5 deletions CSNCensorNetUaProject/Sources/CSNDownloader.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
#import <Foundation/Foundation.h>

////////////////////////////////////////////////////////////////////////////////
@interface CSNDownloader : NSObject

+ (instancetype)sharedDownloader;

/**
@return NSString with path to downloaded file
*/
- (NSString *)addFileToDownloaderAtURL:(NSURL *)anURL;
- (void)addFileToDownloaderAtURL:(NSString *)anURL;

@end
20 changes: 6 additions & 14 deletions CSNCensorNetUaProject/Sources/CSNDownloader.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,14 @@ - (instancetype)init
return self;
}

- (NSString *)addFileToDownloaderAtURL:(NSURL *)anURL
- (void)addFileToDownloaderAtURL:(NSString *)anURL
{
NSString *thePathToFile = [self pathToDownloadedFileAtURL:anURL];
NSString *thePathToFile = CSNPathToStoredFileFromURLString(anURL);

if (![[NSFileManager defaultManager] fileExistsAtPath:thePathToFile])
{
NSURLSessionDownloadTask *theTask = [self.session downloadTaskWithURL:anURL
NSURLSessionDownloadTask *theTask = [self.session
downloadTaskWithURL:[NSURL URLWithString:anURL]
completionHandler:^(NSURL *aLocation, NSURLResponse *aResponse, NSError *anError)
{
if (nil != anError)
Expand All @@ -69,7 +70,7 @@ - (NSString *)addFileToDownloaderAtURL:(NSURL *)anURL
{
if (![theManager createDirectoryAtPath:[thePathToFile
stringByDeletingLastPathComponent]
withIntermediateDirectories:NO
withIntermediateDirectories:YES
attributes:nil error:&theError])
{
#warning Handle error
Expand All @@ -85,17 +86,8 @@ - (NSString *)addFileToDownloaderAtURL:(NSURL *)anURL
}];
[theTask resume];
}
return thePathToFile;
}

#pragma mark Private methods
- (NSString *)pathToDownloadedFileAtURL:(NSURL *)anURL
{
NSString *theDirectory = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,
NSUserDomainMask, YES) lastObject];
NSString *thePath = [theDirectory stringByAppendingFormat:@"/%@/%ld.jpg",
[[NSBundle mainBundle] bundleIdentifier], [anURL hash]];
return thePath;
}
#pragma mark - Private methods

@end
3 changes: 1 addition & 2 deletions CSNCensorNetUaProject/Sources/CSNNews.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
#import <Foundation/Foundation.h>
#import <CoreData/CoreData.h>

////////////////////////////////////////////////////////////////////////////////
Expand All @@ -19,7 +18,7 @@
@property (nonatomic, copy) NSDate *pubDate;
@property (nonatomic, copy) NSString *comments;
@property (nonatomic, copy) NSString *enclosure;
@property (nonatomic, copy) NSString *imagePath;
@property (nonatomic, readonly) NSString *imagePath;
@property (nonatomic, copy) NSString *content;

@end
12 changes: 3 additions & 9 deletions CSNCensorNetUaProject/Sources/CSNNews.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,11 @@
////////////////////////////////////////////////////////////////////////////////
@implementation CSNNews

@dynamic title, link, details, guid, pubDate, comments, enclosure, imagePath, content;
@dynamic title, link, details, guid, pubDate, comments, enclosure, content;

- (NSString *)description
- (NSString *)imagePath
{
return [NSString stringWithFormat:@"News: %@\nPublication date: %@\nDetails: "
"%@\nContent: %@\nPath to image: %@\nImage URL: %@\nLink: %@\n"
"Comments: %@\nPersistant link: %@",
self.title, [NSDateFormatter localizedStringFromDate:self.pubDate
dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle],
self.details, self.content, self.imagePath, self.enclosure,
self.link, self.comments, self.guid];
return CSNPathToStoredFileFromURLString(self.enclosure);
}

@end
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
#import <UIKit/UIKit.h>

////////////////////////////////////////////////////////////////////////////////
@class CSNNews;
Expand Down
22 changes: 22 additions & 0 deletions CSNCensorNetUaProject/Sources/CSNPrefixHeader.pch
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//
// CSNPrefixHeader.pch
// CSNCensorNetUaProject
//
// Created by Dmytro Omelchuk on 4/7/15.
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////

#ifndef CSNCensorNetUaProject_CSNPrefixHeader_pch
#define CSNCensorNetUaProject_CSNPrefixHeader_pch

#ifdef __OBJC__

#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>

#endif

#import "CSNCommonFunctions.h"

#endif
2 changes: 0 additions & 2 deletions CSNCensorNetUaProject/Sources/CSNXMLParserOperation.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
#import <Foundation/Foundation.h>

////////////////////////////////////////////////////////////////////////////////
extern NSString *const kTitleName;
Expand All @@ -17,7 +16,6 @@ extern NSString *const kPubDateName;
extern NSString *const kCommentsName;
extern NSString *const kContentName;
extern NSString *const kEnclosureName;
extern NSString *const kImagePathName;

////////////////////////////////////////////////////////////////////////////////
@interface CSNXMLParserOperation : NSOperation
Expand Down
7 changes: 3 additions & 4 deletions CSNCensorNetUaProject/Sources/CSNXMLParserOperation.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
NSString *const kContentName = @"content";
NSString *const kEnclosureName = @"enclosure";
NSString *const kURLName = @"url";
NSString *const kImagePathName = @"imagePath";

////////////////////////////////////////////////////////////////////////////////
@interface CSNXMLParserOperation () <NSXMLParserDelegate>
Expand Down Expand Up @@ -128,11 +127,11 @@ - (void)parser:(NSXMLParser *)aParser didStartElement:(NSString *)anElementName
else if (nil != self.currentNews && [anElementName isEqualToString:kEnclosureName])
{
NSString *theURL = anAttributeDictionary[kURLName];
if (nil != theURL)
if ([theURL containsString:@"censor_news_small"])
{
self.currentNews[kEnclosureName] = theURL;
self.currentNews[kImagePathName] = [[[CSNDownloader sharedDownloader]
addFileToDownloaderAtURL:[NSURL URLWithString:theURL]] copy];
[[CSNDownloader sharedDownloader]
addFileToDownloaderAtURL:theURL];
}
}

Expand Down
1 change: 0 additions & 1 deletion CSNCensorNetUaProject/main.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
// Copyright (c) 2015 Dmytro Omelchuk. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////
#import <UIKit/UIKit.h>
#import "AppDelegate.h"

////////////////////////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 350e161

Please sign in to comment.