lundi 11 avril 2016

best desian pattern for service based android application

I 'm writing a news android application which have 3 kind of items: news, image gallery and journal. I need to get list of these items from web service and then after click on every item in list call service again to load every item detail and I use Volley to networking manager. the problem is i do not know how to organize my code to have good architecture like mcv or mvp .for example this is one of my fragment :

public class TCenterFragment extends android.support.v4.app.ListFragment {


//private String id_tree_news = "8611";
//private String Node_id = "394";

int cnt = 10;
ListView listView;
ArrayList<NewsItem> newsItems;
private NewsListAdapter adapter;
private  String termId;
ProgressDialog pDialog;


@Override
public View onCreateView(LayoutInflater i, ViewGroup container, Bundle savedInstanceState) {

    termId=getString(R.string.service_center_termId);
    ReadFromCache(Utility.MakeGetlistString(getActivity(), Config.NEWS_NODE_ID, termId));

    return super.onCreateView(i, container, savedInstanceState);
}

public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    listView = getListView();
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            Bundle send = new Bundle();
            send.putString("id_node", String.valueOf(Config.NEWS_NODE_ID));
            send.putString("term_id", String.valueOf(adapter.getItem(position).get_termId()));
            send.putString("id_list_news",
                    adapter.getItemId(position) + "");
            Intent i = new Intent(getActivity(),
                    ContentActivity.class);
            i.putExtras(send);
            startActivity(i);
        }
    });
    listView.setOnScrollListener(new AbsListView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {

        }

        @Override
        public void onScroll(AbsListView view,
                             int firstVisibleItem,
                             int visibleItemCount,
                             int totalItemCount) {
            if (listView.getLastVisiblePosition() == listView
                    .getAdapter().getCount() - 1
                    && listView.getChildAt(
                    listView.getChildCount() - 1)
                    .getBottom() <= listView
                    .getHeight()
                    &&
                    listView.getAdapter().getCount() < newsItems.size()) {
                ////
                ShowWaitingDialog();
                ///
                cnt = cnt + 10;

                int currentPosition = listView.getFirstVisiblePosition();
                adapter = new NewsListAdapter(getActivity(), newsItems.subList(0, cnt <= newsItems.size() ? cnt : newsItems.size()));
                listView.setAdapter(adapter);
                listView.setSelectionFromTop(currentPosition + 1, 0);


                pDialog.hide();

            }

        }
    });


}

private void ShowWaitingDialog() {
    pDialog = new ProgressDialog(
            getActivity());
    pDialog.setMessage(getString(R.string.loading));
    pDialog.setIndeterminate(true);
    pDialog.setCancelable(false);
    pDialog.show();
}

private void ReadFromCache(String service_address) {

    Cache cache = MyApplication.getInstance().getRequestQueue().getCache();
    Cache.Entry entry = cache.get(service_address);

    pDialog = new ProgressDialog(getActivity());
    pDialog.setMessage(getString(R.string.loading));
    pDialog.show();
    if (entry != null) {
        // fetch the data from cache
        try {
            String data = new String(entry.data, "UTF-8");
            try {
                JSONArray jsonArray = new JSONArray(data);
                newsItems = NewsItem.fromJson(jsonArray,
                        Integer.valueOf(termId));

                adapter = new NewsListAdapter(getActivity(), newsItems.subList(0, 10));
                setListAdapter(adapter);
                pDialog.hide();


            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    } else {

        //!!! not test
        Utility.ShowFaildMessage(getActivity());
    }
}

I read data from cache with a method and display the result in activity . i want to have separate layer for service and notify my interface when response is ready. but i do not know how to do that.which design pattern fit to this application ? can i have an interface with two method :getitem and getlist and implement these two method in every service class(newservice,galleryservice,journalservice). can anyone give me some suggestion?I do not know so much about design pattern.

Aucun commentaire:

Enregistrer un commentaire