WinRT app guide: Step 12: Async CRUD with SQLite and an awesome piechart
To read the other steps in the step by step guide for creating a Metr0/ WInRT application go here
I mentioned a few times that synchronous programming can be a bit problematic when programming Windows Store Applications. Asynchronous programming is basically a must- as it is considered good practice and gives a better user experience as the user doesn’t have to wait for something to complete to keep using the app. It is all about visual feedback, responsive applications and fast applications.
Piechart from Telerik RadControls for Windows 8 / Windows Store Apps
Today we focus on those three things a little bit more.
First of all, we need to give some feedback that the user hasn’t typed in numbers if he/she hasn’t in the time estimate and time passed boxes. We do this by simple handling the text changed event, checking the input and indicating with a red border if it isn’t a number. If the user chooses to submit anyway, the value will be set to the previous value. Hah!
Visual feedback on wrong input is important in Windows Store Apps
More on the visual side, we have now added a beautiful piechart displaying the average of 100% of the different scores. Pretty cool I reckon! The user can see how he/she prioritizes, which promotes more interaction and makes the application more alive. I’ve of course used the Telerik charts for Windows 8, 1- I love charts, 2- I love even more charts that are easy to set up. We will get back to the charts later in the guide, as we will add many more and I’ll show you how you can customize them even more. Notice for now that I’ve set a default style for the labels, and that the values and labels are bound to an observable collection.
Now, the async CRUD. Big cred to @mBrit (give him a follow) for this bit, he has put together the async part for the SQLite wrapper, and as you can see, the implementation is easy and neat. Now we just need to change our code so we, as we should, work with Tasks. Instead of posting all the code here, I’ve uploaded the app so you can download it and have a look at it.
[sourcecode language=“csharp”]
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;
using Mihsp.Model;
using System.Linq;
using SQLite;
namespace Mihsp.Repository
{
public class StudyActivityRepository
{
private static readonly string _dbPath = Path.Combine(Windows.Storage.ApplicationData.Current.LocalFolder.Path, “db.sqlite”);
public Task AddAsync(StudyActivity studyActivity)
{
return new SQLiteAsyncConnection(\_dbPath).InsertAsync(studyActivity);
}
public Task RemoveAsync(StudyActivity studyActivity)
{
return new SQLiteAsyncConnection(\_dbPath).DeleteAsync(studyActivity);
}
public Task UpdateAsync(StudyActivity studyActivity)
{
return new SQLiteAsyncConnection(\_dbPath).UpdateAsync(studyActivity);
}
public Task<List<StudyActivity>> GetAllAsync()
{
return new SQLiteAsyncConnection(\_dbPath).Table<StudyActivity>().ToListAsync();
}
public async Task<IEnumerable<IGrouping<string, StudyActivity>>> GetAllGroupedAsync()
{
var list = await GetAllAsync();
return list.OrderBy(x => x.Title).GroupBy(x => x.CategoryName);
}
}
}
[/sourcecode]
When the app starts we try to create a new table, but if it already exists the creating automatically gets ignored. Sweet! One less problem to think about!
Next time we will do some more layout stuff, test the app on a real slate and add some finishing touches!
Comments
Last modified on 2012-10-14