Skip to content

Commit

Permalink
Rewrite to use new webAPI
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxJohansen committed Jul 4, 2018
1 parent 96177a0 commit cdeb67f
Show file tree
Hide file tree
Showing 14 changed files with 134 additions and 67 deletions.
2 changes: 1 addition & 1 deletion Diplomatic/Models/Field.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Newtonsoft.Json;
using System;

namespace Diplomatic
namespace Diplomatic.Models
{
[Serializable]
public class Field
Expand Down
20 changes: 20 additions & 0 deletions Diplomatic/Models/Signature.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using Newtonsoft.Json;

namespace Diplomatic.Models
{
[Serializable]
public class Signature
{
[JsonProperty("name")]
public string Name { get; set; }
[JsonProperty("id")]
public string Id { get; set; }
public Uri ImageUri {
get {
string basePath = $"https://qri7p78aml.execute-api.eu-west-2.amazonaws.com/dev/preview/signature/{Id}";
return new Uri(basePath + "?width=200&height=80");
}
}
}
}
18 changes: 7 additions & 11 deletions Diplomatic/Models/Template.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@
using System;
using System.Collections.Generic;
using Newtonsoft.Json;

namespace Diplomatic
namespace Diplomatic.Models
{
[Serializable]
public class Template
{
[JsonProperty("name")]
public string TemplateName { get; set; }
[JsonProperty("path")]
public string Path { get; set; }
[JsonProperty("fields")]
public IEnumerable<Field> Fields { get; set; }
public IEnumerable<Field> Fields { get; }
public Signature Signature { get; set; }
public string Name { get; set; }
public bool HasSignature { get; }

public Template(string name, string path, IEnumerable<Field> fields)
public Template(bool signature, IEnumerable<Field> fields)
{
TemplateName = name;
Path = path;
Fields = fields;
HasSignature = signature;
}
}
}
31 changes: 31 additions & 0 deletions Diplomatic/Utils/WebSignatureProvider.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using Newtonsoft.Json;

namespace Diplomatic.Utils
{
using Models;

public class WebSignatureProvider
{
private readonly IEnumerable<Signature> signatures;
public IEnumerable<Signature> GetTemplates() => signatures.Select(obj => obj);

public WebSignatureProvider()
{
var request = WebRequest.Create(new Uri("https://qri7p78aml.execute-api.eu-west-2.amazonaws.com/dev/signatures.json"));
request.ContentType = "application/json";
request.Method = "GET";

using (var response = request.GetResponse() as HttpWebResponse)
using (var reader = new StreamReader(response.GetResponseStream()))
{
string content = reader.ReadToEnd();
signatures = JsonConvert.DeserializeObject<List<Signature>>(content);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Diplomatic.Utils
{
using Models;
public class WebTemplateProvider
{
private readonly List<Template> templates;
Expand All @@ -22,7 +23,12 @@ public WebTemplateProvider()
using (var reader = new StreamReader(response.GetResponseStream()))
{
string content = reader.ReadToEnd();
templates = JsonConvert.DeserializeObject<List<Template>>(content);
Dictionary<string, Template> rawTemplates = JsonConvert.DeserializeObject<Dictionary<string, Template>>(content);
foreach (var entry in rawTemplates)
{
entry.Value.Name = entry.Key;
}
templates = rawTemplates.Values.ToList();
}
}
}
Expand Down
33 changes: 7 additions & 26 deletions Diplomatic/ViewModels/SignaturePickerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,39 +1,20 @@
using Xamarin.Forms;
using System.Linq;

namespace Diplomatic.ViewModels
{
using Models;
using Utils;

public class SignaturePickerViewModel
{
// List of signature items to be displayed.
public ImageCell[] SignatureItems { get; set; }
public Signature[] Signatures { get; set; }
public Template SelectedTemplate { get; set; }

public SignaturePickerViewModel(Template selectedTemplate)
{
SelectedTemplate = selectedTemplate;
SignatureItems = new ImageCell[]
{
// Creating a few signatures for testing purposes
new ImageCell()
{
ImageSource = "donaldtrump.png",
Text = "Donald Trump",
Detail = "President"

},
new ImageCell()
{
ImageSource = "gavinbelson.png",
Text = "Gavin Belson",
Detail = "Visionary behind 'The box 3'"
},
new ImageCell()
{
ImageSource = "waltdisney.png",
Text = "Walt Disney",
Detail = "Interesting"
}
};
Signatures = new WebSignatureProvider().GetTemplates().ToArray();
}

}
}
6 changes: 4 additions & 2 deletions Diplomatic/ViewModels/TemplatePickerViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System.Collections.Generic;
using System.Linq;
using Diplomatic.Utils;


namespace Diplomatic.ViewModels
{
using Models;
public class TemplatePickerViewModel
{
public Template[] TemplateList { get; set; }
public Template[] Templates { get; set; }
public TemplatePickerViewModel()
{
TemplateList = new WebTemplateProvider().GetTemplates().ToArray();
Templates = new WebTemplateProvider().GetTemplates().ToArray();
}
}
}
1 change: 1 addition & 0 deletions Diplomatic/ViewModels/TextFieldViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Diplomatic.ViewModels
{
using Models;
public class TextFieldViewModel
{
public Template SelectedTemplate { get; set; }
Expand Down
3 changes: 0 additions & 3 deletions Diplomatic/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,5 @@ void OnLink ( object sender, EventArgs e)
{
Device.OpenUri(new Uri("https://github.com/Dualog-students/Diplomatic"));
}



}
}
11 changes: 3 additions & 8 deletions Diplomatic/Views/Signatures.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<StackLayout Padding="20,0,20,0" BackgroundColor="{StaticResource backgroundColor}">
<Label Text="Pick a signature" HorizontalOptions="Center"/>
<!-- Display list of signatures -->
<ListView ItemsSource="{Binding SignatureItems}" BackgroundColor="Transparent">
<ListView ItemsSource="{Binding Signatures}" ItemSelected="NextPage" BackgroundColor="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
Expand All @@ -19,10 +19,10 @@
</Grid.ColumnDefinitions>

<!--Image -->
<Image Grid.Column="0" Source="{Binding ImageSource}" Aspect="AspectFit" />
<Image Grid.Column="0" Source="{Binding ImageUri}" Aspect="AspectFit" />
<!-- Text -->
<Label Grid.Column="1"
Text="{Binding Text}"
Text="{Binding Name}"
FontAttributes="Bold"
HorizontalTextAlignment="Center"
VerticalTextAlignment="End" />
Expand All @@ -31,11 +31,6 @@
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
<!-- Button to continue
Clicked="NextPage"
-->
<Label Text="{Binding SelectedTemplate.TemplateName}" />
<Button Text="Next" Style="{StaticResource nextPageButton}" Clicked="NextPage"/>
</StackLayout>
</ScrollView>
</ContentPage.Content>
Expand Down
11 changes: 8 additions & 3 deletions Diplomatic/Views/Signatures.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,32 @@
using System;
using System.Collections.Generic;
using Diplomatic.ViewModels;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace Diplomatic.Views
{
using Models;
using ViewModels;

public partial class Signatures : ContentPage
{
public Signatures()
{
InitializeComponent();
}

async void NextPage(object sender, EventArgs e)
async void NextPage(object sender, SelectedItemChangedEventArgs e)
{
var template = ((SignaturePickerViewModel)BindingContext).SelectedTemplate;
template.Signature = (Signature)e.SelectedItem;
var queryParams = new List<string> { };
foreach (var field in template.Fields)
{
queryParams.Add($"{field.Name.ToLower()}={field.Value}");
}
queryParams.Add($"signature={template.Signature.Id}");
var query = string.Join("&", queryParams.ToArray());
string final = "https://qri7p78aml.execute-api.eu-west-2.amazonaws.com/dev/" + template.TemplateName.ToLower() + "?" + query;
string final = "https://qri7p78aml.execute-api.eu-west-2.amazonaws.com/dev/" + template.Name.ToLower() + "?" + query;
var endpoint = new Uri(Uri.EscapeUriString(final));
var next = new Result
{
Expand Down
11 changes: 8 additions & 3 deletions Diplomatic/Views/Templates.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
<StackLayout Padding="20,0,20,0" BackgroundColor="{StaticResource backgroundColor}">
<Label Text="Pick a template" HorizontalOptions="Center" />
<!-- Insert template logic here-->
<ListView ItemsSource="{Binding TemplateList}" ItemSelected="OnSelection" SeparatorVisibility="None" BackgroundColor="Transparent">
<ListView ItemsSource="{Binding Templates}" ItemSelected="OnSelection" SeparatorVisibility="None" BackgroundColor="Transparent">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid ColumnSpacing="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*" />
</Grid.ColumnDefinitions>
<!-- This image thing wont work tho-->
<Label Grid.Column="0" Grid.Row="0" Text="{Binding TemplateName}" FontAttributes="Bold" FontSize="Large" HorizontalTextAlignment="Start" VerticalTextAlignment="Center"/>
<Label Grid.Column="0"
Grid.Row="0"
Text="{Binding Name}"
FontAttributes="Bold"
FontSize="Large"
HorizontalTextAlignment="Start"
VerticalTextAlignment="Center"/>
</Grid>
</ViewCell>
</DataTemplate>
Expand Down
11 changes: 7 additions & 4 deletions Diplomatic/Views/Templates.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
using Diplomatic.ViewModels;
using Xamarin.Forms;

namespace Diplomatic.Views
{
using Models;
using ViewModels;

public partial class Templates : ContentPage
{

public Templates()
{
InitializeComponent();
Expand All @@ -18,8 +19,10 @@ async void OnSelection(object sender, SelectedItemChangedEventArgs e)
{
return; //ItemSelected is called on deselection, which results in SelectedItem being set to null
}
var next = new TextFields();
next.BindingContext = new TextFieldViewModel((Template)e.SelectedItem);
var next = new TextFields
{
BindingContext = new TextFieldViewModel((Template)e.SelectedItem)
};

await Navigation.PushAsync(next);
}
Expand Down
35 changes: 30 additions & 5 deletions Diplomatic/Views/TextFields.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
using System;
using Diplomatic.ViewModels;
using Xamarin.Forms;
namespace Diplomatic.Views
{
using ViewModels;
using Models;
using System.Collections.Generic;

public partial class TextFields : ContentPage
{
public TextFields()
Expand All @@ -12,11 +15,33 @@ public TextFields()

private async void NextPage(object sender, EventArgs e)
{
var next = new Signatures
Template selectedTemplate = ((TextFieldViewModel)BindingContext).SelectedTemplate;

if (selectedTemplate.HasSignature)
{
var next = new Signatures
{
BindingContext = new SignaturePickerViewModel(selectedTemplate)
};
await Navigation.PushAsync(next);
}
else
{
BindingContext = new SignaturePickerViewModel(((TextFieldViewModel)BindingContext).SelectedTemplate)
};
await Navigation.PushAsync(next);
var template = ((TextFieldViewModel)BindingContext).SelectedTemplate;
var queryParams = new List<string> { };
foreach (var field in template.Fields)
{
queryParams.Add($"{field.Name.ToLower()}={field.Value}");
}
var query = string.Join("&", queryParams.ToArray());
string final = "https://qri7p78aml.execute-api.eu-west-2.amazonaws.com/dev/" + template.Name.ToLower() + "?" + query;
var endpoint = new Uri(Uri.EscapeUriString(final));
var next = new Result
{
BindingContext = new ResultViewModel(endpoint)
};
await Navigation.PushAsync(next);
}
}
}
}

0 comments on commit cdeb67f

Please sign in to comment.