Skip to content

Commit

Permalink
Source Link & Save Image
Browse files Browse the repository at this point in the history
  • Loading branch information
CDillinger committed Jul 31, 2014
1 parent 0297fee commit a3cd0be
Show file tree
Hide file tree
Showing 7 changed files with 109 additions and 42 deletions.
6 changes: 4 additions & 2 deletions Source/RedditVisualizer/Helpers/Posts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,11 @@ class Posts
catch
{
p.PostType = RedditPost.URLType.Other;
}
}

p.Data.NonCachedURL = p.Data.URL;
}

return new Tuple<List<RedditPost>, string, string>(posts, before, after);
}

Expand Down
12 changes: 7 additions & 5 deletions Source/RedditVisualizer/Models/RedditPost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ public class LinkData

public string Author { get; set; }

public string Thumbnail { get; set; }

public string Permalink { get; set; }

public string URL { get; set; }
public string Thumbnail { get; set; }

public string Permalink { get; set; }

public string URL { get; set; }

public string NonCachedURL { get; set; }

public string Domain { get; set; }

Expand Down
8 changes: 4 additions & 4 deletions Source/RedditVisualizer/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RedditVisualizer")]
[assembly: AssemblyTitle("Reddit Visualizer")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RedditVisualizer")]
[assembly: AssemblyCopyright("Copyright © 2014")]
[assembly: AssemblyCompany("Collin Dillinger")]
[assembly: AssemblyProduct("Reddit Visualizer")]
[assembly: AssemblyCopyright("Copyright © Collin Dillinger 2014")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

Expand Down
2 changes: 1 addition & 1 deletion Source/RedditVisualizer/RedditVisualizer.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<OutputType>WinExe</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RedditVisualizer</RootNamespace>
<AssemblyName>RedditVisualizer</AssemblyName>
<AssemblyName>Reddit Visualizer</AssemblyName>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Expand Down
73 changes: 55 additions & 18 deletions Source/RedditVisualizer/ViewModels/MainWindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics;
using Microsoft.Win32;
using RedditVisualizer.Helpers;
using RedditVisualizer.Models;
using RedditVisualizer.Views;
Expand Down Expand Up @@ -106,13 +107,48 @@ public enum PostType
SelfText
}

#endregion

#region Methods

public void OpenFeatured()
{
Process.Start("http://www.reddit.com" + FeaturedPost.Data.Permalink);
#endregion

#region Methods

public void OpenFeatured()
{
Process.Start("http://www.reddit.com" + FeaturedPost.Data.Permalink);
}

public void OpenLink()
{
Process.Start(FeaturedPost.Data.NonCachedURL);
}

public async Task SaveImageAs()
{
var dialog = new SaveFileDialog
{
FileName = FeaturedPost.Data.NonCachedURL.Substring(FeaturedPost.Data.NonCachedURL.LastIndexOf('/') + 1)
};
switch (FeaturedPost.PostType)
{
case RedditPost.URLType.GIF:
dialog.Filter = "GIF Image (*.gif) | *.gif";
break;
case RedditPost.URLType.Image:
if (FeaturedPost.Data.NonCachedURL.EndsWith(".png"))
dialog.Filter = "PNG Files (*.png) | *.png";
else if (FeaturedPost.Data.NonCachedURL.EndsWith(".jpg") || FeaturedPost.Data.NonCachedURL.EndsWith(".jpg"))
dialog.Filter = "JPG Files (*.jpg) | *.jpg";
else
{
var extension = FeaturedPost.Data.NonCachedURL.Substring(FeaturedPost.Data.NonCachedURL.LastIndexOf('.') + 1);
dialog.Filter = string.Format("{0} Files (*.{1}) | *.{1}", extension.ToUpper(), extension.ToLower());
}
break;
}
var success = dialog.ShowDialog();
if (success == null || success == false)
return;

await Helpers.CacheImage.SaveImageAsync(FeaturedPost.Data.NonCachedURL, dialog.FileName);
}

private static async Task CacheImageLocally(RedditPost post)
Expand Down Expand Up @@ -377,19 +413,20 @@ public async void GoToNextPost()

public async Task CheckPostStuff()
{
await CacheImageLocally(FeaturedPost);

if (FeaturedPost != null)
Window.GoToButton.IsEnabled = true;
await CacheImageLocally(FeaturedPost);

if (FeaturedPost != null)
{
Window.GoToButton.IsEnabled = true;
Window.OpenLinkButton.IsEnabled = FeaturedPost.PostType != RedditPost.URLType.SelfText;
Window.SaveImageButton.IsEnabled = FeaturedPost.PostType == RedditPost.URLType.Image || FeaturedPost.PostType == RedditPost.URLType.GIF;
}
else
Window.GoToButton.IsEnabled = false;
Window.GoToButton.IsEnabled = Window.OpenLinkButton.IsEnabled = Window.SaveImageButton.IsEnabled = false;

if (Posts.IndexOf(FeaturedPost) < 1)
Window.PreviousImageButton.IsEnabled = false;
else
Window.PreviousImageButton.IsEnabled = true;
Window.PreviousImageButton.IsEnabled = Posts.IndexOf(FeaturedPost) >= 1;

if (Posts.IndexOf(FeaturedPost) >= Posts.Count - 1 && _loading == true)
if (Posts.IndexOf(FeaturedPost) >= Posts.Count - 1 && _loading)
{
Window.NextImageButton.IsEnabled = false;
Window.NextImageButton.Content = "Fetching More Posts...";
Expand Down
23 changes: 17 additions & 6 deletions Source/RedditVisualizer/Views/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:elements="clr-namespace:RedditVisualizer.Views.Elements"
xmlns:gif="http://wpfanimatedgif.codeplex.com"
Title="Reddit Visualizer" Height="350" Width="525" KeyDown="Window_KeyDown" MouseMove="Window_MouseMove">
Title="Reddit Visualizer" MinHeight="350" MinWidth="525" KeyDown="Window_KeyDown" MouseMove="Window_MouseMove" Height="535" Width="768">
<Grid Margin="10,10,10,10">
<Grid>
<Grid.RowDefinitions>
Expand Down Expand Up @@ -113,7 +113,18 @@
<Line Y1="5" Y2="20" Stroke="Black"/>
</StackPanel>
</elements:AlignableWrapPanel>
<Button x:Name="GoToButton" Grid.ColumnSpan="3" Grid.Row="7" Content="Go To Post" IsEnabled="False" Click="GoToButton_Click"/>
<Grid Grid.Column="0" Grid.ColumnSpan="3" Grid.Row="7">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="2*"/>
<ColumnDefinition Width="5"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Button x:Name="SaveImageButton" Grid.Column="0" Content="Save Image" IsEnabled="False" Click="SaveImageButton_OnClickButton_Click"/>
<Button x:Name="GoToButton" Grid.Column="2" Content="Go To Post" IsEnabled="False" Click="GoToButton_Click"/>
<Button x:Name="OpenLinkButton" Grid.Column="4" Content="Open Link" IsEnabled="False" Click="OpenLinkButton_OnClickButton_Click"/>
</Grid>
</Grid>
</Grid>
<Grid>
Expand All @@ -126,10 +137,10 @@
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Image x:Name="LeftArrowImage" Grid.Row="1" Grid.Column="0" Opacity="0.3" Source="/RedditVisualizer;component/Media/LeftArrow.png" Height="50" HorizontalAlignment="Left" VerticalAlignment="Center" />
<Image x:Name="RightArrowImage" Grid.Row="1" Grid.Column="1" Opacity="0.3" Source="/RedditVisualizer;component/Media/RightArrow.png" Height="50" HorizontalAlignment="Right" VerticalAlignment="Center" />
<Button x:Name="PreviousImageButton" Grid.Row="1" Grid.Column="0" Opacity="0" Click="PreviousImageButton_Click"/>
<Button x:Name="NextImageButton" Grid.Row="1" Grid.Column="1" Opacity="0" Click="NextImageButton_Click"/>
<Image x:Name="LeftArrowImage" Grid.Row="1" Grid.Column="0" Opacity="0.3" Source="/Reddit Visualizer;component/Media/LeftArrow.png" Height="50" HorizontalAlignment="Left" VerticalAlignment="Center" />
<Image x:Name="RightArrowImage" Grid.Row="1" Grid.Column="1" Opacity="0.3" Source="/Reddit Visualizer;component/Media/RightArrow.png" Height="50" HorizontalAlignment="Right" VerticalAlignment="Center" />
<Button x:Name="PreviousImageButton" Grid.Row="1" Grid.Column="0" Opacity="0" Click="PreviousImageButton_Click" ContextMenuService.ShowOnDisabled="True"/>
<Button x:Name="NextImageButton" Grid.Row="1" Grid.Column="1" Opacity="0" Click="NextImageButton_Click" ContextMenuService.ShowOnDisabled="True"/>
</Grid>
</Grid>
</Window>
27 changes: 21 additions & 6 deletions Source/RedditVisualizer/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public partial class MainWindow : Window

public MainWindow()
{
InitializeComponent();
InitializeComponent();

DataContext = ViewModel = new MainWindowViewModel(this);
}
Expand All @@ -38,16 +38,31 @@ private async void GetPostsButton_Click(object sender, RoutedEventArgs e)

private void GoToButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.OpenFeatured();
ViewModel.OpenFeatured();
}

private async void SaveImageButton_OnClickButton_Click(object sender, RoutedEventArgs e)
{
await ViewModel.SaveImageAs();
}

private void OpenLinkButton_OnClickButton_Click(object sender, RoutedEventArgs e)
{
ViewModel.OpenLink();
}

private void PreviousImageButton_Click(object sender, RoutedEventArgs e)
{
{

if (ViewModel.FeaturedPost == null)
return;
ViewModel.GoToPreviousPost();
}

private void NextImageButton_Click(object sender, RoutedEventArgs e)
{
private void NextImageButton_Click(object sender, RoutedEventArgs e)
{
if (ViewModel.FeaturedPost == null)
return;
ViewModel.GoToNextPost();
}

Expand Down Expand Up @@ -190,6 +205,6 @@ private void Window_MouseMove(object sender, MouseEventArgs e)
LeftArrowImage.Opacity = 0.4;
RightArrowImage.Opacity = 0.4;
}
}
}
}
}

0 comments on commit a3cd0be

Please sign in to comment.