Chorong Android is a little Android utility providing MVP development modules, UI component, Google API wrappers, and extras.
To use only core things (Module, Task, ...)
dependencies {
compile 'net.poksion:chorong-android-core:0.1.29'
}
If you want use UI stuff (ToolbarActivity, FlatCardRecyclerView, ...), append 'ui' module
dependencies {
compile 'net.poksion:chorong-android-core:0.1.29'
compile 'net.poksion:chorong-android-ui:0.1.29'
}
SampleForPersistence is an entry point to show how implement activity with MVP pattern using chorong-android. It consists of 3 major parts : SampleForPersistence, SampleForPersistencePresenter and SampleItemRepository
SampleForPersistence is the Activity and plays role for assembler and View.
In Android, The Activity Component plays the assembler - initializing components, connecting each components and triggering the first job. SampleForPersistence acts exaclty same (except using Assembler)
The entry point of ToolbarActivity (SampleForPersistence extends this) is onCreateContentView and SampleForPersistence overrides it
@Override
protected void onCreateContentView(LayoutInflater layoutInflater, ViewGroup container, Bundle savedInstanceState) {
ModuleFactory.assemble(SampleForPersistence.class, this, new SampleForPersistenceAssembler(this, container));
initCardView();
presenter.readDb();
}
SampleForPersistence is kind of the view in Superving Controller and it plays less role for Passive View. (You can find the diffrence of these in here). It has only two mehtod and isFinishing
uses Acitivty.isFisinishg
(do not need to re-implementing), so the importan method is only one : void showItems(List<DbItemModel> itemList)
@Override
public void showItems(List<SampleItem> itemList) {
for (SampleItem model : itemList) {
cardRecyclerView.addItem(
sampleItemViewModelUtil.makeViewModel(model),
sampleItemViewModelUtil.makeViewBinder(new SampleItemClickHandler() {
@Override
public void onItemClick(String id) {
presenter.reloadItem(id);
}
}));
}
cardRecyclerView.notifyDataSetChanged();
}
The main UI component of SampleForPersistence is FlatCardRecyclerView. SampleItemViewModelUtil is helper class for making ViewModel/ViewBinder used in FlatCardRecyclerView
See also : Module
SampleForPersistencePresenter does the business logic with realted dependeices.
There are two main depending components:
TaskRunner provides how running tasks - synchronous or asynchronous, running on executor, etc. We prefer to use TaskRunnerAsyncShared for IO task and TaskRunnerSync for testing (for easy to test, if the test does not need to run asynchronously)
SampleItemRepository is model that handles the database. chrong-android provides ObjectStore, StoreAccessor - SampleItemRepository is the application commponent that using DatabaseProxyManager and DatabaseProxyManager basically uses ObjectStore/StoreAccessor.
SampleForPersistencePresenter does really simple logic : adding Items with SampleItemRepository and notifying result to View. You can find this in test code.
@Test
public void view_should_show_items_stored_on_repository() {
List<SampleItem> items = new ArrayList<>();
SampleItem item = new SampleItem();
item.id = "dummy-id";
item.name = "dummy-name";
item.date = "dummy-date";
items.add(item);
sampleItemRepository.storeAll(items);
presenter.readDb();
verify(view, times(1)).showItems(captor.capture());
List<SampleItem> values = captor.getValue();
assertThat(values.size()).isEqualTo(1);
assertThat(values.get(0).id).isEqualTo("dummy-id");
}
See also : Task
SampleItemRepository is the core model in this sample. It uses DatabaseProxyManager to save entity to DB.
DatabaseProxyManager manages persistence proxies that read/write data from DB when realted StoreAccessor is called with getter/setter.
See also : Store