Advertisement
Guest User

VFDFirebaseManager+Users

a guest
Nov 12th, 2018
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //
  2. //  VFDFirebaseManager+Users.h
  3. //  VK-Feed
  4. //
  5. //  Created by Дмитрий Кучин on 03/10/2018.
  6. //  Copyright © 2018 VFeed. All rights reserved.
  7. //
  8.  
  9. #import "VFDFirebaseManager.h"
  10. #import "VFDFirebaseUser.h"
  11. #import "VFDFirebasePack.h"
  12.  
  13. extern NSString * _Nonnull const VFDUsersKey;
  14. extern NSString * _Nonnull const VFDUserKey;
  15.  
  16. @interface VFDFirebaseManager (Users)
  17. - (void)getUserByID:(NSNumber * _Nonnull)identifier completion:(void (^)(VFDFirebaseUser *user, NSError *error))completion;
  18. - (void)updateCurrentUserOnServer;
  19. - (void)addPackInAuthors:(VFDFirebasePack * _Nonnull)pack;
  20. - (void)updateServerSettings;
  21.  
  22. - (NSString * _Nonnull)userIdentifier:(NSNumber *)identifier;
  23.  
  24. @end
  25.  
  26. ///////////////////IMPLEMENTATION//////////////////////
  27.  
  28. //
  29. //  VFDFirebaseManager+Users.m
  30. //  VK-Feed
  31. //
  32. //  Created by Дмитрий Кучин on 03/10/2018.
  33. //  Copyright © 2018 VFeed. All rights reserved.
  34. //
  35.  
  36. #import "VFDFirebaseManager+Users.h"
  37. #import "VFDSettingsModel.h"
  38.  
  39. NSString * _Nonnull const VFDUsersKey = @"users";
  40. NSString * _Nonnull const VFDUserKey = @"user";
  41. NSString * _Nonnull const VFDOwnerKey = @"owner_packs";
  42. NSString * _Nonnull const VFDPackKey = @"packs";
  43. NSString * _Nonnull const VFDSettingsKey = @"settings";
  44.  
  45. @implementation VFDFirebaseManager (Users)
  46.  
  47. - (void)getUserByID:(NSNumber *)identifier completion:(void (^)(VFDFirebaseUser *user, NSError *error))completion {
  48.     FIRDocumentReference *userRef = [[self.dataBase collectionWithPath:VFDUsersKey] documentWithPath:[self userIdentifier:identifier]];
  49.     [self.dataBase runTransactionWithBlock:^id _Nullable(FIRTransaction * _Nonnull transaction, NSError * _Nullable __autoreleasing * _Nullable errorPointer) {
  50.         NSError *error;
  51.         FIRDocumentSnapshot *snapshotUser = [transaction getDocument:userRef error:errorPointer];
  52.         guard(snapshotUser.data != nil) else { completion(nil, error); return nil; }
  53.         VFDFirebaseUser *user = [VFDFirebaseUser.alloc initWithValue:snapshotUser.data];
  54.         completion(user, nil);
  55.         return nil;
  56.     } completion:^(id  _Nullable result, NSError * _Nullable error) {}];
  57. }
  58.  
  59. - (void)updateCurrentUserOnServer {
  60.     [self getUserByID:VKSERVICE.currentUser.currentId completion:^(VFDFirebaseUser *user, NSError *error) {
  61.         if (user == nil) {
  62.             [self saveUserOnServer:VKSERVICE.currentUser.user];
  63.         } else {
  64.             [VFDSettingsModel.sharedInstance settingsFromServer:user.settings];
  65.         }
  66.     }];
  67. }
  68.  
  69. - (void)saveUserOnServer:(VFDUser *)user {
  70.     VFDFirebaseUser *firebaseUser = [VFDFirebaseUser.alloc initWithUser:user];
  71.     [[[self.dataBase collectionWithPath:VFDUsersKey] documentWithPath:[self userIdentifier:user.id]] setData:firebaseUser.dictionary completion:^(NSError * _Nullable error) {
  72.         [REALMSERVICE saveTransactionWithBlock:^(RLMRealm *realm) {
  73.             [realm addOrUpdateObject:firebaseUser];
  74.         }];
  75.     }];
  76. }
  77.  
  78. - (void)updateServerSettings {
  79.     FIRDocumentReference *userRef = [[self.dataBase collectionWithPath:VFDUsersKey] documentWithPath:[self userIdentifier:VKSERVICE.currentUser.currentId]];
  80.     [self.dataBase runTransactionWithBlock:^id _Nullable(FIRTransaction * _Nonnull snapshot, NSError * _Nullable __autoreleasing * _Nullable errorPointer) {
  81.         guard(*errorPointer == nil) else { return nil; }
  82.         [snapshot updateData:@{ VFDSettingsKey : VFDSettingsModel.sharedInstance.allValuesInDictionary } forDocument:userRef];
  83.         return nil;
  84.     } completion:^(id  _Nullable result, NSError * _Nullable error) {}];
  85. }
  86.  
  87. - (void)addPackInAuthors:(VFDFirebasePack *)pack {
  88.     FIRDocumentReference *userRef = [[self.dataBase collectionWithPath:VFDUsersKey] documentWithPath:[self userIdentifier:VKSERVICE.currentUser.currentId]];
  89.     [self.dataBase runTransactionWithBlock:^id _Nullable(FIRTransaction * _Nonnull snapshot, NSError * _Nullable __autoreleasing * _Nullable errorPointer) {
  90.         FIRDocumentSnapshot *userSnapshot = [snapshot getDocument:userRef error:errorPointer];
  91.         guard(*errorPointer == nil) else { return nil; }
  92.         guardvar(NSMutableArray, packs, userSnapshot.data[VFDOwnerKey], else { packs = NSMutableArray.array; })
  93.         [packs addObject:pack.identifier];
  94.         [snapshot updateData:@{ VFDOwnerKey : packs } forDocument:userRef];
  95.         return nil;
  96.     } completion:^(id  _Nullable result, NSError * _Nullable error) {}];
  97. }
  98.  
  99. #pragma mark - Convertation
  100.  
  101. - (NSString *)userIdentifier:(NSNumber *)identifier {
  102.     return [NSString stringWithFormat:@"%@%@", VFDUserKey, identifier];
  103. }
  104.  
  105. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement