Skip to content

Instantly share code, notes, and snippets.

@rid00z
Last active August 29, 2015 14:05
Show Gist options
  • Save rid00z/c77f31f2297d39c69c1b to your computer and use it in GitHub Desktop.
Save rid00z/c77f31f2297d39c69c1b to your computer and use it in GitHub Desktop.
Remote Data Source
/*
* Note* this is for demo purposes only and is not a example of good network code,
* http://www.michaelridland.com/mobile/asp-net-mvc-xamarin-mashups/
*/
public class RemoteDataSource : IDataSource
{
static string HostBase = "http://192.168.56.101:49203";
public RemoteDataSource ()
{
}
public async Task<IEnumerable<JellyBeanValue>> GetJellyBeanValues ()
{
return (await GetServerData ()).JellyBeanValues;
}
public async Task<IEnumerable<MyJellyBean>> GetMyJellyBeans ()
{
return (await GetServerData ()).MyJellyBeans;
}
async Task<SyncContainer> GetServerData()
{
HttpClient client = new HttpClient ();
var result = client.GetStringAsync (HostBase + "/JellyBeans/GetAllData").Result;
var syncContainer = JsonConvert.DeserializeObject<SyncContainer> (result);
//cheap mans offline for this sample, put all into localstoage
var sql = Xamarin.Forms.DependencyService.Get<ISQLiteFactory> ().GetConnection ("app.db");
sql.CreateTable<MyJellyBean> ();
sql.CreateTable<JellyBeanValue> ();
if (sql.Table<MyJellyBean>().Count() < 1)
sql.InsertAll (syncContainer.MyJellyBeans);
if (sql.Table<JellyBeanValue>().Count() < 1)
sql.InsertAll (syncContainer.JellyBeanValues);
return syncContainer;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment