Skip to content

Commit

Permalink
Clean up the Load From Gist Dialog
Browse files Browse the repository at this point in the history
- Load list asynchronously
- Add support for Searching the result list
- Show count of Gists received on Status bar
- Add browser navigation link in the list
  • Loading branch information
RickStrahl committed Jun 9, 2019
1 parent ef0102f commit 2d222d1
Show file tree
Hide file tree
Showing 14 changed files with 176 additions and 127 deletions.
6 changes: 3 additions & 3 deletions Build/Distribution/version.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"summary": "Create and embed code snippets as Gists, and open from and save documents to Gists",
"description": "This addin allows you to take selected code in the editor, or code pasted from the clipboard and publish it as a Github Gist and embed it into the document.\r\n\r\nThe addin can also open and save documents (Markdown and code) from Gists.\r\n\r\n*** Important: In order to use this plug in and get the <script> link to render, you have to set the `EditorAllowRenderScriptTags: true` in the Markdown Monster settings. Without this setting the script tag won't render as script and Gists are not properly embedded.",
"releaseNotes": null,
"version": "0.8.5",
"minVersion": "1.11.16",
"version": "0.8.9",
"minVersion": "1.17.8",
"author": "© Rick Strahl, West Wind Technologies",
"updated": "2019-05-08T12:00:00Z"
"updated": "2019-06-09T12:00:00Z"
}
Binary file modified Build/addin.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions Build/version.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"summary": "Create and embed code snippets as Gists, and open from and save documents to Gists",
"description": "This addin allows you to take selected code in the editor, or code pasted from the clipboard and publish it as a Github Gist and embed it into the document.\r\n\r\nThe addin can also open and save documents (Markdown and code) from Gists.\r\n\r\n*** Important: In order to use this plug in and get the <script> link to render, you have to set the `EditorAllowRenderScriptTags: true` in the Markdown Monster settings. Without this setting the script tag won't render as script and Gists are not properly embedded.",
"releaseNotes": null,
"version": "0.8.5",
"minVersion": "1.11.16",
"version": "0.8.9",
"minVersion": "1.17.8",
"author": "© Rick Strahl, West Wind Technologies",
"updated": "2019-05-08T12:00:00Z"
"updated": "2019-06-09T12:00:00Z"
}
6 changes: 3 additions & 3 deletions GistIntegrationAddin/GistClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,11 @@ public static bool DeleteGist(string id, string githubUserToken = null)
/// Retrieves a list of recent gists from a given user
/// </summary>
/// <param name="userId"></param>
public static List<GistItem> ListGistsForUser(string userId, string githubToken = null)
public static async Task<List<GistItem>> ListGistsForUserAsync(string userId, string githubToken = null)
{
var settings = new HttpRequestSettings
{
Url = GistUrl + $"/users/{userId}/gists",
Url = GistUrl + $"/users/{userId}/gists?per_page=100",
HttpVerb = "GET"
};
settings.Headers.Add("User-agent", "Markdown Monster Markdown Editor Gist Add-in");
Expand All @@ -273,7 +273,7 @@ public static List<GistItem> ListGistsForUser(string userId, string githubToken
List<GistItem> gists = new List<GistItem>();
try
{
var giststructs = HttpUtils.JsonRequest<List<GistStructure>>(settings);
var giststructs = await HttpUtils.JsonRequestAsync<List<GistStructure>>(settings);

foreach (var giststruct in giststructs)
{
Expand Down
87 changes: 74 additions & 13 deletions GistIntegrationAddin/LoadAndSaveGistModel.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,16 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Threading;
using FontAwesome.WPF;
using GistIntegration.Annotations;
using MahApps.Metro.Controls.Dialogs;
using MarkdownMonster;
using MarkdownMonster.Windows;
using Westwind.Utilities;

namespace GistIntegration
{
Expand Down Expand Up @@ -42,6 +40,65 @@ public PasteCodeAsGistConfiguration Configuration
private PasteCodeAsGistConfiguration _configuration;



/// <summary>
/// The search term on the form that filters the list
/// </summary>
public string SearchTerm
{
get { return _SearchTerm; }
set
{
if (value == _SearchTerm) return;
_SearchTerm = value;

OnPropertyChanged(nameof(SearchTerm));
OnPropertyChanged(nameof(GistList));
OnPropertyChanged(nameof(FilteredGistList));
}
}
private string _SearchTerm;



public string ListCount
{
get
{
if (_gistList == null || _gistList.Count < 1)
return "No Gists";

if (_gistList.Count == 1)
return "1 Gist";

return $"{_gistList.Count} Gists";
}
}


public ObservableCollection<GistItem> FilteredGistList
{
get
{
if (string.IsNullOrEmpty(SearchTerm))
{
OnPropertyChanged(nameof(ListCount));
return _gistList;
}

var lst = _gistList.Where(g =>
StringUtils.Contains(g.filename, SearchTerm, StringComparison.InvariantCultureIgnoreCase) ||
StringUtils.Contains(g.description, SearchTerm,
StringComparison.InvariantCultureIgnoreCase));


var col = new ObservableCollection<GistItem>(lst);

OnPropertyChanged(nameof(ListCount));
return col;
}
}

public ObservableCollection<GistItem> GistList
{
get { return _gistList; }
Expand All @@ -50,12 +107,15 @@ public ObservableCollection<GistItem> GistList
if (Equals(value, _gistList)) return;
_gistList = value;
OnPropertyChanged();
OnPropertyChanged(nameof(FilteredGistList));
OnPropertyChanged(nameof(ListCount));
}
}

private ObservableCollection<GistItem> _gistList = new ObservableCollection<GistItem>();




public GistItem ActiveItem
{
get { return _activeItem; }
Expand Down Expand Up @@ -108,7 +168,7 @@ public string GistUsername
/// Loads gists with progress info
/// </summary>
/// <param name="loadOrSaveWindow"></param>
public void LoadGists(dynamic loadOrSaveWindow)
public async Task LoadGists(dynamic loadOrSaveWindow)
{
ObservableCollection<GistItem> gists;

Expand All @@ -120,17 +180,18 @@ public void LoadGists(dynamic loadOrSaveWindow)

dynamic window = loadOrSaveWindow;

window.ShowStatus("Retrieving Gists from Github...");

gists = new ObservableCollection<GistItem>(GistClient.ListGistsForUser(GistUsername,
window.Status.ShowStatusProgress("Retrieving Gists from Github...");
gists = new ObservableCollection<GistItem>(await GistClient.ListGistsForUserAsync(GistUsername,
Configuration.GithubUserToken));

if (gists == null || gists.Count < 1 || gists[0].hasError)
{
window.SetStatusIcon(FontAwesomeIcon.Warning, Colors.Orange);
window.ShowStatus("Failed to retrieve Gists from Github...", 7000);

window.Status.ShowStatusError("Failed to retrieve Gists from Github...");
return;
}
window.ShowStatus("Retrieved Gists from Github.", 5000);
window.Status.ShowStatusSuccess($"Retrieved {gists.Count} Gists from Github for {GistUsername}.");

foreach (var gist in gists)
{
Expand Down
48 changes: 38 additions & 10 deletions GistIntegrationAddin/LoadGistWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,34 @@
</Button>
</Grid>

<Label Margin="0,5,0,0" Grid.Row="2">Git Username</Label>
<TextBox Name="TextGistUsername" Text="{Binding GistUsername}" FontSize="15"/>
<Grid Margin="0,12,0,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="200"/>
</Grid.ColumnDefinitions>

<StackPanel Margin="0">
<TextBox Name="TextGistUsername"
Text="{Binding GistUsername,UpdateSourceTrigger=PropertyChanged,Delay=800}"
controls:TextBoxHelper.Watermark="Github Username"
FontSize="15"/>
</StackPanel>

<StackPanel Grid.Column="1" >
<TextBox Name="TextSearch" Margin="10,0,0,0"
Text="{Binding SearchTerm, UpdateSourceTrigger=PropertyChanged,Delay=200}"
controls:TextBoxHelper.Watermark="Search"
FontSize="15"/>
</StackPanel>
</Grid>
</StackPanel>

<ListBox Name="ListGists" IsSynchronizedWithCurrentItem="True" Margin="0,12,0,20" Grid.Row="1"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
SelectedItem="{Binding ActiveItem}" BorderBrush="#999" BorderThickness="1"
MouseDoubleClick="ListGists_MouseDoubleClick"
ItemsSource="{Binding GistList}">
ItemsSource="{Binding FilteredGistList}">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid Margin="5,3,15,7">
Expand All @@ -75,24 +93,32 @@
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>

<TextBlock Text="{Binding description}" FontWeight="DemiBold" Grid.Row="0"/>
<Button fa:Awesome.Content="Remove" Foreground="DarkRed" BorderThickness="0" FontSize="15"
<Button fa:Awesome.Content="ExternalLink" Foreground="Goldenrod" BorderThickness="0" FontSize="15"
Grid.Column="1" Height="15" MinHeight="20" Width="20"
Background="Transparent"
ToolTip="Open Gist in Browser"
Padding="0" Margin="5,-1,0,0"
Click="ButtonBrowseToGist_Click"/>
<Button fa:Awesome.Content="Remove" Foreground="IndianRed" BorderThickness="0" FontSize="15"
Grid.Column="2" Height="15" MinHeight="20" Width="20"
Background="Transparent"
ToolTip="Delete Gist on Github"
Padding="0" Margin="5,-1,0,0"
Click="ButtonDeleteGist_Click"/>
</Grid>

<Grid Grid.Row="1">
<Grid.ColumnDefinitions>
<Grid Grid.Row="1" Opacity="0.65">
<Grid.ColumnDefinitions >
<ColumnDefinition Width="*" />
<ColumnDefinition Width="auto" />
</Grid.ColumnDefinitions>


<TextBlock Text="{Binding filename}" FontStyle="Italic" Grid.Column="0" FontSize="11"/>
<TextBlock Text="{Binding filename}" FontStyle="Italic" Grid.Column="0" FontSize="11"/>
<TextBlock Text="{Binding updated}" FontStyle="Italic" Grid.Column="1" FontSize="11"/>
</Grid>
</Grid>
Expand All @@ -102,7 +128,7 @@
</Grid>


<StatusBar Grid.Row="1" Height="30" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" FontSize="12">
<StatusBar Grid.Row="1" Padding="2" VerticalAlignment="Bottom" HorizontalAlignment="Stretch" FontSize="13.5">
<StatusBar.ItemsPanel>
<ItemsPanelTemplate>
<Grid>
Expand All @@ -121,8 +147,10 @@
<StatusBarItem Grid.Column="1">
<TextBlock Name="StatusText" x:FieldModifier="public" HorizontalAlignment="Left">Ready</TextBlock>
</StatusBarItem>
<StatusBarItem Grid.Column="2">
<TextBlock Name="StatusImageSize" x:FieldModifier="public" HorizontalAlignment="Left"></TextBlock>
<StatusBarItem Grid.Column="2" Margin="0,0,10,0">
<TextBlock Name="StatusListCount"
Text="{Binding ListCount}"
x:FieldModifier="public" HorizontalAlignment="Left"></TextBlock>
</StatusBarItem>

</StatusBar>
Expand Down
Loading

0 comments on commit 2d222d1

Please sign in to comment.