Skip to content

Commit

Permalink
Merge #2163 Simplify IUser
Browse files Browse the repository at this point in the history
  • Loading branch information
politas committed Oct 27, 2017
2 parents 80731c5 + 387115c commit 3ac3cf6
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 81 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes to this project will be documented in this file.
### Bugfixes
- [Build] Add skip_deploy to GitHub release deploy provider (#2151 by: dbent; reviewed: politas)
- [Build] Fix build errors for UpdateCol (#2153 by: politas; reviewed: Olympic1)
- [Core] Simplify IUser (#2163 by: HebaruSan; reviewed: politas)

## v1.22.6 (Guiana)

Expand Down
7 changes: 0 additions & 7 deletions Cmdline/ConsoleUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,6 @@ protected override void ReportProgress(string format, int percent)
Console.Write("\r\n{0}", format);
}
}
protected override void ReportDownloadsComplete(Uri[] urls, string[] filenames, Exception[] errors)
{
}
public override int WindowWidth
{
get { return Console.WindowWidth; }
}

}
}
3 changes: 1 addition & 2 deletions Core/Net/NetAsyncDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ private void CurlWatchThread(int index, CurlEasy easy, FileStream stream)
);
}
}

public void DownloadAndWait(ICollection<KeyValuePair<Uri, long>> urls)
{
// Start the download!
Expand Down Expand Up @@ -319,7 +319,6 @@ private void triggerCompleted(Uri[] file_urls, string[] file_paths, Exception[]
}
// Signal that we're done.
complete_or_canceled.Set();
User.RaiseDownloadsCompleted(file_urls, file_paths, errors);
}

/// <summary>
Expand Down
75 changes: 17 additions & 58 deletions Core/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,54 +5,26 @@

namespace CKAN
{
public delegate int DisplaySelectionDialog(string message, params object[] args);
public delegate void DisplayMessage(string message, params object[] args);
public delegate bool DisplayYesNoDialog(string message);
public delegate void DisplayError(string message, params object[] args);
public delegate void ReportProgress(string format, int percent);
public delegate void DownloadsComplete(Uri[] urls, string[] filenames, Exception[] errors);

public interface IUser
{
event DisplayYesNoDialog AskUser;
event DisplaySelectionDialog AskUserForSelection;
event DisplayMessage Message;
event DisplayError Error;
event ReportProgress Progress;
event DownloadsComplete DownloadsComplete;
int WindowWidth { get; }
bool Headless { get; }

int RaiseSelectionDialog(string message, params object[] args);
void RaiseMessage(string message, params object[] url);
void RaiseProgress(string message, int percent);
bool RaiseYesNoDialog(string question);
int RaiseSelectionDialog(string message, params object[] args);
void RaiseError(string message, params object[] args);
void RaiseDownloadsCompleted(Uri[] file_urls, string[] file_paths, Exception[] errors);

void RaiseProgress(string message, int percent);
void RaiseMessage(string message, params object[] url);
}

//Can be used in tests to supress output or as a base class for other types of user.
//It supplies no opp event handlers so that subclasses can avoid null checks.
//It supplies no op event handlers so that subclasses can avoid null checks.
public class NullUser : IUser
{
public static readonly IUser User = new NullUser();

public NullUser()
{
AskUser += DisplayYesNoDialog;
AskUserForSelection += DisplaySelectionDialog;
Message += DisplayMessage;
Error += DisplayError;
Progress += ReportProgress;
DownloadsComplete += ReportDownloadsComplete;
}

public event DisplayYesNoDialog AskUser;
public event DisplaySelectionDialog AskUserForSelection;
public event DisplayMessage Message;
public event DisplayError Error;
public event ReportProgress Progress;
public event DownloadsComplete DownloadsComplete;
public NullUser() { }

public virtual bool Headless
{
Expand All @@ -64,10 +36,6 @@ protected virtual bool DisplayYesNoDialog(string message)
return true;
}

protected virtual void DisplayMessage(string message, params object[] args)
{
}

protected virtual int DisplaySelectionDialog(string message, params object[] args)
{
return 0;
Expand All @@ -80,44 +48,35 @@ protected virtual void DisplayError(string message, params object[] args)
protected virtual void ReportProgress(string format, int percent)
{
}
protected virtual void ReportDownloadsComplete(Uri[] urls, string[] filenames, Exception[] errors)
{
}

public virtual int WindowWidth
protected virtual void DisplayMessage(string message, params object[] args)
{
get { return -1; }
}

public void RaiseMessage(string message, params object[] url)
public bool RaiseYesNoDialog(string question)
{
Message(message, url);
return DisplayYesNoDialog(question);
}

public void RaiseProgress(string message, int percent)
public int RaiseSelectionDialog(string message, params object[] args)
{
Progress(message, percent);
return DisplaySelectionDialog(message, args);
}

public bool RaiseYesNoDialog(string question)
public void RaiseError(string message, params object[] args)
{
//Return value will be from last handler added.
return AskUser(question);
DisplayError(message, args);
}

public int RaiseSelectionDialog(string message, params object[] args)
public void RaiseProgress(string message, int percent)
{
return AskUserForSelection(message, args);
ReportProgress(message, percent);
}

public void RaiseError(string message, params object[] args)
public void RaiseMessage(string message, params object[] args)
{
Error(message, args);
DisplayMessage(message, args);
}

public void RaiseDownloadsCompleted(Uri[] file_urls, string[] file_paths, Exception[] errors)
{
DownloadsComplete(file_urls, file_paths, errors);
}
}
}
10 changes: 3 additions & 7 deletions GUI/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ protected override void OnLoad(EventArgs e)

installWorker = new BackgroundWorker { WorkerReportsProgress = true, WorkerSupportsCancellation = true };
installWorker.RunWorkerCompleted += PostInstallMods;
installWorker.DoWork += InstallMods;
installWorker.DoWork += InstallMods;

var old_YesNoDialog = currentUser.displayYesNo;
currentUser.displayYesNo = YesNoDialog;
Expand Down Expand Up @@ -385,7 +385,7 @@ protected override void OnLoad(EventArgs e)
}

pluginController = new PluginController(pluginsPath, true);

CurrentInstance.RebuildKSPSubDir();

NavInit(); // initialize navigation. this should be called as late
Expand Down Expand Up @@ -1077,7 +1077,7 @@ private void FocusMod(string key, bool exactMatch, bool showAsFirst=false)
}
else
{
row_match = mod.Name.StartsWith(key, StringComparison.OrdinalIgnoreCase) ||
row_match = mod.Name.StartsWith(key, StringComparison.OrdinalIgnoreCase) ||
mod.Abbrevation.StartsWith(key, StringComparison.OrdinalIgnoreCase) ||
mod.Identifier.StartsWith(key, StringComparison.OrdinalIgnoreCase);
}
Expand Down Expand Up @@ -1237,9 +1237,5 @@ protected override void ReportProgress(string format, int percent)
Main.Instance.SetProgress(percent);
}

public override int WindowWidth
{
get { return -1; }
}
}
}
7 changes: 0 additions & 7 deletions Netkan/ConsoleUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,13 +72,6 @@ protected override void ReportProgress(string format, int percent)
Console.Write("\r\n{0}", format);
}
}
protected override void ReportDownloadsComplete(Uri[] urls, string[] filenames, Exception[] errors)
{
}
public override int WindowWidth
{
get { return Console.WindowWidth; }
}

}
}

0 comments on commit 3ac3cf6

Please sign in to comment.