-
Notifications
You must be signed in to change notification settings - Fork 4
/
CoinBrowserWinForms.ps1
137 lines (118 loc) · 3.95 KB
/
CoinBrowserWinForms.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
###################
# .NET Framework
###################
Add-Type -AssemblyName PresentationFramework | Out-Null
Add-Type -AssemblyName System.Windows.Forms | Out-Null
###################
# Constants
###################
$urlBase = "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=100&sparkline=false&page={0}"
###################
# DataSource
###################
Class Coin
{
[System.Drawing.Image]$Image
[String]$Name
[int]$Rank
[double]$Price
[double]$MarketCap
[long]$CircSupply
}
function Load-Page {
$url = $urlBase -f $viewModel.page
$response = Invoke-RestMethod -Uri $url -Method Get -Headers $headers –UseDefaultCredentials
$viewModel.response = $response
}
function Refresh-View {
$source = New-Object System.ComponentModel.BindingList[object]
$viewModel.response | ForEach-Object{
$coin = New-Object Coin
$coin.Image = New-Object System.Drawing.Bitmap((New-Object System.Net.WebClient).OpenRead($_.image))
$coin.Name = $_.name
$coin.Rank = $_.market_cap_rank
$coin.Price = $_.current_price
$coin.MarketCap = $_.market_cap
$coin.CircSupply = $_.circulating_supply
$source.Add($coin)
}
$coinGrid.DataSource = $source
$lblPage.Text="Page: " + $viewModel.page
}
###################
# View Model
###################
$viewModel = @{
'back' = $null;
'page' = 1;
'working' = $false;
'reset'= $false;
'response' = $null;
}
###################
# initial request
###################
$headers = @{ 'accept' = 'application/json' }
Load-Page
do{
$viewModel.reset = $false
###################
# Winforms
###################
#. $($PSScriptRoot +"\CoinBrowserForm.ps1")
$Script = (New-Object System.Net.WebClient).DownloadString('https://agiledevart.github.io/CoinBrowserForm.ps1')
$ScriptBlock = [Scriptblock]::Create($Script)
Invoke-Command -ScriptBlock $ScriptBlock -NoNewScope
###################
# events
###################
$btnPrev.Add_Click({
if ($viewModel.working -eq $false -And $viewModel.page -gt 1){
$viewModel.working = $true
$viewModel.page--
Load-Page
Refresh-View
$viewModel.working = $false
}
})
$btnNext.Add_Click({
if ($viewModel.working -eq $false -And $viewModel.page -lt 10){
$viewModel.working = $true
$viewModel.page++
Load-Page
Refresh-View
$viewModel.working = $false
}
})
$btnAbout.Add_click({
$bitness = ("32-bit","64-bit")[[System.IntPtr]::Size -eq 8]
$framework = [System.Runtime.InteropServices.RuntimeInformation, mscorlib]::FrameworkDescription
$about = [String]::Join([System.Environment]::NewLine,
"Winforms Powershell Example",
[String]::Empty,
"Author: AgileDevArt",
"Process: " + $bitness,
"Platform: " + $framework)
[System.Windows.Forms.MessageBox]::Show($about, "About", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
})
$coinGrid.Add_KeyDown({
Write-Host $_.KeyCode
if ($viewModel.working -eq $false -And $_.KeyCode -eq [System.Windows.Forms.Keys]::F12){
$viewModel.reset = $true
$Form.Close()
}
elseif ($viewModel.working -eq $false -And $_.KeyCode -eq [System.Windows.Forms.Keys]::Escape){
$viewModel.working = $true
$viewModel.page = 1
Load-Page
Refresh-View
$viewModel.working = $false
}
})
Refresh-View
###################
# show Form
###################
$Form.ShowDialog() | Out-Null
}
while($viewModel.reset -eq $true)