-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bam. Connections are saving. We're up and running.
- Loading branch information
1 parent
6a2adf7
commit 0b07336
Showing
15 changed files
with
585 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using System.Windows; | ||
using System.Windows.Interactivity; | ||
|
||
namespace NModbus.Tools.Base.Behaviors | ||
{ | ||
/// <summary> | ||
/// Provides a simple interface for windows that should be closeable from their view model. | ||
/// </summary> | ||
public class CloseableBehavior : Behavior<Window> | ||
{ | ||
private ICloseableViewModel _closeable; | ||
|
||
/// <summary> | ||
/// Called after the behavior is attached to an AssociatedObject. | ||
/// </summary> | ||
/// <remarks> | ||
/// Override this to hook up functionality to the AssociatedObject. | ||
/// </remarks> | ||
protected override void OnAttached() | ||
{ | ||
AssociatedObject.Closing += AssociatedObject_Closing; | ||
AssociatedObject.DataContextChanged += AssociatedObject_DataContextChanged; | ||
AssociatedObject.Closed += AssociatedObject_Closed; | ||
|
||
_closeable = this.AssociatedObject.DataContext as ICloseableViewModel; | ||
|
||
if (_closeable != null) | ||
_closeable.Close += Closeable_Close; | ||
|
||
base.OnAttached(); | ||
} | ||
|
||
/// <summary> | ||
/// Called when the behavior is being detached from its AssociatedObject, but before it has actually occurred. | ||
/// </summary> | ||
/// <remarks> | ||
/// Override this to unhook functionality from the AssociatedObject. | ||
/// </remarks> | ||
protected override void OnDetaching() | ||
{ | ||
base.OnDetaching(); | ||
|
||
AssociatedObject.Closing -= AssociatedObject_Closing; | ||
AssociatedObject.DataContextChanged -= AssociatedObject_DataContextChanged; | ||
AssociatedObject.Closed -= AssociatedObject_Closed; | ||
} | ||
|
||
void AssociatedObject_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (_closeable != null) | ||
_closeable.Close -= Closeable_Close; | ||
|
||
_closeable = e.NewValue as ICloseableViewModel; | ||
|
||
if (_closeable != null) | ||
_closeable.Close += Closeable_Close; | ||
} | ||
|
||
void Closeable_Close(object sender, CloseEventArgs e) | ||
{ | ||
//https://social.msdn.microsoft.com/Forums/vstudio/en-US/c95f1acb-5dee-4670-b779-b07b06afafff/where-is-modal-property?forum=wpf | ||
if (System.Windows.Interop.ComponentDispatcher.IsThreadModal) | ||
{ | ||
AssociatedObject.DialogResult = e.DialogResult; | ||
} | ||
|
||
AssociatedObject.Close(); | ||
} | ||
|
||
void AssociatedObject_Closing(object sender, System.ComponentModel.CancelEventArgs e) | ||
{ | ||
if (_closeable != null) | ||
{ | ||
if (!_closeable.CanClose()) | ||
{ | ||
e.Cancel = true; | ||
} | ||
} | ||
} | ||
|
||
void AssociatedObject_Closed(object sender, System.EventArgs e) | ||
{ | ||
_closeable?.Closed(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
|
||
namespace NModbus.Tools.Base.Behaviors | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
public interface ICloseableViewModel | ||
{ | ||
/// <summary> | ||
/// Fired when the view model requests that its container be closed. | ||
/// </summary> | ||
event EventHandler<CloseEventArgs> Close; | ||
|
||
/// <summary> | ||
/// Returns true if the container can be closed, false otherwise. | ||
/// </summary> | ||
/// <returns></returns> | ||
bool CanClose(); | ||
|
||
/// <summary> | ||
/// Called when the view has been closed. | ||
/// </summary> | ||
void Closed(); | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public class CloseEventArgs : EventArgs | ||
{ | ||
/// <summary> | ||
/// | ||
/// </summary> | ||
/// <param name="dialogResult"></param> | ||
public CloseEventArgs(bool? dialogResult) | ||
{ | ||
this.DialogResult = dialogResult; | ||
} | ||
|
||
/// <summary> | ||
/// | ||
/// </summary> | ||
public bool? DialogResult { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using Newtonsoft.Json; | ||
using NModbus.Tools.Base.Model; | ||
using NModbus.Tools.Base.View; | ||
using NModbus.Tools.Base.ViewModel; | ||
using NModbus.Tools.Interfaces; | ||
using System; | ||
using System.IO; | ||
using System.Windows; | ||
|
||
namespace NModbus.Tools.Base | ||
{ | ||
public class ConnectionSelectionService : IConnectionSelectionService | ||
{ | ||
private static readonly string FilePath; | ||
|
||
static ConnectionSelectionService() | ||
{ | ||
FilePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "NModbus Tools", "connections.json"); | ||
} | ||
|
||
public Connection GetConnection() | ||
{ | ||
var connections = GetConnections(); | ||
|
||
var viewModel = new ConnectionSelectionViewModel(connections); | ||
|
||
var view = new ConnectionSelectionDialog | ||
{ | ||
DataContext = viewModel | ||
}; | ||
|
||
if (view.ShowDialog() == true) | ||
{ | ||
SaveConnections(viewModel.ToModel()); | ||
|
||
return viewModel.SelectedConnection.ToModel(); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
private void SaveConnections(SavedConnections connections) | ||
{ | ||
var json = JsonConvert.SerializeObject(connections); | ||
|
||
var directory = Path.GetDirectoryName(FilePath); | ||
|
||
Directory.CreateDirectory(directory); | ||
|
||
File.WriteAllText(FilePath, json); | ||
} | ||
|
||
private SavedConnections GetConnections() | ||
{ | ||
try | ||
{ | ||
var json = File.ReadAllText(FilePath); | ||
|
||
var connections = JsonConvert.DeserializeObject<SavedConnections>(json); | ||
|
||
if (connections.Connections == null) | ||
{ | ||
connections.Connections = new Connection[] { }; | ||
} | ||
|
||
return connections; | ||
} | ||
catch(Exception) | ||
{ | ||
return new SavedConnections | ||
{ | ||
Connections = new Connection[] { } | ||
}; | ||
} | ||
|
||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.