public void AddDocument(object data, CompletionHandler handler)
 {
     _collectionReference.AddDocument(data.ToNativeFieldValues(), (error) =>
     {
         handler?.Invoke(error == null ? null : ExceptionMapper.Map(error));
     });
 }
示例#2
0
        public async Task <Result <CollectionDocumentChangeEventArgs> > CreateDocument(IScreen context, CollectionReference collection)
        {
            if (collection is FileCollectionReference)
            {
                return(await AddFileToDatabase(context, collection.Database));
            }

            var addDocumentOptions = new AddDocumentOptions(collection);

            var optionsResult = await ShowHostDialog(context).For(addDocumentOptions);

            if (optionsResult.Action is AddDocumentOptions.ACTION_CANCEL)
            {
                return(Result.Failure <CollectionDocumentChangeEventArgs>(Fails.Canceled));
            }

            var newId  = optionsResult.Model.NewId;
            var newDoc = new BsonDocument
            {
                ["_id"] = newId
            };

            var documentReference = collection.AddDocument(newDoc);

            var documentsCreated = new CollectionDocumentChangeEventArgs(ReferenceNodeChangeAction.Add, documentReference, collection)
            {
                PostAction = (optionsResult.Model.EditAfterCreate || optionsResult.Action is AddDocumentOptions.ACTION_OK_AND_EDIT) ? "edit" : null
            };

            return(Result.Ok(documentsCreated));
        }
示例#3
0
        public Task <Result <CollectionDocumentChangeEventArgs> > ImportDataFromText(CollectionReference collection, string textData)
        {
            try
            {
                if (string.IsNullOrWhiteSpace(textData))
                {
                    return(Task.FromResult(Result.Ok(CollectionDocumentChangeEventArgs.Nome)));
                }

                var newValue = JsonSerializer.Deserialize(textData);
                var newDocs  = new List <DocumentReference>();
                if (newValue.IsArray)
                {
                    foreach (var value in newValue.AsArray)
                    {
                        var doc = value.AsDocument;
                        var documentReference = collection.AddDocument(doc);
                        newDocs.Add(documentReference);
                    }
                }
                else
                {
                    var doc = newValue.AsDocument;
                    var documentReference = collection.AddDocument(doc);
                    newDocs.Add(documentReference);
                }

                var documentsUpdate = new CollectionDocumentChangeEventArgs(ReferenceNodeChangeAction.Add, newDocs, collection);

                return(Task.FromResult(Result.Ok(documentsUpdate)));
            }
            catch (Exception e)
            {
                var message = "Failed to import document from text content: " + e.Message;
                Logger.Warning(e, "Cannot process clipboard data.");
                _applicationInteraction.ShowError(e, message, "Import Error");

                return(Task.FromResult(Result.Failure <CollectionDocumentChangeEventArgs>(message)));
            }
        }
        public Task <IDocumentReference> AddDocumentAsync(object data)
        {
            var tcs = new TaskCompletionSource <IDocumentReference>();
            DocumentReference documentReference = null;

            documentReference = _wrapped.AddDocument(data.ToDictionary(), error => {
                if (error == null)
                {
                    tcs.SetResult(new DocumentReferenceWrapper(documentReference));
                }
                else
                {
                    tcs.SetException(new FirebaseException(error.LocalizedDescription));
                }
            });
            return(tcs.Task);
        }