Last active
August 29, 2015 14:05
-
-
Save rid00z/c77f31f2297d39c69c1b to your computer and use it in GitHub Desktop.
Remote Data Source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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