-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextViewPage.xaml.cs
96 lines (87 loc) · 3.06 KB
/
TextViewPage.xaml.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
85
86
87
88
89
90
91
92
93
94
95
96
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices.WindowsRuntime;
using Windows.ApplicationModel.Activation;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.Graphics.Display;
using Windows.Storage;
using Windows.Storage.Pickers;
using Windows.Storage.Provider;
using Windows.UI.ViewManagement;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;
using NGopher.Gopher;
namespace NGopher
{
/// <summary>
/// An empty page that can be used on its own or navigated to within a Frame.
/// </summary>
public sealed partial class TextViewPage : Page, IFileSavePickerContinuable
{
private GopherClient _gopher;
private string _content;
private string _selector;
public TextViewPage()
{
this.InitializeComponent();
}
/// <summary>
/// Invoked when this page is about to be displayed in a Frame.
/// </summary>
/// <param name="e">Event data that describes how this page was reached.
/// This parameter is typically used to configure the page.</param>
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
if (e.Parameter == null) return;
var server = e.Parameter as string;
var x = server.Split('\x01');
var y = x[1].Split('\x02');
_gopher = new GopherClient
{
Server = x[0],
Port = Convert.ToUInt16(y[0])
};
_selector = y[1];
SelectorTextBlock.Text = _selector.ToUpper();
var content = await _gopher.MakeTextRequest(_selector);
_content = content.ToString();
ContentTextBlock.Text = _content;
}
private void AppBarButton_Click(object sender, RoutedEventArgs e)
{
var fn = _selector.Split('/').Last().Replace(".txt", "");
var fsp = new FileSavePicker
{
SuggestedStartLocation = PickerLocationId.Downloads,
SuggestedFileName = fn
};
fsp.FileTypeChoices.Add("Plain Text", new List<string> { ".txt" });
fsp.PickSaveFileAndContinue();
}
public async void ContinueFileSavePicker(FileSavePickerContinuationEventArgs args)
{
StorageFile file = args.File;
if (file == null) return; // saving cancelled
CachedFileManager.DeferUpdates(file);
await FileIO.WriteTextAsync(file, _content); // save _content to file
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete)
{
// successfully saved file
}
else
{
// oh no
}
}
}
}