|
1 | | -using Prism.Mvvm; |
| 1 | +using CodeHubDesktop.Models; |
| 2 | +using HandyControl.Controls; |
| 3 | +using HandyControl.Data; |
| 4 | +using Newtonsoft.Json; |
| 5 | +using Prism.Commands; |
| 6 | +using Prism.Mvvm; |
| 7 | +using System; |
| 8 | +using System.Net.Http; |
| 9 | +using System.Windows; |
2 | 10 |
|
3 | 11 | namespace CodeHubDesktop.ViewModels |
4 | 12 | { |
5 | 13 | public class SnippetHistoryViewModel : BindableBase |
6 | 14 | { |
| 15 | + #region OnlineSnipet |
| 16 | + private string _SearchText; |
| 17 | + public string SearchText |
| 18 | + { |
| 19 | + get { return _SearchText; } |
| 20 | + set { SetProperty(ref _SearchText, value); } |
| 21 | + } |
| 22 | + |
| 23 | + private string _SnippetLanguage; |
| 24 | + public string SnippetLanguage |
| 25 | + { |
| 26 | + get { return _SnippetLanguage; } |
| 27 | + set { SetProperty(ref _SnippetLanguage, value); } |
| 28 | + } |
| 29 | + |
| 30 | + private string _SnippetDate; |
| 31 | + public string SnippetDate |
| 32 | + { |
| 33 | + get { return _SnippetDate; } |
| 34 | + set { SetProperty(ref _SnippetDate, value); } |
| 35 | + } |
| 36 | + |
| 37 | + private string _SnippetUrl; |
| 38 | + public string SnippetUrl |
| 39 | + { |
| 40 | + get { return _SnippetUrl; } |
| 41 | + set { SetProperty(ref _SnippetUrl, value); } |
| 42 | + } |
| 43 | + |
| 44 | + private string _SnippetTitle; |
| 45 | + public string SnippetTitle |
| 46 | + { |
| 47 | + get { return _SnippetTitle; } |
| 48 | + set { SetProperty(ref _SnippetTitle, value); } |
| 49 | + } |
| 50 | + private string _SnippetDetail; |
| 51 | + public string SnippetDetail |
| 52 | + { |
| 53 | + get { return _SnippetDetail; } |
| 54 | + set { SetProperty(ref _SnippetDetail, value); } |
| 55 | + } |
| 56 | + |
| 57 | + private string _SnippetScript; |
| 58 | + public string SnippetScript |
| 59 | + { |
| 60 | + get { return _SnippetScript; } |
| 61 | + set { SetProperty(ref _SnippetScript, value); } |
| 62 | + } |
| 63 | + |
| 64 | + private Visibility _PanelVisibility = Visibility.Hidden; |
| 65 | + public Visibility PanelVisibility |
| 66 | + { |
| 67 | + get { return _PanelVisibility; } |
| 68 | + set { SetProperty(ref _PanelVisibility, value); } |
| 69 | + } |
| 70 | + public DelegateCommand<FunctionEventArgs<string>> OnSearchStartedCommand { get; private set; } |
| 71 | + |
| 72 | + #endregion |
7 | 73 | public SnippetHistoryViewModel() |
8 | 74 | { |
| 75 | + OnSearchStartedCommand = new DelegateCommand<FunctionEventArgs<string>>(OnSearchStarted); |
| 76 | + } |
| 77 | + |
| 78 | + private async void OnSearchStarted(FunctionEventArgs<string> e) |
| 79 | + { |
| 80 | + try |
| 81 | + { |
| 82 | + PanelVisibility = Visibility.Hidden; |
| 83 | + if (string.IsNullOrEmpty(SearchText)) |
| 84 | + { |
| 85 | + return; |
| 86 | + } |
| 87 | + string url = string.Empty; |
| 88 | + if (SearchText.StartsWith("http")) |
| 89 | + { |
| 90 | + url = SearchText.Replace("snippet", "api/v1/snippet"); |
| 91 | + } |
| 92 | + else |
| 93 | + { |
| 94 | + url = $"http://codehub.pythonanywhere.com/api/v1/snippet/{SearchText}"; |
| 95 | + } |
| 96 | + using var client = new HttpClient(); |
| 97 | + HttpResponseMessage response = await client.GetAsync(url); |
| 98 | + response.EnsureSuccessStatusCode(); |
| 99 | + var resp = await response.Content.ReadAsStringAsync(); |
| 100 | + var parse = JsonConvert.DeserializeObject<GetSnippetModel>(resp); |
| 101 | + SnippetTitle = parse.title; |
| 102 | + SnippetDetail = parse.detail; |
| 103 | + SnippetDate = parse.pub_date; |
| 104 | + SnippetScript = parse.script; |
| 105 | + SnippetUrl = parse.link; |
| 106 | + SnippetLanguage = parse.language; |
| 107 | + PanelVisibility = Visibility.Visible; |
| 108 | + } |
| 109 | + catch (Exception ex) |
| 110 | + { |
| 111 | + Growl.Error(ex.Message); |
| 112 | + } |
9 | 113 |
|
10 | 114 | } |
11 | 115 | } |
|
0 commit comments