Created
March 21, 2016 16:09
-
-
Save codemillmatt/a32edcb928caef55b2c6 to your computer and use it in GitHub Desktop.
Akavache Sample From Akavache Is AKA Awesome Blog
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
public class App : Application | |
{ | |
AkaConsumer consumer; | |
public App() | |
{ | |
// The root page of your application | |
consumer = new AkaConsumer(); | |
var contentLabel = new Label() { HorizontalTextAlignment = TextAlignment.Center }; | |
var loadButton = new Button() | |
{ | |
Text = "Load More Data", | |
HorizontalOptions = LayoutOptions.CenterAndExpand | |
}; | |
var displayButton = new Button() | |
{ | |
Text = "Display The Data", | |
HorizontalOptions = LayoutOptions.CenterAndExpand | |
}; | |
loadButton.Clicked += (s, e) => | |
{ | |
consumer.LoadData(); | |
}; | |
displayButton.Clicked += (s, e) => | |
{ | |
contentLabel.Text = string.Empty; | |
foreach (var item in consumer.allTodos) | |
{ | |
contentLabel.Text = contentLabel.Text + " " + item.ToDoItemId + Environment.NewLine; | |
} | |
}; | |
var content = new ContentPage | |
{ | |
Title = "AkaTest", | |
Content = new StackLayout | |
{ | |
VerticalOptions = LayoutOptions.Center, | |
Children = { | |
loadButton, | |
displayButton, | |
contentLabel | |
} | |
} | |
}; | |
MainPage = new NavigationPage(content); | |
} | |
} | |
public class AkaConsumer | |
{ | |
public List<ToDoItem> allTodos = new List<ToDoItem>(); | |
int overallCounter = 0; | |
public void LoadData() | |
{ | |
BlobCache.LocalMachine.GetAndFetchLatest("todoconstant", async () => | |
{ | |
return await GetMoreItems(); | |
}).Subscribe((cacheThenUpdate) => | |
{ | |
// Need to check what's already been added | |
// Going to keep adding the previous results | |
allTodos.AddRange(cacheThenUpdate); | |
}); | |
} | |
async Task<List<ToDoItem>> GetMoreItems() | |
{ | |
var returnList = new List<ToDoItem>(); | |
await Task.Delay(50); | |
var counter = 3; | |
for (int i = 0; i < counter; i++) | |
{ | |
var newTDI = new ToDoItem() { ToDoItemId = overallCounter++, | |
Description = "Loop Counter: " + i.ToString() }; | |
returnList.Add(newTDI); | |
} | |
return returnList; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment