Ejemplo n.º 1
0
        protected virtual Task <EntityViewModel <TEntity> > GetViewModel(IQueryable <TEntity> queryable, int page, int size)
        {
            return(Task.Run <EntityViewModel <TEntity> >(() =>
            {
                EntityViewModel <TEntity> model = new EntityViewModel <TEntity>(queryable, page, size);
                model.Headers = Metadata.ViewProperties;

                EntityViewButton createButton = new EntityViewButton();
                createButton.Name = "Create";
                createButton.GetInvokeDelegate = new EntityViewButtonCommandDelegate(viewer =>
                {
                    return new Task(async() =>
                    {
                        var item = EntityQueryable.Create();
                        bool?result = null;
                        await viewer.Dispatcher.Invoke(async() =>
                        {
                            viewer.IsLoading = true;
                            var editor = await GetEditor(item);
                            result = editor.ShowDialog();
                        });
                        if (result == true)
                        {
                            await Update(item);
                            model.UpdateTotalPage();
                            model.Items = model.Items.Concat(new TEntity[] { item }).ToArray();
                        }
                        viewer.Dispatcher.Invoke(() =>
                        {
                            viewer.IsLoading = false;
                        });
                    });
                });
                model.ViewButtons = new IViewButton[] { createButton };

                EntityItemButton editButton = new EntityItemButton();
                editButton.Name = "Edit";
                editButton.GetInvokeDelegate = new EntityItemButtonCommandDelegate((viewer, entity) =>
                {
                    return new Task(async() =>
                    {
                        var item = (TEntity)entity;
                        bool?result = null;
                        await viewer.Dispatcher.Invoke(async() =>
                        {
                            viewer.IsLoading = true;
                            var editor = await GetEditor(item);
                            result = editor.ShowDialog();
                        });
                        if (result == true)
                        {
                            await Update(item);
                        }
                        viewer.Dispatcher.Invoke(() =>
                        {
                            viewer.IsLoading = false;
                        });
                    });
                });
                EntityItemButton removeButton = new EntityItemButton();
                removeButton.Name = "Remove";
                removeButton.GetInvokeDelegate = new EntityItemButtonCommandDelegate((viewer, entity) =>
                {
                    return new Task(async() =>
                    {
                        viewer.Dispatcher.Invoke(() =>
                        {
                            viewer.IsLoading = true;
                        });
                        await EntityQueryable.RemoveAsync(entity.Index);
                        model.Items = await EntityQueryable.ToArrayAsync(queryable.Skip(page).Take(size));
                        viewer.Dispatcher.Invoke(() =>
                        {
                            viewer.IsLoading = false;
                        });
                    });
                });
                model.ItemButtons = new IEntityViewButton[] { editButton, removeButton };

                return model;
            }));
        }