/
ViewModel.cs
84 lines (75 loc) · 2.38 KB
/
ViewModel.cs
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
namespace GithubToc
{
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
public sealed class ViewModel : INotifyPropertyChanged
{
private string? tableOfContents;
private Uri? source;
public event PropertyChangedEventHandler? PropertyChanged;
public string? TableOfContents
{
get => this.tableOfContents;
set
{
if (value == this.tableOfContents)
{
return;
}
this.tableOfContents = value;
this.OnPropertyChanged();
}
}
public Uri? Source
{
get => this.source;
set
{
if (Equals(value, this.source))
{
return;
}
this.source = value;
this.OnPropertyChanged();
this.Refresh();
}
}
public void Refresh()
{
try
{
string markdown;
switch (this.Source?.Scheme)
{
case "http":
case "https":
////using (var client = new WebClient())
////{
//// markdown = await client.DownloadStringTaskAsync(this.Source);
////}
this.TableOfContents = "not implemented";
return;
case "file":
markdown = File.ReadAllText(this.Source.LocalPath);
break;
default:
this.TableOfContents = string.Empty;
return;
}
this.TableOfContents = string.IsNullOrEmpty(markdown)
? string.Empty
: GithubToc.TableOfContents.Create(markdown);
}
catch (Exception ex)
{
this.TableOfContents = ex.Message;
}
}
private void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}