SchedulePresenter.cs :  » Windows-Presentation-Foundation » Caliburn » ContactManager » Presenters » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Windows Presentation Foundation » Caliburn 
Caliburn » ContactManager » Presenters » SchedulePresenter.cs
namespace ContactManager.Presenters{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using Caliburn.Core;
    using Caliburn.Core.IoC;
    using Caliburn.ModelFramework;
    using Caliburn.PresentationFramework;
    using Caliburn.PresentationFramework.ApplicationModel;
    using Caliburn.PresentationFramework.RoutedMessaging;
    using Caliburn.PresentationFramework.Screens;
    using Caliburn.WPF.ApplicationFramework;
    using Interfaces;
    using Model;
    using Services.Interfaces;
    using Web;

    [PerRequest(typeof(ISchedulePresenter))]
    public class SchedulePresenter : Screen, ISchedulePresenter
    {
        private readonly IScheduleService _scheduleService;
        private readonly ISettings _settings;
        private DailySchedule _currentSchedule;
        private readonly UndoRedoManager _undoRedoManager;
        private bool _changingSchedule;
        private readonly List<DailySchedule> _alteredSchedules = new List<DailySchedule>();
        private readonly BindableCollection<Contact> _contacts = new BindableCollection<Contact>();

        public SchedulePresenter(IScheduleService scheduleService, ISettings settings)
        {
            _scheduleService = scheduleService;
            _settings = settings;
            _undoRedoManager = new UndoRedoManager();
        }

        public UndoRedoManager UndoRedoManager
        {
            get { return _undoRedoManager; }
        }

        public DailySchedule CurrentSchedule
        {
            get { return _currentSchedule; }
            set
            {
                var previousSchedule = _currentSchedule;

                if(_currentSchedule != null)
                {
                    _undoRedoManager.Unregister(_currentSchedule);
                    _currentSchedule.PropertyChanged -= SchedulePropertyChanged;
                }

                _currentSchedule = value;

                var nextSchedule = _currentSchedule;

                if(_currentSchedule != null)
                {
                    _undoRedoManager.Register(_currentSchedule);

                    if(!_alteredSchedules.Contains(_currentSchedule))
                        _alteredSchedules.Add(_currentSchedule);

                    _currentSchedule.PropertyChanged += SchedulePropertyChanged;
                    _currentSchedule.Validate();
                }

                if(previousSchedule != null &&
                   nextSchedule != null &&
                   !_changingSchedule)
                {
                    _undoRedoManager.Push(
                        () =>{
                            _changingSchedule = true;
                            CurrentSchedule = previousSchedule;
                            _changingSchedule = false;
                        },
                        () =>{
                            _changingSchedule = true;
                            CurrentSchedule = nextSchedule;
                            _changingSchedule = false;
                        });
                }

                NotifyOfPropertyChange(() => CurrentSchedule);
                NotifyOfPropertyChange(() => CanSaveChanges);
            }
        }

        public bool IsDirty
        {
            get
            {
                foreach(var alteredSchedule in _alteredSchedules)
                {
                    if(alteredSchedule.IsDirty)
                        return true;
                }

                return false;
            }
        }

        public bool IsValid
        {
            get
            {
                foreach(var alteredSchedule in _alteredSchedules)
                {
                    if(!alteredSchedule.IsValid)
                        return false;
                }

                return true;
            }
        }

        private void SchedulePropertyChanged(object sender, PropertyChangedEventArgs e)
        {
            if(e.PropertyName == "IsDirty" || e.PropertyName == "IsValid")
                NotifyOfPropertyChange(() => CanSaveChanges);
        }

        public void GotoPreviousDay()
        {
            CurrentSchedule = _scheduleService.GetScheduleFor(CurrentSchedule.Date.AddDays(-1));
        }

        public void GotoNextDay()
        {
            CurrentSchedule = _scheduleService.GetScheduleFor(CurrentSchedule.Date.AddDays(1));
        }

        public IResult AddAppointment()
        {
            var appointment = new Appointment {Time = _settings.EarliestAppointment};
            _currentSchedule.AddAppointment(appointment);

            if(_contacts.Count > 0)
            {
                appointment.AllContacts = _contacts;
                return null;
            }

            return new WebServiceResult<ContactServiceClient, GetAllContactsCompletedEventArgs>(
                x => x.GetAllContactsAsync(),
                x =>{
                    x.Result.Apply(dto => _contacts.Add(Map.ToContact(dto)));
                    _contacts.Apply(appointment.AllContacts.Add);
                });
        }

        public void RemoveAppointment(Appointment appointmentToRemove)
        {
            _currentSchedule.RemoveAppointment(appointmentToRemove);
        }

        public bool CanSaveChanges
        {
            get { return IsDirty && IsValid; }
        }

        public void SaveChanges()
        {
            _undoRedoManager.Clear();
            _scheduleService.SaveAll();
        }

        public void UndoScheduleChange()
        {
            _undoRedoManager.Undo();
        }

        public void RedoScheduleChange()
        {
            _undoRedoManager.Redo();
        }

        protected override void OnInitialize()
        {
            base.OnInitialize();

            DisplayName = "Daily Schedule";
            CurrentSchedule = _scheduleService.GetScheduleFor(DateTime.Today);
        }

        protected override void OnShutdown()
        {
            base.OnShutdown();
            _scheduleService.CancelChanges();
        }

        public override bool CanShutdown()
        {
            return !IsDirty;
        }

        public ISubordinate CreateShutdownModel()
        {
            if(IsValid)
            {
                return new Question(
                    this,
                    "The schedule has not been saved.  Do you want to save before closing?"
                    ) {Answer = Answer.Yes};
            }

            return new Question(
                this,
                "The schedule is invalid.  Changes will be lost.  Do you still want to close?",
                Answer.Yes, Answer.No
                );
        }

        public bool CanShutdown(ISubordinate shutdownModel)
        {
            var question = (Question)shutdownModel;

            if(IsValid)
            {
                if(question.Answer == Answer.Cancel)
                    return false;

                if(question.Answer == Answer.Yes)
                    SaveChanges();

                return true;
            }

            if(question.Answer == Answer.Yes)
                return true;

            return false;
        }
    }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.