Skip to content

Commit 106b120

Browse files
committed
Add SnippetOnline
1 parent 7c00079 commit 106b120

File tree

9 files changed

+274
-14
lines changed

9 files changed

+274
-14
lines changed

CodeHubDesktop/Data/AppConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ internal class AppConfig
99

1010
public string Lang { get; set; } = "fa-IR";
1111
public bool IsModernStyle { get; set; } = false;
12+
public bool StoreSnippet { get; set; } = false;
1213
public SkinType Skin { get; set; } = SkinType.Default;
1314
}
1415
}

CodeHubDesktop/MultiLanguage/Language/Lang.Designer.cs

Lines changed: 63 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CodeHubDesktop/MultiLanguage/Language/Lang.fa-IR.resx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168
<data name="DownloadNewVer" xml:space="preserve">
169169
<value>دانلود بروزرسانی جدید</value>
170170
</data>
171+
<data name="General" xml:space="preserve">
172+
<value>عمومی</value>
173+
</data>
171174
<data name="Github" xml:space="preserve">
172175
<value>کدهاب دسکتاپ</value>
173176
</data>
@@ -177,6 +180,15 @@
177180
<data name="HamburgerMenuHeaderItem" xml:space="preserve">
178181
<value>کدهاب 1.0.0</value>
179182
</data>
183+
<data name="HistoryDate" xml:space="preserve">
184+
<value>تاریخ انتشار</value>
185+
</data>
186+
<data name="HistoryLanguage" xml:space="preserve">
187+
<value>زبان</value>
188+
</data>
189+
<data name="HistoryLink" xml:space="preserve">
190+
<value>لینک</value>
191+
</data>
180192
<data name="LatestVersion" xml:space="preserve">
181193
<value>شما از آخرین نسخه استفاده میکنید</value>
182194
</data>
@@ -198,6 +210,12 @@
198210
<data name="Published" xml:space="preserve">
199211
<value>انتشار در</value>
200212
</data>
213+
<data name="SearchbarPlaceHolder" xml:space="preserve">
214+
<value>آیدی اسنیپت یا لینک...</value>
215+
</data>
216+
<data name="SearchbarTitle" xml:space="preserve">
217+
<value>دریافت اسنیپت با آیدی یا لینک</value>
218+
</data>
201219
<data name="SelectLanguage" xml:space="preserve">
202220
<value>زبان خودتان را انتخاب کنید...</value>
203221
</data>
@@ -207,6 +225,9 @@
207225
<data name="Settings" xml:space="preserve">
208226
<value>تنظیمات</value>
209227
</data>
228+
<data name="SettingStoreSnippet" xml:space="preserve">
229+
<value>ذخیره اسنیپت ها</value>
230+
</data>
210231
<data name="ShieldSubject" xml:space="preserve">
211232
<value>لینک به حافظه کپی شد</value>
212233
</data>

CodeHubDesktop/MultiLanguage/Language/Lang.resx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,9 @@ Desktop Version Work with CodeHub API.</value>
169169
<data name="DownloadNewVer" xml:space="preserve">
170170
<value>Download New Update</value>
171171
</data>
172+
<data name="General" xml:space="preserve">
173+
<value>General</value>
174+
</data>
172175
<data name="Github" xml:space="preserve">
173176
<value>CodeHub Desktop</value>
174177
</data>
@@ -178,6 +181,15 @@ Desktop Version Work with CodeHub API.</value>
178181
<data name="HamburgerMenuHeaderItem" xml:space="preserve">
179182
<value>Code Hub v1.0.0</value>
180183
</data>
184+
<data name="HistoryDate" xml:space="preserve">
185+
<value>Publish Date</value>
186+
</data>
187+
<data name="HistoryLanguage" xml:space="preserve">
188+
<value>Language</value>
189+
</data>
190+
<data name="HistoryLink" xml:space="preserve">
191+
<value>Link</value>
192+
</data>
181193
<data name="LatestVersion" xml:space="preserve">
182194
<value>You are using Latest Version</value>
183195
</data>
@@ -199,6 +211,12 @@ Desktop Version Work with CodeHub API.</value>
199211
<data name="Published" xml:space="preserve">
200212
<value>Published at</value>
201213
</data>
214+
<data name="SearchbarPlaceHolder" xml:space="preserve">
215+
<value>Enter Snippet Id or Url...</value>
216+
</data>
217+
<data name="SearchbarTitle" xml:space="preserve">
218+
<value>Get Snippet By Id or Url</value>
219+
</data>
202220
<data name="SelectLanguage" xml:space="preserve">
203221
<value>Choose your Language...</value>
204222
</data>
@@ -208,6 +226,9 @@ Desktop Version Work with CodeHub API.</value>
208226
<data name="Settings" xml:space="preserve">
209227
<value>Settings</value>
210228
</data>
229+
<data name="SettingStoreSnippet" xml:space="preserve">
230+
<value>Save Snippets</value>
231+
</data>
211232
<data name="ShieldSubject" xml:space="preserve">
212233
<value>Link Copied to Clipborad</value>
213234
</data>
Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,44 @@
1-
using Prism.Mvvm;
1+
using Prism.Commands;
2+
using Prism.Mvvm;
3+
using System;
4+
using System.Windows;
25

36
namespace CodeHubDesktop.ViewModels
47
{
58
public class SettingsViewModel : BindableBase
69
{
10+
public static SettingsViewModel Instance;
11+
private HorizontalAlignment _ContentAlignment;
12+
public HorizontalAlignment ContentAlignment
13+
{
14+
get { return _ContentAlignment; }
15+
set { SetProperty(ref _ContentAlignment, value); }
16+
}
17+
18+
private bool _GetIsCheckedToggle;
19+
public bool GetIsCheckedToggle
20+
{
21+
get { return _GetIsCheckedToggle; }
22+
set { SetProperty(ref _GetIsCheckedToggle, value); }
23+
}
24+
25+
public DelegateCommand<object> StoreSnippetCommand { get; private set; }
26+
727
public SettingsViewModel()
828
{
29+
Instance = this;
30+
StoreSnippetCommand = new DelegateCommand<object>(OnStoreSnippet);
931

32+
GetIsCheckedToggle = GlobalData.Config.StoreSnippet;
33+
}
34+
35+
private void OnStoreSnippet(object isChecked)
36+
{
37+
if ((bool)isChecked != GlobalData.Config.StoreSnippet)
38+
{
39+
GlobalData.Config.StoreSnippet = (bool)isChecked;
40+
GlobalData.Save();
41+
}
1042
}
1143
}
1244
}

CodeHubDesktop/ViewModels/SnippetHistoryViewModel.cs

Lines changed: 105 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,115 @@
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;
210

311
namespace CodeHubDesktop.ViewModels
412
{
513
public class SnippetHistoryViewModel : BindableBase
614
{
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
773
public SnippetHistoryViewModel()
874
{
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+
}
9113

10114
}
11115
}

CodeHubDesktop/Views/Settings.xaml

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
11
<UserControl x:Class="CodeHubDesktop.Views.Settings"
22
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
33
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4-
xmlns:prism="http://prismlibrary.com/"
4+
xmlns:prism="http://prismlibrary.com/"
5+
xmlns:hc="https://handyorg.github.io/handycontrol"
6+
xmlns:lang="clr-namespace:CodeHubDesktop.MultiLanguage.Language"
7+
xmlns:ext="clr-namespace:CodeHubDesktop.DynamicLanguage" xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
8+
ext:Translation.ResourceManager="{x:Static lang:Lang.ResourceManager}"
59
prism:ViewModelLocator.AutoWireViewModel="True">
6-
<Grid>
7-
8-
</Grid>
10+
<hc:TransitioningContentControl>
11+
<StackPanel>
12+
<hc:Divider Content="{ext:Loc General}"/>
13+
<ToggleButton x:Name="tg" Style="{StaticResource ToggleButtonSwitch}" Content="{ext:Loc SettingStoreSnippet}" IsChecked="{Binding GetIsCheckedToggle}" Command="{Binding StoreSnippetCommand}" CommandParameter="{Binding IsChecked, RelativeSource={RelativeSource Self}}" />
14+
</StackPanel>
15+
</hc:TransitioningContentControl>
916
</UserControl>

CodeHubDesktop/Views/Settings.xaml.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ public partial class Settings : UserControl
1010
public Settings()
1111
{
1212
InitializeComponent();
13+
14+
if (GlobalData.Config.Lang.Equals("fa-IR"))
15+
tg.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
16+
else
17+
tg.HorizontalAlignment = System.Windows.HorizontalAlignment.Right;
1318
}
1419
}
1520
}

0 commit comments

Comments
 (0)