Skip to content

Commit

Permalink
qq
Browse files Browse the repository at this point in the history
  • Loading branch information
sashavtyurina committed Mar 11, 2014
1 parent 23db8be commit 1d14563
Show file tree
Hide file tree
Showing 17 changed files with 990 additions and 199 deletions.
12 changes: 11 additions & 1 deletion TochkaK/LibraryBook.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,19 @@
@interface LibraryBook : NSObject
@property (nonatomic)NSInteger ID;
@property (strong, nonatomic) NSString* title;
@property (strong, nonatomic) NSString* subTitle;
@property (strong, nonatomic) NSString* authorTitle;
@property (strong, nonatomic) NSString* url;
@property (strong, nonatomic) NSString* published;
@property (nonatomic) BOOL free;
@property (strong, nonatomic) NSString* description;

-(LibraryBook*) initWithID:(NSInteger) ID title:(NSString*) title authorTitle:(NSString*) authorTitle url:(NSString*)url free:(BOOL)free;
-(LibraryBook*) initWithID:(NSInteger) ID
title:(NSString*) title
subtitle:(NSString*) subtitle
authorTitle:(NSString*) authorTitle
url:(NSString*)url
published:(NSString*) published
free:(BOOL)free
description:(NSString*) description;
@end
26 changes: 20 additions & 6 deletions TochkaK/LibraryBook.m
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,35 @@
#import "LibraryBook.h"

@implementation LibraryBook
@synthesize ID = _id;
@synthesize title = _title;
@synthesize authorTitle = _authorTitle;
@synthesize free = _free;
@synthesize url = _url;
-(LibraryBook*) initWithID:(NSInteger) ID title:(NSString*) title authorTitle:(NSString*) authorTitle url:(NSString*)url free:(BOOL)free
//@synthesize ID = _id;
//@synthesize title = _title;
//@synthesize authorTitle = _authorTitle;
//@synthesize free = _free;
//@synthesize url = _url;
//@synthesize description = _description;
//@synthesize subTitle = _subTitle;
//@synthesize published = _published;

-(LibraryBook*) initWithID:(NSInteger) ID
title:(NSString*) title
subtitle:(NSString *)subtitle
authorTitle:(NSString *)authorTitle
url:(NSString *)url
published:(NSString *)published
free:(BOOL)free
description:(NSString *)description
{
self = [super init];
if (self)
{
self.ID = ID;
self.title = title;
self.subTitle = subtitle;
self.authorTitle = authorTitle;
self.url = url;
self.published = published;
self.free = free;
self.description = description;
}
return self;
}
Expand Down
9 changes: 6 additions & 3 deletions TochkaK/LibraryBookCreator.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
#import "LibraryBook.h"

@interface LibraryBookCreator : NSObject
@property (strong, nonatomic) NSMutableArray* books;
-(NSArray*) booksFromJSON: (NSData*)data error:(NSError**) error;
-(void) complementBook:(LibraryBook*) book withDetailedDescription:(NSData*) complementaryData;
//@property (strong, nonatomic) NSMutableArray* books;

+(NSArray*) booksFromJSON: (NSData*)data error:(NSError**) error;

//-(void) complementBook:(LibraryBook*) book withDetailedDescription:(NSData*) complementaryData;
+(LibraryBook*) singleBookFromJSON:(NSData*)data error:(NSError**)error;
@end
91 changes: 65 additions & 26 deletions TochkaK/LibraryBookCreator.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,49 +8,88 @@

#import "LibraryBookCreator.h"



@implementation LibraryBookCreator
@synthesize books = _books;
-(NSMutableArray*) books
{
if (!_books)
{
_books = [[NSMutableArray alloc] init];
}
return _books;
}
//@synthesize books = _books;
//-(NSMutableArray*) books
//{
// if (!_books)
// {
// _books = [[NSMutableArray alloc] init];
// }
// return _books;
//}

-(NSArray*) booksFromJSON: (NSData*)data error:(NSError**) error
+(NSArray*) booksFromJSON: (NSData*)data error:(NSError**) error
{
//convert dataRecieved to JSON object
NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);

NSError* err = nil;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:&err];
NSMutableArray* books = [[NSMutableArray alloc] init];
// NSString* str = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// NSLog(@"%@", str);

if (err)
{
NSLog(@"An error occured. %@", err.description);
}
//NSError* err = nil;
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
//
// if (error)
// {
// NSLog(@"An error occured. %@", &error.description);
// }

NSArray* booksRaw = [json valueForKey:@"books"];

for (NSDictionary* bookRaw in booksRaw) {
LibraryBook* book = [[LibraryBook alloc]
initWithID: [[bookRaw objectForKey:@"id"] integerValue]
title:[bookRaw objectForKey:@"title"]
subtitle:@""
authorTitle:[bookRaw valueForKey:@"author_title"]
url:[bookRaw valueForKey:@"img"]
free:[[bookRaw objectForKey:@"free"] boolValue]];
[self.books addObject:book];
published:@""
free:[[bookRaw objectForKey:@"free"] boolValue]
description:@"" ];

//[self.books addObject:book];
[books addObject:book];
}
NSLog(@"Count = %d", booksRaw.count);
//NSLog(@"Count = %d", booksRaw.count);



return self.books;
return books;
}

-(void) complementBook:(LibraryBook*) book withDetailedDescription:(NSData*) complementaryData
{}
+(LibraryBook*) singleBookFromJSON:(NSData*)data error:(NSError**)error
{

NSDictionary* jsonRaw = [NSJSONSerialization JSONObjectWithData:data options:0 error:error];
NSDictionary* json = jsonRaw[@"book"];
NSInteger ID = [[json valueForKey:@"id"] integerValue];
NSString* title = [json valueForKey:@"title"];
NSString* subtitle = [json valueForKey:@"subtitle"];
NSString* authorTitle = [json valueForKey:@"author_title"];
NSString* url = [json valueForKey:@"img"];
NSString* published = [json valueForKey:@"published"];
BOOL free = [[json valueForKey:@"free"] boolValue];
NSString* description = [json valueForKey:@"description"];


title = [title isEqual:[NSNull null]] ? @"Unknown name" : title;
authorTitle = [authorTitle isEqual:[NSNull null]] ? @"Unknown author" : authorTitle;
url = [url isEqual:[NSNull null]] ? @"" : url;
description = [description isEqual:[NSNull null]] ? @"No description available" : description;
published = [published isEqual:[NSNull null]] ? @"" : published;
subtitle = [subtitle isEqual:[NSNull null]] ? @"" : subtitle;


LibraryBook* book = [[LibraryBook alloc] initWithID:ID
title:title
subtitle:subtitle
authorTitle:authorTitle
url:url
published:published
free:free
description:description];
return book;

}
@end
4 changes: 3 additions & 1 deletion TochkaK/LibraryDetailedBookViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#import "LibraryBook.h"

@interface LibraryDetailedBookViewController : UIViewController
@property (weak, nonatomic) LibraryBook* bookToShow;
@property (strong, nonatomic) LibraryBook* bookToShow;

-(void) updateUI;

@end
136 changes: 120 additions & 16 deletions TochkaK/LibraryDetailedBookViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,92 @@ @interface LibraryDetailedBookViewController ()
@property (strong, nonatomic) UILabel* titleLabel;
@property (strong, nonatomic) UILabel* authorTitleLabel;
@property (strong, nonatomic) UIImageView* coverImage;
@property (strong, nonatomic) UILabel* descriptionLabel;
@property (strong, nonatomic) UILabel* subtitleLable;
@property (strong, nonatomic) UILabel* publishedLabel;
@property (strong, nonatomic) UILabel* freeLabel;

@property (strong, nonatomic) UIScrollView* scrollView;
@end

@implementation LibraryDetailedBookViewController
@synthesize titleLabel = _titleLabel;
@synthesize authorTitleLabel = _authorTitleLabel;
@synthesize coverImage = _coverImage;
- (UIImageView *)coverImage
@synthesize bookToShow = _bookToShow;

-(UILabel*) titleLabel
{
if (!_coverImage)
if (!_titleLabel)
{
_coverImage = [[UIImageView alloc] initWithFrame:CGRectMake(250, 40, 50, 50)];
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 20, 240, 25)];
}
return _coverImage;
return _titleLabel;
}

-(UILabel*) titleLabel

-(UILabel*) subtitleLable
{
if (!_titleLabel)
if (!_subtitleLable)
{
_titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 40, 300, 50)];
_subtitleLable = [[UILabel alloc] initWithFrame:CGRectMake(20, 50, 240, 25)];
}
return _titleLabel;
return _subtitleLable;
}

-(UILabel*) authorTitleLabel
{
if (!_authorTitleLabel)
{
_authorTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(40, 90, 300, 50)];
_authorTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 80, 300, 25)];
}
return _authorTitleLabel;
}

-(UILabel*) publishedLabel
{
if (!_publishedLabel)
{
_publishedLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 110, 300, 25)];
}
return _publishedLabel;
}

-(UILabel*) freeLabel
{
if (!_freeLabel)
{
_freeLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 140, 300, 25)];
}
return _freeLabel;
}

-(UILabel*) descriptionLabel
{
if (!_descriptionLabel)
{
_descriptionLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 170, 300, 75)];
}
return _descriptionLabel;
}

-(LibraryBook*) bookToShow
{
if (!_bookToShow)
{
_bookToShow = [[LibraryBook alloc] init];
}
return _bookToShow;
}

- (UIImageView *)coverImage
{
if (!_coverImage)
{
_coverImage = [[UIImageView alloc] initWithFrame:CGRectMake(250, 40, 50, 50)];
}
return _coverImage;
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
Expand All @@ -58,23 +111,74 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- (void)viewDidLoad
{
[super viewDidLoad];
CGPoint p = self.view.bounds.origin;
CGSize s = self.view.bounds.size;
self.view.backgroundColor = [UIColor whiteColor];

self.scrollView = [[UIScrollView alloc] initWithFrame:self.view.frame];
self.scrollView.backgroundColor = [UIColor whiteColor];
[self.view addSubview:self.scrollView];

// Do any additional setup after loading the view.
self.titleLabel.backgroundColor = [UIColor clearColor];
self.titleLabel.textColor = [UIColor blackColor];
self.titleLabel.font = [UIFont fontWithName:@"Courier" size:30];
self.titleLabel.font = [UIFont fontWithName:@"Courier" size:24];
self.titleLabel.text = @"";

self.subtitleLable.textColor = [UIColor blackColor];
self.subtitleLable.font = [UIFont fontWithName:@"Courier" size:20];
self.subtitleLable.text = @"";

self.authorTitleLabel.textColor = [UIColor grayColor];
self.authorTitleLabel.font = [UIFont fontWithName:@"Courier" size:20];
self.authorTitleLabel.text = @"";

self.publishedLabel.textColor = [UIColor grayColor];
self.publishedLabel.font = [UIFont fontWithName:@"Courier" size:20];
self.publishedLabel.text = @"";

self.freeLabel.textColor = [UIColor grayColor];
self.freeLabel.font = [UIFont fontWithName:@"Courier" size:20];
self.freeLabel.text = @"";

self.descriptionLabel.text = @"Description: ";
self.descriptionLabel.textColor = [UIColor blackColor];
self.descriptionLabel.font = [UIFont fontWithName:@"Courier" size:18];
self.descriptionLabel.lineBreakMode = NSLineBreakByWordWrapping;
self.descriptionLabel.numberOfLines = 0;

self.coverImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.bookToShow.url]]];
[self.scrollView addSubview:self.coverImage];

[self.scrollView addSubview:self.titleLabel];
[self.scrollView addSubview:self.subtitleLable];
[self.scrollView addSubview:self.authorTitleLabel];
[self.scrollView addSubview:self.publishedLabel];
[self.scrollView addSubview:self.freeLabel];
[self.scrollView addSubview:self.descriptionLabel];
}

-(void) updateUI
{
self.titleLabel.text = self.bookToShow.title;
self.authorTitleLabel.text = self.bookToShow.authorTitle;
self.descriptionLabel.text = [NSString stringWithFormat:@"%@: %@", @"Description", self.bookToShow.description];
self.subtitleLable.text = self.bookToShow.subTitle;
self.freeLabel.text = self.bookToShow.free ? @"Бесплатная": @"Платная";
self.publishedLabel.text = self.bookToShow.published;

self.coverImage.image = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.bookToShow.url]]];

[self.view addSubview:self.coverImage];
[self.view addSubview:self.titleLabel];
[self.view addSubview:self.authorTitleLabel];
[self.descriptionLabel sizeToFit];
[self.titleLabel sizeToFit];
[self.authorTitleLabel sizeToFit];
[self.subtitleLable sizeToFit];
[self.freeLabel sizeToFit];
[self.publishedLabel sizeToFit];

self.scrollView.contentSize = CGSizeMake(320, self.titleLabel.frame.size.height+ self.authorTitleLabel.frame.size.height + self.descriptionLabel.frame.size.height + 50);
}

-(void) upd
{
self.titleLabel.text = self.bookToShow.title;
}

- (void)didReceiveMemoryWarning
Expand Down
7 changes: 6 additions & 1 deletion TochkaK/LibraryManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@
#import "LibraryBookCreator.h"
@interface LibraryManager : NSObject <NSURLConnectionDelegate>
@property (strong, nonatomic) NSArray* books;
-(NSArray*) getBooks;
@property (weak, nonatomic) LibraryBook* requestedBook;

-(void) startGettingBooks;
-(void) startGettingDetailedDescriptionOfBookWithID:(NSInteger) ID;


-(LibraryManager*) initWithSourceURL:(NSURL*) sourceURL;

@end
Loading

0 comments on commit 1d14563

Please sign in to comment.