<commit_before>//
//  CloudKitSynchronizer+Sharing.swift
//  Pods
//
//  Created by <NAME> on 07/04/2019.
//

import Foundation
import CloudKit

@objc public extension CloudKitSynchronizer {
    
    fileprivate func modelAdapter(for object: AnyObject) -> ModelAdapter? {
        for modelAdapter in modelAdapters {
            if modelAdapter.record(for: object) != nil {
                return modelAdapter
            }
        }
        return nil
    }
    
    /**
     Returns the locally stored `CKShare` for a given model object.
     - Parameter object  The model object.
     - Returns: `CKShare` stored for the given object.
     */
    @objc func share(for object: AnyObject) -> CKShare? {
        guard let modelAdapter = modelAdapter(for: object) else {
            return nil
        }
        return modelAdapter.share(for: object)
    }
    
    /**
     Saves the given `CKShare` locally for the given model object.
     - Parameters:
     - share The `CKShare`.
     - object  The model object.
     */
    @objc func saveShare(_ share: CKShare, for object: AnyObject) {
        guard let modelAdapter = modelAdapter(for: object) else {
            return
        }
        modelAdapter.save(share: share, for: object)
    }
    
    /**
     Deletes any `CKShare` locally stored  for the given model object.
     - Parameters:
     - object  The model object.
     */
    @objc func deleteShare(for object: AnyObject) {
        guard let modelAdapter = modelAdapter(for: object) else {
            return
        }
        modelAdapter.deleteShare(for: object)
    }
    
    /**
     Creates and uploads a new `CKShare` for the given model object.
     - Parameters:
     - object The model object to share.
     - publicPermission  The permissions to be used for the new share.
     - participants: The participants to add to this share.
     - completion: Closure that gets called with an optional error when the operation is completed.
     
     */
    @objc func share(object: AnyObject, publicPermission: CKShare.Participant.Permission, participants: [CKShare.Participant], completion: ((CKShare?, Error?) -> ())?) {
        
        guard let modelAdapter = modelAdapter(for: object),
            let record = modelAdapter.record(for: object) else {
                return
        }
        let share = CKShare(rootRecord: record)
        share.publicPermission = publicPermission
        for participant in participants {
            share.addParticipant(participant)
        }
        
        addMetadata(to: [record, share])
        
        let operation = CKModifyRecordsOperation(recordsToSave: [record, share], recordIDsToDelete: nil)
        
        operation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, operationError in
            
            self.dispatchQueue.async {
                
                let uploadedShare = savedRecords?.first { $0 is CKShare} as? CKShare
                
                if let savedRecords = savedRecords,
                    operationError == nil,
                    let share = uploadedShare {
                    
                    modelAdapter.prepareToImport()
                    let records = savedRecords.filter { $0 != share }
                    modelAdapter.didUpload(savedRecords: records)
                    modelAdapter.persistImportedChanges(completion: { (error) in
                        
                        self.dispatchQueue.async {
                            <commit_msg>missing completion handler call<commit_after>//
//  CloudKitSynchronizer+Sharing.swift
//  Pods
//
//  Created by <NAME> on 07/04/2019.
//

import Foundation
import CloudKit

@objc public extension CloudKitSynchronizer {
    
    fileprivate func modelAdapter(for object: AnyObject) -> ModelAdapter? {
        for modelAdapter in modelAdapters {
            if modelAdapter.record(for: object) != nil {
                return modelAdapter
            }
        }
        return nil
    }
    
    /**
     Returns the locally stored `CKShare` for a given model object.
     - Parameter object  The model object.
     - Returns: `CKShare` stored for the given object.
     */
    @objc func share(for object: AnyObject) -> CKShare? {
        guard let modelAdapter = modelAdapter(for: object) else {
            return nil
        }
        return modelAdapter.share(for: object)
    }
    
    /**
     Saves the given `CKShare` locally for the given model object.
     - Parameters:
     - share The `CKShare`.
     - object  The model object.
     */
    @objc func saveShare(_ share: CKShare, for object: AnyObject) {
        guard let modelAdapter = modelAdapter(for: object) else {
            return
        }
        modelAdapter.save(share: share, for: object)
    }
    
    /**
     Deletes any `CKShare` locally stored  for the given model object.
     - Parameters:
     - object  The model object.
     */
    @objc func deleteShare(for object: AnyObject) {
        guard let modelAdapter = modelAdapter(for: object) else {
            return
        }
        modelAdapter.deleteShare(for: object)
    }
    
    /**
     Creates and uploads a new `CKShare` for the given model object.
     - Parameters:
     - object The model object to share.
     - publicPermission  The permissions to be used for the new share.
     - participants: The participants to add to this share.
     - completion: Closure that gets called with an optional error when the operation is completed.
     
     */
    @objc func share(object: AnyObject, publicPermission: CKShare.Participant.Permission, participants: [CKShare.Participant], completion: ((CKShare?, Error?) -> ())?) {
        
        guard let modelAdapter = modelAdapter(for: object),
            let record = modelAdapter.record(for: object) else {
                completion?(nil, nil)
                return
        }
        let share = CKShare(rootRecord: record)
        share.publicPermission = publicPermission
        for participant in participants {
            share.addParticipant(participant)
        }
        
        addMetadata(to: [record, share])
        
        let operation = CKModifyRecordsOperation(recordsToSave: [record, share], recordIDsToDelete: nil)
        
        operation.modifyRecordsCompletionBlock = { savedRecords, deletedRecordIDs, operationError in
            
            self.dispatchQueue.async {
                
                let uploadedShare = savedRecords?.first { $0 is CKShare} as? CKShare
                
                if let savedRecords = savedRecords,
                    operationError == nil,
                    let share = uploadedShare {
                    
                    modelAdapter.prepareToImport()
                    let records = savedRecords.filter { $0 != share }
                    modelAdapter.didUpload(savedRecords: records)
                    modelAdapter.persistImportedChanges(completion: { (error) in
                        
                        self.dispatchQueue.async {
                            
{'fraction_non_alphanumeric': 0.07735902521960895, 'fraction_numerical': 0.0028336639274582033, 'mean_word_length': 1.8258606885508406, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1792082', 'n_tokens_mistral': 1799, 'n_tokens_neox': 1726, 'n_words': 539}