UserTableViewCell.m 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /*
  2. * Copyright 2019 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import "UserTableViewCell.h"
  17. #import "FIRUser.h"
  18. @implementation UserTableViewCell {
  19. /** @var _lastPhotoURL
  20. @brief Used to make sure only the last requested image is used to update the UIImageView.
  21. */
  22. NSURL *_lastPhotoURL;
  23. }
  24. - (void)updateContentsWithUser:(FIRUser *)user {
  25. _userInfoDisplayNameLabel.text = user.displayName;
  26. _userInfoEmailLabel.text = user.email;
  27. _userInfoUserIDLabel.text = user.uid;
  28. NSMutableArray<NSString *> *providerIDs = [NSMutableArray array];
  29. for (id<FIRUserInfo> userInfo in user.providerData) {
  30. [providerIDs addObject:userInfo.providerID];
  31. }
  32. _userInfoProviderListLabel.text = [providerIDs componentsJoinedByString:@", "];
  33. NSURL *photoURL = user.photoURL;
  34. _lastPhotoURL = photoURL; // to prevent eariler image overwrites later one.
  35. if (photoURL) {
  36. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^() {
  37. UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:photoURL]];
  38. dispatch_async(dispatch_get_main_queue(), ^() {
  39. if (photoURL == _lastPhotoURL) {
  40. _userInfoProfileURLImageView.image = image;
  41. }
  42. });
  43. });
  44. } else {
  45. _userInfoProfileURLImageView.image = nil;
  46. }
  47. }
  48. @end