Skip to content

Commit

Permalink
A star
Browse files Browse the repository at this point in the history
  • Loading branch information
duncte123 committed Jan 13, 2020
1 parent 817fde0 commit d860578
Show file tree
Hide file tree
Showing 18 changed files with 372 additions and 41 deletions.
3 changes: 3 additions & 0 deletions KentekenShit.Android/KentekenShit.Android.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SkiaSharp.Extended" Version="1.60.0" />
<PackageReference Include="SkiaSharp.Svg" Version="1.60.0" />
<PackageReference Include="SkiaSharp.Views.Forms" Version="1.68.1.1" />
<PackageReference Include="sqlite-net-pcl" Version="1.6.292" />
<PackageReference Include="Xamarin.Forms" Version="4.4.0.991477" />
<PackageReference Include="Xamarin.Android.Support.Core.Utils" Version="28.0.0.3" />
Expand Down
3 changes: 3 additions & 0 deletions KentekenShit.iOS/KentekenShit.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,9 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SkiaSharp.Extended" Version="1.60.0" />
<PackageReference Include="SkiaSharp.Svg" Version="1.60.0" />
<PackageReference Include="SkiaSharp.Views.Forms" Version="1.68.1.1" />
<PackageReference Include="sqlite-net-pcl" Version="1.6.292" />
<PackageReference Include="Xamarin.Forms" Version="4.4.0.991477" />
<PackageReference Include="Xamarin.Essentials" Version="1.3.1" />
Expand Down
1 change: 1 addition & 0 deletions KentekenShit/Assets/star-regular.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions KentekenShit/Assets/star-solid.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
79 changes: 79 additions & 0 deletions KentekenShit/Icon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
using System;
using System.IO;
using SkiaSharp;
using SkiaSharp.Views.Forms;
using Xamarin.Forms;
using SKSvg = SkiaSharp.Extended.Svg.SKSvg;

namespace KentekenShit
{
public class Icon : Frame
{
#region Private Members

private readonly SKCanvasView _canvasView = new SKCanvasView();

#endregion

#region Bindable Properties

#region ResourceId

public static readonly BindableProperty ResourceIdProperty = BindableProperty.Create(
nameof(ResourceId), typeof(string), typeof(Icon), default(string), propertyChanged: RedrawCanvas);

public string ResourceId
{
get => (string)GetValue(ResourceIdProperty);
set => SetValue(ResourceIdProperty, value);
}

#endregion

#endregion

public Icon()
{
Padding = new Thickness(0);
BackgroundColor = Color.Transparent;
HasShadow = false;
Content = _canvasView;
_canvasView.PaintSurface += CanvasViewOnPaintSurface;
}

private static void RedrawCanvas(BindableObject bindable, object oldvalue, object newvalue)
{
Icon svgIcon = bindable as Icon;
svgIcon?._canvasView.InvalidateSurface();
}

private void CanvasViewOnPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
SKCanvas canvas = args.Surface.Canvas;
canvas.Clear();

if (string.IsNullOrEmpty(ResourceId))
return;

using (Stream stream = GetType().Assembly.GetManifestResourceStream(ResourceId))
{
SKSvg svg = new SKSvg();
svg.Load(stream);

SKImageInfo info = args.Info;
canvas.Translate(info.Width / 2f, info.Height / 2f);

SKRect bounds = svg.ViewBox;
float xRatio = info.Width / bounds.Width;
float yRatio = info.Height / bounds.Height;

float ratio = Math.Min(xRatio, yRatio);

canvas.Scale(ratio);
canvas.Translate(-bounds.MidX, -bounds.MidY);

canvas.DrawPicture(svg.Picture);
}
}
}
}
5 changes: 5 additions & 0 deletions KentekenShit/KentekenShit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="SkiaSharp.Extended" Version="1.60.0" />
<PackageReference Include="SkiaSharp.Svg" Version="1.60.0" />
<PackageReference Include="SkiaSharp.Views.Forms" Version="1.68.1.1" />
<PackageReference Include="sqlite-net-pcl" Version="1.6.292" />
<PackageReference Include="Xamarin.Forms" Version="4.4.0.991477" />
<PackageReference Include="Xamarin.Essentials" Version="1.3.1" />
Expand All @@ -16,5 +19,7 @@
<EmbeddedResource Update="Views\DetailsPage.xaml">
<Generator>MSBuild:UpdateDesignTimeXaml</Generator>
</EmbeddedResource>
<EmbeddedResource Include="Assets\star-regular.svg" />
<EmbeddedResource Include="Assets\star-solid.svg" />
</ItemGroup>
</Project>
15 changes: 14 additions & 1 deletion KentekenShit/Models/Item.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,24 @@
using System;
using SQLite;
using System;

namespace KentekenShit.Models
{
public class Item
{
[PrimaryKey, AutoIncrement]
public string Id { get; set; }
public string Text { get; set; }
public string Description { get; set; }

public string Plate { get; set; }
public string Make { get; set; }
public string Seets { get; set; }
public string Cylinders { get; set; }
public string Doors { get; set; }
public string Wheels { get; set; }
public string Price { get; set; }
public string TaxiSign { get; set; }

public bool InFavoirites { get; set; }
}
}
2 changes: 1 addition & 1 deletion KentekenShit/Services/IDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public interface IDataStore<T>
Task<bool> UpdateItemAsync(T item);
Task<bool> DeleteItemAsync(string id);
Task<T> GetItemAsync(string id);
Task<IEnumerable<T>> GetItemsAsync(bool forceRefresh = false);
Task<List<T>> GetItemsAsync(bool forceRefresh = false);
}
}
19 changes: 12 additions & 7 deletions KentekenShit/Services/MockDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@ public MockDataStore()
{
items = new List<Item>()
{
new Item { Id = Guid.NewGuid().ToString(), Text = "First item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Second item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Third item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Fourth item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Fifth item", Description="This is an item description." },
new Item { Id = Guid.NewGuid().ToString(), Text = "Sixth item", Description="This is an item description." }
new Item {
Id = Guid.NewGuid().ToString(),
Plate="48XRFF",
Make="Toyota",
Seets="5",
Cylinders="4",
Doors="4",
Wheels="4",
Price="5",
TaxiSign="No",
},
};
}

Expand Down Expand Up @@ -52,7 +57,7 @@ public async Task<Item> GetItemAsync(string id)
return await Task.FromResult(items.FirstOrDefault(s => s.Id == id));
}

public async Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
public async Task<List<Item>> GetItemsAsync(bool forceRefresh = false)
{
return await Task.FromResult(items);
}
Expand Down
26 changes: 20 additions & 6 deletions KentekenShit/Services/SQLiteDataStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,53 @@
using System.IO;
using System.Threading.Tasks;
using KentekenShit.Models;
using SQLite;

namespace KentekenShit.Services
{
public class SQLiteDataStore : IDataStore<Item>
{
readonly SQLiteAsyncConnection _database;

public SQLiteDataStore()
{
var dbPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "database.db");

_database = new SQLiteAsyncConnection(dbPath);
_database.CreateTableAsync<Item>().Wait();
}

public Task<bool> AddItemAsync(Item item)
{
throw new System.NotImplementedException();
return Task.Run(() => {
return _database.InsertAsync(item).Result > 0 ? true : false;
});
}

public Task<bool> UpdateItemAsync(Item item)
{
throw new System.NotImplementedException();
return Task.Run(() => {
return _database.UpdateAsync(item).Result > 0 ? true : false;
});
}

public Task<bool> DeleteItemAsync(string id)
{
throw new System.NotImplementedException();
return _database.Table<Item>()
.DeleteAsync((i) => i.Id == id)
.ContinueWith((a) => a.Result > 0 ? true : false);
}

public Task<Item> GetItemAsync(string id)
{
throw new System.NotImplementedException();
return _database.Table<Item>()
.Where(i => i.Id == id)
.FirstOrDefaultAsync();
}

public Task<IEnumerable<Item>> GetItemsAsync(bool forceRefresh = false)
public Task<List<Item>> GetItemsAsync(bool forceRefresh = false)
{
throw new System.NotImplementedException();
return _database.Table<Item>().ToListAsync();
}
}
}
58 changes: 58 additions & 0 deletions KentekenShit/ViewModels/HistoryViewModel.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Threading.Tasks;

using Xamarin.Forms;

using KentekenShit.Models;
using KentekenShit.Views;

namespace KentekenShit.ViewModels
{
public class HistoryViewModel : BaseViewModel
{
public ObservableCollection<Item> Items { get; set; }
public Command LoadItemsCommand { get; set; }

public HistoryViewModel()
{
Title = "History";
Items = new ObservableCollection<Item>();
LoadItemsCommand = new Command(async () => await ExecuteLoadItemsCommand());

MessagingCenter.Subscribe<NewItemPage, Item>(this, "AddItem", async (obj, item) =>
{
var newItem = item as Item;
Items.Add(newItem);
await DataStore.AddItemAsync(newItem);
});
}

async Task ExecuteLoadItemsCommand()
{
if (IsBusy)
return;

IsBusy = true;

try
{
Items.Clear();
var items = await DataStore.GetItemsAsync(true);
foreach (var item in items)
{
Items.Add(item);
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
}
finally
{
IsBusy = false;
}
}
}
}
6 changes: 5 additions & 1 deletion KentekenShit/ViewModels/ItemDetailViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;

using KentekenShit.Models;
using Xamarin.Forms;

namespace KentekenShit.ViewModels
{
Expand All @@ -9,8 +10,11 @@ public class ItemDetailViewModel : BaseViewModel
public Item Item { get; set; }
public ItemDetailViewModel(Item item = null)
{
Title = item?.Text;
Title = "Licnce plate " + item?.Plate;
Item = item;
}

public string FavText => Item?.InFavoirites ?? false ? "Remove from favorites" : "Add to favorites";
public Color BtnColor => Item?.InFavoirites ?? false ? Color.Gold : Color.LightGray;
}
}
Loading

0 comments on commit d860578

Please sign in to comment.