f-miyu / Plugin.CloudFirestore

MIT License
121 stars 44 forks source link

Question: How to Handle Firestore Map Fields and Firestore Map Arrays with Reactive #110

Open BrysonScales opened 1 year ago

BrysonScales commented 1 year ago

I am currently using the Plugin.CloudFirestore.Sample project as a reference for creating my own application and would like to know how I should code my project to upload documents containing Firestore Maps and Firestore Arrays that have nested Firestore Maps then listen for document changes and display these changes using Xamarin Forms and Reactive. I would also like to know the maximum amount of Map conversions that can be handled at a time while converting from a Firestore Document to a .Net Model Class. To clarify, if the ExampleItem Class mentioned below contains another Model Class as a property, how would the class and reactive class viewmodel code look to upload/listen for changes. If anyone knows the answer to my question, it would be awesome if you could download the Plugin.CloudFirestore.Sample project, make the changes to the file, share a response explaining what you added, and share a download link.

    public class TodoItem
    {
        public static string CollectionPath = "todoItems";

        //Should I use DocumentConverters for converting to the "DataMap" and "DataMapArray" properties? If so what would the code for them look like?

        [Id]
        public string? Id { get; set; }

        public string? Name { get; set; }

        public string? Notes { get; set; }

        public ExampleItem1? DataMap { get; set; }

        public ObservableCollection<ExampleItem1>? DataMapArray { get; set; }

        [ServerTimestamp(CanReplace = false)]
        public Timestamp CreatedAt { get; set; }

        [ServerTimestamp]
        public Timestamp UpdatedAt { get; set; }
    }

    public class ExampleItem1
    {
        // Should I use? [Id] on the Id property or any other Plugin.CloudFirestore.Attributes?

        public string? Id { get; set; }

        public string? Name { get; set; }

        public ExampleItem2? DataMap { get; set; }

        public ObservableCollection<ExampleItem2>? DataMapArray { get; set; }
    }

    public class TodoItemViewModel : BindableBase
    {
        public string? Id { get; }

        public ReactivePropertySlim<string?> Name { get; set; } = new ReactivePropertySlim<string?>();
        public ReactivePropertySlim<string?> Notes { get; set; } = new ReactivePropertySlim<string?>();
        public ReactivePropertySlim<ExampleItem?> DataMap { get; set; } = new ReactivePropertySlim<ExampleItem?>();
        public ReactivePropertySlim<ObservableCollection<ExampleItem>?> DataMapArray { get; set; } = new ReactivePropertySlim<ObservableCollection<ExampleItem>?>();

        public TodoItemViewModel(TodoItem item)
        {
            Id = item.Id;
            Name.Value = item.Name;
            Notes.Value = item.Notes;
            DataMap.Value = item.DataMap;
            DataMapArray.Value = item.DataMapArray;
        }

        public void Update(string? name, string? notes, ExampleItem1? dataMap, ObservableCollection<ExampleItem1>? dataMapArray)
        {
            Name.Value = name;
            Notes.Value = notes;
            DataMap.Value = dataMap;
            DataMapArray.Value = dataMapArray;
        }
    }

Thanks, Bryson