Aerogear Android M2

Posted By Hoyt Summers Pittman

Aerogear Android M2

The Aeorgear Android library has gotten some new features since our last release in December.

First and foremost is an API for interacting with paged data from REST providers. Now you can simply add a PageConfig to a PipeConfig and begin using paged resources on an Aerogear Controller powered site. Alternatively, you can provide PageResultExtractor and DefaultParameterProvider to interact with paged data sources on other services such as Reddit, Github, or Twitter.

Secondly we have included a persistent SQLite backed datastore, SQLStore. It functions similarly to MemoryStore but instead uses Android’s native database to provide CRUD operations.

Pagination

API Review

PageConfig
A bean which is used to set configuration options for a Pipe to describe pagination.
PageResultExtractor
An interface which defines how to parse a read response into Pagining information. Default Implementations are URIBodyPageParser and URIPageHeaderParser
PagedList
A List extension which wraps the results of a read or readWithFilter operation and has the methods next and previous which call a Pipe and pass ReadFilters which return a PagedList for the next or previous datasets. The default implementation is WrappingPagedList.
ParameterProvider
An interface which is responsible for consuming ReadFilters and emitting URIs which can be consumed by the server with the correct paging implementation. The default implementation is DefaultParameterProvider.

Examples

see AGReddit Post

SQLStore

API Review

StoreTypes
An enum class with options MEMORY and SQL.
SQLStore
A Store implementation which writes to a backing SQLLite impl.

Examples

Creating a SQLStore

To create a SQLStore we can use a StoreConfig bean and DataManager. A SQL store needs to be opened using the open method. Open takes a Callback which is called after the database is ready for writing.

StoreConfig sqlStoreConfig = new StoreConfig();  
sqlStoreConfig.setContext(Robolectric.application.getApplicationContext());  
sqlStoreConfig.setType(SQL);  
sqlStoreConfig.setKlass(Data.class);  
Store store = dataManager.store("sqlStore", sqlStoreConfig);  
store.open(/*callback*/);  

Saving Data

Please note, open has already been called on store.

Data data = new Data(10, "name", "description");//Id, name, description  
store.save(data);  
Data readData = store.read(10);  

Clearing the data store

store.reset();  
Data readData = store.read(10);  
Assert.assertNull(readData);  

Reading All Data

store.save(new Data(1, "name", "description"));  
store.save(new Data((2, "name", "description"));  
store.save(new Data((3, "name2", "description"));  
store.save(new Data((4, "name2", "description"));  
store.save(new Data((5, "name", "description2"));  
store.save(new Data((6, "name2", "description2"));

List<data> allData = new ArrayList</data><data>(store.readAll());  
Collections.sort(allData);  
Assert.assertEquals(6, allData.size());  
Assert.assertEquals("name", allData.get(0).getName());  
Assert.assertEquals("name2", allData.get(5).getName());  

Deleting Data

loadBulkData(); //Loads 6 "Data" Elements
store.remove(1); //Deletes the element with ID 1  

Searching Data

Note: Filtering only supports exact matches

loadBulkData(); //Loads 6 "Data" Elements

result = store.readWithFilter(null);  
Assert.assertEquals(6, result.size());

filter = new ReadFilter();  
where = new JSONObject();  
where.put("name", "name2");  
filter.setWhere(where);  
result = store.readWithFilter(filter);  
Assert.assertEquals(3, result.size());

filter = new ReadFilter();  
where = new JSONObject();  
where.put("name", "name2");  
where.put("description", "description");  
filter.setWhere(where);  
result = store.readWithFilter(filter);  
Assert.assertEquals(2, result.size());  

Jan 30th, 2013

No Comments! Be The First!

Leave a Reply