Advertisement
axelso

17 Oct 17, @14.49 => ProfileViewController show Data fromAPI

Oct 17th, 2017
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  ProfileViewController.m
  3. //  Moselo
  4. //
  5. //  Created by Ritchie Nathaniel on 2/17/17.
  6. //  Copyright © 2017 Moselo. All rights reserved.
  7. //
  8.  
  9. #import "ProfileViewController.h"
  10. #import "ProfileView.h"
  11. #import "ProfileTableViewCell.h"
  12. #import "ProfileHeaderTableViewCell.h"
  13. #import "ExpertSettingViewController.h"
  14. #import "PickerViewController.h"
  15. #import "SetProfilePictureViewController.h"
  16. #import "UserProfileSettingViewController.h"
  17. #import "AddServiceViewController.h"
  18. #import "WebViewViewController.h"
  19. #import "HelpViewController.h"
  20. //AS TEMP LinkedAccount
  21. #import "LinkedAccountViewController.h"
  22.  
  23. @interface ProfileViewController () <UITableViewDelegate, UITableViewDataSource, ProfileHeaderTableViewCellDelegate>
  24.  
  25. @property (strong, nonatomic) ProfileView *profileView;
  26. //AS TEMP
  27. @property (strong, nonatomic) NSString *name;
  28. @property (strong, nonatomic) NSString *username;
  29. @property (strong, nonatomic) NSString *avatarSmall;
  30. @property (strong, nonatomic) NSString *avatarMedium;
  31. @property (strong, nonatomic) NSString *avatarLarge;
  32. //END
  33.  
  34. @property (nonatomic) CGFloat newCellHeight;
  35. @property (nonatomic) BOOL isCellHeightUpdated;
  36.  
  37. - (void)applyNowButtonDidTapped;
  38.  
  39. @end
  40.  
  41. @implementation ProfileViewController
  42.  
  43. #pragma mark - Lifecycle
  44. - (id)init {
  45.     self = [super init];
  46.    
  47.     if(self) {
  48.         self.title = NSLocalizedString(@"Account", @"");
  49.     }
  50.    
  51.     return self;
  52. }
  53.  
  54. - (void)loadView {
  55.     [super loadView];
  56.    
  57.     _profileView = [[ProfileView alloc] initWithFrame:[BaseView frameWithNavigationBar]];
  58.     [self.view addSubview:self.profileView];
  59. }
  60.  
  61. - (void)viewDidLoad {
  62.     [super viewDidLoad];
  63.     // Do any additional setup after loading the view.
  64.     self.profileView.tableView.delegate = self;
  65.     self.profileView.tableView.dataSource = self;
  66.    
  67.     [self.profileView.applyNowButton addTarget:self action:@selector(applyNowButtonDidTapped) forControlEvents:UIControlEventTouchDown];
  68.    
  69.     //AS TEMP
  70.     //initiate empty string
  71.     _name = @"";
  72.     _username = @"";
  73.     _avatarLarge = @"";
  74.     _isCellHeightUpdated = NO;
  75.     //END
  76. }
  77.  
  78. - (void)viewWillAppear:(BOOL)animated {
  79.     [super viewWillAppear:animated];
  80.    
  81.     NSDictionary *activeUser = [[NSUserDefaults standardUserDefaults] secretObjectForKey:PREFS_ACTIVE_USER];
  82.     NSString *userRole = [activeUser valueForKeyPath:@"userRole.roleName"];
  83.    
  84.     if([userRole isEqualToString:@"user"]) {
  85.         self.profileView.profileViewType = ProfileViewTypeUser;
  86.         [self.profileView.tableView reloadData];
  87.     }
  88.     else if([userRole isEqualToString:@"expert"]) {
  89.         self.profileView.profileViewType = ProfileViewTypeExpert;
  90.         [self.profileView.tableView reloadData];
  91.     }
  92.    
  93.     //AS TEMP
  94.     //get data user profile
  95.     [DataManager callAPIGetUserProfileWithSuccess:^(UserModel *userData) {
  96.         self.name = userData.name;
  97.         self.username = userData.username;
  98.        
  99.         AvatarURLModel *avatarURL = userData.avatarURL;
  100.         self.avatarSmall = avatarURL.small;
  101.         self.avatarMedium = avatarURL.medium;
  102.         self.avatarLarge = avatarURL.large;
  103.        
  104.         NSLog(@"Name : %@", self.name);
  105.         NSLog(@"Username : %@", self.username);
  106.         NSLog(@"image : %@", self.self.avatarLarge);
  107.         self.isCellHeightUpdated = NO;
  108.         [self.profileView.tableView reloadData];
  109.     } failure:^(NSError *error) {
  110.         NSLog(@"failure block!!!");
  111.     }];
  112.     //END
  113. }
  114.  
  115. - (void)didReceiveMemoryWarning {
  116.     [super didReceiveMemoryWarning];
  117.     // Dispose of any resources that can be recreated.
  118. }
  119.  
  120. #pragma mark - Data Source
  121. #pragma mark UITableView
  122. - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
  123.    
  124.     if (indexPath.row == 0) {
  125.         if(self.isCellHeightUpdated) {
  126.             return  self.newCellHeight;
  127.         }
  128.         else {
  129.             return 193.0f;
  130.         }
  131.     }
  132.    
  133.     return 56.0f;
  134. }
  135.  
  136. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  137.     return 1;
  138. }
  139.  
  140. - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  141.    
  142.     //V1 Temporary Hidden
  143. //    if(self.profileView.profileViewType == ProfileViewTypeExpert) {
  144. //        return 5;
  145. //    }
  146. //    else if(self.profileView.profileViewType == ProfileViewTypeUser) {
  147. //        return 4;
  148. //    }
  149.    
  150.     if(self.profileView.profileViewType == ProfileViewTypeExpert) {
  151.         return 4 + 1;
  152.     }
  153.     else if(self.profileView.profileViewType == ProfileViewTypeUser) {
  154.         return 3 + 1;
  155.     }
  156.    
  157.     //END V1 Temporary Hidden
  158.    
  159.     return 0;
  160. }
  161.  
  162. - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
  163.     return FLT_MIN;
  164. }
  165.  
  166. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  167.  
  168.     if (self.profileView.profileViewType == ProfileViewTypeExpert && indexPath.row == 0) {
  169.         static NSString *cellHeaderID = @"ProfileHeaderTableViewCell";
  170.         ProfileHeaderTableViewCell *cellHeader = [tableView dequeueReusableCellWithIdentifier:cellHeaderID];
  171.  
  172.         if (nil == cellHeader) {
  173.             cellHeader = [[ProfileHeaderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  174.                                                            reuseIdentifier:cellHeaderID];
  175.             [cellHeader setSelectionStyle:UITableViewCellSelectionStyleNone];
  176.         }
  177.    
  178.         //5. sesudah buat delegate daftar dl di view controller yang pakainy
  179.         cellHeader.delegate = self;
  180.         cellHeader.userInteractionEnabled = YES;
  181.         if(self.profileView.profileViewType == ProfileViewTypeExpert) {
  182.           [cellHeader setPreviewButtonExpertVisible:YES];
  183.         }
  184.         else {
  185.             [cellHeader setPreviewButtonExpertVisible:NO];
  186.         }
  187.        
  188.         //NSString *fullname = @"Axel Soedarsono Axel Soedarsono";
  189.         //[cellHeader setProfileHeaderTableViewCellWithImageURL:@"" name:fullname username:@"axelsoedarsono"];
  190.        
  191.         //AS TEMP set data from API
  192.         [cellHeader setProfileHeaderTableViewCellWithImageURL:self.avatarLarge name:self.name username:self.username];
  193.         //END
  194.        
  195.        
  196.         return cellHeader;
  197.     }
  198.     else if (self.profileView.profileViewType == ProfileViewTypeUser && indexPath.row == 0) {
  199.         static NSString *cellHeaderID = @"ProfileHeaderTableViewCell";
  200.         ProfileHeaderTableViewCell *cellHeader = [tableView dequeueReusableCellWithIdentifier:cellHeaderID];
  201.        
  202.         if (nil == cellHeader) {
  203.             cellHeader = [[ProfileHeaderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  204.                                                            reuseIdentifier:cellHeaderID];
  205.             [cellHeader setSelectionStyle:UITableViewCellSelectionStyleNone];
  206.         }
  207.        
  208.         //5. sesudah buat delegate daftar dl di view controller yang pakainy
  209.         cellHeader.delegate = self;
  210.         cellHeader.userInteractionEnabled = YES;
  211.         if(self.profileView.profileViewType == ProfileViewTypeExpert) {
  212.             [cellHeader setPreviewButtonExpertVisible:YES];
  213.         }
  214.         else {
  215.             [cellHeader setPreviewButtonExpertVisible:NO];
  216.         }
  217.        
  218.         NSString *fullname = @"Axel Soedarsono Axel Soedarsono";
  219.         [cellHeader setProfileHeaderTableViewCellWithImageURL:@"" name:fullname username:@"axelsoe"];
  220.        
  221.         return cellHeader;
  222.     }
  223.     else {
  224.         static NSString *cellID = @"ProfileTableViewCell";
  225.  
  226.         ProfileTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
  227.        
  228.         if(nil == cell) {
  229.             cell = [[ProfileTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
  230.                                                reuseIdentifier:cellID];
  231.             [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
  232.         }
  233.        
  234.         if(self.profileView.profileViewType == ProfileViewTypeExpert) {
  235.             if(indexPath.row == 1) {
  236.                 //Expert Setting
  237.                 [cell setProfileTableViewCellType:ProfileTableViewCellTypeExpertSetting];
  238.             }
  239.             else if(indexPath.row == 2) {
  240.                 //Help
  241.                 [cell setProfileTableViewCellType:ProfileTableViewCellTypeHelp];
  242.             }
  243.             else if (indexPath.row == 3) {
  244.                 //About Us
  245.                 [cell setProfileTableViewCellType:ProfileTableViewCellTypeInformation];
  246.             }
  247.             else if (indexPath.row == 4) {
  248.                 //App Version
  249.                 [cell setProfileTableViewCellType:ProfileTableViewCellTypeAppVersion];
  250.             }
  251.         }
  252.         else if (self.profileView.profileViewType == ProfileViewTypeUser) {
  253.             if(indexPath.row == 1) {
  254.                 //Help
  255.                 [cell setProfileTableViewCellType:ProfileTableViewCellTypeHelp];
  256.             }
  257.             else if(indexPath.row == 2) {
  258.                 //About Us
  259.                 [cell setProfileTableViewCellType:ProfileTableViewCellTypeInformation];
  260.             }
  261.             else if (indexPath.row == 3) {
  262.                 //App Version
  263.                 [cell setProfileTableViewCellType:ProfileTableViewCellTypeAppVersion];
  264.             }
  265.         }
  266.         return cell;
  267.     }
  268.     //END V1 Temporary Hidden
  269.    
  270. }
  271.  
  272. #pragma mark - Delegate
  273. #pragma mark UITableView
  274. - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
  275.     if(self.profileView.profileViewType == ProfileViewTypeExpert) {
  276.         if(indexPath.row == 1) {
  277.             //Expert Setting
  278.             ExpertSettingViewController *expertSettingViewController = [[ExpertSettingViewController alloc] init];
  279.             expertSettingViewController.hidesBottomBarWhenPushed = YES;
  280.             [self.navigationController pushViewController:expertSettingViewController animated:YES];
  281.         }
  282.         else if(indexPath.row == 2) {
  283.             //Help
  284.             HelpViewController *helpViewController = [[HelpViewController alloc] init];
  285.             helpViewController.hidesBottomBarWhenPushed = YES;
  286.             [self.navigationController pushViewController:helpViewController animated:YES];
  287.         }
  288.         else if(indexPath.row == 3) {
  289.             //About Us
  290.             WebViewViewController *viewController = [[WebViewViewController alloc] init];
  291.             [viewController setWebViewViewControllerType:WebViewViewControllerTypeAboutUs];
  292.             [viewController setWebViewViewControllerHeightType:WebViewViewControllerTypeWithNavigation];
  293.             [self.navigationController pushViewController:viewController animated:YES];
  294.         }
  295.         else if(indexPath.row == 4) {
  296.             //AS TEMP to show Linked Account UI
  297.             LinkedAccountViewController *linkedAccountViewController = [[LinkedAccountViewController alloc] init];
  298.             linkedAccountViewController.hidesBottomBarWhenPushed = YES;
  299.             [self.navigationController pushViewController:linkedAccountViewController animated:YES];
  300.             //END TEMP
  301.         }
  302.     }
  303.     else if (self.profileView.profileViewType == ProfileViewTypeUser) {
  304.         if(indexPath.row == 1) {
  305.             //Help
  306.             HelpViewController *helpViewController = [[HelpViewController alloc] init];
  307.             helpViewController.hidesBottomBarWhenPushed = YES;
  308.             [self.navigationController pushViewController:helpViewController animated:YES];
  309.         }
  310.         else if(indexPath.row == 2) {
  311.             //About Us
  312.             WebViewViewController *viewController = [[WebViewViewController alloc] init];
  313.             [viewController setWebViewViewControllerType:WebViewViewControllerTypeAboutUs];
  314.             [viewController setWebViewViewControllerHeightType:WebViewViewControllerTypeWithNavigation];
  315.             [self.navigationController pushViewController:viewController animated:YES];
  316.         }
  317.         //AS TEMP to show Linked Account UI
  318.         else if(indexPath.row == 3) {
  319.             //LinkedAccount
  320.             LinkedAccountViewController *linkedAccountController = [[LinkedAccountViewController alloc] init];
  321.             linkedAccountController.hidesBottomBarWhenPushed = YES;
  322.             [self.navigationController pushViewController:linkedAccountController animated:YES];
  323.         }
  324.         //END TEMP
  325.     }
  326. }
  327.  
  328. //6.Implement Delegate
  329. #pragma mark ProfileHeaderTableViewCell
  330. - (void)profileHeaderTableViewCellResizeViewWithTotalHeight:(CGFloat)height {
  331.    
  332.     if(!self.isCellHeightUpdated) {
  333.         self.isCellHeightUpdated = YES;
  334.         self.newCellHeight = height;
  335.         [self.profileView.tableView reloadData];
  336.     }
  337. }
  338.  
  339. - (void)profileHeaderTableViewDidTappedEditProfileButton {
  340.     UserProfileSettingViewController *userProfileSettingViewController = [[UserProfileSettingViewController alloc] init];
  341.     userProfileSettingViewController.hidesBottomBarWhenPushed = YES;
  342.     [self.navigationController pushViewController:userProfileSettingViewController animated:YES];
  343. }
  344.  
  345. - (void)profileHeaderTableViewDidTappedPreviewProfileButton {
  346.     //not yet implement
  347.     NSLog(@"PreviewProfileButton pressed!");
  348. }
  349.  
  350. #pragma mark - Custom Method
  351. - (void)applyNowButtonDidTapped {
  352.     WebViewViewController *viewController = [[WebViewViewController alloc] init];
  353.     [viewController setWebViewViewControllerType:WebViewViewControllerTypeApplyExpert];
  354.     [viewController setWebViewViewControllerHeightType:WebViewViewControllerTypeWithoutNavigation];
  355.     [self presentViewController:viewController animated:YES completion:nil];
  356. }
  357.  
  358. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement