Skip to content
pavlo-savchuk edited this page May 20, 2016 · 7 revisions

Model setup

Required to instantiate Model and DatabaseFacade.

public class Model

Model is a singleton holder for LSClient and SimpleItemManager. The following gives an fragment of Model class.

public class Model {
    private LSClient client;
    private SimpleItemManager simpleItemManager;

    public LSClient getClient() {
        return client;
    }  

    public SimpleItemManager getSimpleItemManager() {
        return simpleItemManager;
    }

    private Model(Context context) {    
        client = new LSClient(context);
        simpleItemManager = new SimpleItemManager(client);
    }
}

DatabaseFacade template

public class DatabaseFacade

DatabaseFacade is a singleton that wraps DatabaseHelper and SQLiteDatabase. It is responsible for: open/close a database, create query, save/update/delete database table. You can use it without any changes.

Server requests template

Create server requests

public class BaseRequestBuilder

For example, here is an implementation of BaseRequestBuilder.

public class StubItemRequestBuilder extends BaseRequestBuilder {

   public ItemRequestBuilder() {
       setRequestMethod(BaseRequest.RequestMethod.GET);
       setResponseFormat(BaseRequest.ResponseFormat.JSON);
       Type listType = new TypeToken<List<StubItemVO>>() {}.getType();
       setResponseClassSpecifier(listType);
       setRequestURL(“set your url here”);
   }
}
Perform server requests
public class ItemStubManager extends SynchronizedDatabaseManager<List<StubItemVO>, Bundle, String>

In order to perform server request SynchrondizedDatabaseManager should be extended. For each request a separate manager should be created. Then it is needed to create a fetch request in getFetchRequest() method.


##Application

RecyclerViewAdapter template

public class StubRecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder>

The main feature of RecyclerAdapteris is a possibility to load feed. For example, the following methods are useful to load page.

  private void loadPage() {
   Model.instance().getSimpleItemManager().pullPage();
}

public void enableDataLoad() {
   Model.instance().getSimpleItemManager().addDataFetchCompleteListener(listener);
   loadPage();
}

public void disableDataLoad() {
   Model.instance().getSimpleItemManager().removeDataFetchCompleteListener(listener);
}

Activity template

public class SimpleListActivity extends BaseActivity

SimpleListActivity consists of CoordinatorLayout with wrap RecyclerView. It is using RecyclerAdapter to disable/enable data loading.

public class SimpleListActivity extends BaseActivity {

private StubItemAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        adapter = new StubItemAdapter(this);
        adapter.enableDataLoad();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if(adapter != null)
        {
            adapter.disableDataLoad();
        }
    }
}

Database

DatabaseHelper template

To work with a database you need create a subclass of the SQLiteOpenHelper class. In this class you need to override the methods to create and update your database.

All queries to create set of tables are placed in string resources. For example, this snippet defines the table name and column names for a single table:

<resources>
    <string name="create_table_stub_items">
        create table stub_items(
        _id string primary key,
        imageURL string,
        description string,
        isFavorite integer,
        page_id string
        )
    </string>
</resources>

Data access object template

public class StubItemDAO extends AbstractDAO<StubItemVO, String>

You need to extend AbstractDAO and implement abstract methods.