Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // VFDFirebaseManager+Users.h
- // VK-Feed
- //
- // Created by Дмитрий Кучин on 03/10/2018.
- // Copyright © 2018 VFeed. All rights reserved.
- //
- #import "VFDFirebaseManager.h"
- #import "VFDFirebaseUser.h"
- #import "VFDFirebasePack.h"
- extern NSString * _Nonnull const VFDUsersKey;
- extern NSString * _Nonnull const VFDUserKey;
- @interface VFDFirebaseManager (Users)
- - (void)getUserByID:(NSNumber * _Nonnull)identifier completion:(void (^)(VFDFirebaseUser *user, NSError *error))completion;
- - (void)updateCurrentUserOnServer;
- - (void)addPackInAuthors:(VFDFirebasePack * _Nonnull)pack;
- - (void)updateServerSettings;
- - (NSString * _Nonnull)userIdentifier:(NSNumber *)identifier;
- @end
- ///////////////////IMPLEMENTATION//////////////////////
- //
- // VFDFirebaseManager+Users.m
- // VK-Feed
- //
- // Created by Дмитрий Кучин on 03/10/2018.
- // Copyright © 2018 VFeed. All rights reserved.
- //
- #import "VFDFirebaseManager+Users.h"
- #import "VFDSettingsModel.h"
- NSString * _Nonnull const VFDUsersKey = @"users";
- NSString * _Nonnull const VFDUserKey = @"user";
- NSString * _Nonnull const VFDOwnerKey = @"owner_packs";
- NSString * _Nonnull const VFDPackKey = @"packs";
- NSString * _Nonnull const VFDSettingsKey = @"settings";
- @implementation VFDFirebaseManager (Users)
- - (void)getUserByID:(NSNumber *)identifier completion:(void (^)(VFDFirebaseUser *user, NSError *error))completion {
- FIRDocumentReference *userRef = [[self.dataBase collectionWithPath:VFDUsersKey] documentWithPath:[self userIdentifier:identifier]];
- [self.dataBase runTransactionWithBlock:^id _Nullable(FIRTransaction * _Nonnull transaction, NSError * _Nullable __autoreleasing * _Nullable errorPointer) {
- NSError *error;
- FIRDocumentSnapshot *snapshotUser = [transaction getDocument:userRef error:errorPointer];
- guard(snapshotUser.data != nil) else { completion(nil, error); return nil; }
- VFDFirebaseUser *user = [VFDFirebaseUser.alloc initWithValue:snapshotUser.data];
- completion(user, nil);
- return nil;
- } completion:^(id _Nullable result, NSError * _Nullable error) {}];
- }
- - (void)updateCurrentUserOnServer {
- [self getUserByID:VKSERVICE.currentUser.currentId completion:^(VFDFirebaseUser *user, NSError *error) {
- if (user == nil) {
- [self saveUserOnServer:VKSERVICE.currentUser.user];
- } else {
- [VFDSettingsModel.sharedInstance settingsFromServer:user.settings];
- }
- }];
- }
- - (void)saveUserOnServer:(VFDUser *)user {
- VFDFirebaseUser *firebaseUser = [VFDFirebaseUser.alloc initWithUser:user];
- [[[self.dataBase collectionWithPath:VFDUsersKey] documentWithPath:[self userIdentifier:user.id]] setData:firebaseUser.dictionary completion:^(NSError * _Nullable error) {
- [REALMSERVICE saveTransactionWithBlock:^(RLMRealm *realm) {
- [realm addOrUpdateObject:firebaseUser];
- }];
- }];
- }
- - (void)updateServerSettings {
- FIRDocumentReference *userRef = [[self.dataBase collectionWithPath:VFDUsersKey] documentWithPath:[self userIdentifier:VKSERVICE.currentUser.currentId]];
- [self.dataBase runTransactionWithBlock:^id _Nullable(FIRTransaction * _Nonnull snapshot, NSError * _Nullable __autoreleasing * _Nullable errorPointer) {
- guard(*errorPointer == nil) else { return nil; }
- [snapshot updateData:@{ VFDSettingsKey : VFDSettingsModel.sharedInstance.allValuesInDictionary } forDocument:userRef];
- return nil;
- } completion:^(id _Nullable result, NSError * _Nullable error) {}];
- }
- - (void)addPackInAuthors:(VFDFirebasePack *)pack {
- FIRDocumentReference *userRef = [[self.dataBase collectionWithPath:VFDUsersKey] documentWithPath:[self userIdentifier:VKSERVICE.currentUser.currentId]];
- [self.dataBase runTransactionWithBlock:^id _Nullable(FIRTransaction * _Nonnull snapshot, NSError * _Nullable __autoreleasing * _Nullable errorPointer) {
- FIRDocumentSnapshot *userSnapshot = [snapshot getDocument:userRef error:errorPointer];
- guard(*errorPointer == nil) else { return nil; }
- guardvar(NSMutableArray, packs, userSnapshot.data[VFDOwnerKey], else { packs = NSMutableArray.array; })
- [packs addObject:pack.identifier];
- [snapshot updateData:@{ VFDOwnerKey : packs } forDocument:userRef];
- return nil;
- } completion:^(id _Nullable result, NSError * _Nullable error) {}];
- }
- #pragma mark - Convertation
- - (NSString *)userIdentifier:(NSNumber *)identifier {
- return [NSString stringWithFormat:@"%@%@", VFDUserKey, identifier];
- }
- @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement