Skip to content

Commit

Permalink
Merge pull request #6 from Ko1ors/drag&drop
Browse files Browse the repository at this point in the history
Drag&Drop, Dynamic Settings, ETH Module
  • Loading branch information
Ko1ors committed May 7, 2022
2 parents d0dad20 + 89108a4 commit 2c94d1b
Show file tree
Hide file tree
Showing 48 changed files with 1,621 additions and 281 deletions.
30 changes: 29 additions & 1 deletion ETCModule/ETCLoadModule.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using ETCModule.Models;
using ETCModule.Settings;
using ETCModule.Views;
using ModularWidget;
using ModularWidget.Models;
using Prism.Ioc;
using Prism.Modularity;
using Prism.Regions;
Expand All @@ -14,16 +16,42 @@ public class ETCLoadModule : IModule
private readonly string regName = "etcregion";
private MainUC etcView = new MainUC();
private EtcInformation etcInfo;

private readonly AppSettings _appSettings;

public ETCLoadModule(AppSettings appSettings)
{
_appSettings = appSettings;
}

public void OnInitialized(IContainerProvider containerProvider)
{
InitSettings();

regionManager = containerProvider.Resolve<IRegionManager>();
Manager.RegionCreated += Manager_RegionCreated;
Manager.RegionRequest(regName);
etcInfo = new EtcInformation();
etcInfo = new EtcInformation(_appSettings);
etcInfo.Completed += UpdateView;

etcInfo.Start();
}

private void InitSettings()
{
var menu = new SettingsMenu(Constants.Menu.MenuKey, "ETC Module Settings");
menu.Parameters.Add(new SettingsParameter<string>(Constants.Parameters.Wallet, "ETC Wallet", string.Empty));
menu.Parameters.Add(new SettingsParameter<int>(Constants.Parameters.UpdateTime, "Update time (in minutes)", 5));

if (!_appSettings.MenuExists(menu.Key))
_appSettings.AddOrUpdateMenu(menu);
foreach (var parameter in menu.Parameters)
{
if (!_appSettings.ParameterExists(menu.Key, parameter.Key))
_appSettings.AddOrUpdateParameter(menu.Key, parameter);
}
}

private void UpdateView()
{
etcView.Dispatcher.Invoke(() =>
Expand Down
54 changes: 29 additions & 25 deletions ETCModule/Models/EtcInformation.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
using ETCModule.Data;
using ETCModule.Settings;
using ModularWidget;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Threading;
using System.Windows;

namespace ETCModule.Models
{
public class EtcInformation
{
private int maxTries = 5;
private readonly Object inProgress = new object();
private readonly object inProgress = new object();

public event Notify Completed;

Expand All @@ -21,21 +21,40 @@ public class EtcInformation
public string lastWalletBalance;
public EtcPrice lastEtcPrice;

private readonly string settingsPath = AppDomain.CurrentDomain.BaseDirectory + "etc-settings.json";
private readonly Double divisor = 1000000000000000000d;

private System.Timers.Timer timer;

private readonly AppSettings _appSettings;

public EtcInformation(AppSettings settings)
{
_appSettings = settings;
}

public void Start()
{
LoadSettings();
SetTimer();
Update();
Manager.UpdateRequested += Update;
}

private void SetTimer()
{
var time = _appSettings.Get<int>(Constants.Parameters.UpdateTime);
if (time <= 0)
time = 5;
timer = new System.Timers.Timer(time * 60 * 1000);
timer.Elapsed += (s, e) => Update();
timer.AutoReset = true;
timer.Enabled = true;
}

private void Update()
{
lock (inProgress)
{
if (!String.IsNullOrEmpty(etcWalletAddress))
if (!string.IsNullOrEmpty(etcWalletAddress))
if (!TryGetEtcWalletBalance())
return;
if (!TryGetEtcPrice())
Expand Down Expand Up @@ -79,33 +98,18 @@ private bool TryGetEtcWalletBalance()
private string GetNonEmptyEtcWallet()
{
string wallet = null;
while (String.IsNullOrEmpty(wallet))
while (string.IsNullOrEmpty(wallet))
{
wallet = EtcRequest.GetRandomWallets(10).Result.Find(e => !e.Balance.Equals(0) && Convert.ToDouble(e.Balance) / divisor > 1 )?.Address;
wallet = EtcRequest.GetRandomWallets(10).Result.Find(e => !e.Balance.Equals(0) && Convert.ToDouble(e.Balance) / divisor > 1)?.Address;
}
return wallet;
}

private void LoadSettings()
{
if (File.Exists(settingsPath))
{
etcWalletAddress = File.ReadAllText(settingsPath);
switch (etcWalletAddress)
{
case "default":
if (AppSettings.isLoaded)
etcWalletAddress = AppSettings.ethWallet;
else
etcWalletAddress = null;
break;
case "random":
etcWalletAddress = GetNonEmptyEtcWallet();
break;
}
}
else
File.WriteAllText(settingsPath, etcWalletAddress);
etcWalletAddress = _appSettings.Get<string>(Constants.Parameters.Wallet);
if (string.Equals(etcWalletAddress, "random", StringComparison.InvariantCultureIgnoreCase))
etcWalletAddress = GetNonEmptyEtcWallet();
}

private void OnComplete()
Expand Down
16 changes: 16 additions & 0 deletions ETCModule/Settings/Constants.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace ETCModule.Settings
{
public class Constants
{
public class Menu
{
public const string MenuKey = "etcModuleSettings";
}

public class Parameters
{
public const string Wallet = "etcWallet";
public const string UpdateTime = "etcUpdateTime";
}
}
}
17 changes: 17 additions & 0 deletions ETCModule/Settings/SettingsExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using ModularWidget;

namespace ETCModule.Settings
{
internal static class SettingsExtension
{
public static T Get<T>(this AppSettings settings, string parameterKey)
{
return settings.Get<T>(Constants.Menu.MenuKey, parameterKey);
}

public static object Get(this AppSettings settings, string parameterKey)
{
return settings.GetParameter(Constants.Menu.MenuKey, parameterKey)?.Value;
}
}
}
10 changes: 10 additions & 0 deletions ETHModule/Data/BlockReward.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace ETHModule.Data
{
public class BlockReward : Root
{
[JsonProperty("result")]
public BlockRewardResult Result;
}
}
26 changes: 26 additions & 0 deletions ETHModule/Data/BlockRewardResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using Newtonsoft.Json;
using System.Collections.Generic;

namespace ETHModule.Data
{
public class BlockRewardResult
{
[JsonProperty("blockNumber")]
public string BlockNumber;

[JsonProperty("timeStamp")]
public string TimeStamp;

[JsonProperty("blockMiner")]
public string BlockMiner;

[JsonProperty("blockReward")]
public string BlockReward;

[JsonProperty("uncles")]
public List<object> Uncles;

[JsonProperty("uncleInclusionReward")]
public string UncleInclusionReward;
}
}
13 changes: 13 additions & 0 deletions ETHModule/Data/ETHCompositeModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
namespace ETHModule.Data
{
public class ETHCompositeModel
{
public EthPrice EthPrice { get; set; }

public EthGasPrice EthGasPrice { get; set; }

public double AvgBlockReward { get; set; }

public double WalletBalance { get; set; }
}
}
10 changes: 10 additions & 0 deletions ETHModule/Data/EthGasPrice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace ETHModule.Data
{
public class EthGasPrice : Root
{
[JsonProperty("result")]
public EthGasPriceResult Result;
}
}
19 changes: 19 additions & 0 deletions ETHModule/Data/EthGasPriceResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace ETHModule.Data
{
public class EthGasPriceResult
{
[JsonProperty("LastBlock")]
public string LastBlock;

[JsonProperty("SafeGasPrice")]
public string SafeGasPrice;

[JsonProperty("ProposeGasPrice")]
public string ProposeGasPrice;

[JsonProperty("FastGasPrice")]
public string FastGasPrice;
}
}
10 changes: 10 additions & 0 deletions ETHModule/Data/EthPrice.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace ETHModule.Data
{
public class EthPrice : Root
{
[JsonProperty("result")]
public EthPriceResult Result;
}
}
19 changes: 19 additions & 0 deletions ETHModule/Data/EthPriceResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace ETHModule.Data
{
public class EthPriceResult
{
[JsonProperty("ethbtc")]
public string Ethbtc;

[JsonProperty("ethbtc_timestamp")]
public string EthbtcTimestamp;

[JsonProperty("ethusd")]
public string Ethusd;

[JsonProperty("ethusd_timestamp")]
public string EthusdTimestamp;
}
}
13 changes: 13 additions & 0 deletions ETHModule/Data/Root.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using Newtonsoft.Json;

namespace ETHModule.Data
{
public abstract class Root
{
[JsonProperty("status")]
public string Status;

[JsonProperty("message")]
public string Message;
}
}
10 changes: 10 additions & 0 deletions ETHModule/Data/WalletBalance.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using Newtonsoft.Json;

namespace ETHModule.Data
{
public class WalletBalance : Root
{
[JsonProperty("result")]
public string Result;
}
}
61 changes: 61 additions & 0 deletions ETHModule/ETHModule.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0-windows</TargetFramework>
<OutputType>Library</OutputType>
<GenerateAssemblyInfo>false</GenerateAssemblyInfo>
<UseWPF>true</UseWPF>
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
<AppendRuntimeIdentifierToOutputPath>false</AppendRuntimeIdentifierToOutputPath>
<CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<OutputPath>..\ModularWidget\bin\Debug\Modules\</OutputPath>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<OutputPath>..\ModularWidget\bin\Release\Modules\</OutputPath>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="Images\block-icon.svg" />
<None Remove="Images\ethereumLogo.svg" />
<None Remove="Images\ethwallet-icon.png" />
<None Remove="Images\ethwallet-icon2.png" />
<None Remove="Images\gas-icon.svg" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\ModularWidget\ModularWidget.csproj" />
</ItemGroup>

<ItemGroup>
<Resource Include="Images\block-icon.svg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\ethereumLogo.svg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
<Resource Include="Images\ethwallet-icon.png" />
<Resource Include="Images\ethwallet-icon2.png" />
<Resource Include="Images\gas-icon.svg">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Resource>
</ItemGroup>

<ItemGroup>
<Page Update="UserControls\BlockRewardUC.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="UserControls\EthPriceUC.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="UserControls\EthWalletBalanceUC.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
<Page Update="UserControls\GasTrackerUC.xaml">
<XamlRuntime>$(DefaultXamlRuntime)</XamlRuntime>
</Page>
</ItemGroup>
</Project>
Loading

0 comments on commit 2c94d1b

Please sign in to comment.