Advertisement
Guest User

Passive MVP -Backend Code Example

a guest
Jan 30th, 2015
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.36 KB | None | 0 0
  1. namespace Backend.Bootstrap
  2. {
  3.     /// <summary>
  4.     /// Occurs when the user requests to quit the application.
  5.     /// </summary>    
  6.     public delegate void QuitApplicationEventHandler(object sender, ActionEventArgs e);
  7.  
  8.     /// <summary>
  9.     /// Starts the bootstrap process.
  10.     /// </summary>    
  11.     public delegate void StartBootstrapEventHandler();
  12.  
  13.     /// <summary>
  14.     /// Defines the bootstrap view.
  15.     /// </summary>
  16.     public interface IBootstrapView : IView
  17.     {
  18.         /// <summary>
  19.         /// Occurs when the user requests to quit the application.
  20.         /// </summary>
  21.         event QuitApplicationEventHandler QuitApplication;
  22.  
  23.         /// <summary>
  24.         /// Starts the bootstrap process.
  25.         /// </summary>
  26.         event StartBootstrapEventHandler StartBootstrap;
  27.  
  28.         /// <summary>
  29.         /// Title of the window.
  30.         /// </summary>
  31.         string Title { set; }
  32.  
  33.         /// <summary>
  34.         /// Progress of bootstrap.
  35.         /// </summary>
  36.         ProgressInfo Progress { set; }
  37.     }
  38.  
  39.  
  40. /// <summary>
  41.     /// Defines the bootstrap presenter.
  42.     /// </summary>
  43.     public class BootstrapPresenter : Presenter<IBootstrapView>
  44.     {
  45.         /// <summary>
  46.         /// Creates a new <see cref="BootstrapPresenter"/>.
  47.         /// </summary>        
  48.         public BootstrapPresenter(IBootstrapView view)
  49.             : base(view)
  50.         {
  51.             view.Title = "[KdG ExamenTool] v3.2.0 | Bootstrap";
  52.  
  53.             view.QuitApplication += QuitApplication;
  54.             view.StartBootstrap += StartBootstrap;
  55.         }        
  56.  
  57.         #region Common
  58.         /// <summary>
  59.         /// Random object.
  60.         /// </summary>
  61.         private Random _random = new Random();
  62.  
  63.         /// <summary>
  64.         /// Helper method for progress reporting.
  65.         /// </summary>        
  66.         private void ReportProgress(ref BackgroundWorker backgroundWorker, int percentage = 0, ProgressInfo progressInfo = null)
  67.         {
  68.             if (backgroundWorker.WorkerReportsProgress) {
  69.                 backgroundWorker.ReportProgress(percentage, progressInfo);
  70.             }
  71.         }
  72.         #endregion
  73.  
  74.         #region Bootstrap Process
  75.         /// <summary>
  76.         /// Holds a reference to the bootstrap background worker.
  77.         /// </summary>
  78.         private BackgroundWorker _bootstrapWorker;
  79.  
  80.         /// <summary>
  81.         /// Action to perform when a cancellation is catched.
  82.         /// </summary>
  83.         private Action _bootstrapCancelAction;
  84.  
  85.         /// <summary>
  86.         /// Starts the bootstrap process.
  87.         /// </summary>
  88.         private void StartBootstrap()
  89.         {
  90.             if (_bootstrapWorker == null) {
  91.                 _bootstrapWorker = new BackgroundWorker();
  92.                 _bootstrapWorker.WorkerReportsProgress = true;
  93.                 _bootstrapWorker.WorkerSupportsCancellation = true;                
  94.             }
  95.  
  96.             if (_bootstrapWorker.IsBusy) {
  97.                 return;
  98.             }
  99.  
  100.             _bootstrapWorker.DoWork += BootstrapWorkerTask;
  101.             _bootstrapWorker.ProgressChanged += BootstrapWorkerReportProgress;
  102.             _bootstrapWorker.RunWorkerCompleted += BootstrapWorkerTaskCompleted;            
  103.  
  104.             _bootstrapWorker.RunWorkerAsync();
  105.         }
  106.  
  107.         private void BootstrapWorkerTask(object sender, DoWorkEventArgs e)
  108.         {
  109.             ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten..." });
  110.             Thread.Sleep(_random.Next(250, 1250));
  111.             BootstrapWorkerCheckCancel();
  112.  
  113.             ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Controleren instellingen." });
  114.             Thread.Sleep(_random.Next(250, 1250));
  115.             BootstrapWorkerCheckCancel();
  116.  
  117.             ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Detecteren platform." });
  118.             Thread.Sleep(_random.Next(250, 1250));
  119.             BootstrapWorkerCheckCancel();
  120.  
  121.             ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Woordenboeken inlezen." });
  122.             Thread.Sleep(_random.Next(250, 1250));
  123.             BootstrapWorkerCheckCancel();
  124.  
  125.             ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Locaties registreren." });
  126.             Thread.Sleep(_random.Next(250, 1250));
  127.             BootstrapWorkerCheckCancel();
  128.  
  129.             ReportProgress(ref _bootstrapWorker, progressInfo: new ProgressInfo { IsIndeterminate = true, Message = "Opstarten — Interfaces opbouwen." });
  130.             Thread.Sleep(_random.Next(250, 1250));
  131.             BootstrapWorkerCheckCancel();
  132.         }
  133.  
  134.         private void BootstrapWorkerReportProgress(object sender, ProgressChangedEventArgs e)
  135.         {
  136.             if (e.UserState != null) {
  137.                 View.Progress = e.UserState as ProgressInfo;                
  138.             }
  139.         }
  140.  
  141.         private void BootstrapWorkerTaskCompleted(object sender, RunWorkerCompletedEventArgs e)
  142.         {
  143.             BootstrapWorkerCheckCancel();
  144.             View.Progress = new ProgressInfo { IsIndeterminate = false, Message = "Opstarten voltooid.", Progress = 100, Range = new Int32Range(0, 100) };            
  145.             _bootstrapWorker.Dispose();
  146.             _bootstrapWorker = null;
  147.             BootstrapWorkerCheckCancel();
  148.         }        
  149.  
  150.         /// <summary>
  151.         /// Checks whether a cancellation is pending and performs the cancellation action, if provided.
  152.         /// </summary>
  153.         private void BootstrapWorkerCheckCancel()
  154.         {
  155.             if (_bootstrapWorker == null) {
  156.                 if (_bootstrapCancelAction != null) {
  157.                     _bootstrapCancelAction();
  158.                 }
  159.                 return;
  160.             }
  161.  
  162.             if (_bootstrapWorker.CancellationPending) {
  163.                 if (_bootstrapCancelAction != null) {
  164.                     _bootstrapCancelAction();
  165.                 }
  166.             }
  167.         }
  168.         #endregion
  169.  
  170.         #region Quit Application
  171.         /// <summary>
  172.         /// Quits the application.
  173.         /// </summary>        
  174.         private void QuitApplication(object sender, ActionEventArgs e)
  175.         {
  176.             if (e.HasAction) {
  177.                 _bootstrapCancelAction = () => {
  178.                     e.Action ();
  179.                     System.Environment.Exit (0);
  180.                 };
  181.             } else {
  182.                 _bootstrapCancelAction = () => System.Environment.Exit (0);
  183.             }
  184.  
  185.             if (_bootstrapWorker == null) {
  186.                 _bootstrapCancelAction ();
  187.             } else {
  188.                 _bootstrapWorker.CancelAsync();
  189.             }
  190.         }
  191.         #endregion
  192.     }
  193.  
  194.  
  195. /// <summary>
  196.     /// Information on progress.
  197.     /// </summary>
  198.     public class ProgressInfo
  199.     {
  200.         /// <summary>
  201.         /// Gets/sets the progress message.
  202.         /// </summary>
  203.         public string Message { get; set; }
  204.  
  205.         /// <summary>
  206.         /// Determines whether indeterminate or not.
  207.         /// </summary>
  208.         public bool IsIndeterminate { get; set; }
  209.  
  210.         /// <summary>
  211.         /// Progress range.
  212.         /// Ususally in percentage, 0 - 100.
  213.         /// </summary>
  214.         public IRange<int> Range { get; set; }
  215.  
  216.         /// <summary>
  217.         /// The current progress.
  218.         /// </summary>
  219.         public int Progress { get; set; }
  220.     }
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement