Advertisement
Guest User

Untitled

a guest
Jun 17th, 2016
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 43.62 KB | None | 0 0
  1. //
  2. // ServerManager.h
  3. // IronWorldProject
  4. //
  5. // Created by Oleg Petruk on 31.05.16.
  6. // Copyright © 2016 petruska. All rights reserved.
  7. //
  8.  
  9. #import <Foundation/Foundation.h>
  10. #import <VK-ios-sdk/VKUser.h>
  11. @class UIImage;
  12. @class LoginResponceServerModel;
  13. @class RegistrationResponceServerModel;
  14. @class FaceBookUserServerModel;
  15. @class UploadAvatarResponceServerModel;
  16. @class SinglePlaceServerModel;
  17. @class PlaceShortInfoServerModel;
  18.  
  19.  
  20. @interface ServerManager : NSObject
  21.  
  22. // codes of responce status
  23. enum {
  24. SUCCESS = 200,
  25. EMAIL_EXIST = -103,
  26. EMAIL_NOT_FOUND = -100,
  27. WRONG_EMAIL = -101,
  28. NOT_FOUND = -3,
  29. CANNOT_CREATE_USER = -2,
  30. WRONG_PASSWORD = -201,
  31. EMPTY_PASSWORD = -202,
  32. FILE_CANNOT_SAVE = -304,
  33. CANNOT_SAVE_DATE = -666,
  34. CANNOT_REMOVE_DATE = -777,
  35. EMPTY_VALUES = -888,
  36. CONNECTION_ERROR = -1009
  37. };
  38.  
  39. // Singletond
  40. + (ServerManager *) sharedManager;
  41.  
  42. #pragma mark -Image
  43.  
  44. - (void)loadSingleImageFromURL:(NSURL *)imageURL
  45. onSuccess:(void(^)(UIImage * image)) success
  46. onFailure:(void(^)(NSError* error)) failure;
  47.  
  48. #pragma mark -Auth
  49.  
  50. - (void)loginUserWithEmail:(NSString *)email
  51. password:(NSString *)password
  52. onSuccess:(void(^)(LoginResponceServerModel * loginResponceServerModel)) success
  53. onFailure:(void(^)(NSError* error)) failure;
  54.  
  55. - (void)registrateUserWithFirstName:(NSString *)firstName
  56. surnameName:(NSString *)surname
  57. email:(NSString *)email
  58. password:(NSString *)password
  59. gender:(NSInteger )genderType
  60. birthday:(NSString *)birthday
  61. onSuccess:(void(^)(RegistrationResponceServerModel * registrationResponceServerModel)) success
  62. onFailure:(void(^)(NSError* error)) failure;
  63.  
  64. - (void)registrateUserWithVK:(VKUser *) vkUser
  65. userID:(NSString *)userID
  66. onSuccess:(void(^)(RegistrationResponceServerModel * registrationResponceServerModel)) success
  67. onFailure:(void(^)(NSError* error)) failure;
  68.  
  69.  
  70. - (void)registrateUserWithFacebook:(FaceBookUserServerModel *) faceBookUserServerModel
  71. onSuccess:(void(^)(RegistrationResponceServerModel * registrationResponceServerModel)) success
  72. onFailure:(void(^)(NSError* error)) failure;
  73.  
  74. - (void)sendNewPasswordToEmail:(NSString *)email
  75. onSuccess:(void(^)(id resultJSON)) success
  76. onFailure:(void(^)(NSError* error)) failure;
  77.  
  78.  
  79. - (void)updateProfileAvatarWithImage:(UIImage *)avatar withToken:(NSString *) token
  80. onSuccess:(void(^)(UploadAvatarResponceServerModel * uploadAvatarResponceServerModel)) success
  81. onFailure:(void(^)(NSError* error)) failure;
  82.  
  83. #pragma -BOOKS
  84.  
  85. // for book we download JSON for navigation on book
  86. - (void)loadJSONFileForURL:(NSURL *)URL
  87. onSuccess:(void(^)(NSString * JSONStr)) success
  88. onFailure:(void(^)(NSError* error)) failure;
  89.  
  90. // ask urls for books, count of urls
  91. - (void)getBooksPagesWithBookID:(NSString *)bookID
  92. onSuccess:(void(^)(NSArray * imagesNameArray)) success
  93. onFailure:(void(^)(NSError* error)) failure;
  94.  
  95. // Download all images witch we get from getBooksPagesWithBookID request
  96. - (void)loadArrayOfImages:(NSArray *)arrayOfImages
  97. withProgress:(void(^)(UIImage * downloadedImage, NSInteger indexOfImage))progress
  98. onFailure:(void(^)(NSInteger indexOfImage))failure;
  99.  
  100. // for example, user downloaded 100 images and save them to cahse, but he lost internet connection, we must delete alredy donwloaded images
  101. - (void)deleteArrayOfURLFromCashe:(NSArray *)arrayOfURLs;
  102.  
  103.  
  104. #pragma mark -MAPS
  105.  
  106. - (void)getInfAboutSinglePlaceWithID:(NSString *)placeID
  107. onSuccess:(void(^)(SinglePlaceServerModel * singlePlaceServerModel)) success
  108. onFailure:(void(^)(NSError* error)) failure;
  109.  
  110.  
  111. - (void)getShortInfoAboutPlaceWithID:(NSString *)placeID
  112. userLatPos:(NSString *)latPos
  113. andUserLngPos:(NSString *)lngPos
  114. onSuccess:(void(^)(PlaceShortInfoServerModel * placeShortInfoServerModel)) success
  115. onFailure:(void(^)(NSError* error)) failure;
  116.  
  117.  
  118. // for map
  119. - (void)getMarkersForMapWithFiltersArray:(NSArray *)filtersArray
  120. radius:(NSString *)radius
  121. latPosition:(NSString *)lat
  122. lngPosition:(NSString *)lng
  123. onSuccess:(void(^)(NSArray * markersArray)) success
  124. onFailure:(void(^)(NSError* error)) failure;
  125.  
  126. // for collection
  127. - (void)getMarkersForCollectionWithFiltersArray:(NSArray *)filtersArray
  128. radius:(NSString *)radius
  129. latPosition:(NSString *)lat
  130. lngPosition:(NSString *)lng
  131. withCount:(NSInteger )count
  132. andOffset:(NSInteger )offset
  133. onSuccess:(void(^)(NSArray * placesArray)) success
  134. onFailure:(void(^)(NSError* error)) failure;
  135.  
  136. - (void)searchMarkersWithText:(NSString *)text
  137. latPosition:(NSString *)lat
  138. lngPosition:(NSString *)lng
  139. radius:(NSString *)radius
  140. withCount:(NSInteger )count
  141. andOffset:(NSInteger )offset
  142. onSuccess:(void(^)(NSArray * markersArray)) success
  143. onFailure:(void(^)(NSError* error)) failure;
  144.  
  145.  
  146. - (void)getMarkersWithIdArray:(NSArray *)idsArray
  147. withCount:(NSInteger )count
  148. andOffset:(NSInteger )offset
  149. onSuccess:(void(^)(NSArray * markersArray)) success
  150. onFailure:(void(^)(NSError* error)) failure;
  151.  
  152.  
  153.  
  154. @end
  155.  
  156.  
  157. //
  158. // ServerManager.m
  159. // IronWorldProject
  160. //
  161. // Created by Oleg Petruk on 31.05.16.
  162. // Copyright © 2016 petruska. All rights reserved.
  163. //
  164.  
  165. #import "ServerManager.h"
  166. #import <AFNetworking/AFNetworking.h>
  167. #import "LoginResponceServerModel.h"
  168. #import "RegistrationResponceServerModel.h"
  169. #import <SDWebImage/UIImageView+WebCache.h>
  170. #import <SDWebImage/SDImageCache.h>
  171. #import "FaceBookUserServerModel.h"
  172. #import "UploadAvatarResponceServerModel.h"
  173. #import "SinglePlaceServerModel.h"
  174. #import "MarkerServerModel.h"
  175. #import "PlaceShortInfoServerModel.h"
  176. #import "SearchPlaceServerModel.h"
  177. #import "PlaceListServerModel.h"
  178.  
  179. @interface ServerManager ()
  180.  
  181. @property (strong, nonatomic) AFHTTPSessionManager * requestOperationManager;
  182.  
  183. @end
  184.  
  185. @implementation ServerManager
  186.  
  187. - (id)init {
  188.  
  189. self = [super init];
  190. if (self) {
  191.  
  192.  
  193. NSURLSessionConfiguration * config = [NSURLSessionConfiguration defaultSessionConfiguration];
  194. config.HTTPAdditionalHeaders = @{@"Accept": @"application/json, text/javascript, ​*/*​; q=0.01",
  195. @"X-Requested-With": @"XMLHttpRequest"};
  196.  
  197. NSURL * url = [NSURL URLWithString:@"http://ironworld-pro.carswar.com/"];
  198. self.requestOperationManager = [[AFHTTPSessionManager alloc]initWithBaseURL:url sessionConfiguration:config];
  199.  
  200.  
  201. self.requestOperationManager.requestSerializer = [AFJSONRequestSerializer serializer];
  202. }
  203. return self;
  204. }
  205.  
  206. + (ServerManager *) sharedManager {
  207.  
  208. static ServerManager * manager = nil;
  209.  
  210. static dispatch_once_t onceToken;
  211. dispatch_once(&onceToken, ^{
  212.  
  213. manager = [[ServerManager alloc] init];
  214. });
  215.  
  216. return manager;
  217. }
  218.  
  219. #pragma mark -Image
  220.  
  221. - (void)loadSingleImageFromURL:(NSURL *)imageURL
  222. onSuccess:(void(^)(UIImage * image)) success
  223. onFailure:(void(^)(NSError* error)) failure {
  224.  
  225. SDWebImageManager *manager = [SDWebImageManager sharedManager];
  226. [manager downloadImageWithURL:imageURL
  227. options:0
  228. progress:nil
  229. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  230.  
  231. if (image) {
  232. success(image);
  233.  
  234. } else if (error) {
  235.  
  236. failure(error);
  237. }
  238. }];
  239. }
  240.  
  241. #pragma mark -Auth
  242.  
  243. - (void)loginUserWithEmail:(NSString *)email
  244. password:(NSString *)password
  245. onSuccess:(void(^)(LoginResponceServerModel * loginResponceServerModel)) success
  246. onFailure:(void(^)(NSError* error)) failure {
  247.  
  248. NSDictionary * params = @{@"email" : email,
  249. @"password" : password};
  250.  
  251. NSLog(@"LOGIN PARAMS: %@",params);
  252.  
  253. [self.requestOperationManager POST:@"Auth/login" parameters:params constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  254.  
  255. NSLog(@"LOGIN RESPONCE: %@",responseObject);
  256.  
  257. if (success) {
  258.  
  259. LoginResponceServerModel * loginResponceServerModel = [[LoginResponceServerModel alloc]initWithServerResponce:responseObject];
  260. success(loginResponceServerModel);
  261. }
  262.  
  263. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  264.  
  265. NSLog(@"LOGIN ERROR: %@",error);
  266.  
  267. if (failure)
  268. failure(error);
  269. }];
  270. }
  271.  
  272. - (void)registrateUserWithFirstName:(NSString *)firstName
  273. surnameName:(NSString *)surname
  274. email:(NSString *)email
  275. password:(NSString *)password
  276. gender:(NSInteger )genderType
  277. birthday:(NSString *)birthday
  278. onSuccess:(void(^)(RegistrationResponceServerModel * registrationResponceServerModel)) success
  279. onFailure:(void(^)(NSError* error)) failure {
  280.  
  281. NSDictionary * params = @{@"first_name" : firstName,
  282. @"last_name" : surname,
  283. @"email" : email,
  284. @"password" : password,
  285. @"gender" : [NSString stringWithFormat:@"%ld",(long)genderType],
  286. @"birthday" : birthday};
  287.  
  288.  
  289. NSLog(@"REGISTRATION PARAMS: %@",params);
  290.  
  291. [self.requestOperationManager POST:@"Auth/register" parameters:params constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  292.  
  293. NSLog(@"REGISTRATION RESPONCE: %@",responseObject);
  294. if (success) {
  295.  
  296. RegistrationResponceServerModel * registrationResponceServerModel = [[RegistrationResponceServerModel alloc]initWithServerResponce:responseObject];
  297. success(registrationResponceServerModel);
  298. }
  299.  
  300. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  301.  
  302. NSLog(@"REGISTRATION ERROR: %@",error);
  303.  
  304. if (failure)
  305. failure(error);
  306. }];
  307.  
  308. }
  309.  
  310.  
  311. - (void)registrateUserWithVK:(VKUser *) vkUser
  312. userID:(NSString *)userID
  313. onSuccess:(void(^)(RegistrationResponceServerModel * registrationResponceServerModel)) success
  314. onFailure:(void(^)(NSError* error)) failure {
  315.  
  316. NSDictionary * params = @{@"first_name" : vkUser.first_name,
  317. @"last_name" : vkUser.last_name,
  318. @"email" : @"NO_EMAIL_VK",
  319. @"gender" : vkUser.sex,
  320. @"birthday" : vkUser.bdate,
  321. @"network" : @"vkontakte",
  322. @"identity" : userID,
  323. @"profile" : userID,
  324. @"avatar_url" : vkUser.photo_100};
  325.  
  326. NSLog(@"SOCIAL REGISTRATION PARAMS: %@",params);
  327.  
  328. [self.requestOperationManager POST:@"Auth/uloginAuthAjax"
  329. parameters:params
  330. constructingBodyWithBlock:nil
  331. progress:nil
  332. success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  333.  
  334. NSLog(@"SOCIAL REGISTRATION RESPONCE: %@",responseObject);
  335.  
  336. if (success) {
  337.  
  338. RegistrationResponceServerModel * registrationResponceServerModel = [[RegistrationResponceServerModel alloc]initWithServerResponce:responseObject];
  339. success(registrationResponceServerModel);
  340. }
  341.  
  342. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  343.  
  344. NSLog(@"SOCIAL REGISTRATION ERROR: %@",error);
  345.  
  346. if (failure)
  347. failure(error);
  348. }];
  349. }
  350.  
  351. - (void)registrateUserWithFacebook:(FaceBookUserServerModel *) faceBookUserServerModel
  352. onSuccess:(void(^)(RegistrationResponceServerModel * registrationResponceServerModel)) success
  353. onFailure:(void(^)(NSError* error)) failure {
  354.  
  355. NSDictionary * params = @{@"first_name" : faceBookUserServerModel.facebookUSerName,
  356. @"last_name" : faceBookUserServerModel.facebookUserSurename,
  357. @"email" : faceBookUserServerModel.facebookUserEmail,
  358. @"gender" : faceBookUserServerModel.facebookUserGender,
  359. @"birthday" : faceBookUserServerModel.facebookUSerBirthday,
  360. @"network" : @"facebook",
  361. @"identity" : faceBookUserServerModel.facebookUserID,
  362. @"profile" : faceBookUserServerModel.facebookUserID,
  363. @"avatar_url" : faceBookUserServerModel.facebookUserUserAvatar};
  364.  
  365. NSLog(@"SOCIAL REGISTRATION PARAMS: %@",params);
  366.  
  367. [self.requestOperationManager POST:@"Auth/uloginAuthAjax"
  368. parameters:params
  369. constructingBodyWithBlock:nil
  370. progress:nil
  371. success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  372.  
  373. NSLog(@"SOCIAL REGISTRATION RESPONCE: %@",responseObject);
  374.  
  375. if (success) {
  376.  
  377. RegistrationResponceServerModel * registrationResponceServerModel = [[RegistrationResponceServerModel alloc]initWithServerResponce:responseObject];
  378. success(registrationResponceServerModel);
  379. }
  380.  
  381. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  382.  
  383. NSLog(@"SOCIAL REGISTRATION ERROR: %@",error);
  384.  
  385. if (failure)
  386. failure(error);
  387. }];
  388. }
  389.  
  390. - (void)sendNewPasswordToEmail:(NSString *)email
  391. onSuccess:(void(^)(id resultJSON)) success
  392. onFailure:(void(^)(NSError* error)) failure {
  393.  
  394. NSDictionary * params = @{@"email" : email};
  395.  
  396. NSLog(@"sendNewPasswordToEmail PARAMS: %@",params);
  397.  
  398. [self.requestOperationManager POST:@"Auth/forgotPassword" parameters:params constructingBodyWithBlock:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  399.  
  400. NSLog(@"sendNewPasswordToEmail RESPONCE: %@",responseObject);
  401.  
  402. if (responseObject)
  403. success(responseObject);
  404.  
  405. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  406.  
  407.  
  408. NSLog(@"sendNewPasswordToEmail ERROR %@",error);
  409.  
  410. if (failure)
  411. failure(error);
  412. }];
  413. }
  414.  
  415.  
  416. - (void)updateProfileAvatarWithImage:(UIImage *)avatar withToken:(NSString *) token
  417. onSuccess:(void(^)(UploadAvatarResponceServerModel * uploadAvatarResponceServerModel)) success
  418. onFailure:(void(^)(NSError* error)) failure {
  419.  
  420. NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];
  421. config.HTTPAdditionalHeaders = @{@"Content-Transfer-Encoding": @"binary\r\n"};
  422.  
  423.  
  424. NSURL* url = [NSURL URLWithString:@"http://ironworld-pro.carswar.com/"];
  425. AFHTTPSessionManager * requestOperationManager = [[AFHTTPSessionManager alloc]initWithBaseURL:url sessionConfiguration:config];
  426. requestOperationManager.requestSerializer = [AFJSONRequestSerializer serializer];
  427.  
  428.  
  429.  
  430. NSDictionary * params = @{@"token" : token,
  431. @"avatar_url" : avatar };
  432.  
  433. NSData * imageData = UIImageJPEGRepresentation(avatar, 0.5);
  434.  
  435. [requestOperationManager POST:@"portfolio/saveData" parameters:params constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) {
  436.  
  437. [formData appendPartWithFileData:imageData
  438. name:@"avatar_url"
  439. fileName:@"testAvatar.jpg"
  440. mimeType:@"image/jpeg"];
  441.  
  442. } progress:^(NSProgress * _Nonnull uploadProgress) {
  443.  
  444. NSLog(@"\n\n\n%lld : %lld",uploadProgress.totalUnitCount,uploadProgress.completedUnitCount);
  445.  
  446.  
  447. } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  448.  
  449. NSLog(@"updateProfileAvatarWithImage RESPONCE: %@",responseObject);
  450.  
  451.  
  452. if (responseObject) {
  453.  
  454. NSString * statusStr = responseObject[@"status"];
  455.  
  456. UploadAvatarResponceServerModel * uploadAvatarResponceServerModel = [UploadAvatarResponceServerModel new];
  457. uploadAvatarResponceServerModel.requestStatus = statusStr.integerValue;
  458. uploadAvatarResponceServerModel.avatarURLOnServer = responseObject[@"file_150_150"];
  459. success(uploadAvatarResponceServerModel);
  460.  
  461. } else {
  462.  
  463. failure(nil);
  464. }
  465.  
  466.  
  467. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  468.  
  469. NSLog(@"updateProfileAvatarWithImage ERROR: %@",error);
  470.  
  471. if (failure)
  472. failure(error);
  473. }];
  474. }
  475.  
  476. #pragma mark -BOOKS
  477.  
  478. - (void)loadArrayOfImages:(NSArray *)arrayOfImages
  479. withProgress:(void(^)(UIImage * downloadedImage, NSInteger indexOfImage))progress
  480. onFailure:(void(^)(NSInteger indexOfImage))failure {
  481.  
  482. for (NSInteger i = 0; i < arrayOfImages.count; i++) {
  483.  
  484. SDWebImageManager *manager = [SDWebImageManager sharedManager];
  485. [manager downloadImageWithURL:arrayOfImages[i]
  486. options:0
  487. progress:nil
  488. completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, BOOL finished, NSURL *imageURL) {
  489.  
  490. if (image) {
  491.  
  492. progress(image,i);
  493.  
  494. } else {
  495.  
  496. failure(i);
  497. }
  498. }];
  499. }
  500. }
  501.  
  502. - (void)deleteArrayOfURLFromCashe:(NSArray *)arrayOfURLs {
  503.  
  504. for (NSInteger i = 0; i < arrayOfURLs.count; i++) {
  505.  
  506. [[SDImageCache sharedImageCache]removeImageForKey:arrayOfURLs[i] fromDisk:YES];
  507. }
  508. }
  509.  
  510. -(void)getBooksPagesWithBookID:(NSString *)bookID
  511. onSuccess:(void(^)(NSArray * imagesNameArray)) success
  512. onFailure:(void(^)(NSError* error)) failure {
  513.  
  514.  
  515. NSDictionary * params = @{@"book_id" : bookID};
  516.  
  517. [self.requestOperationManager POST:@"books/info"
  518. parameters:params
  519. constructingBodyWithBlock:nil
  520. progress:nil
  521. success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  522.  
  523. NSLog(@"getBooksPagesWithBookID RESPONCE: %@",responseObject);
  524.  
  525. NSDictionary * responceDict = responseObject[@"data"];
  526.  
  527. NSMutableArray * pagesUrlArray = [NSMutableArray new];
  528. NSString * directory = responceDict[@"directory"];
  529. NSString * format = responceDict[@"format"];
  530. NSString * pagesCount = responceDict[@"pages"];
  531.  
  532. for (NSInteger i = 1; i < [pagesCount intValue]; i++) {
  533.  
  534. NSString * finatlPageURL = [NSString stringWithFormat:@"%@page%ldoutput.%@",directory,(long)i,format];
  535. [pagesUrlArray addObject:finatlPageURL];
  536. }
  537.  
  538. if (success)
  539. success(pagesUrlArray);
  540.  
  541. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  542.  
  543.  
  544. NSLog(@"getMarkersForMapWithFiltersArray ERROR %@",error);
  545.  
  546. if (failure)
  547. failure(error);
  548. }];
  549. }
  550.  
  551. - (void)loadJSONFileForURL:(NSURL *)URL
  552. onSuccess:(void(^)(NSString * JSONStr)) success
  553. onFailure:(void(^)(NSError* error)) failure {
  554.  
  555. __block NSJSONSerialization * JSON;
  556. NSURLSession *session = [NSURLSession sharedSession];
  557. [[session dataTaskWithURL:URL
  558. completionHandler:^(NSData *data,
  559. NSURLResponse *response,
  560. NSError *error) {
  561.  
  562. if (error) {
  563.  
  564. failure(error);
  565.  
  566. } else {
  567.  
  568. JSON = [NSJSONSerialization JSONObjectWithData:data
  569. options:0
  570. error:nil];
  571.  
  572. if (JSON) {
  573.  
  574. NSData * JSONDate = [NSJSONSerialization dataWithJSONObject:JSON options:0 error:nil];
  575. NSString * JSONStr = [[NSString alloc] initWithData:JSONDate encoding:NSUTF8StringEncoding];
  576. success(JSONStr);
  577. } else {
  578.  
  579. failure(nil);
  580. }
  581. }
  582. }] resume];
  583. }
  584.  
  585. #pragma mark -MAP
  586.  
  587. - (void)getInfAboutSinglePlaceWithID:(NSString *)placeID
  588. onSuccess:(void(^)(SinglePlaceServerModel * singlePlaceServerModel)) success
  589. onFailure:(void(^)(NSError* error)) failure {
  590.  
  591. NSDictionary * params = @{@"id" : placeID,
  592. @"version" : @"1.0",
  593. @"is_info" : @"1"
  594. };
  595.  
  596. NSLog(@"getInfAboutSinglePlaceWithID PARAMS: %@",params);
  597.  
  598. [self.requestOperationManager POST:@"map/getInfo"
  599. parameters:params
  600. constructingBodyWithBlock:nil
  601. progress:nil
  602. success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  603.  
  604. NSLog(@"getInfAboutSinglePlaceWithID RESPONCE: %@",responseObject);
  605.  
  606. NSArray * array = responseObject[@"data"];
  607. SinglePlaceServerModel * singlePlaceServerModel = [[SinglePlaceServerModel alloc]initWithServerResponse:array];
  608.  
  609. if (success)
  610. success(singlePlaceServerModel);
  611.  
  612. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  613.  
  614. if (failure)
  615. failure(error);
  616.  
  617. NSLog(@"getInfAboutSinglePlaceWithID ERROR %@",error);
  618.  
  619. }];
  620. }
  621.  
  622. - (void)getShortInfoAboutPlaceWithID:(NSString *)placeID
  623. userLatPos:(NSString *)latPos
  624. andUserLngPos:(NSString *)lngPos
  625. onSuccess:(void(^)(PlaceShortInfoServerModel * placeShortInfoServerModel)) success
  626. onFailure:(void(^)(NSError* error)) failure {
  627.  
  628.  
  629. NSDictionary * params = @{@"id" : placeID,
  630. @"version" : @"1.0",
  631. @"is_info" : @"2",
  632. @"user_lat" : latPos,
  633. @"user_lng" : lngPos
  634. };
  635.  
  636. NSLog(@"getInfAboutSinglePlaceWithID PARAMS: %@",params);
  637. [self.requestOperationManager POST:@"map/getInfo"
  638. parameters:params
  639. constructingBodyWithBlock:nil
  640. progress:nil
  641. success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  642.  
  643. NSLog(@"getShortInfoAboutPlaceWithID RESPONCE: %@",responseObject);
  644.  
  645. NSArray * arrayResponce = responseObject[@"data"];
  646. NSDictionary * responseDict = arrayResponce[0]; // nice JSON
  647. PlaceShortInfoServerModel * singlePlaceServerModel = [[PlaceShortInfoServerModel alloc]initWithServerResponce:responseDict];
  648.  
  649. if (success)
  650. success(singlePlaceServerModel);
  651.  
  652. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  653.  
  654. if (failure)
  655. failure(error);
  656.  
  657. NSLog(@"getShortInfoAboutPlaceWithID ERROR %@",error);
  658. }];
  659.  
  660. }
  661.  
  662. - (void)getMarkersForMapWithFiltersArray:(NSArray *)filtersArray
  663. radius:(NSString *)radius
  664. latPosition:(NSString *)lat
  665. lngPosition:(NSString *)lng
  666. onSuccess:(void(^)(NSArray * markersArray)) success
  667. onFailure:(void(^)(NSError* error)) failure {
  668.  
  669. // i can't send normal array to server, so we did this...
  670. NSString * typeIDStr = @"";
  671. for (NSInteger i = 0; i < filtersArray.count; i++)
  672. typeIDStr = [typeIDStr stringByAppendingString:[NSString stringWithFormat:@"%@#",filtersArray[i]]];
  673.  
  674. if ([typeIDStr length] > 0)
  675. typeIDStr = [typeIDStr substringToIndex:[typeIDStr length] - 1];
  676.  
  677.  
  678. NSDictionary * params = @{@"type_id" : typeIDStr,
  679. @"distance" : radius,
  680. @"center_lat" : lat,
  681. @"center_lng" : lng,
  682. @"offset" : @"0",
  683. @"limit" : @"0",
  684. @"is_info" : @"0",
  685. @"version" : @"1.0"
  686. };
  687.  
  688.  
  689. NSLog(@"getMarkersForMapWithFiltersArray PARAMS: %@",params);
  690.  
  691.  
  692. [self.requestOperationManager POST:@"map/show"
  693. parameters:params
  694. constructingBodyWithBlock:nil
  695. progress:nil
  696. success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  697.  
  698. NSLog(@"getMarkersForMapWithFiltersArray RESPONCE: %@",responseObject);
  699.  
  700. NSArray * markersResponceArray = responseObject[@"data"];
  701. NSMutableArray * markersModelArray = [NSMutableArray new];
  702. for (NSInteger i = 0; i < markersResponceArray.count; i++) {
  703.  
  704. MarkerServerModel * markerServerModel = [[MarkerServerModel alloc]initWithServerResponse:markersResponceArray[i]];
  705. [markersModelArray addObject:markerServerModel];
  706. }
  707.  
  708. if (success)
  709. success(markersModelArray);
  710.  
  711. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  712.  
  713. if (failure)
  714. failure(error);
  715.  
  716. NSLog(@"getMarkersForMapWithFiltersArray ERROR %@",error);
  717. }];
  718. }
  719.  
  720. // for collection
  721. - (void)getMarkersForCollectionWithFiltersArray:(NSArray *)filtersArray
  722. radius:(NSString *)radius
  723. latPosition:(NSString *)lat
  724. lngPosition:(NSString *)lng
  725. withCount:(NSInteger )count
  726. andOffset:(NSInteger )offset
  727. onSuccess:(void(^)(NSArray * placesArray)) success
  728. onFailure:(void(^)(NSError* error)) failure {
  729.  
  730. // i can't send normal array to server, so we did this...
  731. NSString * typeIDStr = @"";
  732. for (NSInteger i = 0; i < filtersArray.count; i++)
  733. typeIDStr = [typeIDStr stringByAppendingString:[NSString stringWithFormat:@"%@#",filtersArray[i]]];
  734.  
  735. if ([typeIDStr length] > 0)
  736. typeIDStr = [typeIDStr substringToIndex:[typeIDStr length] - 1];
  737.  
  738. NSDictionary * params = @{@"type_id" : typeIDStr,
  739. @"distance" : radius,
  740. @"center_lat" : lat,
  741. @"center_lng" : lng,
  742. @"limit" : [NSString stringWithFormat:@"%ld",(long)count],
  743. @"offset" : [NSString stringWithFormat:@"%ld",(long)offset],
  744. @"is_info" : @"1",
  745. @"version" : @"1.0"};
  746.  
  747. NSLog(@"getPlacesFromServerWithFiltersArray PARAMS: %@",params);
  748.  
  749. [self.requestOperationManager POST:@"map/show"
  750. parameters:params
  751. constructingBodyWithBlock:nil
  752. progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  753.  
  754. NSLog(@"getPlacesFromServerWithFiltersArray RESPONCE: %@",responseObject);
  755.  
  756.  
  757. NSArray * placesResponceArray = responseObject[@"data"];
  758. NSMutableArray * placeModelArray = [NSMutableArray new];
  759. for (NSInteger i = 0; i < placesResponceArray.count; i++) {
  760.  
  761. PlaceListServerModel * placeListServerModel = [[PlaceListServerModel alloc]initWithServerResponce:placesResponceArray[i]];
  762. [placeModelArray addObject:placeListServerModel];
  763. }
  764.  
  765. NSMutableArray * fakeArray = [NSMutableArray new];
  766. for (NSInteger i = 0; i < placeModelArray.count; i++) {
  767.  
  768. PlaceListServerModel * placeListServerModel = placeModelArray[i];
  769. if (i < 10)
  770. placeListServerModel.placeVIPStatus = YES;
  771. else
  772. placeListServerModel.placeVIPStatus = NO;
  773. [fakeArray addObject:placeListServerModel];
  774. }
  775.  
  776. success(fakeArray);
  777.  
  778.  
  779. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  780.  
  781. NSLog(@"getPlacesFromServerWithFiltersArray ERROR: %@",error);
  782.  
  783. if (failure)
  784. failure(error);
  785. }];
  786. }
  787.  
  788. - (void)searchMarkersWithText:(NSString *)text
  789. latPosition:(NSString *)lat
  790. lngPosition:(NSString *)lng
  791. radius:(NSString *)radius
  792. withCount:(NSInteger )count
  793. andOffset:(NSInteger )offset
  794. onSuccess:(void(^)(NSArray * markersArray)) success
  795. onFailure:(void(^)(NSError* error)) failure {
  796.  
  797. NSDictionary * params = @{@"center_lat" : lat,
  798. @"center_lng" : lng,
  799. @"distance" : radius,
  800. @"limit" : [NSString stringWithFormat:@"%ld",(long)count],
  801. @"search" : text,
  802. @"version" : @"1.0"
  803. };
  804.  
  805. NSLog(@"PARA( %@",params);
  806.  
  807. [self.requestOperationManager POST:@"map/search"
  808. parameters:params
  809. constructingBodyWithBlock:nil
  810. progress:nil
  811. success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  812.  
  813. NSLog(@"searchOnServerWithLatPosition RESPONCE: %@",responseObject);
  814.  
  815. NSArray * placesResponce = responseObject[@"data"];
  816. NSMutableArray * placesModelArray = [NSMutableArray new];
  817. for (NSInteger i = 0; i < placesResponce.count; i++) {
  818.  
  819. SearchPlaceServerModel * searchPlaceServerModel = [[SearchPlaceServerModel alloc]initWithServerResponce:placesResponce[i]];
  820. [placesModelArray addObject:searchPlaceServerModel];
  821. }
  822.  
  823. success(placesModelArray);
  824.  
  825.  
  826. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  827.  
  828. NSLog(@"searchOnServerWithLatPosition ERROR: %@",error);
  829.  
  830. if (failure)
  831. failure(error);
  832. }];
  833. }
  834.  
  835. - (void)getMarkersWithIdArray:(NSArray *)idsArray
  836. withCount:(NSInteger )count
  837. andOffset:(NSInteger )offset
  838. onSuccess:(void(^)(NSArray * markersArray)) success
  839. onFailure:(void(^)(NSError* error)) failure {
  840.  
  841. NSArray * placesIDArrya = [[NSUserDefaults standardUserDefaults]objectForKey:@"favoritesPlacesArray"];
  842.  
  843. NSString * typeIDStr = @"";
  844.  
  845. for (NSInteger i = 0; i < placesIDArrya.count; i++) {
  846. typeIDStr = [typeIDStr stringByAppendingString:[NSString stringWithFormat:@"%@#",placesIDArrya[i]]];
  847. }
  848.  
  849. if ([typeIDStr length] > 0) {
  850. typeIDStr = [typeIDStr substringToIndex:[typeIDStr length] - 1];
  851. }
  852.  
  853.  
  854.  
  855. NSDictionary * params = @{@"id" : typeIDStr,
  856. @"version" : @"1.0",
  857. @"is_info" : @"0"
  858. };
  859.  
  860. NSLog(@"getFavoritesPlacesFromServerWithCount PARAMS: %@",params);
  861.  
  862. [self.requestOperationManager POST:@"map/getInfo"
  863. parameters:params
  864. constructingBodyWithBlock:nil
  865. progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
  866.  
  867. NSLog(@"getFavoritesPlacesFromServerWithCount RESPONCE: %@",responseObject);
  868.  
  869. NSArray * placesResponceArray = responseObject[@"data"];
  870. NSMutableArray * placesArray = [NSMutableArray new];
  871. for (NSInteger i = 0; i < placesResponceArray.count; i++) {
  872.  
  873. MarkerServerModel * placeModel = [[MarkerServerModel alloc]initWithServerResponse:placesResponceArray[i]];
  874. [placesArray addObject:placeModel];
  875. }
  876.  
  877.  
  878.  
  879.  
  880. if (success)
  881. success(placesArray);
  882.  
  883. } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
  884.  
  885. NSLog(@"getPlacesFromServerWithFiltersArray ERROR: %@",error);
  886.  
  887. if (failure)
  888. failure(error);
  889. }];
  890. }
  891.  
  892. @end
  893.  
  894.  
  895.  
  896.  
  897. //
  898. // NewsServerManager.h
  899. // IronWorldProject
  900. //
  901. // Created by Oleg Petruk on 26.05.16.
  902. // Copyright © 2016 petruska. All rights reserved.
  903. //
  904.  
  905. #import <Foundation/Foundation.h>
  906.  
  907. @interface NewsServerManager : NSObject
  908.  
  909. + (NewsServerManager *) sharedManager;
  910.  
  911. // get all tags, we save them and now we know what user want see
  912. - (void)getArrayOfNewsTagsOnSuccess:(void(^)(NSArray * tagsArray)) success
  913. onFailure:(void(^)(NSError* error)) failure;
  914.  
  915. // table of news
  916. - (void)getArrayOfPreviewNewsWithTagsIDArray:(NSArray *)tagsIDArray
  917. fromPage:(NSInteger)page
  918. onSuccess:(void(^)(NSArray * previewNewsArray)) success
  919. onFailure:(void(^)(NSError* error)) failure;
  920.  
  921. @end
  922.  
  923.  
  924. //
  925. // NewsServerManager.m
  926. // IronWorldProject
  927. //
  928. // Created by Oleg Petruk on 26.05.16.
  929. // Copyright © 2016 petruska. All rights reserved.
  930. //
  931.  
  932. #import "NewsServerManager.h"
  933. #import <AFNetworking/AFNetworking.h>
  934. #import "TagServerModel.h"
  935. #import "PreviewNewsServerModel.h"
  936.  
  937. @interface NewsServerManager ()
  938.  
  939. @end
  940.  
  941. @implementation NewsServerManager
  942.  
  943. + (NewsServerManager *) sharedManager {
  944.  
  945. static NewsServerManager * manager = nil;
  946.  
  947. static dispatch_once_t onceToken;
  948. dispatch_once(&onceToken, ^{
  949.  
  950. manager = [[NewsServerManager alloc] init];
  951. });
  952.  
  953. return manager;
  954. }
  955.  
  956. - (void)getArrayOfNewsTagsOnSuccess:(void(^)(NSArray * tagsArray)) success
  957. onFailure:(void(^)(NSError* error)) failure {
  958.  
  959. NSURLSessionConfiguration * configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  960. AFURLSessionManager * manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  961.  
  962.  
  963. NSURL *URL = [NSURL URLWithString:@"http://ironworld.ru/news/json/tags.php"];
  964. NSURLRequest *request = [NSURLRequest requestWithURL:URL];
  965. NSURLSessionDataTask * dataTask = [manager dataTaskWithRequest:request
  966. completionHandler:^(NSURLResponse *response,
  967. id responseObject, NSError *error) {
  968.  
  969. if (responseObject) {
  970.  
  971. NSLog(@"getArrayOfNewsTags RESPONCE: %@",responseObject);
  972.  
  973. NSArray * tagsResponceArray = responseObject[@"data"][@"tags"];
  974.  
  975.  
  976. NSMutableArray * tagsModelsArray = [NSMutableArray new];
  977. for (NSInteger i = 0; i < tagsResponceArray.count; i++) {
  978.  
  979. NSDictionary * singleTag = tagsResponceArray[i];
  980. TagServerModel * tagServerModel = [[TagServerModel alloc]initWithServerResponce:singleTag];
  981. [tagsModelsArray addObject:tagServerModel];
  982. }
  983.  
  984. success(tagsModelsArray);
  985. }
  986.  
  987. if (error) {
  988.  
  989. NSLog(@"getArrayOfNewsTags ERROR: %@",error);
  990. failure(error);
  991. }
  992. }];
  993.  
  994. [dataTask resume];
  995. }
  996.  
  997. - (void)getArrayOfPreviewNewsWithTagsIDArray:(NSArray *)tagsIDArray
  998. fromPage:(NSInteger)page
  999. onSuccess:(void(^)(NSArray * previewNewsArray)) success
  1000. onFailure:(void(^)(NSError* error)) failure {
  1001.  
  1002. NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
  1003. AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
  1004.  
  1005.  
  1006. NSString * baseURL = @"http://ironworld.ru/news/json/?";
  1007. NSString * tagsTemp = [self createStringFromArray:tagsIDArray];
  1008. NSString * tagsParam = [NSString stringWithFormat:@"tags={%@}",tagsTemp];
  1009. NSString * pageParam = [NSString stringWithFormat:@"&page=%ld",(long)page];
  1010. NSString * finalURL = [NSString stringWithFormat:@"%@%@%@",baseURL,tagsParam,pageParam];
  1011.  
  1012. NSLog(@"finalURL: %@",finalURL);
  1013.  
  1014. // http://stackoverflow.com/a/32802581 <3
  1015.  
  1016. finalURL = [finalURL stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
  1017.  
  1018. NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:finalURL]];
  1019. NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) {
  1020.  
  1021. if (responseObject) {
  1022.  
  1023. NSLog(@"getArrayOfPreviewNewsWithTagsIDArray RESPONCE: %@",responseObject);
  1024.  
  1025.  
  1026. NSArray * dataResponceArray = responseObject[@"data"];
  1027.  
  1028. NSMutableArray * previewNewsModelArray = [NSMutableArray new];
  1029.  
  1030. for (NSInteger i = 0; i < dataResponceArray.count; i++) {
  1031.  
  1032. NSDictionary * singePreviwNewResponce = dataResponceArray[i];
  1033. PreviewNewsServerModel * previewNewsServerModel = [[PreviewNewsServerModel alloc]initWithServerResponce:singePreviwNewResponce];
  1034. [previewNewsModelArray addObject:previewNewsServerModel];
  1035. }
  1036.  
  1037. success(previewNewsModelArray);
  1038.  
  1039. }
  1040.  
  1041. if (error) {
  1042.  
  1043. NSLog(@"getArrayOfPreviewNewsWithTagsIDArray ERROR: %@",error);
  1044. failure(error);
  1045. }
  1046. }];
  1047. [dataTask resume];
  1048.  
  1049.  
  1050. }
  1051.  
  1052. - (NSString *)createStringFromArray:(NSArray *)array {
  1053.  
  1054. // \"1\":8,\"2\":11
  1055.  
  1056. NSString * finalString = @"";
  1057. for (NSInteger i = 0; i < array.count; i++) {
  1058.  
  1059. NSString * singlePartOfString = array[i];
  1060. NSString * addString = [NSString stringWithFormat:@"\"%d\":%@,",i + 1,singlePartOfString];
  1061. finalString = [finalString stringByAppendingString:addString];
  1062. }
  1063.  
  1064. if ([finalString length] > 0)
  1065. finalString = [finalString substringToIndex:finalString.length - 1];
  1066.  
  1067. return finalString;
  1068. }
  1069.  
  1070. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement