-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCarDetailsViewController.m
More file actions
169 lines (122 loc) · 6.03 KB
/
Copy pathCarDetailsViewController.m
File metadata and controls
169 lines (122 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//
// CarDetailsViewController.m
// Cars90
//
// Created by Marcin Misiorek on 13.12.2015.
// Copyright © 2015 Marcin Misiorek. All rights reserved.
//
#import "CarDetailsViewController.h"
#import "AppDelegate.h"
#import "CarEditViewController.h"
@interface CarDetailsViewController ()
@property (strong, nonatomic) UIImage *carPhotoPortrait;
@property (strong, nonatomic) UIImage *carPhotoLandscape;
@end
@implementation CarDetailsViewController
- (id)initWithAPIManager:(APIManager *)apiManager andCarModel:(CarModel *)carModel andCarFlowController:(CarFlowController *)carFlowController {
self = [super init];
if(self) {
self->_apiManager = apiManager;
self->_carModel = carModel;
self->_carFlowController = carFlowController;
NSLog(@"model %@", carModel);
}
return self;
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:nil];
}
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
[self updateScrollView];
NSString *brand = self.carModel.brand;
NSString *model = self.carModel.model;
[self.modelLabel setText:model];
[self.brandLabel setText:brand];
[self.manufacturedDateLabel setText: [[self dateFormatter] stringFromDate:self.carModel.manufacturedDate]];
[self.registrationNumberLabel setText:self.carModel.registrationNumber];
[self.isAvailableLabel setText:(self.carModel.isAvailable ? @"Yes" : @"No")];
self.carPhoto.contentMode = UIViewContentModeScaleAspectFit;
self.navigationItem.title = [NSString stringWithFormat:@"Details of %@ %@", brand, model];
if(self.carModel.photo) {
CGRect screenRect = [[UIScreen mainScreen] bounds];
float imgWidth = MAX(screenRect.size.width, screenRect.size.height);
[self.apiManager fetchImageForCar:self.carModel andWidth:imgWidth andHeight:imgWidth withSuccess:^(UIImage *image) {
[self prepareCarPhotoImages:image];
[self updateAccordingToOrientationImageView:self.carPhoto withPortraitImage:self.carPhotoPortrait AndLandscapeImage:self.carPhotoLandscape];
} andFailure:^(NSError *error) {
NSLog(@"We have an error");
}];
}
UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStylePlain target:self action:@selector(onClickCarEdit:)];
[self.navigationItem setRightBarButtonItem:barButtonItem];
}
- (void) updateScrollView {
CGRect mainScreenRect = [[UIScreen mainScreen] bounds];
[self.scrollView addSubview:self.contentView];
[self.scrollView setContentSize:CGSizeMake(mainScreenRect.size.width, self.contentView.bounds.size.height+200)];
CGRect newContentFrame = CGRectMake(0, 0, mainScreenRect.size.width, self.contentView.bounds.size.height+200);
[self.contentView setFrame:newContentFrame];
NSLog(@"The size is height: %f and width: %f", mainScreenRect.size.height, mainScreenRect.size.width);
}
- (void) prepareCarPhotoImages:(UIImage*)biggerImg {
self.carPhotoLandscape = biggerImg;
AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
CGRect screenRect = [[UIScreen mainScreen] bounds];
float scale = MIN(screenRect.size.width, screenRect.size.height)/MAX(screenRect.size.width, screenRect.size.height);
self.carPhotoPortrait = [appDelegate.imageManager createImageBaseOnImage:biggerImg andScale:scale];
}
- (void) updateImageView:(UIImageView*)view withImage:(UIImage*)image {
CGRect rect = CGRectMake(0, 0, image.size.width, image.size.height);
[view setFrame:rect];
[view setImage:image];
}
- (void) updateAccordingToOrientationImageView:(UIImageView*)view withPortraitImage:(UIImage*)portraitImg AndLandscapeImage:(UIImage*)landscapeImg {
CGRect screenRect = [[UIScreen mainScreen] bounds];
if(screenRect.size.width > screenRect.size.height) {
[self updateImageView:view withImage:landscapeImg];
} else {
[self updateImageView:view withImage:portraitImg];
}
}
- (void) updateImageScale: (CGRect)oldScreenRect {
CGRect screenRect = [[UIScreen mainScreen] bounds];
CGRect imageRect = self.carPhoto.bounds;
if(imageRect.size.width>imageRect.size.height) {
float scale = screenRect.size.width/oldScreenRect.size.width;
UIImage *oldImage, *newImage;
oldImage = self.carPhoto.image;
AppDelegate *appDelegate = (AppDelegate*) [[UIApplication sharedApplication] delegate];
newImage = [appDelegate.imageManager createImageBaseOnImage:oldImage andScale:scale];
CGRect newImageRect = CGRectMake(0, 0, newImage.size.width, newImage.size.height);
[self.carPhoto setFrame:newImageRect];
[self.carPhoto setImage:newImage];
}
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (NSDateFormatter*) dateFormatter {
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
return formatter;
}
- (void)orientationChanged:(NSNotification*)notification {
[self updateScrollView];
[self updateAccordingToOrientationImageView:self.carPhoto withPortraitImage:self.carPhotoPortrait AndLandscapeImage:self.carPhotoLandscape];
}
- (void)onClickCarEdit: (id) sender {
[self.carFlowController moveToEditWithNavigatorController:self.navigationController andModel:self.carModel];
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end